From 8a67a0b674a6b55b5a939214c4c9a0e1449ac862 Mon Sep 17 00:00:00 2001 From: "Edgar P. Burkhart" <git@edgarpierre.fr> Date: Sun, 9 Mar 2025 20:26:34 +0100 Subject: [PATCH] Add suspend command support and improve power state handling --- hasspy/mqtt.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/hasspy/mqtt.py b/hasspy/mqtt.py index 76932a1..5f60c67 100644 --- a/hasspy/mqtt.py +++ b/hasspy/mqtt.py @@ -2,6 +2,7 @@ import io import json import logging import re +from pathlib import Path from subprocess import run from threading import Thread, Timer from typing import Any, Mapping, Tuple @@ -25,7 +26,6 @@ class HassClient(Client): self.username_pw_set(username, self.config.get("password")) self.interval = self.config.get("interval", 60) - self.power_on = True self.timer = Timer(0, self.publish_state) self.cover = "" @@ -140,6 +140,7 @@ class HassSystemClient(HassClient): "POWER_ON": ["systemctl", "poweroff", "--when=cancel"], "POWER_OFF": ["systemctl", "poweroff", "--when=+1m"], "LOCK": ["loginctl", "lock-sessions"], + "SUSPEND": ["systemctl", "suspend"], } def do_command(self, cmd: str, value: str = "") -> None: @@ -149,11 +150,6 @@ class HassSystemClient(HassClient): if code != 0: log.error(f"Failed to execute command: {cmd}") - if cmd == "POWER_ON": - self.power_on = True - elif cmd == "POWER_OFF": - self.power_on = False - @property def components(self) -> dict[str, dict[str, Any]]: return { @@ -173,12 +169,21 @@ class HassSystemClient(HassClient): "icon": "mdi:account-lock", "payload_press": "LOCK", }, + "suspend": { + "unique_id": f"{self.node_id}_suspend", + "p": "button", + "name": "Suspend", + "icon": "mdi:sleep", + "payload_press": "SUSPEND", + }, } @property def state_payload(self) -> dict[str, Any]: return { - "power": "POWER_ON" if self.power_on else "POWER_OFF", + "power": "POWER_OFF" + if Path("/run/systemd/shutdown/scheduled").exists() + else "POWER_ON", }