This repository has been archived by the owner on Mar 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
monitor.py
executable file
·386 lines (276 loc) · 10.6 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
#!/usr/bin/python
import sys
import yaml
import logging
import os
import tempfile
import argparse
import time
import broadlink_ac_mqtt.AcToMqtt as AcToMqtt
import broadlink_ac_mqtt.classes.broadlink.ac_db as ac_db_version
import signal
import traceback
logger = logging.getLogger(__name__)
AC = None
softwareversion = "1.2.1"
pid = str(os.getpid())
pidfile = "ac_to_mqtt.pid"
pid_stale_time = 5
pid_last_update = 0
do_loop = False
running = False
#***************************************** Get going methods ************************************************
def discover_and_dump_for_config(config):
AC = AcToMqtt.AcToMqtt(config)
devices = AC.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_file_path):
config = {}
##Load config
with open(config_file_path, "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"]
##What ip to bind to
config['bind_to_ip'] = config_file["service"].get("bind_to_ip") or None
##Mqtt settings
config["mqtt_host"] = config_file["mqtt"].get("host")
config["mqtt_port"] = config_file["mqtt"].get("port")
config["mqtt_user"]= config_file["mqtt"].get("user")
config["mqtt_password"] = config_file["mqtt"].get("passwd")
##set client id if set, otherwise just add timestamp to generic to prevent conflicts
config["mqtt_client_id"] = config_file["mqtt"]["client_id"] if config_file["mqtt"]["client_id"] else 'broadlink_to_mqtt-'+str(time.time())
config["mqtt_topic_prefix"] = config_file["mqtt"]["topic_prefix"]
config["mqtt_auto_discovery_topic"] = config_file["mqtt"]["auto_discovery_topic"] if "auto_discovery_topic" in config_file["mqtt"] else False
config["mqtt_auto_discovery_topic_retain"] = config_file["mqtt"]["auto_discovery_topic_retain"] if "auto_discovery_topic_retain" in config_file["mqtt"] else False
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 stop_if_already_running():
if(check_if_running()):
sys.exit()
def init_logging(level,log_file_path):
# Init logging
logging.basicConfig(
filename=log_file_path,
level=level,
format="%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s",
)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# set a format which is simpler for console use
formatter = logging.Formatter('%(message)s')
# tell the handler to use this format
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
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 check_if_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.....
logger.debug("Checking if already running")
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 (1 in contents and (float(contents[1])+ pid_stale_time) < time.time()):
logger.info("Pid is stale, so we'll just overwrite it go on")
else:
logger.info("Pid is still valid, so exit")
sys.exit()
##Write current time
touch_pid_file()
################ Signal handlers
def receiveSignal(signalNumber, frame):
#print('Received:', signalNumber)
stop(signalNumber, frame)
return
def stop(signalNumber = 0, frame = 0):
logger.info("Stopping")
do_loop = False
while running:
logger.info("Waiting to stop")
time.sleep(1)
if AC is not None:
AC.stop()
##clean pid file
if os.path.isfile(pidfile):
os.unlink(pidfile)
sys.exit()
def restart(signalNumber = 0, frame = 0):
""
def init_signal():
# signal.signal(signal.SIGUSR2, receiveSignal)
#signal.signal(signal.SIGPIPE, receiveSignal)
#signal.signal(signal.SIGALRM, receiveSignal)
signal.signal(signal.SIGTERM, stop)
################# Main startup ####################
def start():
##Handle signal
init_signal()
##Just some defaults
##Defaults
global AC
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", type=int )
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")
parser.add_argument("-dir", "--data_dir", help="Data Folder -- Default to folder script is located", default=False)
parser.add_argument("-c", "--config", help="Config file path -- Default to folder script is located + 'config.yml'", default=False)
parser.add_argument("-l", "--logfile", help="Logfile path -- Default to logs folder script is located", default=False)
parser.add_argument("-T", "--test",help="send test set temperature packet, for testing only",action="store_true",default=False)
##Parse args
args = parser.parse_args()
##Set the base path, if set use it, otherwise default to running folder
if args.data_dir:
if os.path.exists(args.data_dir):
data_dir = args.data_dir
else:
print ("Path Not found for Datadir: %s" % (args.data_dir))
sys.exit()
else:
data_dir = os.path.dirname(os.path.realpath(__file__))
##Config File
if args.config:
if os.path.exists(args.config):
config_file_path = args.config
else:
print ("Config file not found: %s" % (args.config))
sys.exit()
else:
if os.path.exists(data_dir+'/settings/config.yml'):
config_file_path = data_dir+'/settings/config.yml'
# elif os.path.exists(data_dir+'\\settings\\config.yml'):
# config_file_path = data_dir+'\\settings\\config.yml'
else:
# config_file_path = data_dir+'/config.yml'
config_file_path = data_dir+'\\settings\config.yml'
##LogFile
if args.logfile:
log_file_path = args.logfile
else:
log_file_path = os.path.dirname(os.path.realpath(__file__))+'/log/out.log'
log_level = logging.DEBUG if args.debug else logging.INFO
init_logging(log_level,log_file_path)
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()])
##Apply the config, then if arguments, override the config values with args
config = read_config(config_file_path)
##Print verions
if args.version:
print ("Monitor Version: %s, Class version:%s" % (softwareversion,ac_db_version.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
##mmmm.. this looks dodgy.. but i'm not python expert
AC = AcToMqtt.AcToMqtt(config)
##Just do a test
if args.test:
AC.test(config)
sys.exit()
try:
##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")
##Connect to Mqtt
AC.connect_mqtt()
if config["self_discovery"]:
devices = AC.discover()
else:
devices = AC.make_device_objects(config['devices'])
if args.dumphaconfig:
AC.dump_homeassistant_config_from_devices(devices)
sys.exit()
##Publish mqtt auto discovery if topic set
if config["mqtt_auto_discovery_topic"]:
AC.publish_mqtt_auto_discovery(devices)
##One loop
do_loop = True if config["daemon_mode"] else False
##Run main loop
while do_loop :
running = True
AC.start(config,devices)
touch_pid_file()
running = False
except KeyboardInterrupt:
logging.debug("User Keyboard interuped")
except Exception as e:
logger.debug(traceback.format_exc())
logger.error(e)
finally:
##cleanup
stop()
if __name__ == "__main__":
start()