3
3
4
4
import asyncio
5
5
import logging
6
- import uuid
7
- from collections .abc import Callable
8
6
from types import TracebackType
9
- from typing import TYPE_CHECKING , Any , Final , cast
7
+ from typing import TYPE_CHECKING , Any , Callable , Dict , Final , Optional , cast
8
+ import uuid
10
9
11
10
from aiohttp import ClientSession
12
11
13
12
from matter_server .common .errors import ERROR_MAP
14
- from matter_server .common .helpers .util import dataclass_from_dict , dataclass_to_dict
15
- from matter_server .common .models import (
13
+
14
+ from ..common .helpers .util import dataclass_from_dict , dataclass_to_dict
15
+ from ..common .models import (
16
16
APICommand ,
17
17
CommandMessage ,
18
18
ErrorResultMessage ,
25
25
ServerInfoMessage ,
26
26
SuccessResultMessage ,
27
27
)
28
-
29
28
from .connection import MatterClientConnection
30
29
from .exceptions import ConnectionClosed , InvalidServerVersion , InvalidState
31
30
from .models .node import MatterNode
@@ -43,8 +42,8 @@ def __init__(self, ws_server_url: str, aiohttp_session: ClientSession):
43
42
"""Initialize the Client class."""
44
43
self .connection = MatterClientConnection (ws_server_url , aiohttp_session )
45
44
self .logger = logging .getLogger (__package__ )
46
- self ._nodes : dict [int , MatterNode ] = {}
47
- self ._result_futures : dict [str , asyncio .Future ] = {}
45
+ self ._nodes : Dict [int , MatterNode ] = {}
46
+ self ._result_futures : Dict [str , asyncio .Future ] = {}
48
47
self ._subscribers : dict [str , list [Callable [[EventType , Any ], None ]]] = {}
49
48
self ._stop_called : bool = False
50
49
self ._loop : asyncio .AbstractEventLoop | None = None
@@ -57,9 +56,9 @@ def server_info(self) -> ServerInfoMessage | None:
57
56
def subscribe (
58
57
self ,
59
58
callback : Callable [[EventType , Any ], None ],
60
- event_filter : EventType | None = None ,
61
- node_filter : int | None = None ,
62
- attr_path_filter : str | None = None ,
59
+ event_filter : Optional [ EventType ] = None ,
60
+ node_filter : Optional [ int ] = None ,
61
+ attr_path_filter : Optional [ str ] = None ,
63
62
) -> Callable [[], None ]:
64
63
"""
65
64
Subscribe to node and server events.
@@ -71,10 +70,16 @@ def subscribe(
71
70
# for fast lookups we create a key based on the filters, allowing
72
71
# a "catch all" with a wildcard (*).
73
72
_event_filter : str
74
- _event_filter = SUB_WILDCARD if event_filter is None else event_filter .value
73
+ if event_filter is None :
74
+ _event_filter = SUB_WILDCARD
75
+ else :
76
+ _event_filter = event_filter .value
75
77
76
78
_node_filter : str
77
- _node_filter = SUB_WILDCARD if node_filter is None else str (node_filter )
79
+ if node_filter is None :
80
+ _node_filter = SUB_WILDCARD
81
+ else :
82
+ _node_filter = str (node_filter )
78
83
79
84
if attr_path_filter is None :
80
85
attr_path_filter = SUB_WILDCARD
@@ -118,7 +123,9 @@ async def commission_on_network(self, setup_pin_code: int) -> MatterNodeData:
118
123
119
124
async def set_wifi_credentials (self , ssid : str , credentials : str ) -> None :
120
125
"""Set WiFi credentials for commissioning to a (new) device."""
121
- await self .send_command (APICommand .SET_WIFI_CREDENTIALS , ssid = ssid , credentials = credentials )
126
+ await self .send_command (
127
+ APICommand .SET_WIFI_CREDENTIALS , ssid = ssid , credentials = credentials
128
+ )
122
129
123
130
async def set_thread_operational_dataset (self , dataset : str ) -> None :
124
131
"""Set Thread Operational dataset in the stack."""
@@ -130,7 +137,7 @@ async def open_commissioning_window(
130
137
timeout : int = 300 ,
131
138
iteration : int = 1000 ,
132
139
option : int = 0 ,
133
- discriminator : int | None = None ,
140
+ discriminator : Optional [ int ] = None ,
134
141
) -> int :
135
142
"""
136
143
Open a commissioning window to commission a device present on this controller to another.
@@ -223,7 +230,10 @@ async def send_command_no_wait(
223
230
if not self .server_info :
224
231
raise InvalidState ("Not connected" )
225
232
226
- if require_schema is not None and require_schema > self .server_info .schema_version :
233
+ if (
234
+ require_schema is not None
235
+ and require_schema > self .server_info .schema_version
236
+ ):
227
237
raise InvalidServerVersion (
228
238
"Command not available due to incompatible server version. Update the Matter "
229
239
f"Server to a version that supports at least api schema { require_schema } ."
@@ -258,10 +268,15 @@ async def start_listening(self, init_ready: asyncio.Event | None = None) -> None
258
268
message_id = uuid .uuid4 ().hex , command = APICommand .START_LISTENING
259
269
)
260
270
await self .connection .send_message (message )
261
- nodes_msg = cast (SuccessResultMessage , await self .connection .receive_message_or_raise ())
271
+ nodes_msg = cast (
272
+ SuccessResultMessage , await self .connection .receive_message_or_raise ()
273
+ )
262
274
# a full dump of all nodes will be the result of the start_listening command
263
275
# create MatterNode objects from the basic MatterNodeData objects
264
- nodes = [MatterNode (dataclass_from_dict (MatterNodeData , x )) for x in nodes_msg .result ]
276
+ nodes = [
277
+ MatterNode (dataclass_from_dict (MatterNodeData , x ))
278
+ for x in nodes_msg .result
279
+ ]
265
280
self ._nodes = {node .node_id : node for node in nodes }
266
281
# once we've hit this point we're all set
267
282
self .logger .info ("Matter client initialized." )
@@ -358,8 +373,8 @@ def _signal_event(
358
373
self ,
359
374
event : EventType ,
360
375
data : Any = None ,
361
- node_id : int | None = None ,
362
- attribute_path : str | None = None ,
376
+ node_id : Optional [ int ] = None ,
377
+ attribute_path : Optional [ str ] = None ,
363
378
) -> None :
364
379
"""Signal event to all subscribers."""
365
380
# instead of iterating all subscribers we iterate over subscription keys
@@ -375,7 +390,7 @@ def _signal_event(
375
390
for callback in self ._subscribers .get (key , []):
376
391
callback (event , data )
377
392
378
- async def __aenter__ (self ) -> MatterClient :
393
+ async def __aenter__ (self ) -> " MatterClient" :
379
394
"""Initialize and connect the Matter Websocket client."""
380
395
await self .connect ()
381
396
return self
0 commit comments