zigpy 2.0.0__tar.gz → 2.1.0__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 (126) hide show
  1. {zigpy-2.0.0/zigpy.egg-info → zigpy-2.1.0}/PKG-INFO +1 -1
  2. {zigpy-2.0.0 → zigpy-2.1.0}/pyproject.toml +7 -0
  3. {zigpy-2.0.0 → zigpy-2.1.0}/tests/test_application.py +50 -0
  4. {zigpy-2.0.0 → zigpy-2.1.0}/tests/test_datastructures.py +64 -0
  5. {zigpy-2.0.0 → zigpy-2.1.0}/tests/test_zcl.py +268 -10
  6. {zigpy-2.0.0 → zigpy-2.1.0}/tests/test_zcl_clusters.py +34 -1
  7. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/application.py +35 -0
  8. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/datastructures.py +8 -3
  9. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/ota/manager.py +22 -11
  10. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/zcl/__init__.py +159 -76
  11. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/zcl/clusters/general.py +65 -0
  12. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/zcl/clusters/greenpower.py +58 -32
  13. zigpy-2.1.0/zigpy/zgp/__init__.py +21 -0
  14. zigpy-2.1.0/zigpy/zgp/crypto.py +212 -0
  15. zigpy-2.1.0/zigpy/zgp/frame.py +145 -0
  16. zigpy-2.1.0/zigpy/zgp/types.py +181 -0
  17. {zigpy-2.0.0 → zigpy-2.1.0/zigpy.egg-info}/PKG-INFO +1 -1
  18. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy.egg-info/SOURCES.txt +2 -0
  19. zigpy-2.0.0/zigpy/zgp/__init__.py +0 -1
  20. zigpy-2.0.0/zigpy/zgp/types.py +0 -59
  21. {zigpy-2.0.0 → zigpy-2.1.0}/COPYING +0 -0
  22. {zigpy-2.0.0 → zigpy-2.1.0}/LICENSE +0 -0
  23. {zigpy-2.0.0 → zigpy-2.1.0}/README.md +0 -0
  24. {zigpy-2.0.0 → zigpy-2.1.0}/setup.cfg +0 -0
  25. {zigpy-2.0.0 → zigpy-2.1.0}/setup.py +0 -0
  26. {zigpy-2.0.0 → zigpy-2.1.0}/tests/test_app_state.py +0 -0
  27. {zigpy-2.0.0 → zigpy-2.1.0}/tests/test_appdb.py +0 -0
  28. {zigpy-2.0.0 → zigpy-2.1.0}/tests/test_appdb_migration.py +0 -0
  29. {zigpy-2.0.0 → zigpy-2.1.0}/tests/test_backups.py +0 -0
  30. {zigpy-2.0.0 → zigpy-2.1.0}/tests/test_config.py +0 -0
  31. {zigpy-2.0.0 → zigpy-2.1.0}/tests/test_datastructures_cpython.py +0 -0
  32. {zigpy-2.0.0 → zigpy-2.1.0}/tests/test_device.py +0 -0
  33. {zigpy-2.0.0 → zigpy-2.1.0}/tests/test_endpoint.py +0 -0
  34. {zigpy-2.0.0 → zigpy-2.1.0}/tests/test_event.py +0 -0
  35. {zigpy-2.0.0 → zigpy-2.1.0}/tests/test_group.py +0 -0
  36. {zigpy-2.0.0 → zigpy-2.1.0}/tests/test_listeners.py +0 -0
  37. {zigpy-2.0.0 → zigpy-2.1.0}/tests/test_packet_callbacks.py +0 -0
  38. {zigpy-2.0.0 → zigpy-2.1.0}/tests/test_serial.py +0 -0
  39. {zigpy-2.0.0 → zigpy-2.1.0}/tests/test_struct.py +0 -0
  40. {zigpy-2.0.0 → zigpy-2.1.0}/tests/test_topology.py +0 -0
  41. {zigpy-2.0.0 → zigpy-2.1.0}/tests/test_types.py +0 -0
  42. {zigpy-2.0.0 → zigpy-2.1.0}/tests/test_zcl_foundation.py +0 -0
  43. {zigpy-2.0.0 → zigpy-2.1.0}/tests/test_zcl_helpers.py +0 -0
  44. {zigpy-2.0.0 → zigpy-2.1.0}/tests/test_zdo.py +0 -0
  45. {zigpy-2.0.0 → zigpy-2.1.0}/tests/test_zdo_types.py +0 -0
  46. {zigpy-2.0.0 → zigpy-2.1.0}/tests/test_zigbee_util.py +0 -0
  47. {zigpy-2.0.0 → zigpy-2.1.0}/tools/__init__.py +0 -0
  48. {zigpy-2.0.0 → zigpy-2.1.0}/tools/regenerate_mypy_ignores.py +0 -0
  49. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/__init__.py +0 -0
  50. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/appdb.py +0 -0
  51. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/appdb_schemas/__init__.py +0 -0
  52. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/appdb_schemas/schema_v0.sql +0 -0
  53. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/appdb_schemas/schema_v1.sql +0 -0
  54. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/appdb_schemas/schema_v10.sql +0 -0
  55. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/appdb_schemas/schema_v11.sql +0 -0
  56. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/appdb_schemas/schema_v12.sql +0 -0
  57. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/appdb_schemas/schema_v13.sql +0 -0
  58. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/appdb_schemas/schema_v14.sql +0 -0
  59. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/appdb_schemas/schema_v15.sql +0 -0
  60. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/appdb_schemas/schema_v2.sql +0 -0
  61. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/appdb_schemas/schema_v3.sql +0 -0
  62. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/appdb_schemas/schema_v4.sql +0 -0
  63. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/appdb_schemas/schema_v5.sql +0 -0
  64. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/appdb_schemas/schema_v6.sql +0 -0
  65. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/appdb_schemas/schema_v7.sql +0 -0
  66. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/appdb_schemas/schema_v8.sql +0 -0
  67. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/appdb_schemas/schema_v9.sql +0 -0
  68. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/backports/__init__.py +0 -0
  69. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/backups.py +0 -0
  70. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/config/__init__.py +0 -0
  71. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/config/defaults.py +0 -0
  72. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/config/validators.py +0 -0
  73. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/const.py +0 -0
  74. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/device.py +0 -0
  75. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/endpoint.py +0 -0
  76. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/event/__init__.py +0 -0
  77. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/event/event_base.py +0 -0
  78. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/exceptions.py +0 -0
  79. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/group.py +0 -0
  80. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/listeners.py +0 -0
  81. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/ota/__init__.py +0 -0
  82. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/ota/image.py +0 -0
  83. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/ota/json_schemas.py +0 -0
  84. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/ota/providers.py +0 -0
  85. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/ota/validators.py +0 -0
  86. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/profiles/__init__.py +0 -0
  87. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/profiles/zgp.py +0 -0
  88. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/profiles/zha.py +0 -0
  89. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/profiles/zll.py +0 -0
  90. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/py.typed +0 -0
  91. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/quirks/__init__.py +0 -0
  92. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/quirks/registry.py +0 -0
  93. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/quirks/v2/__init__.py +0 -0
  94. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/quirks/v2/homeassistant/__init__.py +0 -0
  95. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/quirks/v2/homeassistant/binary_sensor.py +0 -0
  96. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/quirks/v2/homeassistant/number.py +0 -0
  97. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/quirks/v2/homeassistant/sensor.py +0 -0
  98. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/serial.py +0 -0
  99. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/state.py +0 -0
  100. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/topology.py +0 -0
  101. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/types/__init__.py +0 -0
  102. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/types/basic.py +0 -0
  103. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/types/named.py +0 -0
  104. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/types/struct.py +0 -0
  105. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/typing.py +0 -0
  106. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/util.py +0 -0
  107. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/zcl/clusters/__init__.py +0 -0
  108. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/zcl/clusters/closures.py +0 -0
  109. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/zcl/clusters/general_const.py +0 -0
  110. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/zcl/clusters/homeautomation.py +0 -0
  111. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/zcl/clusters/hvac.py +0 -0
  112. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/zcl/clusters/lighting.py +0 -0
  113. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/zcl/clusters/lightlink.py +0 -0
  114. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/zcl/clusters/manufacturer_specific.py +0 -0
  115. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/zcl/clusters/measurement.py +0 -0
  116. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/zcl/clusters/protocol.py +0 -0
  117. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/zcl/clusters/security.py +0 -0
  118. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/zcl/clusters/smartenergy.py +0 -0
  119. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/zcl/clusters/wwah.py +0 -0
  120. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/zcl/foundation.py +0 -0
  121. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/zcl/helpers.py +0 -0
  122. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/zdo/__init__.py +0 -0
  123. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy/zdo/types.py +0 -0
  124. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy.egg-info/dependency_links.txt +0 -0
  125. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy.egg-info/requires.txt +0 -0
  126. {zigpy-2.0.0 → zigpy-2.1.0}/zigpy.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: zigpy
3
- Version: 2.0.0
3
+ Version: 2.1.0
4
4
  Summary: Library implementing a Zigbee stack
5
5
  Author-email: Russell Cloran <rcloran@gmail.com>
6
6
  License: GPL-3.0
@@ -482,6 +482,13 @@ disable_error_code = [
482
482
  "attr-defined", # 35
483
483
  "misc", # 285
484
484
  ]
485
+
486
+ [[tool.mypy.overrides]]
487
+ module = "zigpy.zgp.frame"
488
+ disable_error_code = [
489
+ "assignment", # 11
490
+ "attr-defined", # 17
491
+ ]
485
492
  # Autogenerated mypy overrides: end
486
493
 
487
494
  [tool.coverage.run]
@@ -946,6 +946,31 @@ async def test_force_route_discovery(app, device, packet) -> None:
946
946
  ]
947
947
 
948
948
 
949
+ async def test_request_zigbee_direct_aps_encryption(app, device, packet) -> None:
950
+ await app.request(
951
+ device=device,
952
+ profile=0x1234,
953
+ cluster=clusters.general.ZigbeeDirectConfiguration.cluster_id,
954
+ src_ep=0x9A,
955
+ dst_ep=0xBC,
956
+ sequence=0xDE,
957
+ data=b"test data",
958
+ expect_reply=True,
959
+ use_ieee=False,
960
+ extended_timeout=False,
961
+ )
962
+
963
+ assert app.send_packet.mock_calls == [
964
+ call(
965
+ packet.replace(
966
+ cluster_id=clusters.general.ZigbeeDirectConfiguration.cluster_id,
967
+ tx_options=packet.tx_options | t.TransmitOptions.APS_Encryption,
968
+ priority=t.PacketPriority.NORMAL,
969
+ )
970
+ ),
971
+ ]
972
+
973
+
949
974
  def test_build_source_route_has_relays(app):
950
975
  device = MagicMock()
951
976
  device.relays = [0x1234, 0x5678]
@@ -2044,3 +2069,28 @@ async def test_device_resolver_new_kwarg():
2044
2069
  )
2045
2070
  assert app_default._device_resolver is None
2046
2071
  assert app_default._uninitialized_packet_handler is None
2072
+
2073
+
2074
+ async def test_coordinator_group_subscribe_unsubscribe(
2075
+ app: zigpy.application.ControllerApplication,
2076
+ ) -> None:
2077
+ """Test that the coordinator can subscribe and unsubscribe from group messages."""
2078
+ await app.startup()
2079
+
2080
+ # Subscribe to a group
2081
+ with patch.object(app, "_subscribe_to_multicast_group") as mock_subscribe:
2082
+ await app.subscribe_to_multicast_group(0x1234)
2083
+ await app.subscribe_to_multicast_group(0x1234, endpoint_id=2)
2084
+ assert mock_subscribe.mock_calls == [
2085
+ call(group_id=0x1234, endpoint_id=1),
2086
+ call(group_id=0x1234, endpoint_id=2),
2087
+ ]
2088
+
2089
+ # Unsubscribe from a group
2090
+ with patch.object(app, "_unsubscribe_from_multicast_group") as mock_unsubscribe:
2091
+ await app.unsubscribe_from_multicast_group(0x1234)
2092
+ await app.unsubscribe_from_multicast_group(0x1234, endpoint_id=2)
2093
+ assert mock_unsubscribe.mock_calls == [
2094
+ call(group_id=0x1234, endpoint_id=1),
2095
+ call(group_id=0x1234, endpoint_id=2),
2096
+ ]
@@ -274,6 +274,38 @@ async def test_dynamic_bounded_semaphore_cancellation():
274
274
  assert not sem.locked()
275
275
 
276
276
 
277
+ async def test_dynamic_bounded_semaphore_cancel_waiting_then_task_cancelled() -> None:
278
+ """Test that a cancelled waiter does not corrupt the active-request bookkeeping."""
279
+ sem = datastructures.PriorityDynamicBoundedSemaphore(1)
280
+
281
+ async def acquire():
282
+ async with sem:
283
+ await asyncio.sleep(60)
284
+
285
+ # Hold the only slot so the next acquire must wait
286
+ async with sem:
287
+ assert sem.value == 0
288
+
289
+ task = asyncio.create_task(acquire())
290
+ await asyncio.sleep(0) # Let the task start waiting
291
+ assert len(sem._waiters) == 1
292
+
293
+ # `cancel_waiting` resolves the future with an exception and the task is
294
+ # then cancelled: cancellation wins, so the acquire raises `CancelledError`
295
+ # even though no slot was ever granted.
296
+ sem.cancel_waiting(RuntimeError("rejoin"))
297
+ task.cancel()
298
+
299
+ with pytest.raises(asyncio.CancelledError):
300
+ await task
301
+
302
+ # The waiter never acquired a slot, so the active count is unchanged
303
+ assert sem.value == 0
304
+
305
+ # Releasing the outer slot must not trip the bookkeeping assertion
306
+ assert sem.value == 1
307
+
308
+
277
309
  async def test_priority_lock():
278
310
  """Test priority lock."""
279
311
 
@@ -709,6 +741,38 @@ async def test_request_limiter_cancel_waiting():
709
741
  await task2
710
742
 
711
743
 
744
+ async def test_request_limiter_cancel_waiting_then_task_cancelled() -> None:
745
+ """Test that a cancelled waiter does not corrupt the active-request bookkeeping."""
746
+ limiter = datastructures.RequestLimiter(1, {1: 1.0})
747
+
748
+ async def acquire():
749
+ async with limiter(priority=1):
750
+ await asyncio.sleep(60)
751
+
752
+ # Hold the only slot so the next acquire must wait
753
+ async with limiter(priority=1):
754
+ assert limiter.active_requests == 1
755
+
756
+ task = asyncio.create_task(acquire())
757
+ await asyncio.sleep(0) # Let the task start waiting
758
+ assert limiter.waiting_requests == 1
759
+
760
+ # `cancel_waiting` resolves the future with an exception and the task is
761
+ # then cancelled: cancellation wins, so the acquire raises `CancelledError`
762
+ # even though no slot was ever granted.
763
+ limiter.cancel_waiting(RuntimeError("rejoin"))
764
+ task.cancel()
765
+
766
+ with pytest.raises(asyncio.CancelledError):
767
+ await task
768
+
769
+ # The waiter never acquired a slot, so the active count is unchanged
770
+ assert limiter.active_requests == 1
771
+
772
+ # Releasing the outer slot must not trip the bookkeeping assertion
773
+ assert limiter.active_requests == 0
774
+
775
+
712
776
  async def test_request_limiter_backwards_compatibility() -> None:
713
777
  """Test `max_value` property proxying `max_concurrency`."""
714
778
  limiter = datastructures.RequestLimiter(5, {1: 1.0})
@@ -1,6 +1,7 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  import asyncio
4
+ import logging
4
5
  from typing import Any
5
6
  from unittest import mock
6
7
  from unittest.mock import AsyncMock, MagicMock, call, patch, sentinel
@@ -2639,6 +2640,211 @@ async def test_read_attributes_chunked_by_count(app_mock) -> None:
2639
2640
  }
2640
2641
 
2641
2642
 
2643
+ async def test_read_attributes_insufficient_space_retry_success(app_mock) -> None:
2644
+ """An INSUFFICIENT_SPACE record is re-read individually and can then succeed."""
2645
+
2646
+ class TestCluster(Basic):
2647
+ _skip_registry = True
2648
+
2649
+ class AttributeDefs(Basic.AttributeDefs):
2650
+ attr_0 = foundation.ZCLAttributeDef(id=0xFF00, type=t.uint8_t)
2651
+ attr_1 = foundation.ZCLAttributeDef(id=0xFF01, type=t.uint8_t)
2652
+ attr_2 = foundation.ZCLAttributeDef(id=0xFF02, type=t.uint8_t)
2653
+
2654
+ dev = add_initialized_device(app_mock, nwk=0x1234, ieee=make_ieee(1))
2655
+ cluster = TestCluster(dev.endpoints[1])
2656
+ dev.endpoints[1].add_input_cluster(TestCluster.cluster_id, cluster)
2657
+
2658
+ attrs = [
2659
+ TestCluster.AttributeDefs.attr_0,
2660
+ TestCluster.AttributeDefs.attr_1,
2661
+ TestCluster.AttributeDefs.attr_2,
2662
+ ]
2663
+
2664
+ supported = {
2665
+ TestCluster.AttributeDefs.attr_0: 10,
2666
+ # Runs out of space in the batched read, but fits when read alone
2667
+ TestCluster.AttributeDefs.attr_1: mock.Mock(
2668
+ side_effect=[foundation.Status.INSUFFICIENT_SPACE, 20]
2669
+ ),
2670
+ TestCluster.AttributeDefs.attr_2: 30,
2671
+ }
2672
+
2673
+ events = []
2674
+ cluster.on_event(AttributeReadEvent.event_type, events.append)
2675
+ cluster.on_event(AttributeUpdatedEvent.event_type, events.append)
2676
+
2677
+ with mock_attribute_reads(cluster, supported) as (mock_read, _):
2678
+ success, failure = await cluster.read_attributes(attrs)
2679
+
2680
+ assert success == {
2681
+ TestCluster.AttributeDefs.attr_0: 10,
2682
+ TestCluster.AttributeDefs.attr_1: 20,
2683
+ TestCluster.AttributeDefs.attr_2: 30,
2684
+ }
2685
+ assert failure == {}
2686
+
2687
+ # The batched read, then a solo re-read of the attribute that didn't fit
2688
+ chunks = [call_obj.args[0] for call_obj in mock_read.call_args_list]
2689
+ assert chunks == [[0xFF00, 0xFF01, 0xFF02], [0xFF01]]
2690
+
2691
+ assert events == [
2692
+ AttributeReadEvent(
2693
+ device_ieee=str(dev.ieee),
2694
+ endpoint_id=1,
2695
+ cluster_type=zcl.ClusterType.Server,
2696
+ cluster_id=TestCluster.cluster_id,
2697
+ attribute_name=TestCluster.AttributeDefs.attr_0.name,
2698
+ attribute_id=TestCluster.AttributeDefs.attr_0.id,
2699
+ manufacturer_code=None,
2700
+ raw_value=10,
2701
+ value=10,
2702
+ ),
2703
+ AttributeReadEvent(
2704
+ device_ieee=str(dev.ieee),
2705
+ endpoint_id=1,
2706
+ cluster_type=zcl.ClusterType.Server,
2707
+ cluster_id=TestCluster.cluster_id,
2708
+ attribute_name=TestCluster.AttributeDefs.attr_2.name,
2709
+ attribute_id=TestCluster.AttributeDefs.attr_2.id,
2710
+ manufacturer_code=None,
2711
+ raw_value=30,
2712
+ value=30,
2713
+ ),
2714
+ # No event for attr_1's INSUFFICIENT_SPACE record; the solo re-read then emits
2715
+ # its AttributeReadEvent last, after the two attributes that succeeded in the
2716
+ # batch
2717
+ AttributeReadEvent(
2718
+ device_ieee=str(dev.ieee),
2719
+ endpoint_id=1,
2720
+ cluster_type=zcl.ClusterType.Server,
2721
+ cluster_id=TestCluster.cluster_id,
2722
+ attribute_name=TestCluster.AttributeDefs.attr_1.name,
2723
+ attribute_id=TestCluster.AttributeDefs.attr_1.id,
2724
+ manufacturer_code=None,
2725
+ raw_value=20,
2726
+ value=20,
2727
+ ),
2728
+ ]
2729
+
2730
+
2731
+ async def test_read_attributes_insufficient_space_retry_persistent(app_mock) -> None:
2732
+ """An attribute that still doesn't fit when read alone is a terminal failure."""
2733
+
2734
+ class TestCluster(Basic):
2735
+ _skip_registry = True
2736
+
2737
+ class AttributeDefs(Basic.AttributeDefs):
2738
+ attr_0 = foundation.ZCLAttributeDef(id=0xFF00, type=t.uint8_t)
2739
+ attr_1 = foundation.ZCLAttributeDef(id=0xFF01, type=t.uint8_t)
2740
+
2741
+ dev = add_initialized_device(app_mock, nwk=0x1234, ieee=make_ieee(1))
2742
+ cluster = TestCluster(dev.endpoints[1])
2743
+ dev.endpoints[1].add_input_cluster(TestCluster.cluster_id, cluster)
2744
+
2745
+ attrs = [TestCluster.AttributeDefs.attr_0, TestCluster.AttributeDefs.attr_1]
2746
+
2747
+ supported = {
2748
+ TestCluster.AttributeDefs.attr_0: 10,
2749
+ # Never fits, even when read alone
2750
+ TestCluster.AttributeDefs.attr_1: mock.Mock(
2751
+ return_value=foundation.Status.INSUFFICIENT_SPACE
2752
+ ),
2753
+ }
2754
+
2755
+ with mock_attribute_reads(cluster, supported) as (mock_read, _):
2756
+ success, failure = await cluster.read_attributes(attrs)
2757
+
2758
+ assert success == {TestCluster.AttributeDefs.attr_0: 10}
2759
+ assert failure == {
2760
+ TestCluster.AttributeDefs.attr_1: foundation.Status.INSUFFICIENT_SPACE
2761
+ }
2762
+
2763
+ chunks = [call_obj.args[0] for call_obj in mock_read.call_args_list]
2764
+ assert chunks == [[0xFF00, 0xFF01], [0xFF01]]
2765
+
2766
+
2767
+ async def test_read_attributes_insufficient_space_single_chunk_no_retry(
2768
+ app_mock,
2769
+ ) -> None:
2770
+ """A single-attribute chunk is already isolated, so it is not re-read."""
2771
+
2772
+ class TestCluster(Basic):
2773
+ _skip_registry = True
2774
+
2775
+ class AttributeDefs(Basic.AttributeDefs):
2776
+ attr_0 = foundation.ZCLAttributeDef(id=0xFF00, type=t.uint8_t)
2777
+
2778
+ dev = add_initialized_device(app_mock, nwk=0x1234, ieee=make_ieee(1))
2779
+ cluster = TestCluster(dev.endpoints[1])
2780
+ dev.endpoints[1].add_input_cluster(TestCluster.cluster_id, cluster)
2781
+
2782
+ supported = {
2783
+ TestCluster.AttributeDefs.attr_0: mock.Mock(
2784
+ return_value=foundation.Status.INSUFFICIENT_SPACE
2785
+ ),
2786
+ }
2787
+
2788
+ with mock_attribute_reads(cluster, supported) as (mock_read, _):
2789
+ success, failure = await cluster.read_attributes(
2790
+ [TestCluster.AttributeDefs.attr_0]
2791
+ )
2792
+
2793
+ assert success == {}
2794
+ assert failure == {
2795
+ TestCluster.AttributeDefs.attr_0: foundation.Status.INSUFFICIENT_SPACE
2796
+ }
2797
+
2798
+ # Read exactly once: no redundant solo re-read of an already-isolated attribute
2799
+ chunks = [call_obj.args[0] for call_obj in mock_read.call_args_list]
2800
+ assert chunks == [[0xFF00]]
2801
+
2802
+
2803
+ @pytest.mark.parametrize("omitted_again", [False, True])
2804
+ async def test_read_attributes_omitted_record_retry(
2805
+ app_mock, omitted_again: bool
2806
+ ) -> None:
2807
+ """Records omitted from a response are re-read individually (ZCL R8 §2.5.2.3)."""
2808
+
2809
+ class TestCluster(Basic):
2810
+ _skip_registry = True
2811
+
2812
+ class AttributeDefs(Basic.AttributeDefs):
2813
+ attr_0 = foundation.ZCLAttributeDef(id=0xFF00, type=t.uint8_t)
2814
+ attr_1 = foundation.ZCLAttributeDef(id=0xFF01, type=t.uint8_t)
2815
+
2816
+ dev = add_initialized_device(app_mock, nwk=0x1234, ieee=make_ieee(1))
2817
+ cluster = TestCluster(dev.endpoints[1])
2818
+ dev.endpoints[1].add_input_cluster(TestCluster.cluster_id, cluster)
2819
+
2820
+ attrs = [TestCluster.AttributeDefs.attr_0, TestCluster.AttributeDefs.attr_1]
2821
+
2822
+ supported = {
2823
+ TestCluster.AttributeDefs.attr_0: 10,
2824
+ TestCluster.AttributeDefs.attr_1: mock.Mock(
2825
+ side_effect=[None, None] if omitted_again else [None, 20]
2826
+ ),
2827
+ }
2828
+
2829
+ with mock_attribute_reads(cluster, supported) as (mock_read, _):
2830
+ success, failure = await cluster.read_attributes(attrs)
2831
+
2832
+ if omitted_again:
2833
+ assert success == {TestCluster.AttributeDefs.attr_0: 10}
2834
+ assert failure == {
2835
+ TestCluster.AttributeDefs.attr_1: foundation.Status.INSUFFICIENT_SPACE
2836
+ }
2837
+ else:
2838
+ assert success == {
2839
+ TestCluster.AttributeDefs.attr_0: 10,
2840
+ TestCluster.AttributeDefs.attr_1: 20,
2841
+ }
2842
+ assert failure == {}
2843
+
2844
+ chunks = [call_obj.args[0] for call_obj in mock_read.call_args_list]
2845
+ assert chunks == [[0xFF00, 0xFF01], [0xFF01]]
2846
+
2847
+
2642
2848
  async def test_write_attributes_chunked_by_size(app_mock) -> None:
2643
2849
  """Write_attributes splits requests if a single one would exceed the limit."""
2644
2850
 
@@ -2709,20 +2915,72 @@ def test_chunk_records_by_size(sizes, max_bytes, expected) -> None:
2709
2915
 
2710
2916
 
2711
2917
  @pytest.mark.parametrize(
2712
- ("sizes", "max_bytes"),
2918
+ ("sizes", "max_bytes", "expected"),
2713
2919
  [
2714
2920
  # A single record larger than the limit, on its own
2715
- ([15], 10),
2716
- # An oversized record surrounded by ones that would otherwise fit
2717
- ([3, 15, 4], 10),
2921
+ ([15], 10, [[15]]),
2922
+ # An oversized record does not take the records around it down with it
2923
+ ([3, 15, 4], 10, [[3], [15], [4]]),
2924
+ # Records before an oversized one still pack together
2925
+ ([3, 3, 15, 4], 10, [[3, 3], [15], [4]]),
2926
+ # Consecutive oversized records each get a chunk of their own
2927
+ ([15, 15], 10, [[15], [15]]),
2718
2928
  ],
2719
2929
  )
2720
- def test_chunk_records_by_size_oversized_record(sizes, max_bytes) -> None:
2721
- """A record that on its own exceeds max_bytes can never be sent, so the chunker
2722
- fails loudly instead of emitting an oversized chunk the transport would reject.
2723
- """
2724
- with pytest.raises(ValueError, match="too large to fit in a single request"):
2725
- _chunk_records_by_size(sizes, lambda size: size, max_bytes=max_bytes)
2930
+ def test_chunk_records_by_size_oversized_record(
2931
+ sizes, max_bytes, expected, caplog
2932
+ ) -> None:
2933
+ """A record that on its own exceeds max_bytes is emitted as its own chunk."""
2934
+ with caplog.at_level(logging.DEBUG, logger="zigpy.zcl"):
2935
+ chunks = _chunk_records_by_size(sizes, lambda size: size, max_bytes=max_bytes)
2936
+
2937
+ assert chunks == expected
2938
+ assert caplog.text.count("exceeds the 10 byte request budget") == sum(
2939
+ size > max_bytes for size in sizes
2940
+ )
2941
+
2942
+
2943
+ async def test_write_attributes_oversized_record(app_mock) -> None:
2944
+ """An attribute record over the size budget is still sent, on its own."""
2945
+
2946
+ class TestCluster(Basic):
2947
+ _skip_registry = True
2948
+
2949
+ class AttributeDefs(Basic.AttributeDefs):
2950
+ attr_small = foundation.ZCLAttributeDef(id=0xFF00, type=t.uint8_t)
2951
+ attr_big = foundation.ZCLAttributeDef(id=0xFF01, type=t.LVBytes)
2952
+
2953
+ dev = add_initialized_device(app_mock, nwk=0x1234, ieee=make_ieee(1))
2954
+ cluster = TestCluster(dev.endpoints[1])
2955
+ dev.endpoints[1].add_input_cluster(TestCluster.cluster_id, cluster)
2956
+
2957
+ # 2 bytes of attribute id + 1 type + 1 length prefix + 55 bytes of value
2958
+ oversized = b"\xaa" * 55
2959
+
2960
+ with mock_attribute_writes(
2961
+ cluster,
2962
+ {
2963
+ TestCluster.AttributeDefs.attr_small: foundation.Status.SUCCESS,
2964
+ TestCluster.AttributeDefs.attr_big: foundation.Status.SUCCESS,
2965
+ },
2966
+ ) as (mock_write, _):
2967
+ [results] = await cluster.write_attributes(
2968
+ {
2969
+ TestCluster.AttributeDefs.attr_small: 1,
2970
+ TestCluster.AttributeDefs.attr_big: oversized,
2971
+ }
2972
+ )
2973
+
2974
+ # The small record is sent normally, the oversized one in a request of its own
2975
+ assert mock_write.call_count == 2
2976
+ chunks = [call_obj.args[0] for call_obj in mock_write.call_args_list]
2977
+ assert [[a.attrid for a in chunk] for chunk in chunks] == [[0xFF00], [0xFF01]]
2978
+
2979
+ [big_record] = chunks[1]
2980
+ assert len(big_record.serialize()) == 59 > MAX_ATTRIBUTE_RECORDS_BYTES
2981
+
2982
+ assert len(results) == 2
2983
+ assert all(r.status == foundation.Status.SUCCESS for r in results)
2726
2984
 
2727
2985
 
2728
2986
  def test_manufacturer_id_override_manuf_specific_cluster(app_mock) -> None:
@@ -12,7 +12,7 @@ from zigpy import device, types, zcl
12
12
  import zigpy.endpoint
13
13
  from zigpy.ota import OTA, OtaImagesResult
14
14
  from zigpy.zcl import OtaImageAvailableEvent, OtaQueryCacheUpdatedEvent, foundation
15
- from zigpy.zcl.clusters.general import Basic, Ota, Time
15
+ from zigpy.zcl.clusters.general import Basic, KeepAlive, Ota, Time
16
16
  import zigpy.zcl.clusters.security as sec
17
17
  from zigpy.zdo import types as zdo_t
18
18
 
@@ -137,6 +137,39 @@ async def test_basic_cluster():
137
137
  )
138
138
 
139
139
 
140
+ async def test_keepalive_cluster() -> None:
141
+ ep = MagicMock()
142
+ ep.reply = AsyncMock()
143
+
144
+ cluster = KeepAlive(ep)
145
+
146
+ rsp = await read_attributes(
147
+ cluster,
148
+ [
149
+ KeepAlive.AttributeDefs.tc_keep_alive_base.id,
150
+ KeepAlive.AttributeDefs.tc_keep_alive_jitter.id,
151
+ ],
152
+ )
153
+
154
+ assert rsp.status_records[0] == foundation.ReadAttributeRecord(
155
+ attrid=KeepAlive.AttributeDefs.tc_keep_alive_base.id,
156
+ status=foundation.Status.SUCCESS,
157
+ value=foundation.TypeValue(
158
+ type=foundation.DataTypeId.uint8,
159
+ value=10,
160
+ ),
161
+ )
162
+
163
+ assert rsp.status_records[1] == foundation.ReadAttributeRecord(
164
+ attrid=KeepAlive.AttributeDefs.tc_keep_alive_jitter.id,
165
+ status=foundation.Status.SUCCESS,
166
+ value=foundation.TypeValue(
167
+ type=foundation.DataTypeId.uint16,
168
+ value=300,
169
+ ),
170
+ )
171
+
172
+
140
173
  def _make_patched_datetime(fake_now):
141
174
  """Create a PatchedDatetime class that returns `fake_now` as the current time."""
142
175
 
@@ -995,6 +995,7 @@ class ControllerApplication(zigpy.util.ListenableMixin, abc.ABC):
995
995
  zigpy.zcl.clusters.general.OnOff.cluster_id,
996
996
  zigpy.zcl.clusters.general.Time.cluster_id,
997
997
  zigpy.zcl.clusters.general.Ota.cluster_id,
998
+ zigpy.zcl.clusters.general.KeepAlive.cluster_id,
998
999
  zigpy.zcl.clusters.security.IasAce.cluster_id,
999
1000
  ],
1000
1001
  output_clusters=[
@@ -1133,6 +1134,12 @@ class ControllerApplication(zigpy.util.ListenableMixin, abc.ABC):
1133
1134
  if force_route_discovery:
1134
1135
  tx_options |= t.TransmitOptions.FORCE_ROUTE_DISCOVERY
1135
1136
 
1137
+ if cluster == zigpy.zcl.clusters.general.ZigbeeDirectConfiguration.cluster_id:
1138
+ # All interactions with the Zigbee Direct Configuration cluster require
1139
+ # APS encryption (with the Trust Center link key, in a centralized
1140
+ # security network)
1141
+ tx_options |= t.TransmitOptions.APS_Encryption
1142
+
1136
1143
  await self.send_packet(
1137
1144
  t.ZigbeePacket(
1138
1145
  src=src,
@@ -1667,6 +1674,34 @@ class ControllerApplication(zigpy.util.ListenableMixin, abc.ABC):
1667
1674
  async def _packet_capture_change_channel(self, channel: int) -> None:
1668
1675
  """Change the channel of an active packet capture, internal."""
1669
1676
 
1677
+ async def subscribe_to_multicast_group(
1678
+ self, group_id: t.Group, endpoint_id: int = 1
1679
+ ) -> None:
1680
+ """Ask the coordinator firmware to subscribe to a group, if needed."""
1681
+ await self._subscribe_to_multicast_group(
1682
+ group_id=group_id, endpoint_id=endpoint_id
1683
+ )
1684
+
1685
+ # @abc.abstractmethod
1686
+ async def _subscribe_to_multicast_group(
1687
+ self, group_id: t.Group, endpoint_id: int
1688
+ ) -> None:
1689
+ """Ask the coordinator firmware to subscribe to a group, if needed."""
1690
+
1691
+ async def unsubscribe_from_multicast_group(
1692
+ self, group_id: t.Group, endpoint_id: int = 1
1693
+ ) -> None:
1694
+ """Ask the coordinator firmware to unsubscribe from a group, if needed."""
1695
+ await self._unsubscribe_from_multicast_group(
1696
+ group_id=group_id, endpoint_id=endpoint_id
1697
+ )
1698
+
1699
+ # @abc.abstractmethod
1700
+ async def _unsubscribe_from_multicast_group(
1701
+ self, group_id: t.Group, endpoint_id: int
1702
+ ) -> None:
1703
+ """Ask the coordinator firmware to unsubscribe from a group, if needed."""
1704
+
1670
1705
  async def permit(self, time_s: int = 60, node: t.EUI64 | str | None = None) -> None:
1671
1706
  """Permit joining on a specific node or all router nodes."""
1672
1707
  assert 0 <= time_s <= 254
@@ -145,8 +145,10 @@ class PriorityDynamicBoundedSemaphore:
145
145
  finally:
146
146
  self._waiters.remove(obj)
147
147
  except asyncio.CancelledError:
148
- # Currently the only exception designed be able to occur here.
149
- if fut.done() and not fut.cancelled():
148
+ # Only undo the bookkeeping if the slot was actually granted. If the future
149
+ # instead carries an exception (e.g. from `cancel_waiting`), no slot was
150
+ # ever acquired and there is nothing to release.
151
+ if fut.done() and not fut.cancelled() and fut.exception() is None:
150
152
  # Our Future was successfully set to True via _wake_up_next(),
151
153
  # but we are not about to successfully acquire(). Therefore we
152
154
  # must undo the bookkeeping already done and attempt to wake
@@ -517,7 +519,10 @@ class RequestLimiter:
517
519
  self._waiters.remove(waiter_obj)
518
520
  heapq.heapify(self._waiters)
519
521
  except asyncio.CancelledError:
520
- if fut.done() and not fut.cancelled():
522
+ # Only undo the bookkeeping if the slot was actually granted. If the future
523
+ # instead carries an exception (e.g. from `cancel_waiting`), no slot was
524
+ # ever acquired and there is nothing to release.
525
+ if fut.done() and not fut.cancelled() and fut.exception() is None:
521
526
  self._active_requests_by_tier[effective_tier] -= 1
522
527
 
523
528
  raise
@@ -26,8 +26,12 @@ if TYPE_CHECKING:
26
26
  from zigpy.ota.providers import OtaImageWithMetadata
27
27
 
28
28
 
29
+ # Maximum time between two block requests, to detect when OTA has permanently stalled
29
30
  MAX_TIME_WITHOUT_PROGRESS = 30
30
31
 
32
+ # Separate timeout for the final block, to give devices time to finally verify the OTA
33
+ FINAL_BLOCK_TIMEOUT = 60
34
+
31
35
 
32
36
  def _image_block_size_for_manufacturer(
33
37
  manufacturer_code: int, requested_size: int
@@ -190,17 +194,21 @@ class OTAManager:
190
194
  return
191
195
 
192
196
  try:
193
- await self.ota_cluster.image_block_response(
194
- status=foundation.Status.SUCCESS,
195
- manufacturer_code=self.image.firmware.header.manufacturer_id,
196
- image_type=self.image.firmware.header.image_type,
197
- file_version=self.image.firmware.header.file_version,
198
- file_offset=command.file_offset,
199
- image_data=block,
200
- tsn=hdr.tsn,
201
- )
197
+ async with self.device.application.request_priority(t.PacketPriority.LOW):
198
+ await self.ota_cluster.image_block_response(
199
+ status=foundation.Status.SUCCESS,
200
+ manufacturer_code=self.image.firmware.header.manufacturer_id,
201
+ image_type=self.image.firmware.header.image_type,
202
+ file_version=self.image.firmware.header.file_version,
203
+ file_offset=command.file_offset,
204
+ image_data=block,
205
+ tsn=hdr.tsn,
206
+ )
202
207
 
203
- self._stall_timer.reschedule(MAX_TIME_WITHOUT_PROGRESS)
208
+ if command.file_offset + len(block) < len(self._image_data):
209
+ self._stall_timer.reschedule(MAX_TIME_WITHOUT_PROGRESS)
210
+ else:
211
+ self._stall_timer.reschedule(FINAL_BLOCK_TIMEOUT)
204
212
 
205
213
  # Image block requests can sometimes succeed after the device aborts the
206
214
  # update. We should not allow the progress callback to be called.
@@ -261,7 +269,10 @@ class OTAManager:
261
269
  image_data=block,
262
270
  )
263
271
 
264
- self._stall_timer.reschedule(MAX_TIME_WITHOUT_PROGRESS)
272
+ if offset < len(self._image_data):
273
+ self._stall_timer.reschedule(MAX_TIME_WITHOUT_PROGRESS)
274
+ else:
275
+ self._stall_timer.reschedule(FINAL_BLOCK_TIMEOUT)
265
276
 
266
277
  if (
267
278
  self.progress_callback is not None