zigpy-deconz 1.1.0__tar.gz → 1.1.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. {zigpy_deconz-1.1.0/zigpy_deconz.egg-info → zigpy_deconz-1.1.2}/PKG-INFO +1 -1
  2. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/tests/test_application.py +139 -2
  3. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/tests/test_send_receive.py +11 -15
  4. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/zigpy_deconz/zigbee/application.py +90 -36
  5. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2/zigpy_deconz.egg-info}/PKG-INFO +1 -1
  6. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/COPYING +0 -0
  7. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/LICENSE +0 -0
  8. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/README.md +0 -0
  9. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/pyproject.toml +0 -0
  10. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/setup.cfg +0 -0
  11. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/setup.py +0 -0
  12. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/tests/test_api.py +0 -0
  13. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/tests/test_exception.py +0 -0
  14. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/tests/test_network_state.py +0 -0
  15. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/tests/test_types.py +0 -0
  16. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/tests/test_uart.py +0 -0
  17. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/tests/test_utils.py +0 -0
  18. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/zigpy_deconz/__init__.py +0 -0
  19. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/zigpy_deconz/api.py +0 -0
  20. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/zigpy_deconz/config.py +0 -0
  21. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/zigpy_deconz/exception.py +0 -0
  22. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/zigpy_deconz/types.py +0 -0
  23. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/zigpy_deconz/uart.py +0 -0
  24. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/zigpy_deconz/utils.py +0 -0
  25. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/zigpy_deconz/zigbee/__init__.py +0 -0
  26. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/zigpy_deconz.egg-info/SOURCES.txt +0 -0
  27. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/zigpy_deconz.egg-info/dependency_links.txt +0 -0
  28. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/zigpy_deconz.egg-info/requires.txt +0 -0
  29. {zigpy_deconz-1.1.0 → zigpy_deconz-1.1.2}/zigpy_deconz.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: zigpy-deconz
3
- Version: 1.1.0
3
+ Version: 1.1.2
4
4
  Summary: A library which communicates with Deconz radios for zigpy
5
5
  Author-email: Daniel Schmidt <schmidt.d@aon.at>
6
6
  License: GPL-3.0
@@ -8,6 +8,7 @@ import pytest
8
8
  import zigpy.application
9
9
  import zigpy.config
10
10
  import zigpy.device
11
+ import zigpy.profiles.zll
11
12
  from zigpy.types import EUI64, Channels, KeyData
12
13
  import zigpy.zdo.types as zdo_t
13
14
 
@@ -514,8 +515,6 @@ async def test_add_endpoint_no_free_space(app):
514
515
 
515
516
  app._api.read_parameter = AsyncMock(side_effect=read_param)
516
517
  app._api.write_parameter = AsyncMock()
517
- app._written_endpoints.add(0x00)
518
- app._written_endpoints.add(0x01)
519
518
 
520
519
  with pytest.raises(ValueError):
521
520
  await app.add_endpoint(ENDPOINT.replace(endpoint=1))
@@ -552,6 +551,144 @@ async def test_add_endpoint_no_unnecessary_writes(app):
552
551
  )
553
552
 
554
553
 
554
+ def mock_endpoint_slots(app, slots):
555
+ """Mock the firmware's endpoint slots, keeping track of writes."""
556
+
557
+ async def read_param(param_id, index):
558
+ assert param_id == deconz_api.NetworkParameter.configure_endpoint
559
+
560
+ if index not in slots:
561
+ raise zigpy_deconz.exception.CommandError(
562
+ "Unsupported",
563
+ status=deconz_api.Status.UNSUPPORTED,
564
+ command=None,
565
+ )
566
+
567
+ return deconz_api.IndexedEndpoint(index=index, descriptor=slots[index])
568
+
569
+ async def write_param(param_id, value):
570
+ assert param_id == deconz_api.NetworkParameter.configure_endpoint
571
+ assert value.index in slots
572
+ slots[value.index] = value.descriptor
573
+
574
+ app._api.read_parameter = AsyncMock(side_effect=read_param)
575
+ app._api.write_parameter = AsyncMock(side_effect=write_param)
576
+
577
+
578
+ async def zigpy_endpoints(app):
579
+ """Return the endpoints zigpy registers: HA, ZLL and Green Power."""
580
+ with patch.object(app, "add_endpoint", AsyncMock()) as add_endpoint:
581
+ await zigpy.application.ControllerApplication.register_endpoints(app)
582
+
583
+ return [call.args[0] for call in add_endpoint.await_args_list]
584
+
585
+
586
+ @pytest.mark.parametrize(
587
+ "initial, expected, num_writes",
588
+ [
589
+ # Two slots last configured by zigpy 2.1-2.3: the ZLL endpoint makes room
590
+ (["ha", "zll"], ["ha", "gp"], 1),
591
+ # Two slots already holding the HA and GP endpoints are left alone
592
+ (["ha", "gp"], ["ha", "gp"], 0),
593
+ # Two slots configured by something else entirely
594
+ (["other1", "other2"], ["ha", "gp"], 2),
595
+ # Firmware with more slots keeps the ZLL endpoint
596
+ (["ha", "zll", "other1", "other2"], ["ha", "zll", "gp", "other2"], 1),
597
+ (["ha", "zll", "gp"], ["ha", "zll", "gp"], 0),
598
+ ],
599
+ )
600
+ async def test_register_endpoints(app, initial, expected, num_writes):
601
+ ha, zll, gp = await zigpy_endpoints(app)
602
+ named = {
603
+ "ha": ha,
604
+ "zll": zll,
605
+ "gp": gp,
606
+ "other1": ENDPOINT.replace(endpoint=10),
607
+ "other2": ENDPOINT.replace(endpoint=11),
608
+ }
609
+
610
+ slots = {index: named[name] for index, name in enumerate(initial)}
611
+ mock_endpoint_slots(app, slots)
612
+
613
+ await app.register_endpoints()
614
+
615
+ assert slots == {index: named[name] for index, name in enumerate(expected)}
616
+ assert app._api.write_parameter.await_count == num_writes
617
+
618
+ # Registering again, e.g. when `start_network` is retried, writes nothing
619
+ app._api.write_parameter.reset_mock()
620
+ await app.register_endpoints()
621
+ assert app._api.write_parameter.await_count == 0
622
+
623
+
624
+ async def test_register_endpoints_too_many(app):
625
+ ha, zll, gp = await zigpy_endpoints(app)
626
+ slots = {0: ha, 1: zll}
627
+ mock_endpoint_slots(app, slots)
628
+
629
+ with patch.dict(
630
+ app._config,
631
+ {zigpy.config.CONF_ADDITIONAL_ENDPOINTS: [ENDPOINT.replace(endpoint=10)]},
632
+ ):
633
+ with pytest.raises(ValueError):
634
+ await app.register_endpoints()
635
+
636
+ assert slots == {0: ha, 1: zll}
637
+ app._api.write_parameter.assert_not_called()
638
+
639
+
640
+ async def test_register_endpoints_additional_zll(app):
641
+ """Only zigpy's own ZLL endpoint makes room, a configured one is kept."""
642
+ ha, zll, gp = await zigpy_endpoints(app)
643
+ slots = {0: ha, 1: zll}
644
+ mock_endpoint_slots(app, slots)
645
+
646
+ extra = ENDPOINT.replace(endpoint=3, profile=zigpy.profiles.zll.PROFILE_ID)
647
+
648
+ with patch.dict(app._config, {zigpy.config.CONF_ADDITIONAL_ENDPOINTS: [extra]}):
649
+ with pytest.raises(ValueError):
650
+ await app.register_endpoints()
651
+
652
+ assert slots == {0: ha, 1: zll}
653
+
654
+
655
+ async def test_register_endpoints_override(app):
656
+ """A configured endpoint replaces zigpy's endpoint with the same ID."""
657
+ ha, zll, gp = await zigpy_endpoints(app)
658
+ slots = {0: ha, 1: zll, 2: gp, 3: ENDPOINT.replace(endpoint=10)}
659
+ mock_endpoint_slots(app, slots)
660
+
661
+ override = ha.replace(device_type=0x1234)
662
+
663
+ with patch.dict(app._config, {zigpy.config.CONF_ADDITIONAL_ENDPOINTS: [override]}):
664
+ await app.register_endpoints()
665
+
666
+ assert slots == {0: override, 1: zll, 2: gp, 3: ENDPOINT.replace(endpoint=10)}
667
+
668
+
669
+ async def test_add_endpoint_after_register(app):
670
+ ha, zll, gp = await zigpy_endpoints(app)
671
+ slots = {0: ha, 1: zll, 2: gp, 3: ENDPOINT.replace(endpoint=10)}
672
+ mock_endpoint_slots(app, slots)
673
+
674
+ await app.register_endpoints()
675
+ app._api.write_parameter.assert_not_called()
676
+
677
+ # A new endpoint goes into the free slot, not one of the registered ones
678
+ await app.add_endpoint(ENDPOINT.replace(endpoint=20))
679
+ assert slots == {0: ha, 1: zll, 2: gp, 3: ENDPOINT.replace(endpoint=20)}
680
+
681
+ # Replacing an endpoint with the same ID reuses its slot
682
+ await app.add_endpoint(ENDPOINT.replace(endpoint=20, profile=1234))
683
+ assert slots == {
684
+ 0: ha,
685
+ 1: zll,
686
+ 2: gp,
687
+ 3: ENDPOINT.replace(endpoint=20, profile=1234),
688
+ }
689
+ assert app._api.write_parameter.await_count == 2
690
+
691
+
555
692
  async def test_reset_network_info(app):
556
693
  app.write_network_info = AsyncMock()
557
694
  await app.reset_network_info()
@@ -97,21 +97,6 @@ async def test_send_packet_nwk_no_ack(app, tx_packet): # noqa: F811
97
97
  assert req["radius"] == 0
98
98
 
99
99
 
100
- async def test_send_packet_nwk_aps_encryption(app, tx_packet): # noqa: F811
101
- tx_packet.tx_options |= zigpy_t.TransmitOptions.APS_Encryption
102
-
103
- with patch_data_request(app) as mock_req:
104
- await app.send_packet(tx_packet)
105
-
106
- assert len(mock_req.mock_calls) == 1
107
- req = mock_req.mock_calls[0].kwargs
108
-
109
- # The network key security option is replaced by APS encryption
110
- assert req["tx_options"] == (
111
- t.DeconzTransmitOptions.SECURITY_ENABLED | t.DeconzTransmitOptions.USE_APS_ACKS
112
- )
113
-
114
-
115
100
  async def test_send_packet_source_route(app, tx_packet): # noqa: F811
116
101
  tx_packet.source_route = [0xAABB, 0xCCDD]
117
102
 
@@ -248,3 +233,14 @@ async def test_send_packet_deliver_failure(app, tx_packet): # noqa: F811
248
233
  await app.send_packet(tx_packet)
249
234
 
250
235
  assert "Failed to deliver" in str(e)
236
+
237
+
238
+ async def test_send_packet_disconnected(app, tx_packet): # noqa: F811
239
+ """Sending after the radio was disconnected fails cleanly."""
240
+ app._api = None
241
+
242
+ with pytest.raises(zigpy.exceptions.DeliveryError) as e:
243
+ await app.send_packet(tx_packet)
244
+
245
+ assert "not connected" in str(e)
246
+ assert not app._pending_requests
@@ -19,6 +19,7 @@ import zigpy.device
19
19
  import zigpy.endpoint
20
20
  import zigpy.exceptions
21
21
  from zigpy.exceptions import FormationFailure, NetworkNotFormed
22
+ import zigpy.profiles.zll
22
23
  import zigpy.state
23
24
  import zigpy.types
24
25
  import zigpy.util
@@ -57,6 +58,9 @@ PROTO_VER_NEIGBOURS = 0x0107
57
58
 
58
59
  CONBEE_III_ENERGY_SCAN_ATTEMPTS = 5
59
60
 
61
+ # zigpy registers its ZLL endpoint here, see `ControllerApplication.register_endpoints`
62
+ ZLL_ENDPOINT_ID = 2
63
+
60
64
 
61
65
  class ControllerApplication(zigpy.application.ControllerApplication):
62
66
  SCHEMA = CONFIG_SCHEMA
@@ -79,7 +83,8 @@ class ControllerApplication(zigpy.application.ControllerApplication):
79
83
  self._delayed_neighbor_scan_task = None
80
84
  self._reconnect_task = None
81
85
 
82
- self._written_endpoints = set()
86
+ self._endpoints: list[zdo_t.SimpleDescriptor] = []
87
+ self._pending_endpoints: list[zdo_t.SimpleDescriptor] | None = None
83
88
 
84
89
  async def _watchdog_feed(self):
85
90
  if self._api.protocol_version >= PROTO_VER_WATCHDOG and not (
@@ -102,7 +107,7 @@ class ControllerApplication(zigpy.application.ControllerApplication):
102
107
  raise
103
108
 
104
109
  self._api = api
105
- self._written_endpoints.clear()
110
+ self._endpoints = []
106
111
 
107
112
  async def disconnect(self):
108
113
  if self._delayed_neighbor_scan_task is not None:
@@ -492,56 +497,105 @@ class ControllerApplication(zigpy.application.ControllerApplication):
492
497
  await asyncio.sleep(CHANGE_NETWORK_STATE_DELAY)
493
498
  await self._change_network_state(NetworkState.CONNECTED)
494
499
 
500
+ async def register_endpoints(self) -> None:
501
+ """Register all endpoints at once, so they can be fit into the available slots."""
502
+ self._pending_endpoints = []
503
+
504
+ try:
505
+ await super().register_endpoints()
506
+ descriptors = self._pending_endpoints
507
+ finally:
508
+ self._pending_endpoints = None
509
+
510
+ await self._configure_endpoints(descriptors)
511
+
495
512
  async def add_endpoint(self, descriptor: zdo_t.SimpleDescriptor) -> None:
496
513
  """Register an endpoint on the device, replacing any with conflicting IDs."""
514
+ if self._pending_endpoints is not None:
515
+ self._pending_endpoints.append(descriptor)
516
+ return
497
517
 
498
- endpoints = {}
518
+ await self._configure_endpoints([*self._endpoints, descriptor])
499
519
 
500
- # Read and count the current endpoints. Some firmwares have three, others four.
520
+ async def _read_endpoint_slots(self) -> dict[int, zdo_t.SimpleDescriptor]:
521
+ """Read the endpoint slots of the firmware, they can't be queried otherwise."""
522
+ slots = {}
523
+
524
+ # Most firmwares have two slots, a ConBee II test firmware had four
501
525
  for index in range(255 + 1):
502
526
  try:
503
- current_descriptor = await self._api.read_parameter(
527
+ current = await self._api.read_parameter(
504
528
  NetworkParameter.configure_endpoint, index
505
529
  )
506
530
  except zigpy_deconz.exception.CommandError as ex:
507
531
  assert ex.status == Status.UNSUPPORTED
508
532
  break
509
533
  else:
510
- endpoints[index] = current_descriptor.descriptor
534
+ slots[index] = current.descriptor
511
535
 
512
- LOGGER.debug("Got endpoint slots: %r", endpoints)
536
+ LOGGER.debug("Got endpoint slots: %r", slots)
513
537
 
514
- # Don't write endpoints unnecessarily
515
- if descriptor in endpoints.values():
516
- LOGGER.debug("Endpoint already registered, skipping")
538
+ return slots
517
539
 
518
- # Pretend we wrote it
519
- self._written_endpoints.add(list(endpoints.values()).index(descriptor))
520
- return
540
+ async def _configure_endpoints(
541
+ self, descriptors: list[zdo_t.SimpleDescriptor]
542
+ ) -> None:
543
+ """Fit the endpoints into the slots, writing only the ones that changed."""
544
+ slots = await self._read_endpoint_slots()
545
+
546
+ # A later endpoint replaces an earlier one with the same ID
547
+ descriptors = list({ep.endpoint: ep for ep in descriptors}.values())
548
+
549
+ # Two-slot firmware cannot host zigpy's ZLL endpoint next to the HA and Green
550
+ # Power ones. Nothing is sent from it, so drop it like deCONZ and Z2M do.
551
+ if len(descriptors) > len(slots):
552
+ fitting = [
553
+ ep
554
+ for ep in descriptors
555
+ if not (
556
+ ep.endpoint == ZLL_ENDPOINT_ID
557
+ and ep.profile == zigpy.profiles.zll.PROFILE_ID
558
+ )
559
+ ]
521
560
 
522
- # Keep track of the best endpoint descriptor to replace
523
- target_index = None
561
+ if len(fitting) < len(descriptors):
562
+ LOGGER.debug("Not enough endpoint slots, skipping the ZLL endpoint")
563
+ descriptors = fitting
524
564
 
525
- for index, current_descriptor in endpoints.items():
526
- # Ignore ones we've already written
527
- if index in self._written_endpoints:
528
- continue
565
+ if len(descriptors) > len(slots):
566
+ raise ValueError(
567
+ f"Cannot fit {len(descriptors)} endpoints into {len(slots)} endpoint"
568
+ f" slots: {descriptors!r}"
569
+ )
529
570
 
530
- target_index = index
571
+ assigned: dict[int, zdo_t.SimpleDescriptor] = {}
572
+ missing: list[zdo_t.SimpleDescriptor] = []
531
573
 
532
- if current_descriptor.endpoint == descriptor.endpoint:
533
- # Prefer to replace the endpoint with the same ID
534
- break
574
+ # Don't write endpoints unnecessarily
575
+ for descriptor in descriptors:
576
+ for index, current in slots.items():
577
+ if index not in assigned and current == descriptor:
578
+ assigned[index] = descriptor
579
+ break
580
+ else:
581
+ missing.append(descriptor)
535
582
 
536
- if target_index is None:
537
- raise ValueError(f"No available endpoint slots exist: {endpoints!r}")
583
+ free = [index for index in slots if index not in assigned]
538
584
 
539
- LOGGER.debug("Writing %s to slot %r", descriptor, target_index)
585
+ for descriptor in missing:
586
+ # Prefer to replace the endpoint with the same ID
587
+ index = next(
588
+ (i for i in free if slots[i].endpoint == descriptor.endpoint), free[0]
589
+ )
590
+ free.remove(index)
540
591
 
541
- await self._api.write_parameter(
542
- NetworkParameter.configure_endpoint,
543
- IndexedEndpoint(index=target_index, descriptor=descriptor),
544
- )
592
+ LOGGER.debug("Writing %s to slot %r", descriptor, index)
593
+ await self._api.write_parameter(
594
+ NetworkParameter.configure_endpoint,
595
+ IndexedEndpoint(index=index, descriptor=descriptor),
596
+ )
597
+
598
+ self._endpoints = list(descriptors)
545
599
 
546
600
  async def send_packet(self, packet):
547
601
  LOGGER.debug("Sending packet: %r", packet)
@@ -550,12 +604,7 @@ class ControllerApplication(zigpy.application.ControllerApplication):
550
604
  # The APS data request has no alias parameters
551
605
  raise NotImplementedError("The firmware cannot send with an alias")
552
606
 
553
- if zigpy.types.TransmitOptions.APS_Encryption in packet.tx_options:
554
- # APS encryption is performed with the link key, not the network key, so
555
- # the "use NWK key" option must not be set
556
- tx_options = t.DeconzTransmitOptions.SECURITY_ENABLED
557
- else:
558
- tx_options = t.DeconzTransmitOptions.USE_NWK_KEY_SECURITY
607
+ tx_options = t.DeconzTransmitOptions.USE_NWK_KEY_SECURITY
559
608
 
560
609
  if (
561
610
  zigpy.types.TransmitOptions.ACK in packet.tx_options
@@ -565,6 +614,11 @@ class ControllerApplication(zigpy.application.ControllerApplication):
565
614
  tx_options |= t.DeconzTransmitOptions.USE_APS_ACKS
566
615
 
567
616
  async with self._limit_concurrency(priority=packet.priority):
617
+ # The radio can be disconnected while the request waits its turn, e.g.
618
+ # when a device is replied to after startup has failed
619
+ if self._api is None:
620
+ raise zigpy.exceptions.DeliveryError("Coordinator is not connected")
621
+
568
622
  req_id = self.get_sequence()
569
623
 
570
624
  if req_id in self._pending_requests:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: zigpy-deconz
3
- Version: 1.1.0
3
+ Version: 1.1.2
4
4
  Summary: A library which communicates with Deconz radios for zigpy
5
5
  Author-email: Daniel Schmidt <schmidt.d@aon.at>
6
6
  License: GPL-3.0
File without changes
File without changes
File without changes
File without changes
File without changes