-
Notifications
You must be signed in to change notification settings - Fork 736
/
Copy path__init__.py
489 lines (385 loc) · 15.6 KB
/
__init__.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
"""Quirks implementations for the ZHA component of Homeassistant."""
from __future__ import annotations
import asyncio
import importlib
import importlib.util
import logging
import pathlib
import pkgutil
import sys
import typing
from typing import Any
import zigpy.device
import zigpy.endpoint
from zigpy.quirks import DEVICE_REGISTRY, CustomCluster, CustomDevice
import zigpy.types as t
from zigpy.util import ListenableMixin
from zigpy.zcl import foundation
from zigpy.zcl.clusters.general import PowerConfiguration
from zigpy.zcl.clusters.measurement import OccupancySensing
from zigpy.zcl.clusters.security import IasZone
from zigpy.zdo import types as zdotypes
from .const import (
ATTRIBUTE_ID,
ATTRIBUTE_NAME,
CLUSTER_COMMAND,
COMMAND_ATTRIBUTE_UPDATED,
DEVICE_TYPE,
ENDPOINTS,
INPUT_CLUSTERS,
MANUFACTURER,
MODEL,
MODELS_INFO,
MOTION_EVENT,
NODE_DESCRIPTOR,
OCCUPANCY_EVENT,
OCCUPANCY_STATE,
OFF,
ON,
OUTPUT_CLUSTERS,
PROFILE_ID,
UNKNOWN,
VALUE,
ZHA_SEND_EVENT,
ZONE_STATUS_CHANGE_COMMAND,
)
_LOGGER = logging.getLogger(__name__)
class Bus(ListenableMixin):
"""Event bus implementation."""
def __init__(self, *args, **kwargs):
"""Init event bus."""
super().__init__(*args, **kwargs)
self._listeners = {}
class LocalDataCluster(CustomCluster):
"""Cluster meant to prevent remote calls.
Set _CONSTANT_ATTRIBUTES to provide constant values for attribute ids.
Set _VALID_ATTRIBUTES to provide a list of valid attribute ids that will never be shown as unsupported.
These are attributes that should be populated later.
"""
_CONSTANT_ATTRIBUTES: dict[int, typing.Any] = {}
_VALID_ATTRIBUTES: set[int] = set()
async def bind(self):
"""Prevent bind."""
self.debug("binding LocalDataCluster")
return (foundation.Status.SUCCESS,)
async def unbind(self):
"""Prevent unbind."""
self.debug("unbinding LocalDataCluster")
return (foundation.Status.SUCCESS,)
async def _configure_reporting(self, *args, **kwargs): # pylint: disable=W0221
"""Prevent remote configure reporting."""
self.debug("configuring reporting for LocalDataCluster")
return (foundation.ConfigureReportingResponse.deserialize(b"\x00")[0],)
async def read_attributes_raw(self, attributes, manufacturer=None, **kwargs):
"""Prevent remote reads."""
msg = "reading attributes for LocalDataCluster"
self.debug(f"{msg}: attributes={attributes} manufacturer={manufacturer}")
records = [
foundation.ReadAttributeRecord(
attr, foundation.Status.UNSUPPORTED_ATTRIBUTE, foundation.TypeValue()
)
for attr in attributes
]
for record in records:
if record.attrid in self._CONSTANT_ATTRIBUTES:
record.value.value = self._CONSTANT_ATTRIBUTES[record.attrid]
else:
record.value.value = self._attr_cache.get(record.attrid)
if (
record.value.value is not None
or record.attrid in self._VALID_ATTRIBUTES
):
record.status = foundation.Status.SUCCESS
return (records,)
async def write_attributes(self, attributes, manufacturer=None, **kwargs):
"""Prevent remote writes."""
msg = "writing attributes for LocalDataCluster"
self.debug(f"{msg}: attributes={attributes} manufacturer={manufacturer}")
for attrid, value in attributes.items():
if isinstance(attrid, str):
attrid = self.attributes_by_name[attrid].id
elif attrid not in self.attributes:
self.error("%d is not a valid attribute id", attrid)
continue
self._update_attribute(attrid, value)
return ([foundation.WriteAttributesStatusRecord(foundation.Status.SUCCESS)],)
class EventableCluster(CustomCluster):
"""Cluster that generates events."""
def handle_cluster_request(
self,
hdr: foundation.ZCLHeader,
args: list[Any],
*,
dst_addressing: None
| (t.Addressing.Group | t.Addressing.IEEE | t.Addressing.NWK) = None,
):
"""Send cluster requests as events."""
if (
self.server_commands is not None
and self.server_commands.get(hdr.command_id) is not None
):
self.listener_event(
ZHA_SEND_EVENT,
self.server_commands.get(hdr.command_id, (hdr.command_id)).name,
args,
)
def _update_attribute(self, attrid, value):
super()._update_attribute(attrid, value)
if attrid in self.attributes:
attribute_name = self.attributes[attrid].name
else:
attribute_name = UNKNOWN
self.listener_event(
ZHA_SEND_EVENT,
COMMAND_ATTRIBUTE_UPDATED,
{
ATTRIBUTE_ID: attrid,
ATTRIBUTE_NAME: attribute_name,
VALUE: value,
},
)
class GroupBoundCluster(CustomCluster):
"""Cluster that can only bind to a group instead of direct to hub.
Binding this cluster results in binding to a group that the coordinator
is a member of.
"""
COORDINATOR_GROUP_ID = 0x30 # Group id with only coordinator as a member
async def bind(self):
"""Bind cluster to a group."""
# Ensure coordinator is a member of the group
application = self._endpoint.device.application
coordinator = application.get_device(application.state.node_info.ieee)
await coordinator.add_to_group(
self.COORDINATOR_GROUP_ID,
name="Coordinator Group - Created by ZHAQuirks",
)
# Bind cluster to group
dstaddr = zdotypes.MultiAddress()
dstaddr.addrmode = 1
dstaddr.nwk = self.COORDINATOR_GROUP_ID
dstaddr.endpoint = self._endpoint.endpoint_id
return await self._endpoint.device.zdo.Bind_req(
self._endpoint.device.ieee,
self._endpoint.endpoint_id,
self.cluster_id,
dstaddr,
)
class DoublingPowerConfigurationCluster(CustomCluster, PowerConfiguration):
"""PowerConfiguration cluster implementation.
This implementation doubles battery pct remaining for non standard devices
that don't follow the reporting spec.
"""
BATTERY_PERCENTAGE_REMAINING = 0x0021
def _update_attribute(self, attrid, value):
if attrid == self.BATTERY_PERCENTAGE_REMAINING:
value = value * 2
super()._update_attribute(attrid, value)
class PowerConfigurationCluster(CustomCluster, PowerConfiguration):
"""Common use power configuration cluster."""
BATTERY_VOLTAGE_ATTR = 0x0020
BATTERY_PERCENTAGE_REMAINING = 0x0021
MIN_VOLTS = 1.5 # old 2.1
MAX_VOLTS = 2.8 # old 3.2
def _update_attribute(self, attrid, value):
super()._update_attribute(attrid, value)
if attrid == self.BATTERY_VOLTAGE_ATTR and value not in (0, 255):
super()._update_attribute(
self.BATTERY_PERCENTAGE_REMAINING,
self._calculate_battery_percentage(value),
)
def _calculate_battery_percentage(self, raw_value):
volts = raw_value / 10
volts = max(volts, self.MIN_VOLTS)
volts = min(volts, self.MAX_VOLTS)
percent = round(
((volts - self.MIN_VOLTS) / (self.MAX_VOLTS - self.MIN_VOLTS)) * 200
)
self.debug(
"Voltage [RAW]:%s [Max]:%s [Min]:%s, Battery Percent: %s",
raw_value,
self.MAX_VOLTS,
self.MIN_VOLTS,
percent / 2,
)
return percent
class _Motion(CustomCluster, IasZone):
"""Self reset Motion cluster."""
reset_s: int = 30
def __init__(self, *args, **kwargs):
"""Init."""
super().__init__(*args, **kwargs)
self._loop = asyncio.get_running_loop()
self._timer_handle = None
def _turn_off(self):
self._timer_handle = None
self.debug("%s - Resetting motion sensor", self.endpoint.device.ieee)
self.listener_event(
CLUSTER_COMMAND, 253, ZONE_STATUS_CHANGE_COMMAND, [OFF, 0, 0, 0]
)
class MotionWithReset(_Motion):
"""Self reset Motion cluster.
Optionally send event over device bus.
"""
send_occupancy_event: bool = False
def handle_cluster_request(
self,
hdr: foundation.ZCLHeader,
args: list[Any],
*,
dst_addressing: None
| (t.Addressing.Group | t.Addressing.IEEE | t.Addressing.NWK) = None,
):
"""Handle the cluster command."""
# check if the command is for a zone status change of ZoneStatus.Alarm_1 or ZoneStatus.Alarm_2
if hdr.command_id == ZONE_STATUS_CHANGE_COMMAND and args[0] & 3:
if self._timer_handle:
self._timer_handle.cancel()
self._timer_handle = self._loop.call_later(self.reset_s, self._turn_off)
if self.send_occupancy_event:
self.endpoint.device.occupancy_bus.listener_event(OCCUPANCY_EVENT)
class MotionOnEvent(_Motion):
"""Motion based on received events from occupancy."""
reset_s: int = 120
def __init__(self, *args, **kwargs):
"""Init."""
super().__init__(*args, **kwargs)
self.endpoint.device.motion_bus.add_listener(self)
def motion_event(self):
"""Motion event."""
super().listener_event(
CLUSTER_COMMAND, 254, ZONE_STATUS_CHANGE_COMMAND, [ON, 0, 0, 0]
)
self.debug("%s - Received motion event message", self.endpoint.device.ieee)
if self._timer_handle:
self._timer_handle.cancel()
self._timer_handle = self._loop.call_later(self.reset_s, self._turn_off)
class _Occupancy(CustomCluster, OccupancySensing):
"""Self reset Occupancy cluster."""
reset_s: int = 600
def __init__(self, *args, **kwargs):
"""Init."""
super().__init__(*args, **kwargs)
self._timer_handle = None
self._loop = asyncio.get_running_loop()
def _turn_off(self):
self._timer_handle = None
self._update_attribute(OCCUPANCY_STATE, OFF)
class OccupancyOnEvent(_Occupancy):
"""Self reset occupancy from bus."""
def __init__(self, *args, **kwargs):
"""Init."""
super().__init__(*args, **kwargs)
self.endpoint.device.occupancy_bus.add_listener(self)
def occupancy_event(self):
"""Occupancy event."""
self._update_attribute(OCCUPANCY_STATE, ON)
if self._timer_handle:
self._timer_handle.cancel()
self._timer_handle = self._loop.call_later(self.reset_s, self._turn_off)
class OccupancyWithReset(_Occupancy):
"""Self reset Occupancy cluster and send event on motion bus."""
def _update_attribute(self, attrid, value):
super()._update_attribute(attrid, value)
if attrid == OCCUPANCY_STATE and value == ON:
if self._timer_handle:
self._timer_handle.cancel()
self.endpoint.device.motion_bus.listener_event(MOTION_EVENT)
self._timer_handle = self._loop.call_later(self.reset_s, self._turn_off)
class QuickInitDevice(CustomDevice):
"""Devices with quick initialization from quirk signature."""
signature: dict[str, Any] | None = None
@classmethod
def from_signature(
cls, device: zigpy.device.Device, model: str | None = None
) -> zigpy.device.Device:
"""Update device accordingly to quirk signature."""
assert isinstance(cls.signature, dict)
if model is None:
model = cls.signature[MODEL]
manufacturer = cls.signature.get(MANUFACTURER)
if manufacturer is None:
manufacturer = cls.signature[MODELS_INFO][0][0]
device.node_desc = cls.signature[NODE_DESCRIPTOR]
endpoints = cls.signature[ENDPOINTS]
for ep_id, ep_data in endpoints.items():
endpoint = device.add_endpoint(ep_id)
endpoint.profile_id = ep_data[PROFILE_ID]
endpoint.device_type = ep_data[DEVICE_TYPE]
for cluster_id in ep_data[INPUT_CLUSTERS]:
cluster = endpoint.add_input_cluster(cluster_id)
if cluster.ep_attribute == "basic":
manuf_attr_id = cluster.attributes_by_name[MANUFACTURER].id
cluster._update_attribute( # pylint: disable=W0212
manuf_attr_id, manufacturer
)
cluster._update_attribute( # pylint: disable=W0212
cluster.attributes_by_name[MODEL].id, model
)
for cluster_id in ep_data[OUTPUT_CLUSTERS]:
endpoint.add_output_cluster(cluster_id)
endpoint.status = zigpy.endpoint.Status.ZDO_INIT
device.status = zigpy.device.Status.ENDPOINTS_INIT
device.manufacturer = manufacturer
device.model = model
return device
class NoReplyMixin:
"""A simple mixin.
Allows a cluster to have configurable list of command
ids that do not generate an explicit reply.
"""
void_input_commands: set[int] = {}
async def command(self, command, *args, expect_reply=None, **kwargs):
"""Override the default Cluster command.
expect_reply behavior is based on void_input_commands.
Note that this method changes the default value of
expect_reply to None. This allows the caller to explicitly force
expect_reply to true.
"""
if expect_reply is None and command in self.void_input_commands:
cmd_expect_reply = False
elif expect_reply is None:
cmd_expect_reply = True # the default
else:
cmd_expect_reply = expect_reply
rsp = await super().command(
command, *args, expect_reply=cmd_expect_reply, **kwargs
)
if expect_reply is None and command in self.void_input_commands:
# Pretend we received a default reply
return foundation.GENERAL_COMMANDS[
foundation.GeneralCommand.Default_Response
].schema(command_id=command, status=foundation.Status.SUCCESS)
return rsp
def setup(custom_quirks_path: str | None = None) -> None:
"""Register all quirks with zigpy, including optional custom quirks."""
if custom_quirks_path is not None:
DEVICE_REGISTRY.purge_custom_quirks(custom_quirks_path)
# Import all quirks in the `zhaquirks` package first
for _importer, modname, _ispkg in pkgutil.walk_packages(
path=__path__,
prefix=__name__ + ".",
):
_LOGGER.debug("Loading quirks module %r", modname)
importlib.import_module(modname)
if custom_quirks_path is None:
return
path = pathlib.Path(custom_quirks_path)
_LOGGER.debug("Loading custom quirks from %r", path)
loaded = False
# Treat the custom quirk path (e.g. `/config/custom_quirks/`) itself as a module
for importer, modname, _ispkg in pkgutil.walk_packages(path=[str(path)]):
_LOGGER.debug("Loading custom quirk module %r", modname)
try:
spec = importer.find_spec(modname)
module = importlib.util.module_from_spec(spec)
sys.modules[modname] = module
spec.loader.exec_module(module)
except Exception:
_LOGGER.exception("Unexpected exception importing custom quirk %r", modname)
else:
loaded = True
if loaded:
_LOGGER.warning(
"Loaded custom quirks. Please contribute them to"
" https://github.com/zigpy/zha-device-handlers"
)