|
62 | 62 | from collections.abc import Iterable
|
63 | 63 | from pathlib import Path
|
64 | 64 |
|
65 |
| - from chip.native import PyChipError |
66 |
| - |
67 | 65 | from .server import MatterServer
|
68 | 66 |
|
69 | 67 | DATA_KEY_NODES = "nodes"
|
|
73 | 71 | NODE_SUBSCRIPTION_CEILING_WIFI = 60
|
74 | 72 | NODE_SUBSCRIPTION_CEILING_THREAD = 60
|
75 | 73 | NODE_SUBSCRIPTION_CEILING_BATTERY_POWERED = 600
|
76 |
| -MAX_COMMISSION_RETRIES = 3 |
77 | 74 | NODE_RESUBSCRIBE_ATTEMPTS_UNAVAILABLE = 3
|
78 | 75 | NODE_RESUBSCRIBE_TIMEOUT_OFFLINE = 30 * 60 * 1000
|
79 | 76 | NODE_PING_TIMEOUT = 10
|
@@ -262,34 +259,29 @@ async def commission_with_code(
|
262 | 259 | """
|
263 | 260 | node_id = self._get_next_node_id()
|
264 | 261 |
|
265 |
| - attempts = 0 |
266 |
| - # we retry commissioning a few times as we've seen devices in the wild |
267 |
| - # that are a bit unstable. |
268 |
| - # by retrying, we increase the chances of a successful commission |
269 |
| - while attempts <= MAX_COMMISSION_RETRIES: |
270 |
| - attempts += 1 |
271 |
| - LOGGER.info( |
272 |
| - "Starting Matter commissioning with code using Node ID %s (attempt %s/%s).", |
273 |
| - node_id, |
274 |
| - attempts, |
275 |
| - MAX_COMMISSION_RETRIES, |
276 |
| - ) |
277 |
| - result: ( |
278 |
| - PyChipError | None |
279 |
| - ) = await self._chip_device_controller.commission_with_code( |
280 |
| - node_id, |
281 |
| - code, |
282 |
| - DiscoveryType.DISCOVERY_NETWORK_ONLY |
283 |
| - if network_only |
284 |
| - else DiscoveryType.DISCOVERY_ALL, |
285 |
| - ) |
286 |
| - if result and result.is_success: |
287 |
| - break |
288 |
| - if attempts >= MAX_COMMISSION_RETRIES: |
289 |
| - raise NodeCommissionFailed( |
290 |
| - f"Commission with code failed for node {node_id}." |
| 262 | + LOGGER.info( |
| 263 | + "Starting Matter commissioning with code using Node ID %s.", |
| 264 | + node_id, |
| 265 | + ) |
| 266 | + try: |
| 267 | + commissioned_node_id: int = ( |
| 268 | + await self._chip_device_controller.commission_with_code( |
| 269 | + node_id, |
| 270 | + code, |
| 271 | + DiscoveryType.DISCOVERY_NETWORK_ONLY |
| 272 | + if network_only |
| 273 | + else DiscoveryType.DISCOVERY_ALL, |
291 | 274 | )
|
292 |
| - await asyncio.sleep(5) |
| 275 | + ) |
| 276 | + # We use SDK default behavior which always uses the commissioning Node ID in the |
| 277 | + # generated NOC. So this should be the same really. |
| 278 | + LOGGER.info("Commissioned Node ID: %s vs %s", commissioned_node_id, node_id) |
| 279 | + if commissioned_node_id != node_id: |
| 280 | + raise RuntimeError("Returned Node ID must match requested Node ID") |
| 281 | + except ChipStackError as err: |
| 282 | + raise NodeCommissionFailed( |
| 283 | + f"Commission with code failed for node {node_id}." |
| 284 | + ) from err |
293 | 285 |
|
294 | 286 | LOGGER.info("Matter commissioning of Node ID %s successful.", node_id)
|
295 | 287 |
|
@@ -340,40 +332,35 @@ async def commission_on_network(
|
340 | 332 | if ip_addr is not None:
|
341 | 333 | ip_addr = self.server.scope_ipv6_lla(ip_addr)
|
342 | 334 |
|
343 |
| - attempts = 0 |
344 |
| - # we retry commissioning a few times as we've seen devices in the wild |
345 |
| - # that are a bit unstable. |
346 |
| - # by retrying, we increase the chances of a successful commission |
347 |
| - while attempts <= MAX_COMMISSION_RETRIES: |
348 |
| - attempts += 1 |
349 |
| - result: PyChipError | None |
| 335 | + try: |
350 | 336 | if ip_addr is None:
|
351 | 337 | # regular CommissionOnNetwork if no IP address provided
|
352 | 338 | LOGGER.info(
|
353 |
| - "Starting Matter commissioning on network using Node ID %s (attempt %s/%s).", |
| 339 | + "Starting Matter commissioning on network using Node ID %s.", |
354 | 340 | node_id,
|
355 |
| - attempts, |
356 |
| - MAX_COMMISSION_RETRIES, |
357 | 341 | )
|
358 |
| - result = await self._chip_device_controller.commission_on_network( |
359 |
| - node_id, setup_pin_code, filter_type, filter |
| 342 | + commissioned_node_id = ( |
| 343 | + await self._chip_device_controller.commission_on_network( |
| 344 | + node_id, setup_pin_code, filter_type, filter |
| 345 | + ) |
360 | 346 | )
|
361 | 347 | else:
|
362 | 348 | LOGGER.info(
|
363 |
| - "Starting Matter commissioning using Node ID %s and IP %s (attempt %s/%s).", |
| 349 | + "Starting Matter commissioning using Node ID %s and IP %s.", |
364 | 350 | node_id,
|
365 | 351 | ip_addr,
|
366 |
| - attempts, |
367 |
| - MAX_COMMISSION_RETRIES, |
368 | 352 | )
|
369 |
| - result = await self._chip_device_controller.commission_ip( |
| 353 | + commissioned_node_id = await self._chip_device_controller.commission_ip( |
370 | 354 | node_id, setup_pin_code, ip_addr
|
371 | 355 | )
|
372 |
| - if result and result.is_success: |
373 |
| - break |
374 |
| - if attempts >= MAX_COMMISSION_RETRIES: |
375 |
| - raise NodeCommissionFailed(f"Commissioning failed for node {node_id}.") |
376 |
| - await asyncio.sleep(5) |
| 356 | + # We use SDK default behavior which always uses the commissioning Node ID in the |
| 357 | + # generated NOC. So this should be the same really. |
| 358 | + if commissioned_node_id != node_id: |
| 359 | + raise RuntimeError("Returned Node ID must match requested Node ID") |
| 360 | + except ChipStackError as err: |
| 361 | + raise NodeCommissionFailed( |
| 362 | + f"Commissioning failed for node {node_id}." |
| 363 | + ) from err |
377 | 364 |
|
378 | 365 | LOGGER.info("Matter commissioning of Node ID %s successful.", node_id)
|
379 | 366 |
|
|
0 commit comments