From d48cc1b785df45ec32c85ceee2892515d2042705 Mon Sep 17 00:00:00 2001
From: "Edgar P. Burkhart" <git@edgarpierre.fr>
Date: Sun, 9 Mar 2025 23:20:42 +0100
Subject: [PATCH] Add power notification handling and improve subscription
 management

---
 hasspy/mqtt.py | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/hasspy/mqtt.py b/hasspy/mqtt.py
index 43e5e02..31c52a0 100644
--- a/hasspy/mqtt.py
+++ b/hasspy/mqtt.py
@@ -151,6 +151,15 @@ class HassSystemClient(HassClient):
             if code != 0:
                 log.error(f"Failed to execute command: {cmd}")
 
+        match cmd:
+            case "POWER_OFF":
+                self.publish(
+                    f"{self.node_id}/user/notify",
+                    "System will be shutting down in 1 minute.",
+                )
+            case "POWER_ON":
+                self.publish(f"{self.node_id}/user/notify", "Shutdown cancelled.")
+
     @property
     def state_topic(self) -> str:
         return f"{self.node_id}/system/state"
@@ -243,10 +252,25 @@ class HassUserClient(HassClient):
                 if code != 0:
                     log.error(f"Failed to execute command: {cmd}:{value}")
 
+    def init_subs(self) -> None:
+        super().init_subs()
+        self.subscribe(self.notify_topic)
+        self.message_callback_add(self.notify_topic, self.on_notify)
+
+    def on_notify(self, client: Client, userdata: Any, message: MQTTMessage) -> None:
+        payload = message.payload.decode("utf-8")
+
+        log.info(f"Received notification: {payload}")
+        run_command(["notify-send", payload])
+
     @property
     def availability_topic(self) -> str:
         return f"{self.node_id}/user/availability"
 
+    @property
+    def notify_topic(self) -> str:
+        return f"{self.node_id}/user/notify"
+
     @property
     def state_topic(self) -> str:
         return f"{self.node_id}/user/state"