This repository has been archived by the owner on Nov 10, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ha_publisher.py
336 lines (310 loc) · 16.2 KB
/
ha_publisher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import json
import string
from threading import Lock
import requests
import time
from zeroconf import ServiceBrowser, ServiceStateChange
from zeroconf import Zeroconf
IDENTIFIER = "restsysmon"
HA_URL = "http://192.168.1.8:8123"
HA_TOKEN = "..."
class HASensorPusher:
def __init__(self, identifier=IDENTIFIER):
self.zero = None
self.browser = None
self.nodes = {}
self.running = False
self.identifier = identifier.encode("utf-8")
self.ttl = 10 # seconds between rounds of sensor readings
self.sleep_time = 0.3 # seconds between individual sensor readings
self.lock = Lock()
@staticmethod
def ha_binary_sensor_update(device_id, state="on", attrs=None):
device_id = device_id.replace(" ", "_")
for s in string.punctuation:
device_id = device_id.replace(s, "_")
attrs = attrs or {"friendly_name": device_id,
"state_color": True,
"device_class": "presence"}
print("updating binary sensor - ", device_id)
try:
response = requests.post(
f"{HA_URL}/api/states/binary_sensor.{IDENTIFIER}_{device_id}",
headers={
"Authorization": f"Bearer {HA_TOKEN}",
"content-type": "application/json",
},
data=json.dumps({"state": state, "attributes": attrs}),
)
print(response.text)
except:
print("failed to push data to HA")
@staticmethod
def ha_sensor_update(device_id, state="on", attrs=None):
attrs = attrs or {"friendly_name": device_id,
"state_color": True}
print("updating sensor - ", device_id)
try:
response = requests.post(
f"{HA_URL}/api/states/sensor.{IDENTIFIER}_{device_id}",
headers={
"Authorization": f"Bearer {HA_TOKEN}",
"content-type": "application/json",
},
data=json.dumps({"state": state, "attributes": attrs}),
)
print(response.text)
except:
print("failed to push data to HA")
def on_new_node(self, node):
device_id = node["name"]
print("found", node)
self.on_node_update(node)
self.get_readings()
self.get_info(device_id) # one time sensor readings
def on_lost_node(self, node):
print("lost", node)
self.on_node_update(node)
def on_node_update(self, node):
device_id = node["name"]
self.nodes[device_id] = node
self.ha_binary_sensor_update(device_id, state=node["state"],
attrs={"friendly_name": node["name"],
"state_color": True,
"device_class": "presence"})
def on_service_state_change(self, zeroconf, service_type, name,
state_change):
info = zeroconf.get_service_info(service_type, name)
if info and info.properties:
for key, value in info.properties.items():
if key == b"type" and value == self.identifier:
host = info._properties[b"host"].decode("utf-8")
port = info._properties[b"port"].decode("utf-8")
name = info._properties[b"name"].decode("utf-8")
node = {"host": host, "port": port, "name": name}
node["last_seen"] = time.time()
if state_change is ServiceStateChange.Added:
node["state"] = "on"
self.on_new_node(node)
elif ServiceStateChange.Removed:
node["state"] = "off"
self.on_lost_node(node)
else:
node["state"] = "on"
self.on_node_update(node)
def start(self):
self.zero = Zeroconf()
self.browser = ServiceBrowser(self.zero, "_http._tcp.local.",
handlers=[self.on_service_state_change])
self.running = True
while self.running:
try:
self.get_readings()
time.sleep(self.ttl)
except KeyboardInterrupt:
break
self.stop()
def stop(self):
if self.zero:
self.zero.close()
self.zero = None
self.browser = None
self.running = False
def get_info(self, device_id):
data = self.nodes[device_id]
url = f"{data['host']}:{data['port']}"
readings = {
"os": f"{url}/os_name",
"boot_time": f"{url}/boot_time",
"system": f"{url}/system",
"release": f"{url}/release",
"machine": f"{url}/machine",
"architecture": f"{url}/architecture",
"cpu_count": f"{url}/cpu_count",
"fans": f"{url}/fans",
"network_interfaces": f"{url}/network_interfaces",
"external_ip": f"{url}/external_ip",
"pulseaudio_version": f"{url}/pulseaudio/version",
"is_systemd": f"{url}/is_systemd",
"is_dbus": f"{url}/is_dbus",
"cpu_freq_max": f"{url}/cpu_freq_max",
"cpu_freq_min": f"{url}/cpu_freq_min",
"disk_total": f"{url}/disk_total",
"pulseaudio_hostname": f"{url}/pulseaudio/hostname",
"memory_total": f"{url}/memory_total"
}
for sensor_name, url in readings.items():
self.read_sensor(device_id, sensor_name, url)
time.sleep(self.sleep_time)
def read_sensor(self, device_id, sensor_name, url):
sensor_id = f"{device_id}_{sensor_name}"
try:
res = requests.get("http://" + url)
try:
res = res.json()
except:
res = res.text
try: # numeric
if "/is_" in url:
res = str(res) == "1"
self.ha_binary_sensor_update(sensor_id, state="on" if res else "off",
attrs={"friendly_name": sensor_name,
"state_color": True,
"device_class": "presence"})
elif url.endswith("_count"):
res = int(res)
a = {"friendly_name": sensor_name,
"state_color": True}
self.ha_sensor_update(sensor_id, state=str(res), attrs=a)
else:
res = float(res) # numeric reading
a = {"friendly_name": sensor_name,
"state_color": True}
if "temperature" in url:
a["unit_of_measurement"] = "°C"
self.ha_sensor_update(sensor_id, state=str(res), attrs=a)
except Exception as e:
# text
if isinstance(res, list):
if sensor_id.endswith("pulseaudio_list_cards"):
for e in res:
sensor_id = f"{device_id}_pulseaudio_card_{e['index']}"
f = e.get("card_name") or e.get("name") or sensor_id
self.ha_binary_sensor_update(sensor_id, state="on",
attrs={"friendly_name": f.lower().strip(),
"state_color": True,
"device_class": "presence"})
elif sensor_id.endswith("pulseaudio_list_sources"):
for e in res:
sensor_id = f"{device_id}_pulseaudio_source_{e['index']}"
self.ha_binary_sensor_update(sensor_id, state="on",
attrs={"friendly_name": e.get("name") or sensor_id,
"state_color": True,
"device_class": "presence"})
elif sensor_id.endswith("pulseaudio_list_sinks"):
for e in res:
sensor_id = f"{device_id}_pulseaudio_sink_{e['index']}"
self.ha_binary_sensor_update(sensor_id, state="on",
attrs={"friendly_name": e.get("name") or sensor_id,
"state_color": True,
"device_class": "presence"})
elif sensor_id.endswith("pulseaudio_list_input_sinks"):
# streams of interest, even if not currently playing will report a status
APPS = [
"netflix",
"spotify"
]
for app in APPS:
if any(app == _["application"] for _ in res):
# app names handled below, we just want streams here
# eg, detect netflix in firefox via pulseaudio playback
continue
if any(app == _.get('media_name', "").lower() for _ in res):
# detected application running via pulseaudio playback
state = "on"
mute_state = "off" # TODO - check the playback info
else:
state = mute_state = "off"
sensor_id = f"{device_id}_pulseaudio_stream_{app}"
self.ha_binary_sensor_update(sensor_id, state=state,
attrs={"friendly_name": app,
"state_color": True,
"device_class": "presence"})
sensor_id = f"{device_id}_pulseaudio_stream_{app}_muted"
self.ha_binary_sensor_update(sensor_id, state=mute_state,
attrs={"friendly_name": app + " muted",
"state_color": True,
"device_class": "presence"})
for e in res:
sensor_id = f"{device_id}_pulseaudio_stream_{e['application']}"
f = e.get('media_name') or e.get('name') or e.get('application')
self.ha_binary_sensor_update(sensor_id, state="on" if e.get("playing") else "off",
attrs={"friendly_name": f.lower().strip(),
"state_color": True,
"device_class": "presence"})
sensor_id = f"{device_id}_pulseaudio_stream_{e['application']}_muted"
f = e.get('media_name') or e.get('name') or e.get('application')
self.ha_binary_sensor_update(sensor_id, state="on" if e.get("mute") else "off",
attrs={"friendly_name": f.lower().strip() + " muted",
"state_color": True,
"device_class": "presence"})
sensor_id = f"{device_id}_pulseaudio_stream_{e['application']}_sink"
f = e.get('media_name') or e.get('name') or e.get('application')
self.ha_sensor_update(sensor_id, state=str(e["sink"]),
attrs={"friendly_name": f.lower().strip() + " sink",
"state_color": True,
"device_class": "presence"})
elif sensor_id.endswith("network_interfaces"):
for e in res:
sensor_id = f"{device_id}_network_if_{e}"
self.ha_binary_sensor_update(sensor_id, state="on",
attrs={"friendly_name": e,
"state_color": True,
"device_class": "presence"})
else:
print("## TODO", sensor_id, res)
pass
elif isinstance(res, dict):
# print("## TODO", sensor_id, res)
pass
else:
a = {"friendly_name": sensor_name,
"state_color": True}
self.ha_sensor_update(sensor_id, state=str(res), attrs=a)
except:
return False
return True
def get_readings(self):
with self.lock:
for device_id, data in dict(self.nodes).items():
alive = False
if data["state"] == "on":
url = f"{data['host']}:{data['port']}"
readings = {
"swap_usage": f"{url}/swap_usage",
"swap_total": f"{url}/swap_total",
"disk_usage": f"{url}/disk_usage",
"external_ip": f"{url}/external_ip",
"is_kdeconnect": f"{url}/is_kdeconnect",
"is_pulseaudio": f"{url}/is_pulseaudio",
"is_pipewire": f"{url}/is_pipewire",
"is_plasma": f"{url}/is_plasma",
"is_hivemind": f"{url}/is_hivemind",
"is_ovos": f"{url}/is_ovos",
"is_spotify": f"{url}/is_spotify",
"is_firefox": f"{url}/is_firefox",
"is_upmpdcli": f"{url}/is_upmpdcli",
"is_minidlna": f"{url}/is_minidlna",
"pulseaudio_channel_count": f"{url}/pulseaudio/channel_count",
"pulseaudio_default_sink": f"{url}/pulseaudio/default_sink",
"pulseaudio_default_source": f"{url}/pulseaudio/default_source",
"pulseaudio_list_cards": f"{url}/pulseaudio/list_cards",
"pulseaudio_list_sources": f"{url}/pulseaudio/list_sources",
"pulseaudio_list_sinks": f"{url}/pulseaudio/list_sinks",
"pulseaudio_list_input_sinks": f"{url}/pulseaudio/list_input_sinks",
"cpu_usage": f"{url}/cpu_usage",
"cpu_freq": f"{url}/cpu_freq",
"cpu_temperature": f"{url}/cpu_temperature",
"memory_usage": f"{url}/memory_usage",
"disk_percent": f"{url}/disk_percent",
"battery_percent": f"{url}/battery_percent",
"pulseaudio_now_playing": f"{url}/pulseaudio/now_playing",
"pulseaudio_is_playing": f"{url}/pulseaudio/is_playing",
"pulseaudio_bluez_active": f"{url}/pulseaudio/is_bluez_active",
"pulseaudio_bluez_connected": f"{url}/pulseaudio/is_bluez_connected",
"brightness": f"{url}/brightness"
}
for sensor_name, url in readings.items():
alive = self.read_sensor(device_id, sensor_name, url) or alive
time.sleep(self.sleep_time)
data["state"] = alive
self.on_node_update(data)
if __name__ == "__main__":
ha = HASensorPusher()
ha.start()
while True:
try:
time.sleep(1)
except KeyboardInterrupt:
break
ha.stop()