-
Notifications
You must be signed in to change notification settings - Fork 5
/
__init__.py
422 lines (269 loc) · 9.98 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
import importlib
import logging
import voluptuous as vol
import zigpy.types as t
import homeassistant.helpers.config_validation as cv
DEPENDENCIES = ["zha"]
DOMAIN = "zha_custom"
ATTR_COMMAND = "command"
ATTR_COMMAND_DATA = "command_data"
ATTR_IEEE = "ieee"
DATA_ZHAC = "zha_custom"
SERVICE_CUSTOM = "execute"
_LOGGER = logging.getLogger(__name__)
SERVICE_SCHEMAS = {
SERVICE_CUSTOM: vol.Schema(
{
vol.Optional(ATTR_IEEE): t.EUI64.convert,
vol.Optional(ATTR_COMMAND): cv.string,
vol.Optional(ATTR_COMMAND_DATA): cv.string,
},
extra=vol.ALLOW_EXTRA,
)
}
async def async_setup(hass, config):
"""Set up ZHA from config."""
if DOMAIN not in config:
return True
try:
zha_gw = hass.data["zha"]["zha_gateway"]
except KeyError:
return True
async def custom_service(service):
"""Run command from custom module."""
_LOGGER.info("Running custom service: %s", service)
ieee = service.data.get(ATTR_IEEE)
cmd = service.data.get(ATTR_COMMAND)
cmd_data = service.data.get(ATTR_COMMAND_DATA)
mod_path = "custom_components.{}".format(DOMAIN)
try:
module = importlib.import_module(mod_path)
except ImportError as err:
_LOGGER.error("Couldn't load zha_service module: %s", err)
return
importlib.reload(module)
_LOGGER.debug("module is %s", module)
if cmd:
handler = getattr(module, "command_handler_{}".format(cmd))
await handler(
zha_gw.application_controller, zha_gw, ieee, cmd, cmd_data, service
)
else:
await module.default_command(
zha_gw.application_controller, zha_gw, ieee, cmd, cmd_data, service
)
hass.services.async_register(
DOMAIN, SERVICE_CUSTOM, custom_service, schema=SERVICE_SCHEMAS[SERVICE_CUSTOM]
)
return True
async def default_command(app, listener, ieee, cmd, data, service):
_LOGGER.debug("running default command: %s", service)
async def command_handler_handle_join(app, listener, ieee, cmd, data, service):
"""Rediscover a device.
ieee -- ieee of the device
data -- nwk of the device in decimal format
"""
_LOGGER.debug("running 'handle_join' command: %s", service)
if ieee is None:
return
app.handle_join(int(data, 16), ieee, 0)
async def command_handler_scan_device(*args, **kwargs):
"""Scan a device for all supported attributes and commands.
ieee -- ieee of the device to scan
ToDo: use manufacturer_id to scan for manufacturer specific clusters/attrs.
"""
from . import scan_device
importlib.reload(scan_device)
await scan_device.scan_device(*args, **kwargs)
async def command_handler_get_groups(*args, **kwargs):
"""Get all groups a device is member of.
ieee -- ieee of the device to issue "get_groups" cluster command
"""
from . import groups
importlib.reload(groups)
await groups.get_groups(*args, **kwargs)
async def command_handler_add_group(*args, **kwargs):
"""Add a group to the device.
ieee -- device to issue "add_group" Groups cluster command
data -- group_id of the group to add, in 0xXXXX format
"""
from . import groups
importlib.reload(groups)
await groups.add_group(*args, **kwargs)
async def command_handler_remove_group(*args, **kwargs):
"""Remove a group from the device.
ieee -- device to issue "remove_group" Groups cluster command
data -- group_id of the group to remove in 0xXXXX format
"""
from . import groups
importlib.reload(groups)
await groups.remove_group(*args, **kwargs)
async def command_handler_remove_all_groups(*args, **kwargs):
"""Remove all groups from a device.
ieee -- device to issue "remove all" Groups cluster command
"""
from . import groups
importlib.reload(groups)
await groups.remove_all_groups(*args, **kwargs)
async def command_handler_bind_group(*args, **kwargs):
"""Add group binding to a device.
ieee -- ieee of the remote (device configured with a binding)
data -- group_id
"""
from . import binds
importlib.reload(binds)
await binds.bind_group(*args, **kwargs)
async def command_handler_unbind_group(*args, **kwargs):
"""Remove group binding from a device.
ieee -- ieee of the remote (device configured with a binding)
data -- group_id
"""
from . import binds
importlib.reload(binds)
await binds.unbind_group(*args, **kwargs)
async def command_handler_bind_ieee(*args, **kwargs):
"""IEEE bind device.
ieee -- ieee of the remote (device configured with a binding)
data -- ieee of the target device (device remote sends commands to)
"""
from . import binds
importlib.reload(binds)
await binds.bind_ieee(*args, **kwargs)
async def command_handler_unbind_coordinator(*args, **kwargs):
"""IEEE bind device.
ieee -- ieee of the device to unbind from coordinator
data -- cluster ID to unbind
"""
from . import binds
importlib.reload(binds)
await binds.unbind_coordinator(*args, **kwargs)
async def command_handler_rejoin(app, listener, ieee, cmd, data, service):
"""Leave and rejoin command.
data -- device ieee to allow joining through
ieee -- ieee of the device to leave and rejoin
"""
if ieee is None:
_LOGGER.error("missing ieee")
return
_LOGGER.debug("running 'rejoin' command: %s", service)
src = app.get_device(ieee=ieee)
if data is None:
await app.permit()
else:
await app.permit(node=convert_ieee(data))
res = await src.zdo.request(0x0034, src.ieee, 0x01)
_LOGGER("%s: leave and rejoin result: %s", src, ieee, res)
def command_handler_get_zll_groups(*args, **kwargs):
from . import groups
importlib.reload(groups)
return groups.get_zll_groups(*args, **kwargs)
def command_handler_add_to_group(*args, **kwargs):
"""Add device to a group."""
from . import groups
importlib.reload(groups)
return groups.add_to_group(*args, **kwargs)
def command_handler_remove_from_group(*args, **kwargs):
"""Remove device from a group."""
from . import groups
importlib.reload(groups)
return groups.remove_from_group(*args, **kwargs)
def command_handler_sinope(*args, **kwargs):
from . import sinope
importlib.reload(sinope)
return sinope.sinope_write_test(*args, **kwargs)
def command_handler_get_routes_and_neighbours(*args, **kwargs):
"""Scan a device for neighbours and routes.
ieee -- ieee of the device to scan
"""
from . import neighbours
importlib.reload(neighbours)
return neighbours.routes_and_neighbours(*args, **kwargs)
def command_handler_all_routes_and_neighbours(*args, **kwargs):
"""Scan all devices for neighbours and routes. """
from . import neighbours
importlib.reload(neighbours)
return neighbours.all_routes_and_neighbours(*args, **kwargs)
def command_handler_leave(*args, **kwargs):
from . import zdo
importlib.reload(zdo)
return zdo.leave(*args, **kwargs)
def command_handler_ieee_ping(*args, **kwargs):
from . import zdo
importlib.reload(zdo)
return zdo.ieee_ping(*args, **kwargs)
def command_handler_zigpy_deconz(*args, **kwargs):
"""Zigpy deconz test. """
from . import zigpy_deconz
importlib.reload(zigpy_deconz)
return zigpy_deconz.zigpy_deconz(*args, **kwargs)
def command_handler_ezsp_set_channel(*args, **kwargs):
"""Set EZSP radio channel. """
from . import ezsp
importlib.reload(ezsp)
return ezsp.set_channel(*args, **kwargs)
def command_handler_ezsp_get_token(*args, **kwargs):
"""Set EZSP radio channel. """
from . import ezsp
importlib.reload(ezsp)
return ezsp.get_token(*args, **kwargs)
def command_handler_ezsp_start_mfg(*args, **kwargs):
"""Set EZSP radio channel. """
from . import ezsp
importlib.reload(ezsp)
return ezsp.start_mfg(*args, **kwargs)
def command_handler_ezsp_get_keys(*args, **kwargs):
"""Get EZSP keys. """
from . import ezsp
importlib.reload(ezsp)
return ezsp.get_keys(*args, **kwargs)
def command_handler_ezsp_add_key(*args, **kwargs):
"""Add transient link key. """
from . import ezsp
importlib.reload(ezsp)
return ezsp.add_transient_key(*args, **kwargs)
def command_handler_ezsp_get_ieee_by_nwk(*args, **kwargs):
"""Get EZSP keys. """
from . import ezsp
importlib.reload(ezsp)
return ezsp.get_ieee_by_nwk(*args, **kwargs)
def command_handler_ezsp_get_policy(*args, **kwargs):
"""Get EZSP keys. """
from . import ezsp
importlib.reload(ezsp)
return ezsp.get_policy(*args, **kwargs)
def command_handler_ezsp_clear_keys(*args, **kwargs):
"""Clear key table."""
from . import ezsp
importlib.reload(ezsp)
return ezsp.clear_keys(*args, **kwargs)
def command_handler_ezsp_get_config_value(*args, **kwargs):
"""Get EZSP config value."""
from . import ezsp
importlib.reload(ezsp)
return ezsp.get_config_value(*args, **kwargs)
def command_handler_ezsp_get_value(*args, **kwargs):
"""Get EZSP value."""
from . import ezsp
importlib.reload(ezsp)
return ezsp.get_value(*args, **kwargs)
def command_handler_ota_notify(*args, **kwargs):
"""Set EZSP radio channel. """
from . import ota
importlib.reload(ota)
return ota.notify(*args, **kwargs)
def command_handler_zdo_join_with_code(*args, **kwargs):
from . import zdo
importlib.reload(zdo)
return zdo.join_with_code(*args, **kwargs)
def command_handler_zdo_update_nwk_id(*args, **kwargs):
from . import zdo
importlib.reload(zdo)
return zdo.update_nwk_id(*args, **kwargs)
def command_handler_zdo_scan_now(*args, **kwargs):
from . import zdo
importlib.reload(zdo)
return zdo.topo_scan_now(*args, **kwargs)
def command_handler_zdo_flood_parent_annce(*args, **kwargs):
from . import zdo
importlib.reload(zdo)
return zdo.flood_parent_annce(*args, **kwargs)