-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathdevice_controller.py
1407 lines (1262 loc) · 57.6 KB
/
device_controller.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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Controller that Manages Matter devices."""
# pylint: disable=too-many-lines
from __future__ import annotations
import asyncio
from collections import deque
from datetime import datetime
from functools import partial
import logging
from random import randint
import time
from typing import TYPE_CHECKING, Any, Callable, Iterable, TypeVar, cast
from chip.ChipDeviceCtrl import DeviceProxyWrapper
from chip.clusters import Attribute, Objects as Clusters
from chip.clusters.Attribute import ValueDecodeFailure
from chip.clusters.ClusterObjects import ALL_ATTRIBUTES, ALL_CLUSTERS, Cluster
from chip.exceptions import ChipStackError
from chip.native import PyChipError
from zeroconf import BadTypeInNameException, IPVersion, ServiceStateChange, Zeroconf
from zeroconf.asyncio import AsyncServiceBrowser, AsyncServiceInfo, AsyncZeroconf
from matter_server.common.models import CommissionableNodeData, CommissioningParameters
from matter_server.server.helpers.attributes import parse_attributes_from_read_result
from matter_server.server.helpers.utils import ping_ip
from ..common.errors import (
NodeCommissionFailed,
NodeInterviewFailed,
NodeNotExists,
NodeNotReady,
NodeNotResolving,
)
from ..common.helpers.api import api_command
from ..common.helpers.util import (
create_attribute_path_from_attribute,
dataclass_from_dict,
dataclass_to_dict,
parse_attribute_path,
parse_value,
)
from ..common.models import (
APICommand,
EventType,
MatterNodeData,
MatterNodeEvent,
NodePingResult,
)
from .const import DATA_MODEL_SCHEMA_VERSION, PAA_ROOT_CERTS_DIR
from .helpers.paa_certificates import fetch_certificates
if TYPE_CHECKING:
from chip.ChipDeviceCtrl import ChipDeviceController
from .server import MatterServer
_T = TypeVar("_T")
DATA_KEY_NODES = "nodes"
DATA_KEY_LAST_NODE_ID = "last_node_id"
LOGGER = logging.getLogger(__name__)
NODE_SUBSCRIPTION_CEILING_WIFI = 30
NODE_SUBSCRIPTION_CEILING_THREAD = 60
NODE_SUBSCRIPTION_CEILING_BATTERY_POWERED = 600
MAX_COMMISSION_RETRIES = 3
NODE_RESUBSCRIBE_ATTEMPTS_UNAVAILABLE = 3
NODE_RESUBSCRIBE_TIMEOUT_OFFLINE = 30 * 60 * 1000
NODE_PING_TIMEOUT = 10
NODE_PING_TIMEOUT_BATTERY_POWERED = 60
NODE_MDNS_BACKOFF = 610 # must be higher than (highest) sub ceiling
FALLBACK_NODE_SCANNER_INTERVAL = 1800
MDNS_TYPE_OPERATIONAL_NODE = "_matter._tcp.local."
MDNS_TYPE_COMMISSIONABLE_NODE = "_matterc._udp.local."
ROUTING_ROLE_ATTRIBUTE_PATH = create_attribute_path_from_attribute(
0, Clusters.ThreadNetworkDiagnostics.Attributes.RoutingRole
)
# pylint: disable=too-many-lines,too-many-locals,too-many-statements,too-many-branches,too-many-instance-attributes
class MatterDeviceController:
"""Class that manages the Matter devices."""
chip_controller: ChipDeviceController | None
fabric_id_hex: str
def __init__(
self,
server: MatterServer,
):
"""Initialize the device controller."""
self.server = server
# we keep the last events in memory so we can include them in the diagnostics dump
self.event_history: deque[Attribute.EventReadResult] = deque(maxlen=25)
self._subscriptions: dict[int, Attribute.SubscriptionTransaction] = {}
self._nodes_in_setup: set[int] = set()
self._node_last_seen: dict[int, float] = {}
self._nodes: dict[int, MatterNodeData] = {}
self._last_known_ip_addresses: dict[int, list[str]] = {}
self._last_subscription_attempt: dict[int, int] = {}
self._known_commissioning_params: dict[int, CommissioningParameters] = {}
self.wifi_credentials_set: bool = False
self.thread_credentials_set: bool = False
self.compressed_fabric_id: int | None = None
self._aiobrowser: AsyncServiceBrowser | None = None
self._aiozc: AsyncZeroconf | None = None
self._fallback_node_scanner_timer: asyncio.TimerHandle | None = None
self._fallback_node_scanner_task: asyncio.Task | None = None
self._node_setup_throttle = asyncio.Semaphore(5)
self._mdns_event_timer: dict[str, asyncio.TimerHandle] = {}
self._node_lock: dict[int, asyncio.Lock] = {}
async def initialize(self) -> None:
"""Async initialize of controller."""
# (re)fetch all PAA certificates once at startup
# NOTE: this must be done before initializing the controller
await fetch_certificates()
# Instantiate the underlying ChipDeviceController instance on the Fabric
self.chip_controller = self.server.stack.fabric_admin.NewController(
paaTrustStorePath=str(PAA_ROOT_CERTS_DIR)
)
self.compressed_fabric_id = cast(
int, await self._call_sdk(self.chip_controller.GetCompressedFabricId)
)
self.fabric_id_hex = hex(self.compressed_fabric_id)[2:]
LOGGER.debug("CHIP Device Controller Initialized")
async def start(self) -> None:
"""Handle logic on controller start."""
# load nodes from persistent storage
nodes: dict[str, dict | None] = self.server.storage.get(DATA_KEY_NODES, {})
orphaned_nodes: set[str] = set()
for node_id_str, node_dict in nodes.items():
node_id = int(node_id_str)
if node_dict is None:
# Non-initialized (left-over) node from a failed commissioning attempt.
# NOTE: This code can be removed in a future version
# as this can no longer happen.
orphaned_nodes.add(node_id_str)
continue
try:
node = dataclass_from_dict(MatterNodeData, node_dict, strict=True)
except (KeyError, ValueError):
# constructing MatterNodeData from the cached dict is not possible,
# revert to a fallback object and the node will be re-interviewed
node = MatterNodeData(
node_id=node_id,
date_commissioned=node_dict.get(
"date_commissioned",
datetime(1970, 1, 1),
),
last_interview=node_dict.get(
"last_interview",
datetime(1970, 1, 1),
),
interview_version=0,
)
# always mark node as unavailable at startup until subscriptions are ready
node.available = False
self._nodes[node_id] = node
# cleanup orhpaned nodes from storage
for node_id_str in orphaned_nodes:
self.server.storage.remove(DATA_KEY_NODES, node_id_str)
LOGGER.info("Loaded %s nodes from stored configuration", len(self._nodes))
# set-up mdns browser
self._aiozc = AsyncZeroconf(ip_version=IPVersion.All)
services = [MDNS_TYPE_OPERATIONAL_NODE, MDNS_TYPE_COMMISSIONABLE_NODE]
self._aiobrowser = AsyncServiceBrowser(
self._aiozc.zeroconf,
services,
handlers=[self._on_mdns_service_state_change],
)
# set-up fallback node scanner
self._schedule_fallback_scanner()
async def stop(self) -> None:
"""Handle logic on server stop."""
if self.chip_controller is None:
raise RuntimeError("Device Controller not initialized.")
# shutdown (and cleanup) mdns browser and fallback node scanner
if self._aiobrowser:
await self._aiobrowser.async_cancel()
if self._fallback_node_scanner_timer:
self._fallback_node_scanner_timer.cancel()
if (scan_task := self._fallback_node_scanner_task) and not scan_task.done():
scan_task.cancel()
if self._aiozc:
await self._aiozc.async_close()
# unsubscribe all node subscriptions
for sub in self._subscriptions.values():
await self._call_sdk(sub.Shutdown)
self._subscriptions = {}
# shutdown the sdk device controller
await self._call_sdk(self.chip_controller.Shutdown)
LOGGER.debug("Stopped.")
@api_command(APICommand.GET_NODES)
def get_nodes(self, only_available: bool = False) -> list[MatterNodeData]:
"""Return all Nodes known to the server."""
return [
x
for x in self._nodes.values()
if x is not None and (x.available or not only_available)
]
@api_command(APICommand.GET_NODE)
def get_node(self, node_id: int) -> MatterNodeData:
"""Return info of a single Node."""
if node := self._nodes.get(node_id):
return node
raise NodeNotExists(f"Node {node_id} does not exist or is not yet interviewed")
@api_command(APICommand.COMMISSION_WITH_CODE)
async def commission_with_code(
self, code: str, network_only: bool = False
) -> MatterNodeData:
"""
Commission a device using a QR Code or Manual Pairing Code.
:param code: The QR Code or Manual Pairing Code for device commissioning.
:param network_only: If True, restricts device discovery to network only.
:return: The NodeInfo of the commissioned device.
"""
if self.chip_controller is None:
raise RuntimeError("Device Controller not initialized.")
node_id = self._get_next_node_id()
attempts = 0
# we retry commissioning a few times as we've seen devices in the wild
# that are a bit unstable.
# by retrying, we increase the chances of a successful commission
while attempts <= MAX_COMMISSION_RETRIES:
attempts += 1
LOGGER.info(
"Starting Matter commissioning with code using Node ID %s (attempt %s/%s).",
node_id,
attempts,
MAX_COMMISSION_RETRIES,
)
result: PyChipError | None = await self._call_sdk(
self.chip_controller.CommissionWithCode,
setupPayload=code,
nodeid=node_id,
networkOnly=network_only,
)
if result and result.is_success:
break
if attempts >= MAX_COMMISSION_RETRIES:
raise NodeCommissionFailed(
f"Commission with code failed for node {node_id}."
)
await asyncio.sleep(5)
LOGGER.info("Matter commissioning of Node ID %s successful.", node_id)
# perform full (first) interview of the device
# we retry the interview max 3 times as it may fail in noisy
# RF environments (in case of thread), mdns trouble or just flaky devices.
# retrying both the mdns resolve and (first) interview, increases the chances
# of a successful device commission.
retries = 3
while retries:
try:
await self.interview_node(node_id)
except (NodeNotResolving, NodeInterviewFailed) as err:
if retries <= 0:
raise err
retries -= 1
LOGGER.warning("Unable to interview Node %s: %s", node_id, err)
await asyncio.sleep(5)
else:
break
# make sure we start a subscription for this newly added node
await self._setup_node(node_id)
LOGGER.info("Commissioning of Node ID %s completed.", node_id)
# return full node object once we're complete
return self.get_node(node_id)
@api_command(APICommand.COMMISSION_ON_NETWORK)
async def commission_on_network(
self,
setup_pin_code: int,
filter_type: int = 0,
filter: Any = None, # pylint: disable=redefined-builtin
ip_addr: str | None = None,
) -> MatterNodeData:
"""
Do the routine for OnNetworkCommissioning, with a filter for mDNS discovery.
The filter can be an integer,
a string or None depending on the actual type of selected filter.
NOTE: For advanced usecases only, use `commission_with_code`
for regular commissioning.
Returns full NodeInfo once complete.
"""
if self.chip_controller is None:
raise RuntimeError("Device Controller not initialized.")
node_id = self._get_next_node_id()
if ip_addr is not None:
ip_addr = self.server.scope_ipv6_lla(ip_addr)
attempts = 0
# we retry commissioning a few times as we've seen devices in the wild
# that are a bit unstable.
# by retrying, we increase the chances of a successful commission
while attempts <= MAX_COMMISSION_RETRIES:
attempts += 1
result: PyChipError | None
if ip_addr is None:
# regular CommissionOnNetwork if no IP address provided
LOGGER.info(
"Starting Matter commissioning on network using Node ID %s (attempt %s/%s).",
node_id,
attempts,
MAX_COMMISSION_RETRIES,
)
result = await self._call_sdk(
self.chip_controller.CommissionOnNetwork,
nodeId=node_id,
setupPinCode=setup_pin_code,
filterType=filter_type,
filter=filter,
)
else:
LOGGER.info(
"Starting Matter commissioning using Node ID %s and IP %s (attempt %s/%s).",
node_id,
ip_addr,
attempts,
MAX_COMMISSION_RETRIES,
)
result = await self._call_sdk(
self.chip_controller.CommissionIP,
nodeid=node_id,
setupPinCode=setup_pin_code,
ipaddr=ip_addr,
)
if result and result.is_success:
break
if attempts >= MAX_COMMISSION_RETRIES:
raise NodeCommissionFailed(f"Commissioning failed for node {node_id}.")
await asyncio.sleep(5)
LOGGER.info("Matter commissioning of Node ID %s successful.", node_id)
# perform full (first) interview of the device
# we retry the interview max 3 times as it may fail in noisy
# RF environments (in case of thread), mdns trouble or just flaky devices.
# retrying both the mdns resolve and (first) interview, increases the chances
# of a successful device commission.
retries = 3
while retries:
try:
await self.interview_node(node_id)
except NodeInterviewFailed as err:
if retries <= 0:
raise err
retries -= 1
LOGGER.warning("Unable to interview Node %s: %s", node_id, err)
await asyncio.sleep(5)
else:
break
# make sure we start a subscription for this newly added node
await self._setup_node(node_id)
LOGGER.info("Commissioning of Node ID %s completed.", node_id)
# return full node object once we're complete
return self.get_node(node_id)
@api_command(APICommand.SET_WIFI_CREDENTIALS)
async def set_wifi_credentials(self, ssid: str, credentials: str) -> None:
"""Set WiFi credentials for commissioning to a (new) device."""
if self.chip_controller is None:
raise RuntimeError("Device Controller not initialized.")
await self._call_sdk(
self.chip_controller.SetWiFiCredentials,
ssid=ssid,
credentials=credentials,
)
self.wifi_credentials_set = True
@api_command(APICommand.SET_THREAD_DATASET)
async def set_thread_operational_dataset(self, dataset: str) -> None:
"""Set Thread Operational dataset in the stack."""
if self.chip_controller is None:
raise RuntimeError("Device Controller not initialized.")
await self._call_sdk(
self.chip_controller.SetThreadOperationalDataset,
threadOperationalDataset=bytes.fromhex(dataset),
)
self.thread_credentials_set = True
@api_command(APICommand.OPEN_COMMISSIONING_WINDOW)
async def open_commissioning_window(
self,
node_id: int,
timeout: int = 300,
iteration: int = 1000,
option: int = 1,
discriminator: int | None = None,
) -> CommissioningParameters:
"""Open a commissioning window to commission a device present on this controller to another.
Returns code to use as discriminator.
"""
if self.chip_controller is None:
raise RuntimeError("Device Controller not initialized.")
if (node := self._nodes.get(node_id)) is None or not node.available:
raise NodeNotReady(f"Node {node_id} is not (yet) available.")
if node_id in self._known_commissioning_params:
# node has already been put into commissioning mode,
# return previous parameters
return self._known_commissioning_params[node_id]
if discriminator is None:
discriminator = randint(0, 4095) # noqa: S311
async with self._get_node_lock(node_id):
sdk_result = await self._call_sdk(
self.chip_controller.OpenCommissioningWindow,
nodeid=node_id,
timeout=timeout,
iteration=iteration,
discriminator=discriminator,
option=option,
)
self._known_commissioning_params[node_id] = params = CommissioningParameters(
setup_pin_code=sdk_result.setupPinCode,
setup_manual_code=sdk_result.setupManualCode,
setup_qr_code=sdk_result.setupQRCode,
)
# we store the commission parameters and clear them after the timeout
if TYPE_CHECKING:
assert self.server.loop
self.server.loop.call_later(
timeout, self._known_commissioning_params.pop, node_id, None
)
return params
@api_command(APICommand.DISCOVER)
async def discover_commissionable_nodes(
self,
) -> list[CommissionableNodeData]:
"""Discover Commissionable Nodes (discovered on BLE or mDNS)."""
if self.chip_controller is None:
raise RuntimeError("Device Controller not initialized.")
sdk_result = await self._call_sdk(
self.chip_controller.DiscoverCommissionableNodes,
)
if sdk_result is None:
return []
# ensure list
if not isinstance(sdk_result, list):
sdk_result = [sdk_result]
return [
CommissionableNodeData(
instance_name=x.instanceName,
host_name=x.hostName,
port=x.port,
long_discriminator=x.longDiscriminator,
vendor_id=x.vendorId,
product_id=x.productId,
commissioning_mode=x.commissioningMode,
device_type=x.deviceType,
device_name=x.deviceName,
pairing_instruction=x.pairingInstruction,
pairing_hint=x.pairingHint,
mrp_retry_interval_idle=x.mrpRetryIntervalIdle,
mrp_retry_interval_active=x.mrpRetryIntervalActive,
supports_tcp=x.supportsTcp,
addresses=x.addresses,
rotating_id=x.rotatingId,
)
for x in sdk_result
]
@api_command(APICommand.INTERVIEW_NODE)
async def interview_node(self, node_id: int) -> None:
"""Interview a node."""
if self.chip_controller is None:
raise RuntimeError("Device Controller not initialized.")
try:
LOGGER.info("Interviewing node: %s", node_id)
async with self._get_node_lock(node_id):
read_response: Attribute.AsyncReadTransaction.ReadResponse = (
await self.chip_controller.Read(
nodeid=node_id,
attributes="*",
fabricFiltered=False,
)
)
except ChipStackError as err:
raise NodeInterviewFailed(f"Failed to interview node {node_id}") from err
is_new_node = node_id not in self._nodes
existing_info = self._nodes.get(node_id)
node = MatterNodeData(
node_id=node_id,
date_commissioned=(
existing_info.date_commissioned if existing_info else datetime.utcnow()
),
last_interview=datetime.utcnow(),
interview_version=DATA_MODEL_SCHEMA_VERSION,
available=existing_info.available if existing_info else False,
attributes=parse_attributes_from_read_result(read_response.tlvAttributes),
)
if existing_info:
node.attribute_subscriptions = existing_info.attribute_subscriptions
# work out if the node is a bridge device by looking at the devicetype of endpoint 1
if attr_data := node.attributes.get("1/29/0"):
node.is_bridge = any(x[0] == 14 for x in attr_data)
# save updated node data
self._nodes[node_id] = node
self._write_node_state(node_id, True)
if is_new_node:
# new node - first interview
self.server.signal_event(EventType.NODE_ADDED, node)
else:
# existing node, signal node updated event
# TODO: maybe only signal this event if attributes actually changed ?
self.server.signal_event(EventType.NODE_UPDATED, node)
LOGGER.debug("Interview of node %s completed", node_id)
@api_command(APICommand.DEVICE_COMMAND)
async def send_device_command(
self,
node_id: int,
endpoint_id: int,
cluster_id: int,
command_name: str,
payload: dict,
response_type: Any | None = None,
timed_request_timeout_ms: int | None = None,
interaction_timeout_ms: int | None = None,
) -> Any:
"""Send a command to a Matter node/device."""
if self.chip_controller is None:
raise RuntimeError("Device Controller not initialized.")
if (node := self._nodes.get(node_id)) is None or not node.available:
raise NodeNotReady(f"Node {node_id} is not (yet) available.")
cluster_cls: Cluster = ALL_CLUSTERS[cluster_id]
command_cls = getattr(cluster_cls.Commands, command_name)
command = dataclass_from_dict(command_cls, payload, allow_sdk_types=True)
async with self._get_node_lock(node_id):
return await self.chip_controller.SendCommand(
nodeid=node_id,
endpoint=endpoint_id,
payload=command,
responseType=response_type,
timedRequestTimeoutMs=timed_request_timeout_ms,
interactionTimeoutMs=interaction_timeout_ms,
)
@api_command(APICommand.READ_ATTRIBUTE)
async def read_attribute(
self, node_id: int, attribute_path: str, fabric_filtered: bool = False
) -> dict[str, Any]:
"""Read one or more attribute(s) on a node by specifying an attributepath."""
if self.chip_controller is None:
raise RuntimeError("Device Controller not initialized.")
if (node := self._nodes.get(node_id)) is None or not node.available:
raise NodeNotReady(f"Node {node_id} is not (yet) available.")
endpoint_id, cluster_id, attribute_id = parse_attribute_path(attribute_path)
# Read a list of attributes and/or events from a target node.
# This is basically a re-implementation of the chip controller's Read function
# but one that allows us to send/request custom attributes.
if TYPE_CHECKING:
assert self.server.loop
assert self.chip_controller
future = self.server.loop.create_future()
device = await self._resolve_node(node_id)
async with self._get_node_lock(node_id):
Attribute.Read(
future=future,
eventLoop=self.server.loop,
device=device.deviceProxy,
devCtrl=self.chip_controller,
attributes=[
Attribute.AttributePath(
EndpointId=endpoint_id,
ClusterId=cluster_id,
AttributeId=attribute_id,
)
],
fabricFiltered=fabric_filtered,
).raise_on_error()
result: Attribute.AsyncReadTransaction.ReadResponse = await future
read_atributes = parse_attributes_from_read_result(result.tlvAttributes)
# update cached info in node attributes
self._nodes[node_id].attributes.update(read_atributes)
self._write_node_state(node_id)
return read_atributes
@api_command(APICommand.WRITE_ATTRIBUTE)
async def write_attribute(
self,
node_id: int,
attribute_path: str,
value: Any,
) -> Any:
"""Write an attribute(value) on a target node."""
if self.chip_controller is None:
raise RuntimeError("Device Controller not initialized.")
if (node := self._nodes.get(node_id)) is None or not node.available:
raise NodeNotReady(f"Node {node_id} is not (yet) available.")
endpoint_id, cluster_id, attribute_id = parse_attribute_path(attribute_path)
attribute = cast(
Clusters.ClusterAttributeDescriptor,
ALL_ATTRIBUTES[cluster_id][attribute_id](),
)
attribute.value = parse_value(
name=attribute_path,
value=value,
value_type=attribute.attribute_type.Type,
allow_sdk_types=True,
)
async with self._get_node_lock(node_id):
return await self.chip_controller.WriteAttribute(
nodeid=node_id,
attributes=[(endpoint_id, attribute)],
)
@api_command(APICommand.REMOVE_NODE)
async def remove_node(self, node_id: int) -> None:
"""Remove a Matter node/device from the fabric."""
if self.chip_controller is None:
raise RuntimeError("Device Controller not initialized.")
if node_id not in self._nodes:
raise NodeNotExists(
f"Node {node_id} does not exist or has not been interviewed."
)
LOGGER.info("Removing Node ID %s.", node_id)
# shutdown any existing subscriptions
if sub := self._subscriptions.pop(node_id, None):
await self._call_sdk(sub.Shutdown)
node = self._nodes.pop(node_id)
self.server.storage.remove(
DATA_KEY_NODES,
subkey=str(node_id),
)
LOGGER.info("Node ID %s successfully removed from Matter server.", node_id)
self.server.signal_event(EventType.NODE_REMOVED, node_id)
assert node is not None
attribute_path = create_attribute_path_from_attribute(
0,
Clusters.OperationalCredentials.Attributes.CurrentFabricIndex,
)
fabric_index = node.attributes.get(attribute_path)
if fabric_index is None:
return
result: Clusters.OperationalCredentials.Commands.NOCResponse | None = None
try:
result = await self.chip_controller.SendCommand(
nodeid=node_id,
endpoint=0,
payload=Clusters.OperationalCredentials.Commands.RemoveFabric(
fabricIndex=fabric_index,
),
)
except ChipStackError as err:
LOGGER.warning(
"Removing current fabric from device failed: %s",
str(err) or err.__class__.__name__,
# only log stacktrace if we have debug logging enabled
exc_info=err if LOGGER.isEnabledFor(logging.DEBUG) else None,
)
return
if (
result is None
or result.statusCode
== Clusters.OperationalCredentials.Enums.NodeOperationalCertStatusEnum.kOk
):
LOGGER.info("Successfully removed Home Assistant fabric from device.")
else:
LOGGER.warning(
"Removing current fabric from device failed with status code %d.",
result.statusCode,
)
@api_command(APICommand.SUBSCRIBE_ATTRIBUTE)
async def subscribe_attribute(
self, node_id: int, attribute_path: str | list[str]
) -> None:
"""
Subscribe to given AttributePath(s).
Either supply a single attribute path or a list of paths.
The given attribute path(s) will be added to the list of attributes that
are watched for the given node. This is persistent over restarts.
"""
LOGGER.debug(
"The subscribe_attribute command has been deprecated and will be removed from"
" a future version. You no longer need to call this to subscribe to attribute changes."
)
@api_command(APICommand.PING_NODE)
async def ping_node(self, node_id: int, attempts: int = 1) -> NodePingResult:
"""Ping node on the currently known IP-adress(es)."""
result: NodePingResult = {}
node = self._nodes.get(node_id)
if node is None:
raise NodeNotExists(
f"Node {node_id} does not exist or is not yet interviewed"
)
node_logger = LOGGER.getChild(f"node_{node_id}")
battery_powered = (
node.attributes.get(ROUTING_ROLE_ATTRIBUTE_PATH, 0)
== Clusters.ThreadNetworkDiagnostics.Enums.RoutingRoleEnum.kSleepyEndDevice
)
async def _do_ping(ip_address: str) -> None:
"""Ping IP and add to result."""
timeout = (
NODE_PING_TIMEOUT_BATTERY_POWERED
if battery_powered
else NODE_PING_TIMEOUT
)
if "%" in ip_address:
# ip address contains an interface index
clean_ip, interface_idx = ip_address.split("%", 1)
node_logger.debug(
"Pinging address %s (using interface %s)", clean_ip, interface_idx
)
else:
clean_ip = ip_address
node_logger.debug("Pinging address %s", clean_ip)
result[clean_ip] = await ping_ip(ip_address, timeout, attempts=attempts)
ip_addresses = await self.get_node_ip_addresses(
node_id, prefer_cache=False, scoped=True
)
tasks = [_do_ping(x) for x in ip_addresses]
# TODO: replace this gather with a taskgroup once we bump our py version
await asyncio.gather(*tasks)
# retrieve the currently connected/used address which is used
# by the sdk for communicating with the device
if TYPE_CHECKING:
assert self.chip_controller is not None
if sdk_result := await self._call_sdk(
self.chip_controller.GetAddressAndPort, nodeid=node_id
):
active_address = sdk_result[0]
node_logger.info(
"The SDK is communicating with the device using %s", active_address
)
if active_address not in result and node.available:
# if the sdk is connected to a node, treat the address as pingable
result[active_address] = True
return result
@api_command(APICommand.GET_NODE_IP_ADRESSES)
async def get_node_ip_addresses(
self,
node_id: int,
prefer_cache: bool = False,
scoped: bool = False,
) -> list[str]:
"""Return the currently known (scoped) IP-adress(es)."""
cached_info = self._last_known_ip_addresses.get(node_id, [])
if prefer_cache and cached_info:
return cached_info if scoped else [x.split("%")[0] for x in cached_info]
node = self._nodes.get(node_id)
if node is None:
raise NodeNotExists(
f"Node {node_id} does not exist or is not yet interviewed"
)
node_logger = LOGGER.getChild(f"node_{node_id}")
# query mdns for all IP's
# ensure both fabric id and node id have 16 characters (prefix with zero's)
mdns_name = f"{self.compressed_fabric_id:0{16}X}-{node_id:0{16}X}.{MDNS_TYPE_OPERATIONAL_NODE}"
info = AsyncServiceInfo(MDNS_TYPE_OPERATIONAL_NODE, mdns_name)
if TYPE_CHECKING:
assert self._aiozc is not None
if not await info.async_request(self._aiozc.zeroconf, 3000):
node_logger.info(
"Node could not be discovered on the network, returning cached IP's"
)
return cached_info
ip_adresses = info.parsed_scoped_addresses(IPVersion.All)
# cache this info for later use
self._last_known_ip_addresses[node_id] = ip_adresses
return ip_adresses if scoped else [x.split("%")[0] for x in ip_adresses]
async def _subscribe_node(self, node_id: int) -> None:
"""
Subscribe to all node state changes/events for an individual node.
Note that by using the listen command at server level,
you will receive all (subscribed) node events and attribute updates.
"""
# pylint: disable=too-many-locals,too-many-statements
if self.chip_controller is None:
raise RuntimeError("Device Controller not initialized.")
if self._nodes.get(node_id) is None:
raise NodeNotExists(
f"Node {node_id} does not exist or has not been interviewed."
)
node_logger = LOGGER.getChild(f"node_{node_id}")
node = self._nodes[node_id]
# check if we already have setup subscriptions for this node,
# if so, we need to unsubscribe
if prev_sub := self._subscriptions.get(node_id, None):
node_logger.info("Unsubscribing from existing subscription.")
await self._call_sdk(prev_sub.Shutdown)
del self._subscriptions[node_id]
loop = cast(asyncio.AbstractEventLoop, self.server.loop)
# set-up the actual subscription
def attribute_updated_callback(
path: Attribute.TypedAttributePath,
transaction: Attribute.SubscriptionTransaction,
) -> None:
self._node_last_seen[node_id] = time.time()
new_value = transaction.GetAttribute(path)
# failsafe: ignore ValueDecodeErrors
# these are set by the SDK if parsing the value failed miserably
if isinstance(new_value, ValueDecodeFailure):
return
attr_path = str(path.Path)
old_value = node.attributes.get(attr_path)
# return early if the value did not actually change at all
if old_value == new_value:
return
node_logger.debug(
"Attribute updated: %s - old value: %s - new value: %s",
path,
old_value,
new_value,
)
# work out added/removed endpoints on bridges
if (
node.is_bridge
and path.Path.EndpointId == 0
and path.AttributeType == Clusters.Descriptor.Attributes.PartsList
):
endpoints_removed = set(old_value or []) - set(new_value)
endpoints_added = set(new_value) - set(old_value or [])
if endpoints_removed:
loop.call_soon_threadsafe(
self._handle_endpoints_removed, node_id, endpoints_removed
)
if endpoints_added:
loop.create_task(
self._handle_endpoints_added(node_id, endpoints_added)
)
return
# work out if software version changed
if (
path.AttributeType == Clusters.BasicInformation.softwareVersion
and new_value != old_value
):
# schedule a full interview of the node if the software version changed
loop.create_task(self.interview_node(node_id))
# store updated value in node attributes
node.attributes[attr_path] = new_value
# schedule save to persistent storage
loop.call_soon_threadsafe(self._write_node_state, node_id)
# This callback is running in the CHIP stack thread
loop.call_soon_threadsafe(
self.server.signal_event,
EventType.ATTRIBUTE_UPDATED,
# send data as tuple[node_id, attribute_path, new_value]
(node_id, attr_path, new_value),
)
def event_callback(
data: Attribute.EventReadResult,
transaction: Attribute.SubscriptionTransaction,
) -> None:
# pylint: disable=unused-argument
assert loop is not None
node_logger.debug(
"Received node event: %s - transaction: %s", data, transaction
)
self._node_last_seen[node_id] = time.time()
node_event = MatterNodeEvent(
node_id=node_id,
endpoint_id=data.Header.EndpointId,
cluster_id=data.Header.ClusterId,
event_id=data.Header.EventId,
event_number=data.Header.EventNumber,
priority=data.Header.Priority,
timestamp=data.Header.Timestamp,
timestamp_type=data.Header.TimestampType,
data=data.Data,
)
self.event_history.append(node_event)
loop.call_soon_threadsafe(
self.server.signal_event, EventType.NODE_EVENT, node_event
)
def error_callback(
chipError: int, transaction: Attribute.SubscriptionTransaction
) -> None:
# pylint: disable=unused-argument, invalid-name
node_logger.error("Got error from node: %s", chipError)
def resubscription_attempted(
transaction: Attribute.SubscriptionTransaction,
terminationError: int,
nextResubscribeIntervalMsec: int,
) -> None:
# pylint: disable=unused-argument, invalid-name
node_logger.info(
"Previous subscription failed with Error: %s, re-subscribing in %s ms...",
terminationError,
nextResubscribeIntervalMsec,
)
resubscription_attempt = self._last_subscription_attempt[node_id] + 1
self._last_subscription_attempt[node_id] = resubscription_attempt
# Mark node as unavailable and signal consumers.
# We debounce it a bit so we only mark the node unavailable
# after some resubscription attempts and we shutdown the subscription
# if the resubscription interval exceeds 30 minutes (TTL of mdns).
# The node will be auto picked up by mdns if it's alive again.
if (
node.available
and resubscription_attempt >= NODE_RESUBSCRIBE_ATTEMPTS_UNAVAILABLE
):
node.available = False
self.server.signal_event(EventType.NODE_UPDATED, node)
LOGGER.info("Marked node %s as unavailable", node_id)
if (
not node.available
and nextResubscribeIntervalMsec > NODE_RESUBSCRIBE_TIMEOUT_OFFLINE
):
asyncio.create_task(self._node_offline(node_id))
def resubscription_succeeded(
transaction: Attribute.SubscriptionTransaction,
) -> None:
# pylint: disable=unused-argument, invalid-name
self._node_last_seen[node_id] = time.time()
node_logger.info("Re-Subscription succeeded")
self._last_subscription_attempt[node_id] = 0
# mark node as available and signal consumers
if not node.available:
node.available = True
self.server.signal_event(EventType.NODE_UPDATED, node)
node_logger.info("Setting up attributes and events subscription.")
interval_floor = 0
# determine subscription ceiling based on routing role
# Endpoint 0, ThreadNetworkDiagnostics Cluster, routingRole attribute
# for WiFi devices, this cluster doesn't exist.
routing_role = node.attributes.get(ROUTING_ROLE_ATTRIBUTE_PATH)
if routing_role is None:
interval_ceiling = NODE_SUBSCRIPTION_CEILING_WIFI
elif (
routing_role
== Clusters.ThreadNetworkDiagnostics.Enums.RoutingRoleEnum.kSleepyEndDevice