-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsensors.py
376 lines (284 loc) · 12 KB
/
sensors.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
import json
import logging
import time
from qolsys.exceptions import UnableToParseSensorException
from qolsys.exceptions import UnknownQolsysSensorException
from qolsys.observable import QolsysObservable
from qolsys.partition import QolsysPartition
from qolsys.utils import find_subclass
LOGGER = logging.getLogger(__name__)
class QolsysSensor(QolsysObservable):
NOTIFY_UPDATE_PATTERN = 'update_{attr}'
NOTIFY_UPDATE_STATUS = 'update_status'
NOTIFY_UPDATE_ATTRIBUTES = 'update_attributes'
__SUBCLASSES_CACHE = {}
_common_keys = [
'name',
'status',
'zone_id',
'partition_id',
]
ATTRIBUTES = [
'group',
'state',
'zone_type',
'zone_physical_type',
'zone_alarm_type',
'tampered',
]
def __init__(self, sensor_id: str, name: str, group: str, status: str,
state: str, zone_id: int, zone_type: int,
zone_physical_type: int, zone_alarm_type: int,
partition_id: int, partition: QolsysPartition) -> None:
super().__init__()
self._id = sensor_id
self._name = name
self._group = group
self._status = status
self._state = state
self._zone_id = zone_id
self._zone_type = zone_type
self._zone_physical_type = zone_physical_type
self._zone_alarm_type = zone_alarm_type
self._partition_id = partition_id
self._partition = partition
self._tampered = False
self._last_open_tampered_at = None
self._last_closed_tampered_at = None
@property
def partition(self) -> QolsysPartition:
return self._partition
@partition.setter
def partition(self, partition: QolsysPartition):
self._partition = partition
def update(self, sensor: 'QolsysSensor'):
if self.id != sensor.id:
LOGGER.warning(f"Updating sensor '{self.id}' ({self.name}) with "
f"sensor '{sensor.id}' (different id)")
# Because any of the attributes might have changed and we want to
# be able to notify of all of those changes separately and only if they
# happened, we have to add a bit of smart in there
attributes_updated = False
for attr in ['id'] + self._common_keys + self.ATTRIBUTES:
local_attr = f'_{attr}'
prev_value = getattr(self, local_attr)
new_value = getattr(sensor, attr)
if prev_value != new_value:
setattr(self, local_attr, new_value)
self.notify(change=self.NOTIFY_UPDATE_PATTERN.format(attr=attr),
prev_value=prev_value, new_value=new_value)
if attr in self.ATTRIBUTES:
attributes_updated = True
if attributes_updated:
self.notify(change=self.NOTIFY_UPDATE_ATTRIBUTES)
@property
def id(self):
return self._id
@property
def unique_id(self):
# Check if this sensor's zone_id is the same as the first sensor's
# zone_id we have seen with this id. If it is, we return the sensor
# id directly, if not, we will want to append the zone_id to ensure
# distinct sensor unique ids
if self._partition is None:
raise AttributeError("Partition not set for sensor")
first_sensor = self._partition.sensor(self._id)
if first_sensor is None or first_sensor.zone_id != self._zone_id:
return f"{self._id}_{self._zone_id}"
return self.id
@property
def name(self):
return self._name
@property
def group(self):
return self._group
@property
def status(self):
return self._status
@property
def state(self):
return self._state
@property
def zone_id(self):
return self._zone_id
@property
def zone_type(self):
return self._zone_type
@property
def zone_physical_type(self):
return self._zone_physical_type
@property
def zone_alarm_type(self):
return self._zone_alarm_type
@property
def partition_id(self):
return self._partition_id
@property
def tampered(self):
return self._tampered
@property
def is_open(self):
return self._status == 'Open'
@property
def is_closed(self):
return self._status == 'Closed'
@status.setter
def status(self, value):
new_value = value.capitalize()
if new_value not in ['Open', 'Closed']:
raise AttributeError(f"Invalid value '{value}' for attribute 'status'")
if self._status != new_value:
LOGGER.debug(f"Sensor '{self.id}' ({self.name}) status updated to '{new_value}'")
prev_value = self._status
self._status = new_value
self.notify(change=self.NOTIFY_UPDATE_STATUS,
prev_value=prev_value, new_value=new_value)
@tampered.setter
def tampered(self, value):
new_value = bool(value)
if self._tampered != new_value:
LOGGER.debug(f"Sensor '{self.id}' ({self.name}) tampered updated to '{new_value}'")
prev_value = self._tampered
self._tampered = new_value
self.notify(change=self.NOTIFY_UPDATE_PATTERN.format(attr='tampered'),
prev_value=prev_value, new_value=new_value)
self.notify(change=self.NOTIFY_UPDATE_ATTRIBUTES)
def _next_status_update_is_status(self):
# When we are back from a tamper setting, we get two updates
# subsequently as an open, and then a close, in the same second
return (self._last_open_tampered_at is not None and
self._last_closed_tampered_at is not None and
self._last_closed_tampered_at - self._last_open_tampered_at < 1)
def open(self):
if self.is_open and not self._next_status_update_is_status():
self._last_open_tampered_at = time.time()
self.tampered = True
else:
self.status = 'Open'
def closed(self):
if self.tampered:
self._last_closed_tampered_at = time.time()
self.tampered = False
else:
self.status = 'Closed'
def __str__(self):
return (f"<{type(self).__name__} id={self.id} name={self.name} "
f"group={self.group} status={self.status} "
f"state={self.state} zone_id={self.zone_id} "
f"zone_type={self.zone_type} "
f"zone_physical_type={self.zone_physical_type} "
f"zone_alarm_type={self.zone_alarm_type} "
f"partition_id={self.partition_id}>")
@classmethod
def from_json(cls, data, partition):
if isinstance(data, str):
data = json.loads(data)
sensor_type = data.get('type')
if not sensor_type:
raise UnknownQolsysSensorException(
f'Sensor type not found for sensor {data}'
)
klass = find_subclass(cls, sensor_type, cache=cls.__SUBCLASSES_CACHE,
preserve_capitals=True)
if not klass:
raise UnknownQolsysSensorException(
f"Sensor type '{sensor_type}' unsupported for sensor {data}"
)
return klass.from_json(data, partition, common=cls.from_json_common_data(data))
@classmethod
def from_json_common_data(cls, data):
common_data = {k: v for k, v in data.items()
if k in cls._common_keys or k in cls.ATTRIBUTES}
common_data['sensor_id'] = data['id']
return common_data
@classmethod
def from_json_subclass(cls, subtype, data, partition, common=None):
sensor_type = data.get('type')
if sensor_type != subtype:
raise UnableToParseSensorException(
f"Cannot parse sensor '{sensor_type}'")
if common is None:
common = cls.from_json_common_data(data)
return cls(partition=partition, **common)
class _QolsysSensorWithoutUpdates(object):
pass
class QolsysSensorDoorWindow(QolsysSensor):
@classmethod
def from_json(cls, data, partition, common=None):
return cls.from_json_subclass('Door_Window', data, partition, common)
class QolsysSensorMotion(QolsysSensor):
@classmethod
def from_json(cls, data, partition, common=None):
return cls.from_json_subclass('Motion', data, partition, common)
class QolsysSensorPanelMotion(QolsysSensorMotion):
@classmethod
def from_json(cls, data, partition, common=None):
return cls.from_json_subclass('Panel Motion', data, partition, common)
class QolsysSensorGlassBreak(QolsysSensor):
@classmethod
def from_json(cls, data, partition, common=None):
return cls.from_json_subclass('GlassBreak', data, partition, common)
class QolsysSensorPanelGlassBreak(QolsysSensorGlassBreak, _QolsysSensorWithoutUpdates):
@classmethod
def from_json(cls, data, partition, common=None):
return cls.from_json_subclass('Panel Glass Break', data, partition, common)
class QolsysSensorBluetooth(QolsysSensor, _QolsysSensorWithoutUpdates):
@classmethod
def from_json(cls, data, partition, common=None):
return cls.from_json_subclass('Bluetooth', data, partition, common)
class QolsysSensorSmokeDetector(QolsysSensor):
@classmethod
def from_json(cls, data, partition, common=None):
return cls.from_json_subclass('SmokeDetector', data, partition, common)
class QolsysSensorCODetector(QolsysSensor):
@classmethod
def from_json(cls, data, partition, common=None):
return cls.from_json_subclass('CODetector', data, partition, common)
class QolsysSensorWater(QolsysSensor):
@classmethod
def from_json(cls, data, partition, common=None):
return cls.from_json_subclass('Water', data, partition, common)
class QolsysSensorFreeze(QolsysSensor):
@classmethod
def from_json(cls, data, partition, common=None):
return cls.from_json_subclass('Freeze', data, partition, common)
class QolsysSensorHeat(QolsysSensor):
@classmethod
def from_json(cls, data, partition, common=None):
return cls.from_json_subclass('Heat', data, partition, common)
class QolsysSensorTilt(QolsysSensor):
@classmethod
def from_json(cls, data, partition, common=None):
return cls.from_json_subclass('Tilt', data, partition, common)
class QolsysSensorKeypad(QolsysSensor, _QolsysSensorWithoutUpdates):
@classmethod
def from_json(cls, data, partition, common=None):
return cls.from_json_subclass('Keypad', data, partition, common)
class QolsysSensorAuxiliaryPendant(QolsysSensor, _QolsysSensorWithoutUpdates):
@classmethod
def from_json(cls, data, partition, common=None):
return cls.from_json_subclass('Auxiliary Pendant', data, partition, common)
class QolsysSensorSiren(QolsysSensor, _QolsysSensorWithoutUpdates):
@classmethod
def from_json(cls, data, partition, common=None):
return cls.from_json_subclass('Siren', data, partition, common)
class QolsysSensorKeyFob(QolsysSensor, _QolsysSensorWithoutUpdates):
@classmethod
def from_json(cls, data, partition, common=None):
return cls.from_json_subclass('KeyFob', data, partition, common)
class QolsysSensorTemperature(QolsysSensor):
@classmethod
def from_json(cls, data, partition, common=None):
return cls.from_json_subclass('Temperature', data, partition, common)
class QolsysSensorTakeoverModule(QolsysSensor, _QolsysSensorWithoutUpdates):
@classmethod
def from_json(cls, data, partition, common=None):
return cls.from_json_subclass('TakeoverModule', data, partition, common)
class QolsysSensorTranslator(QolsysSensor, _QolsysSensorWithoutUpdates):
@classmethod
def from_json(cls, data, partition, common=None):
return cls.from_json_subclass('Translator', data, partition, common)
class QolsysSensorDoorbell(QolsysSensor):
@classmethod
def from_json(cls, data, partition, common=None):
return cls.from_json_subclass('Doorbell', data, partition, common)