Compare commits
2 commits
2042c413c7
...
be2381429f
Author | SHA1 | Date | |
---|---|---|---|
be2381429f | |||
98e1e970c1 |
2 changed files with 24 additions and 9 deletions
hasspy
|
@ -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(
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue