From a55e7c7bfe0767407b4dd9e5a0b685814f0b1a55 Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Sun, 8 Dec 2024 10:43:31 +0100 Subject: [PATCH] Add config file ; add static types --- .pre-commit-config.yaml | 4 ++++ config.toml | 4 ++++ oin_thermostat/__main__.py | 5 +++++ oin_thermostat/mqtt.py | 19 ++++++++++++++----- 4 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 config.toml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4d8e2f2..0cb2f3c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -15,3 +15,7 @@ repos: args: ["--max-line-length=88", "--extend-ignore=E203"] exclude: "lyceedupaysdesoule/settings/|migrations" + - repo: https://github.com/pre-commit/mirrors-mypy + rev: "v1.13.0" + hooks: + - id: mypy diff --git a/config.toml b/config.toml new file mode 100644 index 0000000..516ba25 --- /dev/null +++ b/config.toml @@ -0,0 +1,4 @@ +[mqtt] +username = "oin" +password = "n+Bi58l7LxbH5nEJ" +host = "homeassistant.local" diff --git a/oin_thermostat/__main__.py b/oin_thermostat/__main__.py index 853abdb..1e497d7 100644 --- a/oin_thermostat/__main__.py +++ b/oin_thermostat/__main__.py @@ -1,13 +1,18 @@ import logging +import tomllib from .mqtt import HAClient if __name__ == "__main__": + with open("config.toml", "rb") as config_file: + config = tomllib.load(config_file) + logging.basicConfig(level=logging.DEBUG) client = HAClient( "climate.chaudiere", ["sensor.esptic_tempo", "sensor.rte_tempo_prochaine_couleur"], + config.get("mqtt", dict()), ) client.connect() diff --git a/oin_thermostat/mqtt.py b/oin_thermostat/mqtt.py index 7a71055..abf34ff 100644 --- a/oin_thermostat/mqtt.py +++ b/oin_thermostat/mqtt.py @@ -10,21 +10,30 @@ logger = logging.getLogger(__name__) 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.secondary_entities = secondary_entities + self.config = config self.state_topic = "oin/state" self.availability_topic = "oin/availability" 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.selector = Selector(self.send_data) @property - def ha_options(self): + def ha_options(self) -> dict[str, str | dict[str, str]]: return { "dev": { "ids": "oin", @@ -38,10 +47,10 @@ class HAClient: "cmps": self.selector.ha_options, } - def connect(self): + def connect(self) -> None: logger.debug("Connecting to HA...") 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) for entity in self.secondary_entities: