Add HA switch for kiosk telemetry
This commit is contained in:
1 parent
0b7e6a0107
commit
5f67bab758
1 file changed
+37
-1
@@ -6,6 +6,7 @@ import glob
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import socket
|
import socket
|
||||||
|
import threading
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import psutil
|
import psutil
|
||||||
@@ -27,6 +28,14 @@ SAMPLE_SECONDS = max(5, int(os.environ.get("SAMPLE_SECONDS", "10")))
|
|||||||
STATE_TOPIC = f"{MQTT_TOPIC}/state"
|
STATE_TOPIC = f"{MQTT_TOPIC}/state"
|
||||||
AVAILABILITY_TOPIC = f"{MQTT_TOPIC}/availability"
|
AVAILABILITY_TOPIC = f"{MQTT_TOPIC}/availability"
|
||||||
DISCOVERY_PREFIX = os.environ.get("MQTT_DISCOVERY_PREFIX", "homeassistant")
|
DISCOVERY_PREFIX = os.environ.get("MQTT_DISCOVERY_PREFIX", "homeassistant")
|
||||||
|
TELEMETRY_COMMAND_TOPIC = os.environ.get(
|
||||||
|
"TELEMETRY_COMMAND_TOPIC",
|
||||||
|
f"viewroad-commands/{KIOSK_ID}/telemetry/set",
|
||||||
|
)
|
||||||
|
TELEMETRY_STATE_TOPIC = os.environ.get(
|
||||||
|
"TELEMETRY_STATE_TOPIC",
|
||||||
|
f"viewroad-status/{KIOSK_ID}/telemetry/state",
|
||||||
|
)
|
||||||
|
|
||||||
DEVICE = {
|
DEVICE = {
|
||||||
"identifiers": [KIOSK_ENTITY_PREFIX],
|
"identifiers": [KIOSK_ENTITY_PREFIX],
|
||||||
@@ -35,6 +44,8 @@ DEVICE = {
|
|||||||
"model": KIOSK_MODEL,
|
"model": KIOSK_MODEL,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TELEMETRY_ENABLED = threading.Event()
|
||||||
|
|
||||||
METRICS = {
|
METRICS = {
|
||||||
"system_cpu_percent": ("System CPU", "%", None, "measurement", "mdi:cpu-64-bit"),
|
"system_cpu_percent": ("System CPU", "%", None, "measurement", "mdi:cpu-64-bit"),
|
||||||
"system_iowait_percent": ("CPU I/O Wait", "%", None, "measurement", "mdi:timer-sand"),
|
"system_iowait_percent": ("CPU I/O Wait", "%", None, "measurement", "mdi:timer-sand"),
|
||||||
@@ -157,10 +168,29 @@ def publish_discovery(client):
|
|||||||
client.publish(topic, json.dumps(config, separators=(",", ":")), qos=1, retain=True)
|
client.publish(topic, json.dumps(config, separators=(",", ":")), qos=1, retain=True)
|
||||||
|
|
||||||
|
|
||||||
|
def on_message(client, userdata, message):
|
||||||
|
payload = message.payload.decode("utf-8", errors="replace").strip().upper()
|
||||||
|
if payload == "ON":
|
||||||
|
TELEMETRY_ENABLED.set()
|
||||||
|
elif payload == "OFF":
|
||||||
|
TELEMETRY_ENABLED.clear()
|
||||||
|
else:
|
||||||
|
return
|
||||||
|
|
||||||
|
client.publish(TELEMETRY_STATE_TOPIC, payload, qos=1, retain=True)
|
||||||
|
|
||||||
|
|
||||||
def on_connect(client, userdata, flags, reason_code, properties=None):
|
def on_connect(client, userdata, flags, reason_code, properties=None):
|
||||||
"""Restore all MQTT state after the initial connection or a reconnect."""
|
"""Restore all MQTT state after the initial connection or a reconnect."""
|
||||||
publish_discovery(client)
|
publish_discovery(client)
|
||||||
client.publish(AVAILABILITY_TOPIC, "online", qos=1, retain=True)
|
client.publish(AVAILABILITY_TOPIC, "online", qos=1, retain=True)
|
||||||
|
client.subscribe(TELEMETRY_COMMAND_TOPIC, qos=1)
|
||||||
|
client.publish(
|
||||||
|
TELEMETRY_STATE_TOPIC,
|
||||||
|
"ON" if TELEMETRY_ENABLED.is_set() else "OFF",
|
||||||
|
qos=1,
|
||||||
|
retain=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def collect_sample():
|
def collect_sample():
|
||||||
@@ -227,13 +257,19 @@ def main():
|
|||||||
client.username_pw_set(MQTT_USER, MQTT_PASSWORD)
|
client.username_pw_set(MQTT_USER, MQTT_PASSWORD)
|
||||||
client.will_set(AVAILABILITY_TOPIC, "offline", qos=1, retain=True)
|
client.will_set(AVAILABILITY_TOPIC, "offline", qos=1, retain=True)
|
||||||
client.on_connect = on_connect
|
client.on_connect = on_connect
|
||||||
|
client.on_message = on_message
|
||||||
client.reconnect_delay_set(min_delay=1, max_delay=30)
|
client.reconnect_delay_set(min_delay=1, max_delay=30)
|
||||||
client.connect(MQTT_HOST, MQTT_PORT, keepalive=60)
|
client.connect(MQTT_HOST, MQTT_PORT, keepalive=60)
|
||||||
client.loop_start()
|
client.loop_start()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
payload = json.dumps(collect_sample(), separators=(",", ":"), allow_nan=False)
|
if not TELEMETRY_ENABLED.wait(timeout=1):
|
||||||
|
continue
|
||||||
|
|
||||||
|
sample = collect_sample()
|
||||||
|
if TELEMETRY_ENABLED.is_set():
|
||||||
|
payload = json.dumps(sample, separators=(",", ":"), allow_nan=False)
|
||||||
client.publish(STATE_TOPIC, payload, qos=0, retain=False)
|
client.publish(STATE_TOPIC, payload, qos=0, retain=False)
|
||||||
finally:
|
finally:
|
||||||
client.publish(AVAILABILITY_TOPIC, "offline", qos=1, retain=True).wait_for_publish()
|
client.publish(AVAILABILITY_TOPIC, "offline", qos=1, retain=True).wait_for_publish()
|
||||||
|
|||||||
Reference in new issue
Block a user