Add logging to MQTT client and main application; create systemd service file
This commit is contained in:
parent
fa8bb1d8a9
commit
d827788da1
3 changed files with 21 additions and 2 deletions
|
@ -1,10 +1,13 @@
|
|||
import json
|
||||
import logging
|
||||
from subprocess import run
|
||||
from typing import Any, Mapping
|
||||
|
||||
from paho.mqtt.client import Client, MQTTMessage, MQTTMessageInfo
|
||||
from paho.mqtt.enums import CallbackAPIVersion, MQTTErrorCode
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class HassClient(Client):
|
||||
def __init__(self, node_id: str, config: Mapping[str, Any]) -> None:
|
||||
|
@ -61,6 +64,7 @@ class HassClient(Client):
|
|||
)
|
||||
|
||||
def on_connect(self, *args: Any, **kwargs: Any) -> None:
|
||||
log.info("Connected to MQTT broker")
|
||||
self.publish_discovery()
|
||||
self.publish_availability()
|
||||
self.init_subs()
|
||||
|
@ -71,11 +75,11 @@ class HassClient(Client):
|
|||
payload = message.payload.decode("utf-8")
|
||||
|
||||
if not self.power_on and payload == "POWER_ON":
|
||||
print("Cancelling shutdown…")
|
||||
log.info("Cancelling shutdown…")
|
||||
self.power_on = True
|
||||
run(["systemctl", "poweroff", "--when=cancel"])
|
||||
elif self.power_on and payload == "POWER_OFF":
|
||||
print("Powering off…")
|
||||
log.info("Powering off…")
|
||||
self.power_on = False
|
||||
run(["systemctl", "poweroff", "--when=+1m"])
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue