From 119feee5e5044c688722eb6535a10f968c658916 Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" Date: Sun, 8 Dec 2024 11:17:25 +0100 Subject: [PATCH] Move main config to config.toml --- config.toml | 12 +++++++++++- oin_thermostat/__main__.py | 27 +++++++++++++++++++++------ oin_thermostat/mqtt.py | 15 ++++++++++----- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/config.toml b/config.toml index 516ba25..498e003 100644 --- a/config.toml +++ b/config.toml @@ -1,4 +1,14 @@ -[mqtt] +[logging] +level = "DEBUG" + +[homeassistant] +entity = "climate.chaudiere" +secondary_entities = [ + "sensor.esptic_tempo", + "sensor.rte_tempo_prochaine_couleur", +] + +[homeassistant.mqtt] username = "oin" password = "n+Bi58l7LxbH5nEJ" host = "homeassistant.local" diff --git a/oin_thermostat/__main__.py b/oin_thermostat/__main__.py index 1e497d7..b64e819 100644 --- a/oin_thermostat/__main__.py +++ b/oin_thermostat/__main__.py @@ -1,20 +1,35 @@ import logging +import sys import tomllib from .mqtt import HAClient -if __name__ == "__main__": - with open("config.toml", "rb") as config_file: +logger = logging.getLogger(__name__) +config_path = "config.toml" + + +def main(): + with open(config_path, "rb") as config_file: config = tomllib.load(config_file) - logging.basicConfig(level=logging.DEBUG) + logging.basicConfig(**config.get("logging", dict())) + + ha_config = config.get("homeassistant") + if ha_config is None: + logger.error(f"Missing home assistant config in <{config_path}>") + logger.error(f"\t{config}") + sys.exit(1) client = HAClient( - "climate.chaudiere", - ["sensor.esptic_tempo", "sensor.rte_tempo_prochaine_couleur"], - config.get("mqtt", dict()), + ha_config.get("entity"), + ha_config.get("secondary_entities"), + mqtt_config=ha_config.get("mqtt"), ) client.connect() client.loop() + + +if __name__ == "__main__": + main() diff --git a/oin_thermostat/mqtt.py b/oin_thermostat/mqtt.py index abf34ff..c20dc86 100644 --- a/oin_thermostat/mqtt.py +++ b/oin_thermostat/mqtt.py @@ -14,18 +14,20 @@ class HAClient: self, entity: str, secondary_entities: list[str] = [], - config: dict = dict(), + mqtt_config: dict = dict(), ) -> None: self.entity = entity self.secondary_entities = secondary_entities - self.config = config + self.config = mqtt_config self.state_topic = "oin/state" self.availability_topic = "oin/availability" self.client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) + username = self.config.get("username", None) + logger.debug(f"Setting up MQTT with user <{username}>") self.client.username_pw_set( - username=self.config.get("username", None), + username=username, password=self.config.get("password", None), ) @@ -48,9 +50,12 @@ class HAClient: } def connect(self) -> None: - logger.debug("Connecting to HA...") self.client.will_set(self.availability_topic, "offline", retain=True) - self.client.connect(self.config.get("host"), self.config.get("port", 1883)) + + host = self.config.get("host") + port = self.config.get("port", 1883) + logger.debug(f"Connecting to <{host}> on port <{port}>") + self.client.connect(host, port) self.subscribe(entity_topic(self.entity), self.state_update) for entity in self.secondary_entities: