diff --git a/hasspy/__main__.py b/hasspy/__main__.py index 688d500..92744e9 100644 --- a/hasspy/__main__.py +++ b/hasspy/__main__.py @@ -1,5 +1,6 @@ import logging import tomllib +from pathlib import Path from hasspy.mqtt import HassClient @@ -7,7 +8,18 @@ from hasspy.mqtt import HassClient def main() -> None: logging.basicConfig(level=logging.INFO) - with open("/etc/hasspy/config.toml", "rb") as file: + config_file = next( + ( + x + for x in (Path("/etc/hasspy.toml"), Path("/etc/hasspy/config.toml")) + if x.exists() + ), + None, + ) + if not config_file: + raise FileNotFoundError("No configuration file found") + + with open(config_file, "rb") as file: config = tomllib.load(file) ha = HassClient( diff --git a/hasspy/mqtt.py b/hasspy/mqtt.py index 75fec3b..e50a0fc 100644 --- a/hasspy/mqtt.py +++ b/hasspy/mqtt.py @@ -74,14 +74,17 @@ class HassClient(Client): def on_command(self, client: Client, userdata: Any, message: MQTTMessage) -> None: payload = message.payload.decode("utf-8") - if not self.power_on and payload == "POWER_ON": - log.info("Cancelling shutdown…") - self.power_on = True - run(["systemctl", "poweroff", "--when=cancel"]) - elif self.power_on and payload == "POWER_OFF": - log.info("Powering off…") - self.power_on = False - run(["systemctl", "poweroff", "--when=+1m"]) + match payload: + case "POWER_ON": + if not self.power_on: + log.info("Cancelling shutdown…") + self.power_on = True + run(["systemctl", "poweroff", "--when=cancel"]) + case "POWER_OFF": + if self.power_on: + log.info("Powering off…") + self.power_on = False + run(["systemctl", "poweroff", "--when=+1m"]) self.publish_state()