-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathupdater.py
562 lines (469 loc) · 19.8 KB
/
updater.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
import json
import logging
import posixpath
from mqtt.exceptions import UnknownDeviceClassException
from mqtt.exceptions import UnknownMqttWrapperException
from mqtt.utils import normalize_name_to_id
from qolsys.config import QolsysGatewayConfig
from qolsys.partition import QolsysPartition
from qolsys.sensors import QolsysSensor
from qolsys.sensors import QolsysSensorAuxiliaryPendant
from qolsys.sensors import QolsysSensorBluetooth
from qolsys.sensors import QolsysSensorCODetector
from qolsys.sensors import QolsysSensorDoorWindow
from qolsys.sensors import QolsysSensorDoorbell
from qolsys.sensors import QolsysSensorFreeze
from qolsys.sensors import QolsysSensorGlassBreak
from qolsys.sensors import QolsysSensorHeat
from qolsys.sensors import QolsysSensorKeyFob
from qolsys.sensors import QolsysSensorKeypad
from qolsys.sensors import QolsysSensorMotion
from qolsys.sensors import QolsysSensorShock
from qolsys.sensors import QolsysSensorSiren
from qolsys.sensors import QolsysSensorSmokeDetector
from qolsys.sensors import QolsysSensorTakeoverModule
from qolsys.sensors import QolsysSensorTemperature
from qolsys.sensors import QolsysSensorTilt
from qolsys.sensors import QolsysSensorTranslator
from qolsys.sensors import QolsysSensorWater
from qolsys.sensors import _QolsysSensorWithoutUpdates
from qolsys.state import QolsysState
from qolsys.utils import defaultLoggerCallback
from qolsys.utils import find_subclass
LOGGER = logging.getLogger(__name__)
class MqttUpdater(object):
def __init__(self, state: QolsysState, factory: 'MqttWrapperFactory',
callback: callable = None, logger=None):
self._factory = factory
self._callback = callback or defaultLoggerCallback
self._logger = logger or LOGGER
state.register(self, callback=self._state_update)
def _state_update(self, state: QolsysState, change, prev_value=None, new_value=None):
self._logger.debug(f"Received update from state for CHANGE={change}")
if change == QolsysState.NOTIFY_UPDATE_PARTITIONS:
# The partitions have been updated, make sure we are registered for
# all those partitions
for partition in state.partitions:
partition.register(self, callback=self._partition_update)
self._factory.wrap(partition).configure()
# The partition might already have sensors on it, so register
# for each sensor individually too
for sensor in partition.sensors:
sensor.register(self, callback=self._sensor_update)
self._factory.wrap(sensor).configure(partition=partition)
elif change == QolsysState.NOTIFY_UPDATE_ERROR:
# An error has happened on qolsysgw, so we want to update the
# state sensor
wrapped_state = self._factory.wrap(state)
wrapped_state.update_state()
wrapped_state.update_attributes()
def _partition_update(self, partition: QolsysPartition, change, prev_value=None, new_value=None):
self._logger.debug(f"Received update from partition "
f"'{partition.name}' for CHANGE={change}, from "
f"prev_value={prev_value} to new_value={new_value}")
if change == QolsysPartition.NOTIFY_ADD_SENSOR:
sensor = new_value
sensor.register(self, callback=self._sensor_update)
self._factory.wrap(sensor).configure(partition=partition)
elif change == QolsysPartition.NOTIFY_UPDATE_STATUS:
self._factory.wrap(partition).update_state()
elif change == QolsysPartition.NOTIFY_UPDATE_SECURE_ARM:
self._factory.wrap(partition).configure()
elif change == QolsysPartition.NOTIFY_UPDATE_ALARM_TYPE or \
change == QolsysPartition.NOTIFY_UPDATE_ATTRIBUTES:
self._factory.wrap(partition).update_attributes()
def _sensor_update(self, sensor: QolsysSensor, change, prev_value=None, new_value=None):
self._logger.debug(f"Received update from sensor '{sensor.name}' for "
f"CHANGE={change}, from prev_value={prev_value} to "
f"new_value={new_value}")
if change == QolsysSensor.NOTIFY_UPDATE_STATUS:
self._factory.wrap(sensor).update_state()
elif change == QolsysSensor.NOTIFY_UPDATE_ATTRIBUTES:
self._factory.wrap(sensor).update_attributes()
class MqttWrapper(object):
def __init__(self, mqtt_publish: callable, cfg: QolsysGatewayConfig,
mqtt_plugin_cfg, session_token: str) -> None:
self._mqtt_publish = mqtt_publish
self._cfg = cfg
self._birth_topic = mqtt_plugin_cfg.get('birth_topic')
self._will_topic = mqtt_plugin_cfg.get('will_topic')
self._birth_payload = mqtt_plugin_cfg.get('birth_payload')
self._will_payload = mqtt_plugin_cfg.get('will_payload')
self._session_token = session_token
# This is an interesting thing: we can improve the behavior of the
# plugin by using retain=True, but we need to be able to depend on
# AppDaemon's status (and LWT message) to tell us when the connection
# to the system is offline. This approach might also create weird
# behaviors if we remove or add sensors between runs, so there is
# a configuration option to plainly disable it.
self._mqtt_retain = self._cfg.mqtt_retain and \
(self._birth_topic == self._will_topic)
@property
def entity_id(self):
return normalize_name_to_id(self.name)
@property
def config_topic(self):
return posixpath.join(self._cfg.discovery_topic,
self.topic_path, 'config')
@property
def state_topic(self):
return posixpath.join(self._cfg.discovery_topic,
self.topic_path, 'state')
@property
def attributes_topic(self):
return posixpath.join(self._cfg.discovery_topic,
self.topic_path, 'attributes')
@property
def availability_topic(self):
return posixpath.join(self._cfg.discovery_topic,
self.topic_path, 'availability')
@property
def device_availability_topic(self):
return posixpath.join(self._cfg.discovery_topic,
'alarm_control_panel',
self._cfg.panel_unique_id,
'availability')
@property
def payload_available(self):
return 'online'
@property
def payload_unavailable(self):
return 'offline'
@property
def device_payload(self):
payload = {
'name': self._cfg.panel_device_name,
'identifiers': [
self._cfg.panel_unique_id,
],
'manufacturer': 'Qolsys',
'model': 'IQ Panel 2+',
}
# If we have the mac address, this will allow to link the device
# to other related elements in home assistant
if self._cfg.panel_mac:
payload['connections'] = [
['mac', self._cfg.panel_mac],
]
return payload
@property
def configure_availability(self):
availability = [
{
'topic': self.device_availability_topic,
'payload_available': self.payload_available,
'payload_not_available': self.payload_unavailable,
},
]
if self.availability_topic != self.device_availability_topic:
availability.append({
'topic': self.availability_topic,
'payload_available': self.payload_available,
'payload_not_available': self.payload_unavailable,
})
# If we the birth and will topic of the MQTT plugin are the same,
# we can take advantage of this to consider that the panel is offline
# when AppDaemon is (since updates won't work at this point)
if self._will_topic == self._birth_topic:
availability.append({
'topic': self._will_topic,
'payload_available': self._birth_payload,
'payload_not_available': self._will_payload,
})
return availability
def configure(self, **kwargs):
self._mqtt_publish(
namespace=self._cfg.mqtt_namespace,
topic=self.config_topic,
retain=True,
payload=json.dumps(self.configure_payload(**kwargs)),
)
self.set_available()
self.update_state()
self.update_attributes()
def update_attributes(self):
pass
def update_state(self):
pass
def set_available(self):
self._mqtt_publish(
namespace=self._cfg.mqtt_namespace,
topic=self.availability_topic,
retain=self._mqtt_retain,
payload=self.payload_available,
)
def set_unavailable(self):
self._mqtt_publish(
namespace=self._cfg.mqtt_namespace,
topic=self.availability_topic,
retain=True,
payload=self.payload_unavailable,
)
class MqttWrapperQolsysState(MqttWrapper):
def __init__(self, state: QolsysState, *args, **kwargs):
super().__init__(*args, **kwargs)
self._state = state
@property
def name(self):
return self._cfg.panel_unique_id
@property
def entity_id(self):
return f'{self.name}_last_error'
@property
def availability_topic(self):
return self.device_availability_topic
@property
def topic_path(self):
return posixpath.join(
'sensor',
self.entity_id,
)
def configure_payload(self, **kwargs):
payload = {
'name': 'Last Error',
'device_class': 'timestamp',
'state_topic': self.state_topic,
'availability_mode': 'all',
'availability': self.configure_availability,
'json_attributes_topic': self.attributes_topic,
}
payload['unique_id'] = f"{self._cfg.panel_unique_id}_last_error"
payload['device'] = self.device_payload
return payload
def update_attributes(self):
if self._state.last_exception:
exc_type = type(self._state.last_exception).__name__
exc_desc = str(self._state.last_exception)
else:
exc_type = None
exc_desc = None
self._mqtt_publish(
namespace=self._cfg.mqtt_namespace,
topic=self.attributes_topic,
retain=self._mqtt_retain,
payload=json.dumps({
'type': exc_type,
'desc': exc_desc,
}),
)
def update_state(self):
self._mqtt_publish(
namespace=self._cfg.mqtt_namespace,
topic=self.state_topic,
retain=self._mqtt_retain,
payload=(self._state.last_exception.at
if self._state.last_exception
else None),
)
class MqttWrapperQolsysPartition(MqttWrapper):
QOLSYS_TO_HA_STATUS = {
'DISARM': 'disarmed',
'ARM_STAY': 'armed_home',
'ARM_AWAY': 'armed_away',
# 'ARM_NIGHT': 'armed_night',
# '': 'armed_vacation',
# '': 'armed_custom_bypass',
'ENTRY_DELAY': 'pending',
'ALARM': 'triggered',
'EXIT_DELAY': 'arming',
'ARM-AWAY-EXIT-DELAY': 'arming',
'ARM-STAY-EXIT-DELAY': 'arming',
}
def __init__(self, partition: QolsysPartition, *args, **kwargs):
super().__init__(*args, **kwargs)
self._partition = partition
@property
def name(self):
return self._partition.name
@property
def ha_status(self):
status = self.QOLSYS_TO_HA_STATUS.get(self._partition.status)
if not status:
raise ValueError('We need to put a better error here, but '
'we found an unsupported status: '
f"'{self._partition.status}'")
return status
@property
def topic_path(self):
return posixpath.join(
'alarm_control_panel',
self._cfg.panel_unique_id,
self.entity_id,
)
def configure_payload(self, **kwargs):
command_template = {
'partition_id': str(self._partition.id),
'action': '{{ action }}',
'session_token': self._session_token,
}
if (self._cfg.code_arm_required or self._cfg.code_disarm_required) and\
not self._cfg.ha_check_user_code:
# It is the only situation where we actually need to transmit
# the code regularly through mqtt. In any other situation, we can
# use the session token for comparison, which will allow to avoid
# sharing the code in MQTT after initialization
command_template['code'] = '{{ code }}'
secure_arm = (self._partition.secure_arm and
not self._cfg.panel_user_code)
payload = {
'name': self.name,
'state_topic': self.state_topic,
'code_arm_required': self._cfg.code_arm_required or secure_arm,
'code_disarm_required': self._cfg.code_disarm_required,
'code_trigger_required': self._cfg.code_trigger_required or secure_arm,
'command_topic': self._cfg.control_topic,
'command_template': json.dumps(command_template),
'availability_mode': 'all',
'availability': self.configure_availability,
'json_attributes_topic': self.attributes_topic,
}
# As we have a unique ID for the panel, we can setup a unique ID for
# the partition, and create a device to link all of our partitions
# together; this will also allow to interact with the partition in
# the UI, change it's name, assign it to areas, etc.
payload['unique_id'] = f"{self._cfg.panel_unique_id}_p{self._partition.id}"
payload['device'] = self.device_payload
if self._cfg.default_trigger_command:
payload['payload_trigger'] = self._cfg.default_trigger_command
if self._cfg.code_arm_required or self._cfg.code_disarm_required:
code = self._cfg.ha_user_code or self._cfg.panel_user_code
if self._cfg.ha_check_user_code:
payload['code'] = code
elif code is None or code.isdigit():
payload['code'] = 'REMOTE_CODE'
else:
payload['code'] = 'REMOTE_CODE_TEXT'
return payload
def update_attributes(self):
self._mqtt_publish(
namespace=self._cfg.mqtt_namespace,
topic=self.attributes_topic,
retain=self._mqtt_retain,
payload=json.dumps({
'secure_arm': self._partition.secure_arm,
'alarm_type': self._partition.alarm_type,
'last_error_type': self._partition.last_error_type,
'last_error_desc': self._partition.last_error_desc,
'last_error_at': self._partition.last_error_at,
'disarm_failed': self._partition.disarm_failed,
}),
)
def update_state(self):
self._mqtt_publish(
namespace=self._cfg.mqtt_namespace,
topic=self.state_topic,
retain=self._mqtt_retain,
payload=self.ha_status,
)
class MqttWrapperQolsysSensor(MqttWrapper):
PAYLOAD_ON = 'Open'
PAYLOAD_OFF = 'Closed'
QOLSYS_TO_HA_DEVICE_CLASS = {
QolsysSensorAuxiliaryPendant: 'safety',
QolsysSensorBluetooth: 'presence',
QolsysSensorCODetector: 'gas',
QolsysSensorDoorWindow: 'door',
QolsysSensorDoorbell: 'sound',
QolsysSensorFreeze: 'cold',
QolsysSensorGlassBreak: 'vibration',
QolsysSensorHeat: 'heat',
QolsysSensorKeyFob: 'safety',
QolsysSensorKeypad: 'safety',
QolsysSensorMotion: 'motion',
QolsysSensorShock: 'vibration',
QolsysSensorSiren: 'safety',
QolsysSensorSmokeDetector: 'smoke',
QolsysSensorTakeoverModule: 'safety',
QolsysSensorTemperature: 'heat',
QolsysSensorTilt: 'garage_door',
QolsysSensorTranslator: 'safety',
QolsysSensorWater: 'moisture',
}
def __init__(self, sensor: QolsysSensor, *args, **kwargs):
super().__init__(*args, **kwargs)
self._sensor = sensor
@property
def name(self):
return self._sensor.name
@property
def ha_device_class(self):
for base in type(self._sensor).mro():
device_class = self.QOLSYS_TO_HA_DEVICE_CLASS.get(base)
if device_class:
return device_class
errormsg = 'Unable to find a device class to map for '\
f"sensor type {type(self._sensor).__name__}"
if self._cfg.default_sensor_device_class:
LOGGER.warning(f"{errormsg}, defaulting to "
f"'{self._cfg.default_sensor_device_class}' "
"device class.")
return self._cfg.default_sensor_device_class
else:
raise UnknownDeviceClassException(errormsg)
@property
def topic_path(self):
return posixpath.join(
'binary_sensor',
self.entity_id,
)
def configure_payload(self, partition: QolsysPartition, **kwargs):
payload = {
'name': self.name,
'device_class': self.ha_device_class,
'state_topic': self.state_topic,
'payload_on': self.PAYLOAD_ON,
'payload_off': self.PAYLOAD_OFF,
'availability_mode': 'all',
'availability': self.configure_availability,
'json_attributes_topic': self.attributes_topic,
'enabled_by_default': (
self._cfg.enable_static_sensors_by_default or
not isinstance(self._sensor, _QolsysSensorWithoutUpdates)
),
}
# As we have a unique ID for the panel, we can setup a unique ID for
# the partition, and create a device to link all of our partitions
# together; this will also allow to interact with the partition in
# the UI, change it's name, assign it to areas, etc.
payload['unique_id'] = f"{self._cfg.panel_unique_id}_"\
f"s{normalize_name_to_id(self._sensor.unique_id)}"
payload['device'] = self.device_payload
return payload
def update_attributes(self):
attributes = {
k: getattr(self._sensor, k)
for k in self._sensor.ATTRIBUTES
}
self._mqtt_publish(
namespace=self._cfg.mqtt_namespace,
topic=self.attributes_topic,
retain=self._mqtt_retain,
payload=json.dumps(attributes),
)
def update_state(self):
self._mqtt_publish(
namespace=self._cfg.mqtt_namespace,
topic=self.state_topic,
retain=self._mqtt_retain,
payload=self._sensor.status,
)
class MqttWrapperFactory(object):
__WRAPPERCLASSES_CACHE = {}
def __init__(self, *args, **kwargs):
self._args = args
self._kwargs = kwargs
def wrap(self, obj):
# Search the class that corresponds to that type, and use all the
# parents (in order, thanks to the call to mro()) to try and find
# one that works by inheritance
for base in type(obj).mro():
klass = find_subclass(MqttWrapper, base.__name__,
cache=self.__WRAPPERCLASSES_CACHE,
normalize=False)
if klass:
break
if not klass:
raise UnknownMqttWrapperException(
f'Unable to wrap object type {type(obj).__name__}'
)
return klass(obj, *self._args, **self._kwargs)