zigpy 2.0.1__tar.gz → 2.2.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.1/zigpy.egg-info → zigpy-2.2.0}/PKG-INFO +1 -1
  2. {zigpy-2.0.1 → zigpy-2.2.0}/pyproject.toml +7 -1
  3. {zigpy-2.0.1 → zigpy-2.2.0}/tests/test_appdb_migration.py +1 -1
  4. {zigpy-2.0.1 → zigpy-2.2.0}/tests/test_application.py +50 -0
  5. {zigpy-2.0.1 → zigpy-2.2.0}/tests/test_backups.py +0 -1
  6. {zigpy-2.0.1 → zigpy-2.2.0}/tests/test_struct.py +105 -1
  7. {zigpy-2.0.1 → zigpy-2.2.0}/tests/test_types.py +104 -0
  8. {zigpy-2.0.1 → zigpy-2.2.0}/tests/test_zcl.py +204 -61
  9. {zigpy-2.0.1 → zigpy-2.2.0}/tests/test_zcl_clusters.py +34 -1
  10. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/application.py +35 -0
  11. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/ota/manager.py +22 -11
  12. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/quirks/v2/__init__.py +0 -1
  13. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/types/basic.py +51 -0
  14. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/types/struct.py +3 -2
  15. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/zcl/__init__.py +31 -6
  16. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/zcl/clusters/general.py +65 -0
  17. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/zcl/clusters/greenpower.py +61 -35
  18. zigpy-2.2.0/zigpy/zgp/__init__.py +45 -0
  19. zigpy-2.2.0/zigpy/zgp/commands.py +459 -0
  20. zigpy-2.2.0/zigpy/zgp/crypto.py +212 -0
  21. zigpy-2.2.0/zigpy/zgp/types.py +210 -0
  22. {zigpy-2.0.1 → zigpy-2.2.0/zigpy.egg-info}/PKG-INFO +1 -1
  23. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy.egg-info/SOURCES.txt +2 -0
  24. zigpy-2.0.1/zigpy/zgp/__init__.py +0 -1
  25. zigpy-2.0.1/zigpy/zgp/types.py +0 -59
  26. {zigpy-2.0.1 → zigpy-2.2.0}/COPYING +0 -0
  27. {zigpy-2.0.1 → zigpy-2.2.0}/LICENSE +0 -0
  28. {zigpy-2.0.1 → zigpy-2.2.0}/README.md +0 -0
  29. {zigpy-2.0.1 → zigpy-2.2.0}/setup.cfg +0 -0
  30. {zigpy-2.0.1 → zigpy-2.2.0}/setup.py +0 -0
  31. {zigpy-2.0.1 → zigpy-2.2.0}/tests/test_app_state.py +0 -0
  32. {zigpy-2.0.1 → zigpy-2.2.0}/tests/test_appdb.py +0 -0
  33. {zigpy-2.0.1 → zigpy-2.2.0}/tests/test_config.py +0 -0
  34. {zigpy-2.0.1 → zigpy-2.2.0}/tests/test_datastructures.py +0 -0
  35. {zigpy-2.0.1 → zigpy-2.2.0}/tests/test_datastructures_cpython.py +0 -0
  36. {zigpy-2.0.1 → zigpy-2.2.0}/tests/test_device.py +0 -0
  37. {zigpy-2.0.1 → zigpy-2.2.0}/tests/test_endpoint.py +0 -0
  38. {zigpy-2.0.1 → zigpy-2.2.0}/tests/test_event.py +0 -0
  39. {zigpy-2.0.1 → zigpy-2.2.0}/tests/test_group.py +0 -0
  40. {zigpy-2.0.1 → zigpy-2.2.0}/tests/test_listeners.py +0 -0
  41. {zigpy-2.0.1 → zigpy-2.2.0}/tests/test_packet_callbacks.py +0 -0
  42. {zigpy-2.0.1 → zigpy-2.2.0}/tests/test_serial.py +0 -0
  43. {zigpy-2.0.1 → zigpy-2.2.0}/tests/test_topology.py +0 -0
  44. {zigpy-2.0.1 → zigpy-2.2.0}/tests/test_zcl_foundation.py +0 -0
  45. {zigpy-2.0.1 → zigpy-2.2.0}/tests/test_zcl_helpers.py +0 -0
  46. {zigpy-2.0.1 → zigpy-2.2.0}/tests/test_zdo.py +0 -0
  47. {zigpy-2.0.1 → zigpy-2.2.0}/tests/test_zdo_types.py +0 -0
  48. {zigpy-2.0.1 → zigpy-2.2.0}/tests/test_zigbee_util.py +0 -0
  49. {zigpy-2.0.1 → zigpy-2.2.0}/tools/__init__.py +0 -0
  50. {zigpy-2.0.1 → zigpy-2.2.0}/tools/regenerate_mypy_ignores.py +0 -0
  51. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/__init__.py +0 -0
  52. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/appdb.py +0 -0
  53. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/appdb_schemas/__init__.py +0 -0
  54. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/appdb_schemas/schema_v0.sql +0 -0
  55. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/appdb_schemas/schema_v1.sql +0 -0
  56. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/appdb_schemas/schema_v10.sql +0 -0
  57. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/appdb_schemas/schema_v11.sql +0 -0
  58. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/appdb_schemas/schema_v12.sql +0 -0
  59. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/appdb_schemas/schema_v13.sql +0 -0
  60. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/appdb_schemas/schema_v14.sql +0 -0
  61. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/appdb_schemas/schema_v15.sql +0 -0
  62. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/appdb_schemas/schema_v2.sql +0 -0
  63. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/appdb_schemas/schema_v3.sql +0 -0
  64. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/appdb_schemas/schema_v4.sql +0 -0
  65. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/appdb_schemas/schema_v5.sql +0 -0
  66. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/appdb_schemas/schema_v6.sql +0 -0
  67. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/appdb_schemas/schema_v7.sql +0 -0
  68. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/appdb_schemas/schema_v8.sql +0 -0
  69. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/appdb_schemas/schema_v9.sql +0 -0
  70. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/backports/__init__.py +0 -0
  71. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/backups.py +0 -0
  72. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/config/__init__.py +0 -0
  73. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/config/defaults.py +0 -0
  74. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/config/validators.py +0 -0
  75. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/const.py +0 -0
  76. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/datastructures.py +0 -0
  77. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/device.py +0 -0
  78. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/endpoint.py +0 -0
  79. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/event/__init__.py +0 -0
  80. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/event/event_base.py +0 -0
  81. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/exceptions.py +0 -0
  82. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/group.py +0 -0
  83. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/listeners.py +0 -0
  84. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/ota/__init__.py +0 -0
  85. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/ota/image.py +0 -0
  86. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/ota/json_schemas.py +0 -0
  87. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/ota/providers.py +0 -0
  88. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/ota/validators.py +0 -0
  89. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/profiles/__init__.py +0 -0
  90. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/profiles/zgp.py +0 -0
  91. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/profiles/zha.py +0 -0
  92. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/profiles/zll.py +0 -0
  93. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/py.typed +0 -0
  94. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/quirks/__init__.py +0 -0
  95. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/quirks/registry.py +0 -0
  96. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/quirks/v2/homeassistant/__init__.py +0 -0
  97. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/quirks/v2/homeassistant/binary_sensor.py +0 -0
  98. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/quirks/v2/homeassistant/number.py +0 -0
  99. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/quirks/v2/homeassistant/sensor.py +0 -0
  100. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/serial.py +0 -0
  101. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/state.py +0 -0
  102. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/topology.py +0 -0
  103. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/types/__init__.py +0 -0
  104. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/types/named.py +0 -0
  105. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/typing.py +0 -0
  106. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/util.py +0 -0
  107. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/zcl/clusters/__init__.py +0 -0
  108. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/zcl/clusters/closures.py +0 -0
  109. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/zcl/clusters/general_const.py +0 -0
  110. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/zcl/clusters/homeautomation.py +0 -0
  111. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/zcl/clusters/hvac.py +0 -0
  112. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/zcl/clusters/lighting.py +0 -0
  113. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/zcl/clusters/lightlink.py +0 -0
  114. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/zcl/clusters/manufacturer_specific.py +0 -0
  115. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/zcl/clusters/measurement.py +0 -0
  116. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/zcl/clusters/protocol.py +0 -0
  117. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/zcl/clusters/security.py +0 -0
  118. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/zcl/clusters/smartenergy.py +0 -0
  119. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/zcl/clusters/wwah.py +0 -0
  120. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/zcl/foundation.py +0 -0
  121. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/zcl/helpers.py +0 -0
  122. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/zdo/__init__.py +0 -0
  123. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy/zdo/types.py +0 -0
  124. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy.egg-info/dependency_links.txt +0 -0
  125. {zigpy-2.0.1 → zigpy-2.2.0}/zigpy.egg-info/requires.txt +0 -0
  126. {zigpy-2.0.1 → zigpy-2.2.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.1
3
+ Version: 2.2.0
4
4
  Summary: Library implementing a Zigbee stack
5
5
  Author-email: Russell Cloran <rcloran@gmail.com>
6
6
  License: GPL-3.0
@@ -239,7 +239,6 @@ ignore = [
239
239
 
240
240
  [tool.ruff.lint.per-file-ignores]
241
241
  "tests/*.py" = [
242
- "F811", # Redefinition of unused name from line N
243
242
  "PIE794", # Class field defined multiple times
244
243
  "PLC0206", # Extracting value from dictionary without calling `.items()`
245
244
  "PLC0415", # `import` should be at the top-level of a file
@@ -482,6 +481,13 @@ disable_error_code = [
482
481
  "attr-defined", # 35
483
482
  "misc", # 285
484
483
  ]
484
+
485
+ [[tool.mypy.overrides]]
486
+ module = "zigpy.zgp.commands"
487
+ disable_error_code = [
488
+ "assignment", # 22
489
+ "attr-defined", # 27
490
+ ]
485
491
  # Autogenerated mypy overrides: end
486
492
 
487
493
  [tool.coverage.run]
@@ -8,7 +8,7 @@ from aiosqlite.context import contextmanager
8
8
  import pytest
9
9
 
10
10
  from tests.async_mock import AsyncMock, MagicMock, patch
11
- from tests.conftest import app, make_node_desc # noqa: F401
11
+ from tests.conftest import make_node_desc
12
12
  from tests.test_appdb import make_app_with_db
13
13
  import zigpy.appdb
14
14
  import zigpy.appdb_schemas
@@ -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
+ ]
@@ -5,7 +5,6 @@ import logging
5
5
  import pytest
6
6
 
7
7
  from tests.async_mock import AsyncMock
8
- from tests.conftest import app # noqa: F401
9
8
  import zigpy.backups
10
9
  import zigpy.state as app_state
11
10
  import zigpy.types as t
@@ -620,7 +620,9 @@ def test_bitstruct_misaligned():
620
620
  baz: t.uint7_t
621
621
 
622
622
  s = TestStruct(foo=0b1, bar=0b10101010, baz=0b1110111)
623
- assert s.serialize() == bytes([0b1110111_1, 0b0101010_1])
623
+
624
+ # The segment spans two bytes, so the first byte holds the first-declared fields
625
+ assert s.serialize() == bytes([0b0101010_1, 0b1110111_1])
624
626
 
625
627
  s2, remaining = TestStruct.deserialize(s.serialize() + b"asd")
626
628
  assert s == s2
@@ -629,6 +631,108 @@ def test_bitstruct_misaligned():
629
631
  TestStruct.deserialize(b"\xff")
630
632
 
631
633
 
634
+ def test_bitstruct_multi_byte_segment():
635
+ """A bitfield segment wider than one byte is little-endian."""
636
+
637
+ class TestStruct(t.Struct):
638
+ foo: t.uint3_t
639
+ bar: t.uint1_t
640
+ baz: t.uint2_t
641
+ # This field straddles the byte boundary, so the whole struct is one segment
642
+ qux: t.uint3_t
643
+ quux: t.uint7_t
644
+
645
+ s = TestStruct(foo=0b101, bar=0b1, baz=0b10, qux=0b111, quux=0b1010101)
646
+ value = 0b101 | (0b1 << 3) | (0b10 << 4) | (0b111 << 6) | (0b1010101 << 9)
647
+
648
+ # A segment is laid out exactly like the integer of the same width would be
649
+ assert s.serialize() == t.uint16_t(value).serialize()
650
+ assert TestStruct.deserialize(s.serialize() + b"asd") == (s, b"asd")
651
+
652
+
653
+ def test_bitstruct_multi_byte_segment_enums(expose_global):
654
+ """Sub-byte enums and bitmaps pack like the plain integers they derive from."""
655
+
656
+ @expose_global
657
+ class TestEnum(t.enum3):
658
+ FOO = 0b101
659
+
660
+ @expose_global
661
+ class TestBitmap(t.bitmap6):
662
+ BAR = 0b000010
663
+ BAZ = 0b100000
664
+
665
+ class TestStruct(t.Struct):
666
+ first: TestEnum
667
+ second: t.uint4_t
668
+ # Straddles the byte boundary
669
+ third: TestBitmap
670
+ fourth: t.uint3_t
671
+
672
+ s = TestStruct(
673
+ first=TestEnum.FOO,
674
+ second=0b1001,
675
+ third=TestBitmap.BAR | TestBitmap.BAZ,
676
+ fourth=0b110,
677
+ )
678
+ value = 0b101 | (0b1001 << 3) | (0b100010 << 7) | (0b110 << 13)
679
+
680
+ assert s.serialize() == t.uint16_t(value).serialize()
681
+ assert TestStruct.deserialize(s.serialize() + b"asd") == (s, b"asd")
682
+
683
+
684
+ def test_bitstruct_three_byte_segment():
685
+ """Segments wider than two bytes are little-endian as well."""
686
+
687
+ class TestStruct(t.Struct):
688
+ foo: t.uint7_t
689
+ # Byte-serializable but misaligned, like every field after it
690
+ bar: t.uint8_t
691
+ baz: t.uint4_t
692
+ qux: t.uint5_t
693
+
694
+ s = TestStruct(foo=0b1010101, bar=0b11110000, baz=0b1001, qux=0b11011)
695
+ value = 0b1010101 | (0b11110000 << 7) | (0b1001 << 15) | (0b11011 << 19)
696
+
697
+ assert s.serialize() == t.uint24_t(value).serialize()
698
+ assert TestStruct.deserialize(s.serialize() + b"asd") == (s, b"asd")
699
+
700
+
701
+ def test_bitstruct_gp_commissioning_notification_options() -> None:
702
+ """The GP commissioning notification options round trip."""
703
+
704
+ class CommissioningNotificationOptions(t.Struct):
705
+ application_id: t.uint3_t
706
+ rx_after_tx: t.uint1_t
707
+ security_level: t.uint2_t
708
+ security_key_type: t.uint3_t
709
+ security_failed: t.uint1_t
710
+ bidirectional_cap: t.uint1_t
711
+ proxy_info_present: t.uint1_t
712
+ _reserved: t.uint4_t
713
+
714
+ options = CommissioningNotificationOptions(
715
+ application_id=0b010,
716
+ rx_after_tx=1,
717
+ security_level=0b11,
718
+ security_key_type=0b111,
719
+ security_failed=1,
720
+ bidirectional_cap=0,
721
+ proxy_info_present=1,
722
+ _reserved=0,
723
+ )
724
+
725
+ value = (
726
+ 0b010 | (1 << 3) | (0b11 << 4) | (0b111 << 6) | (1 << 9) | (0 << 10) | (1 << 11)
727
+ )
728
+ assert value == 0x0BFA
729
+ assert options.serialize() == b"\xfa\x0b"
730
+ assert options.serialize() == t.uint16_t(value).serialize()
731
+ assert CommissioningNotificationOptions.deserialize(
732
+ options.serialize() + b"asd"
733
+ ) == (options, b"asd")
734
+
735
+
632
736
  def test_non_byte_sized_struct():
633
737
  class TestStruct(t.Struct):
634
738
  foo: t.uint1_t
@@ -458,6 +458,110 @@ def test_fixedlist():
458
458
  assert t.FixedList[t.uint8_t, 2]([1, 2]).serialize() == b"\x01\x02"
459
459
 
460
460
 
461
+ class VariableSizeItem(t.Struct):
462
+ """Item whose serialized size depends on its contents."""
463
+
464
+ id: t.uint8_t
465
+ data: t.LVBytes
466
+
467
+
468
+ def test_size_prefixed_list() -> None:
469
+ lst, rest = t.SizePrefixedList[t.uint16_t, t.uint8_t].deserialize(
470
+ b"\x04\x01\x00\x02\x00" + b"leftover"
471
+ )
472
+
473
+ assert lst == [0x0001, 0x0002]
474
+ assert rest == b"leftover"
475
+ assert lst.serialize() == b"\x04\x01\x00\x02\x00"
476
+ assert isinstance(lst, t.SizePrefixedList[t.uint16_t, t.uint8_t])
477
+
478
+
479
+ def test_size_prefixed_list_empty() -> None:
480
+ lst, rest = t.SizePrefixedList[t.uint16_t, t.uint8_t].deserialize(b"\x00\xaa")
481
+
482
+ assert lst == []
483
+ assert rest == b"\xaa"
484
+ assert lst.serialize() == b"\x00"
485
+
486
+
487
+ def test_size_prefixed_list_variable_size_items() -> None:
488
+ """The prefix delimits a region, so items may differ in size (unlike `LVList`)."""
489
+ items = b"\x01\x02ab" + b"\x02\x00" + b"\x03\x01c"
490
+ lst, rest = t.SizePrefixedList[VariableSizeItem, t.uint8_t].deserialize(
491
+ bytes([len(items)]) + items + b"\xff"
492
+ )
493
+
494
+ assert [(item.id, item.data) for item in lst] == [(1, b"ab"), (2, b""), (3, b"c")]
495
+ assert rest == b"\xff"
496
+ assert lst.serialize() == bytes([len(items)]) + items
497
+
498
+
499
+ def test_size_prefixed_list_size_type() -> None:
500
+ lst, rest = t.SizePrefixedList[t.uint16_t, t.uint16_t].deserialize(
501
+ b"\x04\x00\x01\x00\x02\x00"
502
+ )
503
+
504
+ assert lst == [0x0001, 0x0002]
505
+ assert rest == b""
506
+ assert lst.serialize() == b"\x04\x00\x01\x00\x02\x00"
507
+
508
+
509
+ def test_size_prefixed_list_misaligned_size() -> None:
510
+ """A size that does not cover whole items is malformed, not silently truncated."""
511
+ with pytest.raises(ValueError):
512
+ t.SizePrefixedList[t.uint16_t, t.uint8_t].deserialize(b"\x03\x01\x00\x02\x00")
513
+
514
+
515
+ def test_size_prefixed_list_too_short() -> None:
516
+ with pytest.raises(ValueError):
517
+ t.SizePrefixedList[t.uint16_t, t.uint8_t].deserialize(b"")
518
+
519
+ with pytest.raises(ValueError, match="too short"):
520
+ t.SizePrefixedList[t.uint16_t, t.uint8_t].deserialize(b"\x08\x01\x00")
521
+
522
+
523
+ def test_size_prefixed_list_types() -> None:
524
+ # Brackets create singleton types
525
+ anon_lst1 = t.SizePrefixedList[t.uint16_t, t.uint8_t]
526
+ anon_lst2 = t.SizePrefixedList[t.uint16_t, t.uint8_t]
527
+
528
+ assert anon_lst1._size_type is t.uint8_t
529
+ assert anon_lst1._item_type is t.uint16_t
530
+ assert anon_lst1 is anon_lst2
531
+ assert not issubclass(
532
+ t.SizePrefixedList[t.uint16_t, t.uint8_t],
533
+ t.SizePrefixedList[t.uint8_t, t.uint16_t],
534
+ )
535
+
536
+ # Bracketed types are compatible with explicit subclasses
537
+ class SizePrefixedListSubclass(
538
+ t.SizePrefixedList, item_type=t.uint16_t, size_type=t.uint8_t
539
+ ):
540
+ pass
541
+
542
+ assert issubclass(
543
+ SizePrefixedListSubclass, t.SizePrefixedList[t.uint16_t, t.uint8_t]
544
+ )
545
+ assert isinstance(
546
+ SizePrefixedListSubclass(), t.SizePrefixedList[t.uint16_t, t.uint8_t]
547
+ )
548
+
549
+ # A count-prefixed list and a size-prefixed one are never interchangeable, as their
550
+ # length prefixes mean different things on the wire
551
+ assert not issubclass(
552
+ t.SizePrefixedList[t.uint8_t, t.uint8_t], t.LVList[t.uint8_t, t.uint8_t]
553
+ )
554
+ assert not issubclass(
555
+ t.LVList[t.uint8_t, t.uint8_t], t.SizePrefixedList[t.uint8_t, t.uint8_t]
556
+ )
557
+ assert not isinstance(
558
+ t.LVList[t.uint8_t, t.uint8_t](), t.SizePrefixedList[t.uint8_t, t.uint8_t]
559
+ )
560
+ assert not isinstance(
561
+ t.SizePrefixedList[t.uint8_t, t.uint8_t](), t.LVList[t.uint8_t, t.uint8_t]
562
+ )
563
+
564
+
461
565
  def test_lvlist_types():
462
566
  # Brackets create singleton types
463
567
  anon_lst1 = t.LVList[t.uint16_t, t.uint8_t]