-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths2m.py
More file actions
executable file
·658 lines (613 loc) · 32.7 KB
/
s2m.py
File metadata and controls
executable file
·658 lines (613 loc) · 32.7 KB
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
#!/usr/bin/env python3
# send all system info to mqtt
import logging, time, os, sys, ast
from decimal import Decimal
from subprocess import check_call
from libs.system_info import get_temps, Platform, get_hostname, get_disks, get_disk_space, get_memory, get_cpu, set_proc, get_argon_fan_speed
from libs.myqtt import Myqtt
from libs.optimox import OptiMOX, prox_auth
from libs.parser import Parser
from libs.argon import gethddtemp
from libs.homeassistant import ha_config
hostname = get_hostname()
def setupLogging(DEBUG_MODE=False, conf=None):
config = conf
title = config.COMPUTER_NAME.upper()
log_dir = config.LOG_DIR
if log_dir.startswith("./"):
log_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), log_dir.split("./")[1])
print("Log Dir: ", log_dir)
log_filename = os.path.join(log_dir, config.LOG_FILENAME)
old_log_filename = os.path.join(log_dir, config.OLD_LOG_FILENAME)
try:
os.makedirs(log_dir, exist_ok=True)
except Exception as e:
print(e)
if os.path.exists(log_filename):
if os.path.exists(old_log_filename):
try: os.remove(old_log_filename)
except: print("Couldn't remove old log")
os.rename(log_filename, old_log_filename)
if DEBUG_MODE:
log_level = logging.DEBUG
else:
log_level = logging.INFO
format = "{}: [%(levelname)s] [%(module)s:%(lineno)d] ".format(title) + " - %(asctime)s - %(message)s"
if DEBUG_MODE:
logging.basicConfig(level=log_level,format=format,handlers=[logging.FileHandler(log_filename),logging.StreamHandler()])
else:
logging.basicConfig(filename=log_filename, level=log_level, format=format)
################################################################################################
class System2Mqtt(object):
def __init__(self, conf):
print("Getting config from:\n", conf)
self.config = Parser(conf)
setupLogging(self.config.DEBUG_LOG, self.config)
logging.debug("logging has been set up")
self.lwt_topic = self.config.MQTT_BASE_TOPIC + "/LWT"
self.availability_topic = self.lwt_topic
self.myqtt = Myqtt(host=self.config.MQTT_HOST,
port=self.config.MQTT_PORT,
username=self.config.MQTT_USER,
password=self.config.MQTT_PASSWORD)
self.myqtt.lwt_topic = self.lwt_topic
self.myqtt.topic_callbacks = self.__get_subscription_calbacks()
if self.config.PVE_SYSTEM:
self.pve = OptiMOX(prox_auth(self.config.PVE_HOST,
self.config.PVE_USER,
self.config.PVE_PASSWORD))
self.auto_reconnect = True
self.mounted_disks = []
self.ha_discovery_template = "{}/{}/{}/config".format(self.config.HA_DISCOVERY_BASE, "{}", "{}")
self.publish_period = self.config.PUBLISH_PERIOD
self.first_loop_done = False
def __get_subscription_calbacks(self):
logging.debug("")
sub_dict = {self.config.MQTT_BASE_TOPIC + "/tele/PUBLISH_PERIOD": self.s2m_set_publish_period,
self.config.MQTT_BASE_TOPIC + "/callbacks/s2m_quit": self.quit_s2m,
self.config.MQTT_BASE_TOPIC + "/callbacks/shutdown": self.cb_shutdown,
self.config.MQTT_BASE_TOPIC + "/callbacks/reboot": self.cb_reboot}
return sub_dict
def run(self):
self.config.print_config()
try:
self.process_user_callbacks()
except Exception as e:
logging.error(e, exc_info=True)
self.myqtt.run()
self.wait()
def wait(self):
logging.debug("...")
while True:
while not self.myqtt.client.is_connected():
logging.debug("Trying to connect...")
time.sleep(2)
self.start_publish_loop()
if not self.auto_reconnect:
break
logging.info("Reconnecting...")
logging.warning("Main Loop Ended!")
def start_publish_loop(self):
logging.info("Publish period is set to {} seconds.".format(self.publish_period))
while self.myqtt.client.is_connected():
logging.debug("mqtt flag: {}".format(self.myqtt.connected_flag))
self.publish_all()
time.sleep(int(self.publish_period))
def publish_mount_state(self):
logging.debug("")
ha_type = "binary_sensor"
ha_class = "connectivity"
slug = "/disks/mount/"
base = self.config.MQTT_BASE_TOPIC + slug
try:
if not self.config.PVE_SYSTEM:
disks = get_disks(procpath=self.config.PROCPATH)
for d in disks:
if d == "/":
label = "sysroot"
else:
label = d.split("/")[-1]
final_topic = base + label
logging.debug("\nlabel: {}\n".format(label))
if self.config.STORAGE_INCLUDE:
include = ast.literal_eval(self.config.STORAGE_INCLUDE.lower())
if label.lower() in include:
logging.debug("...included")
pass
else:
logging.debug("...ignoring")
continue
else:
if self.config.STORAGE_EXCLUDE:
exclude = ast.literal_eval(self.config.STORAGE_EXCLUDE.lower())
if not label.lower() in exclude:
pass
else:
continue
logging.info("{} is mounted - publishing to '{}'".format(label, final_topic))
self.myqtt.publish(final_topic, "mounted")
if self.config.HA_DISCOVERY and not self.first_loop_done:
title = label
for char in (" ", "-"):
label = label.lower().replace(char, "_")
device = self.config.COMPUTER_NAME
logging.debug("device: {}".format(device))
for char in (" ", "-"):
device = device.replace(char, "_")
ha_object_id = "s2m_"+device+"_{}_{}".format(label, "mounted")
logging.debug("object_id: {}".format(ha_object_id))
ha_name = "{} Mount State".format(title).title()
logging.debug("ha_name: {}".format(ha_name))
logging.debug("ha_type: {}".format(ha_type))
dtt = self.ha_discovery_template.format(ha_type, ha_object_id)
logging.debug("dtt: {}".format(dtt))
haconfig = ha_config(discovery_topic=dtt, name=ha_name, object_id=ha_object_id,
state_topic=final_topic, device=device, payload_on="mounted",
off_delay=int(self.publish_period)+10, entity_type=ha_type, device_class=ha_class,
availability_topic=self.availability_topic,
payload_available="online",
payload_not_available="offline")
self.myqtt.publish(haconfig[0], haconfig[1], retain=True)
logging.debug("T: {}".format(haconfig[0]))
logging.debug("P: {}".format(haconfig[1]))
else:
logging.debug("HA Discovery not being sent")
elif self.config.PVE_SYSTEM:
storage_data = self.pve.getNodeStorage(self.config.PVE_NODE_NAME)["data"]
for storage in storage_data:
label = storage["storage"]
state = storage["active"]
final_topic = base + label
if self.config.STORAGE_INCLUDE:
include = ast.literal_eval(self.config.STORAGE_INCLUDE.lower())
if label.lower() in include:
pass
else:
continue
else:
if self.config.STORAGE_EXCLUDE:
exclude = ast.literal_eval(self.config.STORAGE_EXCLUDE.lower())
if not label.lower() in exclude:
pass
else:
continue
logging.info("Storage: {}: {}%".format(label, state))
self.myqtt.publish(final_topic, state)
if self.config.HA_DISCOVERY and not self.first_loop_done:
title = label
for char in (" ", "-"):
label = label.lower().replace(char, "_")
device = self.config.COMPUTER_NAME
for char in (" ", "-"):
device = device.replace(char, "_")
ha_object_id = "s2m_"+device+"_{}_{}".format(label, "mounted")
ha_name = "{} Mount State".format(title).title()
dtt = self.ha_discovery_template.format(ha_type, ha_object_id)
haconfig = ha_config(discovery_topic=dtt, name=ha_name, object_id=ha_object_id,
entity_type = ha_type, device_class = ha_class,
state_topic=final_topic, device=device, payload_on=1, payload_off=0,
availability_topic=self.availability_topic,
payload_available="online",
payload_not_available="offline")
self.myqtt.publish(haconfig[0], haconfig[1], retain=True)
else:
logging.warning("Hmm, something went wrong")
except Exception as e:
logging.error(e, exc_info=True)
def publish_disk_space(self):
logging.debug("")
ha_type = "sensor"
ha_icon = "mdi:harddisk"
ha_unit = "%"
slug = "/disks/storage/"
base = self.config.MQTT_BASE_TOPIC + slug
try:
if not self.config.PVE_SYSTEM:
disks = get_disks(procpath=self.config.PROCPATH)
for d in disks:
# space = get_disk_space(d, procpath=self.config.PROCPATH)
if d == "/":
label = "sysroot"
else:
label = d.split("/")[-1]
if self.config.STORAGE_INCLUDE:
include = ast.literal_eval(self.config.STORAGE_INCLUDE.lower())
logging.debug(include)
logging.debug(type(include))
if label.lower() in include:
pass
else:
continue
else:
if self.config.STORAGE_EXCLUDE:
exclude = ast.literal_eval(self.config.STORAGE_EXCLUDE.lower())
if not label.lower() in exclude:
pass
else:
continue
space = get_disk_space(d, procpath=self.config.PROCPATH)
final_topic = base + label
self.myqtt.publish(final_topic, space)
if self.config.HA_DISCOVERY and not self.first_loop_done:
title = label
for char in (" ", "-"):
label = label.lower().replace(char, "_")
device = self.config.COMPUTER_NAME
for char in (" ", "-"):
device = device.replace(char, "_")
ha_object_id = "s2m_"+device+"_{}_{}".format(label, "storage")
ha_name = "{} Storage".format(title).title()
dtt = self.ha_discovery_template.format(ha_type, ha_object_id)
haconfig = ha_config(discovery_topic=dtt, name=ha_name, object_id=ha_object_id, state_topic=final_topic,
device=device, icon=ha_icon, entity_type=ha_type, unit=ha_unit,
availability_topic=self.availability_topic,
payload_available="online",
payload_not_available="offline")
self.myqtt.publish(haconfig[0], haconfig[1], retain=True)
elif self.config.PVE_SYSTEM:
storage_data = self.pve.getNodeStorage(self.config.PVE_NODE_NAME)["data"]
logging.debug(storage_data)
for storage in storage_data:
label = storage["storage"]
pct = int(float(storage["used_fraction"]) * 100)
final_topic = base + label
if self.config.STORAGE_INCLUDE:
include = ast.literal_eval(self.config.STORAGE_INCLUDE.lower())
if label.lower() in include:
pass
else:
continue
else:
if self.config.STORAGE_EXCLUDE:
exclude = ast.literal_eval(self.config.STORAGE_EXCLUDE.lower())
if not label.lower() in exclude:
pass
else:
continue
logging.info("Storage: {}: {}%".format(label, pct))
self.myqtt.publish(final_topic, pct)
if self.config.HA_DISCOVERY and not self.first_loop_done:
title = label
for char in (" ", "-"):
label = label.lower().replace(char, "_")
device = self.config.COMPUTER_NAME
for char in (" ", "-"):
device = device.replace(char, "_")
ha_object_id = "s2m_"+device+"_{}_{}".format(label, "storage")
ha_name = "{} Storage".format(title).title()
dtt = self.ha_discovery_template.format(ha_type, ha_object_id)
haconfig = ha_config(discovery_topic=dtt, name=ha_name, object_id=ha_object_id, state_topic=final_topic,
device=device, icon=ha_icon, entity_type=ha_type, unit=ha_unit,
availability_topic=self.availability_topic,
payload_available="online",
payload_not_available="offline")
self.myqtt.publish(haconfig[0], haconfig[1], retain=True)
else:
logging.warning("Hmm, something went wrong")
except Exception as e:
logging.error(e, exc_info=True)
def publish_cpu_temp(self):
logging.debug("")
ha_type = "sensor"
ha_class = "temperature"
# ha_icon = "mdi:thermometer"
ha_unit = "°C"
slug = "/cpu/temperature"
final_topic = self.config.MQTT_BASE_TOPIC + slug
try:
if self.config.MACOS:
try:
dec = Decimal(get_temps(procpath=self.config.PROCPATH))
temp = str(round(dec, 1))
except Exception as e:
logging.warning(e)
temp = get_temps(procpath=self.config.PROCPATH)
logging.info("CPU temperature: {}°C".format(temp))
self.myqtt.publish(final_topic, temp)
if self.config.HA_DISCOVERY and not self.first_loop_done:
title = self.config.COMPUTER_NAME + " CPU Temperature"
device = self.config.COMPUTER_NAME
for char in (" ", "-"):
device = device.replace(char, "_")
ha_object_id = "s2m_" + device + "_{}".format("cpu_temperature")
ha_name = title.title()
dtt = self.ha_discovery_template.format(ha_type, ha_object_id)
haconfig = ha_config(discovery_topic=dtt, name=ha_name, object_id=ha_object_id, state_topic=final_topic,
device=device, entity_type=ha_type, unit=ha_unit, device_class=ha_class,
availability_topic=self.availability_topic,
payload_available="online",
payload_not_available="offline")
self.myqtt.publish(haconfig[0], haconfig[1], retain=True)
else:
try:
temps = get_temps(procpath=self.config.PROCPATH)["coretemp"]
except:
temps = get_temps(procpath=self.config.PROCPATH)["cpu_thermal"]
c_list = []
for temp in temps:
c_list.append(temp.current)
highest = max(c_list)
logging.info("CPU temperature: {}°C".format(highest))
self.myqtt.publish(final_topic, str(highest))
if self.config.HA_DISCOVERY and not self.first_loop_done:
title = self.config.COMPUTER_NAME + " CPU Temperature"
device = self.config.COMPUTER_NAME
for char in (" ", "-"):
device = device.replace(char, "_")
ha_object_id = "s2m_" + device + "_{}".format("cpu_temperature")
ha_name = title.title()
dtt = self.ha_discovery_template.format(ha_type, ha_object_id)
haconfig = ha_config(discovery_topic=dtt, name=ha_name, object_id=ha_object_id, state_topic=final_topic,
device=device, entity_type=ha_type, unit=ha_unit, device_class=ha_class,
availability_topic=self.availability_topic,
payload_available="online",
payload_not_available="offline")
self.myqtt.publish(haconfig[0], haconfig[1], retain=True)
except Exception as e:
logging.error(e, exc_info=True)
def publish_cpu_usage(self):
logging.debug("")
ha_type = "sensor"
ha_unit = "%"
ha_icon = "mdi:cpu-64-bit"
slug = "/cpu/usage"
final_topic = self.config.MQTT_BASE_TOPIC + slug
try:
if not self.config.PVE_SYSTEM:
cpu = get_cpu(procpath=self.config.PROCPATH)
logging.info("CPU usage: {}%".format(cpu))
self.myqtt.publish(final_topic, cpu)
if self.config.HA_DISCOVERY and not self.first_loop_done:
title = self.config.COMPUTER_NAME + " CPU Usage"
device = self.config.COMPUTER_NAME
for char in (" ", "-"):
device = device.replace(char, "_")
ha_object_id = "s2m_" + device + "_{}".format("cpu")
ha_name = title.title()
dtt = self.ha_discovery_template.format(ha_type, ha_object_id)
haconfig = ha_config(discovery_topic=dtt, name=ha_name, object_id=ha_object_id, state_topic=final_topic,
device=device, icon=ha_icon, entity_type=ha_type, unit=ha_unit,
availability_topic=self.availability_topic,
payload_available="online",
payload_not_available="offline")
self.myqtt.publish(haconfig[0], haconfig[1], retain=True)
elif self.config.PVE_SYSTEM:
cpu = self.pve.getNodeStatus(self.config.PVE_NODE_NAME)["data"]["cpu"]
pct = int(float(cpu) * 100)
logging.info("CPU usage: {}%".format(pct))
if pct > 0:
self.myqtt.publish(final_topic, pct)
if self.config.HA_DISCOVERY and not self.first_loop_done:
title = self.config.COMPUTER_NAME + " CPU Usage"
device = self.config.COMPUTER_NAME
for char in (" ", "-"):
device = device.replace(char, "_")
ha_object_id = "s2m_" + device + "_{}".format("cpu")
ha_name = title.title()
dtt = self.ha_discovery_template.format(ha_type, ha_object_id)
haconfig = ha_config(discovery_topic=dtt, name=ha_name, object_id=ha_object_id, state_topic=final_topic,
device=device, icon=ha_icon, entity_type=ha_type, unit=ha_unit,
availability_topic=self.availability_topic,
payload_available="online",
payload_not_available="offline")
self.myqtt.publish(haconfig[0], haconfig[1], retain=True)
else:
logging.warning("Hmm, something went wrong")
except Exception as e:
logging.error(e, exc_info=True)
def publish_ram(self):
logging.debug("Getting ram")
ha_type = "sensor"
ha_unit = "%"
ha_icon = "mdi:memory"
slug = "/memory"
final_topic = self.config.MQTT_BASE_TOPIC + slug
try:
if not self.config.PVE_SYSTEM:
mem = get_memory(procpath=self.config.PROCPATH)
logging.info("Memory Used: {}%".format(mem))
self.myqtt.publish(final_topic, mem)
if self.config.HA_DISCOVERY and not self.first_loop_done:
title = self.config.COMPUTER_NAME + " Memory Usage"
device = self.config.COMPUTER_NAME
for char in (" ", "-"):
device = device.replace(char, "_")
ha_object_id = "s2m_" + device + "_{}".format("memory")
ha_name = title.title()
dtt = self.ha_discovery_template.format(ha_type, ha_object_id)
haconfig = ha_config(discovery_topic=dtt, name=ha_name, object_id=ha_object_id, state_topic=final_topic,
device=device, icon=ha_icon, entity_type=ha_type, unit=ha_unit,
availability_topic=self.availability_topic,
payload_available="online",
payload_not_available="offline")
self.myqtt.publish(haconfig[0], haconfig[1], retain=True)
elif self.config.PVE_SYSTEM:
ram_dict = self.pve.getNodeStatus(self.config.PVE_NODE_NAME)["data"]["memory"]
used = float(ram_dict["used"])
total = float(ram_dict["total"])
pct = int((used / total) * 100)
logging.info("Ram usage: {}%".format(pct))
self.myqtt.publish(final_topic, pct)
if self.config.HA_DISCOVERY and not self.first_loop_done:
title = self.config.COMPUTER_NAME + " Memory Usage"
device = self.config.COMPUTER_NAME
for char in (" ", "-"):
device = device.replace(char, "_")
ha_object_id = "s2m_" + device + "_{}".format("memory")
ha_name = title.title()
dtt = self.ha_discovery_template.format(ha_type, ha_object_id)
haconfig = ha_config(discovery_topic=dtt, name=ha_name, object_id=ha_object_id, state_topic=final_topic,
device=device, icon=ha_icon, entity_type=ha_type, unit=ha_unit,
availability_topic=self.availability_topic,
payload_available="online",
payload_not_available="offline")
self.myqtt.publish(haconfig[0], haconfig[1], retain=True)
else:
logging.warning("Not MacOS or PVE system")
except Exception as e:
logging.error(e, exc_info=True)
def publish_argon(self):
if self.config.ARGON:
logging.debug("Getting fan speed")
slug = "/fan_speed"
final_topic = self.config.MQTT_BASE_TOPIC + slug
ha_type = "sensor"
ha_unit = "%"
ha_icon = "mdi:fan"
try:
speed = get_argon_fan_speed()
logging.info("Fan Speed: {}%".format(speed))
self.myqtt.publish(final_topic, speed)
if self.config.HA_DISCOVERY and not self.first_loop_done:
title = "Argon Fan Speed"
device = self.config.COMPUTER_NAME
for char in (" ", "-"):
device = device.replace(char, "_")
ha_object_id = "s2m_" + device + "_{}".format("fan_speed")
ha_name = "{}".format(title).title()
dtt = self.ha_discovery_template.format(ha_type, ha_object_id)
haconfig = ha_config(discovery_topic=dtt, name=ha_name, object_id=ha_object_id,
state_topic=final_topic, device=device, icon=ha_icon,
entity_type=ha_type, unit=ha_unit,
availability_topic=self.availability_topic,
payload_available="online",
payload_not_available="offline")
self.myqtt.publish(haconfig[0], haconfig[1], retain=True)
except Exception as e:
logging.error(e, exc_info=True)
logging.debug("Getting hdd temperatures")
slug = "/disks/temperature"
ha_type = "sensor"
ha_class = "temperature"
# ha_icon = "mdi:thermometer"
ha_unit = "°C"
try:
temps = gethddtemp()
for disk, temp in temps.items():
final_topic = self.config.MQTT_BASE_TOPIC + slug + "/" +disk
logging.info("{}: {}°C".format(disk, temp))
self.myqtt.publish(final_topic, temp)
if self.config.HA_DISCOVERY and not self.first_loop_done:
title = disk
for char in (" ", "-"):
disk = disk.lower().replace(char, "_")
device = self.config.COMPUTER_NAME
for char in (" ", "-"):
device = device.replace(char, "_")
ha_object_id = "s2m_"+device+"_{}_{}".format(disk, "temperature")
ha_name = "{} Temperature".format(title).title()
dtt = self.ha_discovery_template.format(ha_type, ha_object_id)
haconfig = ha_config(discovery_topic=dtt, name=ha_name, object_id=ha_object_id,
state_topic=final_topic, device=device, entity_type=ha_type,
unit=ha_unit, device_class=ha_class,
availability_topic=self.availability_topic,
payload_available="online",
payload_not_available="offline")
self.myqtt.publish(haconfig[0], haconfig[1], retain=True)
except Exception as e:
logging.error(e, exc_info=True)
def publish_lwt_binary_sensor(self):
if self.config.HA_DISCOVERY:
ha_type = "binary_sensor"
ha_class = "connectivity"
device = self.config.COMPUTER_NAME.replace(" ", "_").replace("-", "_")
ha_object_id = f"s2m_{device}_lwt"
ha_name = f"{self.config.COMPUTER_NAME} LWT"
discovery_topic = self.ha_discovery_template.format(ha_type, ha_object_id)
haconfig = ha_config(
discovery_topic=discovery_topic,
name=ha_name,
object_id=ha_object_id,
state_topic=self.lwt_topic,
device=device,
entity_type=ha_type,
device_class=ha_class,
payload_on="online",
payload_off="offline"
)
self.myqtt.publish(haconfig[0], haconfig[1], retain=True)
def publish_all(self):
logging.debug("...publishing")
self.myqtt.publish(self.lwt_topic, 'online')
funcs = [self.publish_lwt_binary_sensor,
self.publish_mount_state,
self.publish_disk_space,
self.publish_cpu_temp,
self.publish_cpu_usage,
self.publish_ram,
self.publish_argon]
for f in funcs:
f()
logging.info("publish complete!")
self.first_loop_done = True
################################################################################################
################################ CALLBACKS ##########################################
################################################################################################
def process_user_callbacks(self, *args, **kwargs):
logging.debug("Processing user callbacks...")
if self.config.USER_CALLBACKS:
import user_callbacks as uc
ucb = self.config.CALLBACKS
logging.debug(ucb)
if not type(ucb) == dict:
logging.warning("User callbacks aren't in a dictionary. They are a {}".format(type(ucb)))
import ast
ucb = ast.literal_eval(ucb)
for k, v in ucb.items():
self.myqtt.topic_callbacks[k] = getattr(uc, v)
logging.debug(self.myqtt.topic_callbacks)
def s2m_set_publish_period(self, client, userdata, message):
logging.debug("")
# global PUBLISH_PERIOD
try:
new_publish_period = int(message.payload.decode("utf-8"))
if new_publish_period != self.publish_period:
self.publish_period = new_publish_period
logging.info("Publish period has been set to {} seconds.".format(self.publish_period))
except Exception as e:
logging.error(e, exc_info=True)
self.publish_period = self.config.PUBLISH_PERIOD
def quit_s2m(self, client, userdata, message):
logging.debug("")
if message.payload.decode("utf-8") == "1":
self.auto_reconnect =False
logging.info("Quit called....")
self.myqtt.publish(self.config.MQTT_BASE_TOPIC + "/callbacks/s2m_quit", "")
self.myqtt.publish(self.lwt_topic, "offline", retain=True)
client.loop_stop()
client.disconnect()
def cb_shutdown(self, client, userdata, message):
mpl = message.payload.decode("utf-8")
logging.debug(mpl)
title = "[Shutdown]"
if mpl == "1":
logging.info("Attempting to poweroff...")
self.myqtt.publish(self.config.MQTT_BASE_TOPIC + "/callbacks/shutdown", "")
try:
res = check_call(["shutdown", "-h", "now"])
except Exception as e:
logging.error(e, exc_info=True)
else:
logging.warning("{}: '{}': Not 1 recieved".format(title, mpl))
def cb_reboot(self, client, userdata, message):
mpl = message.payload.decode("utf-8")
logging.debug(mpl)
title = "[Reboot]"
if mpl == "1":
logging.info("Attempting to reboot...")
self.myqtt.publish(self.config.MQTT_BASE_TOPIC + "/callbacks/reboot", "")
try:
res = check_call(["reboot", "now"])
except Exception as e:
logging.error(e, exc_info=True)
else:
logging.warning("{}: '{}': Not 1 recieved".format(title, mpl))
if __name__ == '__main__':
try:
config = sys.argv[1]
except:
config = os.path.join(os.path.dirname(os.path.abspath(__file__)), "s2m.conf")
print("conf: ", config)
s2m = System2Mqtt(config)
time.sleep(1)
s2m.run()