Add config file ; add static types

This commit is contained in:
Edgar P. Burkhart 2024-12-08 10:43:31 +01:00
parent 4d5824cfb5
commit a55e7c7bfe
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227
4 changed files with 27 additions and 5 deletions

View file

@ -15,3 +15,7 @@ repos:
args: ["--max-line-length=88", "--extend-ignore=E203"] args: ["--max-line-length=88", "--extend-ignore=E203"]
exclude: "lyceedupaysdesoule/settings/|migrations" exclude: "lyceedupaysdesoule/settings/|migrations"
- repo: https://github.com/pre-commit/mirrors-mypy
rev: "v1.13.0"
hooks:
- id: mypy

4
config.toml Normal file
View file

@ -0,0 +1,4 @@
[mqtt]
username = "oin"
password = "n+Bi58l7LxbH5nEJ"
host = "homeassistant.local"

View file

@ -1,13 +1,18 @@
import logging import logging
import tomllib
from .mqtt import HAClient from .mqtt import HAClient
if __name__ == "__main__": if __name__ == "__main__":
with open("config.toml", "rb") as config_file:
config = tomllib.load(config_file)
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
client = HAClient( client = HAClient(
"climate.chaudiere", "climate.chaudiere",
["sensor.esptic_tempo", "sensor.rte_tempo_prochaine_couleur"], ["sensor.esptic_tempo", "sensor.rte_tempo_prochaine_couleur"],
config.get("mqtt", dict()),
) )
client.connect() client.connect()

View file

@ -10,21 +10,30 @@ logger = logging.getLogger(__name__)
class HAClient: class HAClient:
def __init__(self, entity, secondary_entities=[]): def __init__(
self,
entity: str,
secondary_entities: list[str] = [],
config: dict = dict(),
) -> None:
self.entity = entity self.entity = entity
self.secondary_entities = secondary_entities self.secondary_entities = secondary_entities
self.config = config
self.state_topic = "oin/state" self.state_topic = "oin/state"
self.availability_topic = "oin/availability" self.availability_topic = "oin/availability"
self.client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) self.client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
self.client.username_pw_set(username="oin", password="n+Bi58l7LxbH5nEJ") self.client.username_pw_set(
username=self.config.get("username", None),
password=self.config.get("password", None),
)
self.screen = Screen() self.screen = Screen()
self.selector = Selector(self.send_data) self.selector = Selector(self.send_data)
@property @property
def ha_options(self): def ha_options(self) -> dict[str, str | dict[str, str]]:
return { return {
"dev": { "dev": {
"ids": "oin", "ids": "oin",
@ -38,10 +47,10 @@ class HAClient:
"cmps": self.selector.ha_options, "cmps": self.selector.ha_options,
} }
def connect(self): def connect(self) -> None:
logger.debug("Connecting to HA...") logger.debug("Connecting to HA...")
self.client.will_set(self.availability_topic, "offline", retain=True) self.client.will_set(self.availability_topic, "offline", retain=True)
self.client.connect("homeassistant.local", 1883, 60) self.client.connect(self.config.get("host"), self.config.get("port", 1883))
self.subscribe(entity_topic(self.entity), self.state_update) self.subscribe(entity_topic(self.entity), self.state_update)
for entity in self.secondary_entities: for entity in self.secondary_entities: