Compare commits

...

2 commits

2 changed files with 24 additions and 9 deletions

View file

@ -1,5 +1,6 @@
import logging import logging
import tomllib import tomllib
from pathlib import Path
from hasspy.mqtt import HassClient from hasspy.mqtt import HassClient
@ -7,7 +8,18 @@ from hasspy.mqtt import HassClient
def main() -> None: def main() -> None:
logging.basicConfig(level=logging.INFO) 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) config = tomllib.load(file)
ha = HassClient( ha = HassClient(

View file

@ -74,14 +74,17 @@ class HassClient(Client):
def on_command(self, client: Client, userdata: Any, message: MQTTMessage) -> None: def on_command(self, client: Client, userdata: Any, message: MQTTMessage) -> None:
payload = message.payload.decode("utf-8") payload = message.payload.decode("utf-8")
if not self.power_on and payload == "POWER_ON": match payload:
log.info("Cancelling shutdown…") case "POWER_ON":
self.power_on = True if not self.power_on:
run(["systemctl", "poweroff", "--when=cancel"]) log.info("Cancelling shutdown…")
elif self.power_on and payload == "POWER_OFF": self.power_on = True
log.info("Powering off…") run(["systemctl", "poweroff", "--when=cancel"])
self.power_on = False case "POWER_OFF":
run(["systemctl", "poweroff", "--when=+1m"]) if self.power_on:
log.info("Powering off…")
self.power_on = False
run(["systemctl", "poweroff", "--when=+1m"])
self.publish_state() self.publish_state()