forked from liaan/broadlink_ac_mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.py
executable file
·656 lines (464 loc) · 19.1 KB
/
monitor.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
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
#!/usr/bin/python
import os
import time
import sys
import logging
import commands
import argparse
import binascii
import yaml
import paho.mqtt.client as mqtt
sys.path.insert(1, os.path.join(os.path.dirname(__file__), './ext/broadlink'))
import ac_db as broadlink
import tempfile
import json
pid = str(os.getpid())
pidfile = tempfile.gettempdir() +"/ac_to_mqtt.pid"
pid_stale_time = 60
pid_last_update = 0
logger = logging.getLogger(__name__)
softwareversion = "1.0.6"
debug = False
#***************************************** Main Class ************************************************
class AcToMqtt:
config = {}
last_update = 0
device_objects = {}
def __init__(self,config):
self.config = config
""
def discover(self):
##Go discovery
discovered_devices = broadlink.discover(timeout=5)
devices = {}
if discovered_devices == None:
error_msg = "No Devices Found, make sure you on the same network segment"
if daemon_mode:
logger.debug(error_msg)
else:
print error_msg
#print "nothing found"
sys.exit()
##Make sure correct device id
for device in discovered_devices:
if device.devtype == 0x4E2a:
devices[device.status['macaddress']] = device
return devices
#device = self.devices["a"]
#self.device = broadlink.ac_db(host=device.host, mac=device.mac,debug=False)
#logger.debug(self.device.host)
#logger.debug( "Device type detected: " +self.device.type)
#logger.debug( "Starting device test()")
def print_yaml_discovered_devices(self):
print yaml.dump(self.discover_devices);
def make_device_objects(self,device_list = None):
device_objects = {}
if device_list == [] or device_list == None:
error_msg = " Cannot make device objects, empty list given"
logger.error(error_msg)
print error_msg
sys.exit()
for device in device_list:
device_objects[device['mac']] = broadlink.gendevice(devtype=0x4E2a, host=(device['ip'],device['port']),mac = bytearray.fromhex(device['mac']), name=device['name'])
return device_objects
def do_loop (self,config, devices = None):
self.device_objects = devices
self.config = config
last_update = 0;
##If there no devices so throw error
if devices == [] or devices == None:
print "No devices defined";
logger.error("No Devices defined, either enable discovery or add them to config");
sys.exit()
while True:
##we are alive ##Update PID file
touch_pid_file()
##Just check status on every update interval
if (last_update + self.config["update_interval"]) > time.time():
#logger.debug("Timeout not done, so lets wait a abit : %s : %s" %(last_update + update_interval,time.time()))
time.sleep(0.5)
continue
try:
last_update = time.time();
for device in devices.values():
##Get the status, the global update interval is used as well to reduce requests to aircons as they slow
status = device.get_ac_status()
#print status
if status:
self.publish_mqtt_info(status);
else:
logger.debug("No status")
except Exception as e:
logger.critical(e)
##Something went wrong, so just exit and let system restart
continue;
##Exit if not daemon_mode
if self.config["daemon_mode"] != True:
break
##Set last update
def dump_homeassistant_config_from_devices(self,devices):
if devices == {}:
print "No devices defined"
sys.exit()
devices_array = self.make_devices_array_from_devices(devices)
if devices_array == {}:
print "something went wrong, no devices found"
sys.exit();
print "*********** start copy below ****************"
a = []
for key in devices_array:
##Echo
device = devices_array[key]
device['platform'] = 'mqtt'
a.append(device)
print yaml.dump({'climate':a})
print "**************** Start copy here ****************"
def make_devices_array_from_devices(self,devices):
devices_array = {}
for device in devices.values():
topic = self.config["mqtt_auto_discovery_topic"]+"/climate/"+device.status["macaddress"]+"/config"
if not device.name :
name = device.status["macaddress"]
else:
name = device.name.encode('ascii','ignore')
device_array = {
"name": name
#,"power_command_topic" : self.config["mqtt_topic_prefix"]+ device.status["macaddress"]+"/power/set"
,"mode_command_topic" : self.config["mqtt_topic_prefix"]+ device.status["macaddress"]+"/mode_homeassistant/set"
,"temperature_command_topic" : self.config["mqtt_topic_prefix"] + device.status["macaddress"]+"/temp/set"
,"fan_mode_command_topic" : self.config["mqtt_topic_prefix"] + device.status["macaddress"]+"/fanspeed_homeassistant/set"
,"action_topic" : self.config["mqtt_topic_prefix"] + device.status["macaddress"]+"/homeassistant/set"
##Read values
,"current_temperature_topic" : self.config["mqtt_topic_prefix"] + device.status["macaddress"]+"/ambient_temp/value"
,"mode_state_topic" : self.config["mqtt_topic_prefix"] + device.status["macaddress"]+"/mode_homeassistant/value"
,"temperature_state_topic" : self.config["mqtt_topic_prefix"] + device.status["macaddress"]+"/temp/value"
,"fan_mode_state_topic" : self.config["mqtt_topic_prefix"] + device.status["macaddress"]+"/fanspeed_homeassistant/value"
,"fan_modes": ["Auto","Low","Medium", "High"]
,"modes": ['off',"cool","heat","fan_only","dry"]
,"max_temp":32.0
,"min_temp":16.0
,"precision": 0.5
}
devices_array[device.status["macaddress"]] = device_array
return devices_array
def publish_mqtt_auto_discovery(self,devices):
if devices == [] or devices == None:
print "No devices defined";
logger.error("No Devices defined, either enable discovery or add them to config");
sys.exit()
##Make an array
devices_array = self.make_devices_array_from_devices(devices)
if devices_array == {}:
print "something went wrong, no devices found"
sys.exit();
for key in devices_array:
device = devices_array[key]
topic = self.config["mqtt_auto_discovery_topic"]+"/climate/"+key+"/config"
##Publish
self._publish(topic,json.dumps(device))
#sys.exit();
def publish_mqtt_info(self,status):
##Publish all values in status
for value,key in enumerate(status):
pubResult = self._publish(self.config["mqtt_topic_prefix"] + status['macaddress']+'/'+key+ '/value',bytes(status[key]))
if pubResult != None:
logger.warning('Publishing Result: "%s"' % mqtt.error_string(pubResult))
if pubResult == mqtt.MQTT_ERR_NO_CONN:
self._connect_mqtt();
break
return
#self._publish(binascii.hexlify(status['macaddress'])+'/'+ 'temp/value',status['temp']);
def _publish(self,topic,value,retain=False,qos=0):
payload = value
logger.debug('publishing on topic "%s", data "%s"' % (topic, payload))
pubResult = self._mqtt.publish(topic, payload=payload, qos=qos, retain=retain)
##If there error, then debug log and return not None
if pubResult[0] != 0:
logger.debug('Publishing Result: "%s"' % mqtt.error_string(pubResult[0]))
return pubResult[0]
def _connect_mqtt(self):
##Setup client
self._mqtt = mqtt.Client(client_id=self.config["mqtt_client_id"], clean_session=True, userdata=None)
##Set last will and testament
self._mqtt.will_set("/aircon/LWT","offline",True)
##Auth
if self.config["mqtt_user"] and self.config["mqtt_password"]:
self._mqtt.username_pw_set(self.config["mqtt_user"],self.config["mqtt_password"])
##Setup callbacks
self._mqtt.on_connect = self._on_mqtt_connect
self._mqtt.on_message = self._on_mqtt_message
self._mqtt.on_log = self._on_mqtt_log
self._mqtt.on_subscribed = self._mqtt_on_subscribe
##Connect
logger.debug("Coneccting to MQTT: %s with client ID = %s" % (self.config["mqtt_host"],self.config["mqtt_client_id"]))
self._mqtt.connect(self.config["mqtt_host"], port=self.config["mqtt_port"], keepalive=60, bind_address="")
##Start
self._mqtt.loop_start() # creates new thread and runs Mqtt.loop_forever() in it.
def _on_mqtt_log(self,client, userdata, level, buf):
if level == mqtt.MQTT_LOG_ERR:
logger.debug("Mqtt log" + buf)
def _mqtt_on_subscribe(self,client, userdata, mid, granted_qos):
logger.debug("Mqtt Subscribed")
def _on_mqtt_message(self, client, userdata, msg):
try:
logger.debug('Mqtt Message Received! Userdata: %s, Message %s' % (userdata, msg.topic+" "+str(msg.payload)))
##Function is second last
function = msg.topic.split('/')[-2]
address = msg.topic.split('/')[-3]
address = address.encode('ascii','ignore')
value = msg.payload
logger.debug('Mqtt decoded --> Function: %s, Address: %s, value: %s' %(function,address,value))
except Exception as e:
logger.critical(e)
return
##Process received ##Probably need to exit here as well if command not send, but should exit on status update above .. grr, hate stupid python
if function == "temp":
try:
if self.device_objects.get(address):
status = self.device_objects[address].set_temperature(float(value))
if status :
self.publish_mqtt_info(status)
else:
logger.debug("Device not on list of devices %s, type:%s" % (address,type(address)))
return
except Exception as e:
logger.critical(e)
return
elif function == "power":
if value.lower() == "on":
status = self.device_objects[address].switch_on()
if status :
self.publish_mqtt_info(status)
elif value.lower() == "off":
status = self.device_objects[address].switch_off()
if status :
self.publish_mqtt_info(status)
else:
logger.debug("Switch on has invalid value, values is on/off received %s",value)
return
elif function == "mode":
status = self.device_objects[address].set_mode(value)
if status :
self.publish_mqtt_info(status)
else:
logger.debug("Mode on has invalid value %s",value)
return
elif function == "fanspeed":
status = self.device_objects[address].set_fanspeed(value)
if status :
self.publish_mqtt_info(status)
else:
logger.debug("Fanspeed on has invalid value %s",value)
return
elif function == "fanspeed_homeassistant":
status = self.device_objects[address].set_fanspeed(value)
if status :
self.publish_mqtt_info(status)
else:
logger.debug("Fanspeed on has invalid value %s",value)
return
elif function == "mode_homekit":
status = self.device_objects[address].set_homekit_mode(value)
if status :
self.publish_mqtt_info(status)
else:
logger.debug("Fanspeed on has invalid value %s",value)
return
elif function == "mode_homeassistant":
status = self.device_objects[address].set_homeassistant_mode(value)
if status :
self.publish_mqtt_info(status)
else:
logger.debug("Fanspeed on has invalid value %s",value)
return
else:
logger.debug("No function match")
return
def _on_mqtt_connect(self, client, userdata, flags, rc):
"""
RC definition:
0: Connection successful
1: Connection refused - incorrect protocol version
2: Connection refused - invalid client identifier
3: Connection refused - server unavailable
4: Connection refused - bad username or password
5: Connection refused - not authorised
6-255: Currently unused.
"""
logger.debug('Mqtt connected! client=%s, userdata=%s, flags=%s, rc=%s' % (client, userdata, flags, rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
sub_topic = "/aircon/+/+/set"
client.subscribe(sub_topic)
logger.debug('Listing on %s for messages' % (sub_topic))
##LWT
self._publish(self.config["mqtt_topic_prefix"]+'LWT','online')
#*****************************************************************************************************
#***************************************** Get going methods ************************************************
def discover_and_dump_for_config(config):
actomqtt = AcToMqtt(config);
devices = actomqtt.discover();
yaml_devices = []
if devices == {}:
print "No devices found, make sure you are on same network broadcast segment as device/s"
sys.exit()
print "*********** start copy below ************"
for device in devices.values():
yaml_devices.append(
{'name':device.name.encode('ascii','ignore'),
'ip':device.host[0]
,'port':device.host[1]
,'mac':device.status['macaddress']}
)
print yaml.dump({'devices':yaml_devices})
print "*********** stop copy above ************"
sys.exit();
def read_config():
config = {}
##Load config
with open(os.path.dirname(os.path.realpath(__file__))+'/config.yml', "r") as ymlfile:
config_file = yaml.load(ymlfile,Loader=yaml.SafeLoader)
##Service settings
config["daemon_mode"] = config_file["service"]["daemon_mode"]
config["update_interval"] = config_file["service"]["update_interval"]
config["self_discovery"] = config_file["service"]["self_discovery"]
##Mqtt settings
config["mqtt_host"] = config_file["mqtt"]["host"]
config["mqtt_port"] = config_file["mqtt"]["port"]
config["mqtt_user"]= config_file["mqtt"]["user"]
config["mqtt_password"] = config_file["mqtt"]["passwd"]
config["mqtt_client_id"] = config_file["mqtt"]["client_id"]
config["mqtt_topic_prefix"] = config_file["mqtt"]["topic_prefix"]
config["mqtt_auto_discovery_topic"] = config_file["mqtt"]["auto_discovery_topic"]
if config["mqtt_topic_prefix"] and config["mqtt_topic_prefix"].endswith("/") == False:
config["mqtt_topic_prefix"] = config["mqtt_topic_prefix"] + "/"
##Devices
if config_file['devices'] != None:
config["devices"] = config_file['devices']
else:
config["devices"] = None
return config
def touch_pid_file():
global pid_last_update
##No need to update very often
if(pid_last_update + pid_stale_time -2 > time.time()):
return
pid_last_update = time.time()
with open(pidfile, 'w') as f:
f.write("%s,%s" % (os. getpid() ,pid_last_update))
def stop_if_already_running():
##Check if there is pid, if there is, then check if valid or stale .. probably should add process id for race conditions but damn, this is a simple scripte.....
if os.path.isfile(pidfile):
logger.debug("%s already exists, checking if stale" % pidfile)
##Check if stale
f = open(pidfile, 'r')
if f.mode =="r":
contents =f.read()
contents = contents.split(',')
##Stale, so go ahead
if (float(contents[1])+ pid_stale_time) < time.time():
logger.info("Pid is stale, so we'll just overwrite it go on")
else:
logger.debug("Pid is still valid, so exit")
sys.exit()
##Write current time
touch_pid_file();
################# Main startup ####################
def main():
##Just some defaults
##Defaults
daemon_mode = False;
update_interval = 30
devices = {}
# Argument parsing
parser = argparse.ArgumentParser(
description='Aircon To MQTT v%s : Mqtt publisher of Duhnham Bush on the Pi.' % softwareversion
)
##HomeAssistant stuff
parser.add_argument("-Hd", "--dumphaconfig",help="Dump the devices as a HA manual config entry",action="store_true",default=False)
parser.add_argument("-Hat", "--mqtt_auto_discovery_topic", help="If specified, will Send the MQTT autodiscovery config for all devices to topic")
parser.add_argument("-b", "--background", help="Run in background",action="store_true",default=False)
##Config helpers
parser.add_argument("-S", "--discoverdump", help="Discover devices and dump config",action="store_true",default=False)
#parser.add_argument("-dh", "--devicehost", help='Aircon Host IP, Default: %s ' % ac_host)
#parser.add_argument("-dm", "--devicemac", help="Ac Mac Address, Default: %s" % ac_mac)
##MQTT stuff
parser.add_argument("-ms", "--mqttserver", help='Mqtt Server, Default:')
parser.add_argument("-mp", "--mqttport", help="Mqtt Port" )
parser.add_argument("-mU", "--mqttuser", help="Mqtt User" )
parser.add_argument("-mP", "--mqttpassword", help="Mqtt Password" )
##Generic
parser.add_argument("-s", "--discover", help="Discover devices",action="store_true",default=False)
parser.add_argument("-d", "--debug", help="set logging level to debug",action="store_true",default=False)
parser.add_argument("-v", "--version", help="Print Verions",action="store_true")
args = parser.parse_args()
# Init logging
logging.basicConfig(filename=os.path.dirname(os.path.realpath(__file__))+'/ac_to_mqtt.log',level=(logging.DEBUG if args.debug else logging.INFO),format='%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s')
#logging.basicConfig(filename='ac_to_mqtt.log',level=(logging.DEBUG if args.debug else logging.INFO),format='%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s')
logger.debug("%s v%s is starting up" % (__file__, softwareversion))
logLevel = {0: 'NOTSET', 10: 'DEBUG', 20: 'INFO', 30: 'WARNING', 40: 'ERROR'}
logger.debug('Loglevel set to ' + logLevel[logging.getLogger().getEffectiveLevel()])
#if args.devicehost:
# ac_host = args.devicehost
# logger.debug("Host: %s"%ac_host)
#if args.devicemac:
# ac_mac = args.devicemac
# logger.debug("Mac: %s"%ac_mac)
##Apply the config, then if arguments, override the config values with args
config = read_config();
##Print verions
if args.version:
print "Monitor Version: %s, Class version:%s" % (softwareversion,broadlink.version)
sys.exit();
##Mqtt Host
if args.mqttserver:
config["mqtt_host"] = args.mqttserver
##Mqtt Port
if args.mqttport:
config["mqtt_port"] = args.mqttport
##Mqtt User
if args.mqttuser:
config["mqtt_user"] = args.mqttuser
##Mqtt Password
if args.mqttpassword:
config["mqtt_password"] = args.mqttpassword
##Mqtt auto discovery topic
if args.mqtt_auto_discovery_topic:
config["mqtt_auto_discovery_topic"] = args.mqtt_auto_discovery_topic
##Self Discovery
if args.discover:
config["self_discovery"] = True
if args.discoverdump:
discover_and_dump_for_config(config)
##Deamon Mode
if args.background:
config["daemon_mode"] = True
##Make sure not already running
stop_if_already_running()
logging.info("Starting Monitor...")
# Start and run the mainloop
logger.debug("Starting mainloop, responding on only events")
try:
##class
actomqtt = AcToMqtt(config);
##Connect to Mqtt
actomqtt._connect_mqtt()
if config["self_discovery"]:
devices = actomqtt.discover()
else:
devices = actomqtt.make_device_objects(config['devices'])
if args.dumphaconfig:
actomqtt.dump_homeassistant_config_from_devices(devices)
sys.exit();
##Publish mqtt auto discovery if topic set
if config["mqtt_auto_discovery_topic"]:
actomqtt.publish_mqtt_auto_discovery(devices)
##Run main loop
actomqtt.do_loop(config,devices);
except KeyboardInterrupt:
logging.debug("User Keyboard interuped")
finally:
os.unlink(pidfile)
logging.info("Stopping Monitor...")
if __name__ == "__main__":
main()