zigpy 2.4.0__tar.gz → 2.4.1__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 (129) hide show
  1. {zigpy-2.4.0/zigpy.egg-info → zigpy-2.4.1}/PKG-INFO +1 -1
  2. {zigpy-2.4.0 → zigpy-2.4.1}/tests/test_appdb.py +113 -2
  3. {zigpy-2.4.0 → zigpy-2.4.1}/tests/test_config.py +3 -3
  4. {zigpy-2.4.0 → zigpy-2.4.1}/tests/test_device.py +84 -1
  5. {zigpy-2.4.0 → zigpy-2.4.1}/tests/test_struct.py +24 -0
  6. {zigpy-2.4.0 → zigpy-2.4.1}/tests/test_types.py +57 -0
  7. {zigpy-2.4.0 → zigpy-2.4.1}/tests/test_zcl.py +14 -0
  8. {zigpy-2.4.0 → zigpy-2.4.1}/tests/test_zcl_clusters.py +404 -147
  9. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/appdb.py +60 -15
  10. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/config/__init__.py +1 -5
  11. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/device.py +20 -20
  12. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/types/basic.py +17 -6
  13. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/types/struct.py +12 -0
  14. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zcl/clusters/general.py +155 -25
  15. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zcl/clusters/hvac.py +1 -1
  16. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zcl/clusters/protocol.py +2 -1
  17. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zcl/clusters/security.py +39 -92
  18. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zcl/clusters/wwah.py +2 -1
  19. {zigpy-2.4.0 → zigpy-2.4.1/zigpy.egg-info}/PKG-INFO +1 -1
  20. {zigpy-2.4.0 → zigpy-2.4.1}/COPYING +0 -0
  21. {zigpy-2.4.0 → zigpy-2.4.1}/LICENSE +0 -0
  22. {zigpy-2.4.0 → zigpy-2.4.1}/README.md +0 -0
  23. {zigpy-2.4.0 → zigpy-2.4.1}/pyproject.toml +0 -0
  24. {zigpy-2.4.0 → zigpy-2.4.1}/setup.cfg +0 -0
  25. {zigpy-2.4.0 → zigpy-2.4.1}/setup.py +0 -0
  26. {zigpy-2.4.0 → zigpy-2.4.1}/tests/test_app_state.py +0 -0
  27. {zigpy-2.4.0 → zigpy-2.4.1}/tests/test_appdb_migration.py +0 -0
  28. {zigpy-2.4.0 → zigpy-2.4.1}/tests/test_application.py +0 -0
  29. {zigpy-2.4.0 → zigpy-2.4.1}/tests/test_backups.py +0 -0
  30. {zigpy-2.4.0 → zigpy-2.4.1}/tests/test_datastructures.py +0 -0
  31. {zigpy-2.4.0 → zigpy-2.4.1}/tests/test_datastructures_cpython.py +0 -0
  32. {zigpy-2.4.0 → zigpy-2.4.1}/tests/test_endpoint.py +0 -0
  33. {zigpy-2.4.0 → zigpy-2.4.1}/tests/test_event.py +0 -0
  34. {zigpy-2.4.0 → zigpy-2.4.1}/tests/test_group.py +0 -0
  35. {zigpy-2.4.0 → zigpy-2.4.1}/tests/test_listeners.py +0 -0
  36. {zigpy-2.4.0 → zigpy-2.4.1}/tests/test_packet_callbacks.py +0 -0
  37. {zigpy-2.4.0 → zigpy-2.4.1}/tests/test_serial.py +0 -0
  38. {zigpy-2.4.0 → zigpy-2.4.1}/tests/test_topology.py +0 -0
  39. {zigpy-2.4.0 → zigpy-2.4.1}/tests/test_zcl_foundation.py +0 -0
  40. {zigpy-2.4.0 → zigpy-2.4.1}/tests/test_zcl_helpers.py +0 -0
  41. {zigpy-2.4.0 → zigpy-2.4.1}/tests/test_zdo.py +0 -0
  42. {zigpy-2.4.0 → zigpy-2.4.1}/tests/test_zdo_types.py +0 -0
  43. {zigpy-2.4.0 → zigpy-2.4.1}/tests/test_zigbee_util.py +0 -0
  44. {zigpy-2.4.0 → zigpy-2.4.1}/tools/__init__.py +0 -0
  45. {zigpy-2.4.0 → zigpy-2.4.1}/tools/regenerate_mypy_ignores.py +0 -0
  46. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/__init__.py +0 -0
  47. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/appdb_schemas/__init__.py +0 -0
  48. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/appdb_schemas/schema_v0.sql +0 -0
  49. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/appdb_schemas/schema_v1.sql +0 -0
  50. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/appdb_schemas/schema_v10.sql +0 -0
  51. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/appdb_schemas/schema_v11.sql +0 -0
  52. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/appdb_schemas/schema_v12.sql +0 -0
  53. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/appdb_schemas/schema_v13.sql +0 -0
  54. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/appdb_schemas/schema_v14.sql +0 -0
  55. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/appdb_schemas/schema_v15.sql +0 -0
  56. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/appdb_schemas/schema_v16.sql +0 -0
  57. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/appdb_schemas/schema_v2.sql +0 -0
  58. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/appdb_schemas/schema_v3.sql +0 -0
  59. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/appdb_schemas/schema_v4.sql +0 -0
  60. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/appdb_schemas/schema_v5.sql +0 -0
  61. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/appdb_schemas/schema_v6.sql +0 -0
  62. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/appdb_schemas/schema_v7.sql +0 -0
  63. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/appdb_schemas/schema_v8.sql +0 -0
  64. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/appdb_schemas/schema_v9.sql +0 -0
  65. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/application.py +0 -0
  66. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/backports/__init__.py +0 -0
  67. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/backups.py +0 -0
  68. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/config/defaults.py +0 -0
  69. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/config/validators.py +0 -0
  70. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/const.py +0 -0
  71. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/datastructures.py +0 -0
  72. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/endpoint.py +0 -0
  73. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/event/__init__.py +0 -0
  74. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/event/event_base.py +0 -0
  75. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/exceptions.py +0 -0
  76. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/group.py +0 -0
  77. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/listeners.py +0 -0
  78. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/ota/__init__.py +0 -0
  79. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/ota/image.py +0 -0
  80. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/ota/json_schemas.py +0 -0
  81. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/ota/manager.py +0 -0
  82. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/ota/providers.py +0 -0
  83. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/ota/validators.py +0 -0
  84. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/profiles/__init__.py +0 -0
  85. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/profiles/zgp.py +0 -0
  86. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/profiles/zha.py +0 -0
  87. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/profiles/zll.py +0 -0
  88. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/py.typed +0 -0
  89. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/quirks/__init__.py +0 -0
  90. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/quirks/registry.py +0 -0
  91. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/quirks/v2/__init__.py +0 -0
  92. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/quirks/v2/homeassistant/__init__.py +0 -0
  93. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/quirks/v2/homeassistant/binary_sensor.py +0 -0
  94. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/quirks/v2/homeassistant/number.py +0 -0
  95. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/quirks/v2/homeassistant/sensor.py +0 -0
  96. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/serial.py +0 -0
  97. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/state.py +0 -0
  98. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/time.py +0 -0
  99. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/topology.py +0 -0
  100. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/types/__init__.py +0 -0
  101. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/types/named.py +0 -0
  102. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/typing.py +0 -0
  103. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/util.py +0 -0
  104. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zcl/__init__.py +0 -0
  105. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zcl/clusters/__init__.py +0 -0
  106. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zcl/clusters/closures.py +0 -0
  107. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zcl/clusters/general_const.py +0 -0
  108. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zcl/clusters/greenpower.py +0 -0
  109. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zcl/clusters/homeautomation.py +0 -0
  110. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zcl/clusters/lighting.py +0 -0
  111. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zcl/clusters/lightlink.py +0 -0
  112. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zcl/clusters/manufacturer_specific.py +0 -0
  113. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zcl/clusters/measurement.py +0 -0
  114. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zcl/clusters/smartenergy.py +0 -0
  115. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zcl/foundation.py +0 -0
  116. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zcl/helpers.py +0 -0
  117. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zdo/__init__.py +0 -0
  118. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zdo/types.py +0 -0
  119. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zgp/__init__.py +0 -0
  120. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zgp/commands.py +0 -0
  121. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zgp/crypto.py +0 -0
  122. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zgp/manager.py +0 -0
  123. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zgp/tunneling.py +0 -0
  124. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zgp/types.py +0 -0
  125. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy/zgp/util.py +0 -0
  126. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy.egg-info/SOURCES.txt +0 -0
  127. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy.egg-info/dependency_links.txt +0 -0
  128. {zigpy-2.4.0 → zigpy-2.4.1}/zigpy.egg-info/requires.txt +0 -0
  129. {zigpy-2.4.0 → zigpy-2.4.1}/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.4.0
3
+ Version: 2.4.1
4
4
  Summary: Library implementing a Zigbee stack
5
5
  Author-email: Russell Cloran <rcloran@gmail.com>
6
6
  License: GPL-3.0
@@ -1,5 +1,6 @@
1
1
  import asyncio
2
2
  from datetime import UTC, datetime, timedelta
3
+ import logging
3
4
  import pathlib
4
5
  import sqlite3
5
6
  import time
@@ -49,7 +50,7 @@ from zigpy.zcl import (
49
50
  OtaQueryCacheUpdatedEvent,
50
51
  UnsupportedAttribute,
51
52
  )
52
- from zigpy.zcl.clusters.general import Basic, Identify, OnOff, Ota
53
+ from zigpy.zcl.clusters.general import Basic, Identify, MultistateInput, OnOff, Ota
53
54
  from zigpy.zcl.foundation import Status as ZCLStatus, ZCLAttributeDef
54
55
  from zigpy.zdo import types as zdo_t
55
56
  from zigpy.zgp.commands import GPGenericSwitchConfiguration
@@ -182,7 +183,8 @@ async def test_database(tmp_path):
182
183
  assert dev.get_signature()[SIG_MANUFACTURER] == "Custom"
183
184
  assert dev.get_signature()[SIG_MODEL] == "Model"
184
185
 
185
- ts = time.time()
186
+ # Updates within `MIN_UPDATE_DELTA` of the stored value aren't saved, so go past it
187
+ ts = time.time() + zigpy.appdb.MIN_UPDATE_DELTA + 1
186
188
  dev.last_seen = ts
187
189
  dev_last_seen = dev.last_seen
188
190
  assert isinstance(dev.last_seen, float)
@@ -1350,6 +1352,115 @@ async def test_attribute_reads_persist(tmp_path) -> None:
1350
1352
  await app2.shutdown()
1351
1353
 
1352
1354
 
1355
+ @pytest.mark.parametrize(
1356
+ ("value", "storable"),
1357
+ [
1358
+ (None, True),
1359
+ (t.uint8_t(1), True),
1360
+ (t.uint64_t(2**63 - 1), True),
1361
+ (t.uint64_t(2**63), False),
1362
+ (t.int64s(-(2**63)), True),
1363
+ (True, True),
1364
+ (zdo_t.LogicalType.Router, True),
1365
+ (t.Single(1.5), True),
1366
+ (t.CharacterString("text"), True),
1367
+ (t.LVBytes(b"\x01\x02"), True),
1368
+ (t.EUI64.convert("aa:bb:cc:dd:11:22:33:44"), True),
1369
+ (t.LVList[t.CharacterString, t.uint16_t](["a", "b"]), False),
1370
+ (t.KeyData(range(16)), False),
1371
+ (make_node_desc(), False),
1372
+ ],
1373
+ )
1374
+ def test_is_storable_value(value, storable) -> None:
1375
+ """Values SQLite cannot bind are detected."""
1376
+ zigpy.appdb._register_sqlite_adapters()
1377
+ assert zigpy.appdb._is_storable_value(value) is storable
1378
+
1379
+
1380
+ async def test_unstorable_attribute_values_not_persisted(tmp_path, caplog) -> None:
1381
+ """An attribute value that cannot be stored does not prevent saving the rest."""
1382
+
1383
+ db = tmp_path / "test.db"
1384
+ app = await make_app_with_db(db)
1385
+
1386
+ dev = app.add_device(nwk=0x1234, ieee=t.EUI64.convert("aa:bb:cc:dd:11:22:33:44"))
1387
+ dev.node_desc = make_node_desc(logical_type=zdo_t.LogicalType.Router)
1388
+
1389
+ for ep_id in (1, 2):
1390
+ ep = dev.add_endpoint(ep_id)
1391
+ ep.status = zigpy.endpoint.Status.ZDO_INIT
1392
+ ep.profile_id = 260
1393
+ ep.device_type = profiles.zha.DeviceType.ON_OFF_SWITCH
1394
+ ep.add_input_cluster(MultistateInput.cluster_id)
1395
+ ep.add_input_cluster(OnOff.cluster_id)
1396
+
1397
+ dev.model = "some model"
1398
+ dev.manufacturer = "some manufacturer"
1399
+
1400
+ # The device save stores the attribute cache in bulk
1401
+ multistate = dev.endpoints[1].multistate_input
1402
+ multistate.update_attribute(MultistateInput.AttributeDefs.present_value, 2)
1403
+ multistate.update_attribute(
1404
+ MultistateInput.AttributeDefs.state_text,
1405
+ t.LVList[t.CharacterString, t.uint16_t](["a", "b"]),
1406
+ )
1407
+ multistate.update_attribute(MultistateInput.AttributeDefs.number_of_states, 2)
1408
+ dev.endpoints[1].on_off.update_attribute(OnOff.AttributeDefs.on_off, 1)
1409
+ dev.endpoints[2].on_off.update_attribute(OnOff.AttributeDefs.on_off, 0)
1410
+
1411
+ with caplog.at_level(logging.DEBUG, logger="zigpy.appdb"):
1412
+ await dev.initialize()
1413
+ await app._dblistener._callback_handlers.join()
1414
+
1415
+ # Attribute events are stored individually
1416
+ dev = app.get_device(ieee=dev.ieee)
1417
+ multistate = dev.endpoints[1].multistate_input
1418
+
1419
+ with mock_attribute_reads(
1420
+ multistate,
1421
+ {
1422
+ MultistateInput.AttributeDefs.state_text: ["c", "d"],
1423
+ MultistateInput.AttributeDefs.description: "some description",
1424
+ },
1425
+ ):
1426
+ await multistate.read_attributes(
1427
+ [
1428
+ MultistateInput.AttributeDefs.state_text,
1429
+ MultistateInput.AttributeDefs.description,
1430
+ ]
1431
+ )
1432
+
1433
+ await app._dblistener._callback_handlers.join()
1434
+
1435
+ assert (
1436
+ "[aa:bb:cc:dd:11:22:33:44:1:0x0012] Not persisting attribute 0x000e"
1437
+ in caplog.text
1438
+ )
1439
+ assert "Not persisting attribute event" in caplog.text
1440
+ assert not any(
1441
+ isinstance(record.exc_info and record.exc_info[1], sqlite3.ProgrammingError)
1442
+ for record in caplog.records
1443
+ )
1444
+
1445
+ await app.shutdown()
1446
+
1447
+ app2 = await make_app_with_db(db)
1448
+ dev2 = app2.get_device(ieee=dev.ieee)
1449
+
1450
+ # Everything but the unstorable value is persisted, including the second endpoint
1451
+ multistate2 = dev2.endpoints[1].multistate_input
1452
+ assert multistate2.get_cached_value("present_value") == 2
1453
+ assert multistate2.get_cached_value("number_of_states") == 2
1454
+ assert multistate2.get_cached_value("description") == "some description"
1455
+ assert dev2.endpoints[1].on_off.get_cached_value("on_off") == 1
1456
+ assert dev2.endpoints[2].on_off.get_cached_value("on_off") == 0
1457
+
1458
+ with pytest.raises(KeyError):
1459
+ multistate2.get_cached_value("state_text")
1460
+
1461
+ await app2.shutdown()
1462
+
1463
+
1353
1464
  async def test_appdb_custom_device_subclass_round_trip(tmp_path) -> None:
1354
1465
  """A resolver returning a `Device` subclass round-trips."""
1355
1466
 
@@ -256,13 +256,13 @@ def test_cv_warn_if_greater(value, limit, should_warn):
256
256
 
257
257
  @pytest.mark.parametrize("value", [0, 0.5, 30, "60"])
258
258
  def test_schema_db_commit_interval(value):
259
- """Test `database_commit_interval` accepts finite, non-negative values."""
259
+ """Test `database_commit_interval` accepts non-negative values."""
260
260
  config = zigpy.config.ZIGPY_SCHEMA({zigpy.config.CONF_DB_COMMIT_INTERVAL: value})
261
261
  assert config[zigpy.config.CONF_DB_COMMIT_INTERVAL] == float(value)
262
262
 
263
263
 
264
- @pytest.mark.parametrize("value", [-1, "inf", float("inf"), "-inf", float("nan")])
264
+ @pytest.mark.parametrize("value", [-1, "-inf", float("nan")])
265
265
  def test_schema_db_commit_interval_invalid(value):
266
- """Test `database_commit_interval` rejects negative and non-finite values."""
266
+ """Test `database_commit_interval` rejects negative and NaN values."""
267
267
  with pytest.raises(vol.Invalid):
268
268
  zigpy.config.ZIGPY_SCHEMA({zigpy.config.CONF_DB_COMMIT_INTERVAL: value})
@@ -7,7 +7,12 @@ from unittest.mock import call
7
7
 
8
8
  import pytest
9
9
 
10
- from tests.conftest import make_node_desc, mock_attribute_reads
10
+ from tests.conftest import (
11
+ add_initialized_device,
12
+ make_ieee,
13
+ make_node_desc,
14
+ mock_attribute_reads,
15
+ )
11
16
  from zigpy import device, endpoint
12
17
  import zigpy.application
13
18
  from zigpy.datastructures import RequestLimiter
@@ -1795,6 +1800,7 @@ async def test_poll_control_checkin_callback(
1795
1800
  tsn=0x12,
1796
1801
  expect_reply=False,
1797
1802
  disable_default_response=True,
1803
+ retries=0,
1798
1804
  )
1799
1805
  ]
1800
1806
  else:
@@ -1805,10 +1811,87 @@ async def test_poll_control_checkin_callback(
1805
1811
  tsn=0x12,
1806
1812
  expect_reply=False,
1807
1813
  disable_default_response=True,
1814
+ retries=0,
1808
1815
  )
1809
1816
  ]
1810
1817
 
1811
1818
 
1819
+ @pytest.mark.parametrize(
1820
+ "exc",
1821
+ [
1822
+ zigpy.exceptions.DeliveryError("Failed to send request: APS_NO_ACK"),
1823
+ TimeoutError(),
1824
+ ],
1825
+ )
1826
+ async def test_poll_control_checkin_callback_send_failure(
1827
+ dev: device.Device, exc: Exception, caplog: pytest.LogCaptureFixture
1828
+ ) -> None:
1829
+ """Test a failed check-in response is logged at debug level and not raised."""
1830
+ ep = dev.add_endpoint(1)
1831
+ poll_control = ep.add_input_cluster(PollControl.cluster_id)
1832
+ poll_control.checkin_response = AsyncMock(side_effect=exc)
1833
+
1834
+ zcl_hdr = foundation.ZCLHeader.cluster(
1835
+ tsn=0x12, command_id=PollControl.ClientCommandDefs.checkin.id
1836
+ )
1837
+
1838
+ with caplog.at_level(logging.DEBUG):
1839
+ await dev.poll_control_checkin_callback(
1840
+ zcl_hdr, PollControl.ClientCommandDefs.checkin.schema()
1841
+ )
1842
+
1843
+ assert len(poll_control.checkin_response.mock_calls) == 1
1844
+ assert "Failed to send check-in response" in caplog.text
1845
+ assert not [r for r in caplog.records if r.levelno >= logging.WARNING]
1846
+
1847
+
1848
+ async def test_poll_control_checkin_response_not_retried(
1849
+ app_mock: zigpy.application.ControllerApplication,
1850
+ caplog: pytest.LogCaptureFixture,
1851
+ ) -> None:
1852
+ """Test a check-in response that fails to send is not retried."""
1853
+ dev = add_initialized_device(app_mock, nwk=0x1234, ieee=make_ieee(1))
1854
+ dev.node_desc = make_node_desc(logical_type=zdo_t.LogicalType.EndDevice)
1855
+ dev.endpoints[1].add_input_cluster(PollControl.cluster_id)
1856
+
1857
+ app_mock.send_packet = AsyncMock(
1858
+ side_effect=zigpy.exceptions.DeliveryError(
1859
+ "Failed to send request: APS_NO_ACK", status=0xB7
1860
+ )
1861
+ )
1862
+
1863
+ hdr = foundation.ZCLHeader.cluster(
1864
+ tsn=0x12, command_id=PollControl.ClientCommandDefs.checkin.id
1865
+ )
1866
+ hdr.frame_control = hdr.frame_control.replace(
1867
+ direction=foundation.Direction.Server_to_Client,
1868
+ disable_default_response=True,
1869
+ )
1870
+
1871
+ with caplog.at_level(logging.DEBUG):
1872
+ app_mock.packet_received(
1873
+ t.ZigbeePacket(
1874
+ src=t.AddrModeAddress(addr_mode=t.AddrMode.NWK, address=dev.nwk),
1875
+ src_ep=1,
1876
+ dst=t.AddrModeAddress(addr_mode=t.AddrMode.NWK, address=0x0000),
1877
+ dst_ep=1,
1878
+ tsn=0x12,
1879
+ profile_id=zha.PROFILE_ID,
1880
+ cluster_id=PollControl.cluster_id,
1881
+ data=t.SerializableBytes(hdr.serialize()),
1882
+ lqi=255,
1883
+ rssi=-30,
1884
+ )
1885
+ )
1886
+
1887
+ # Wait for the callback task to finish
1888
+ await asyncio.gather(*app_mock._tasks)
1889
+
1890
+ assert len(app_mock.send_packet.mock_calls) == 1
1891
+ assert "Failed to send check-in response" in caplog.text
1892
+ assert "callback failed" not in caplog.text
1893
+
1894
+
1812
1895
  async def test_begin_fast_polling_with_cluster(dev: device.Device) -> None:
1813
1896
  """Test beginning fast polling when PollControl cluster exists."""
1814
1897
  ep = dev.add_endpoint(1)
@@ -876,6 +876,30 @@ def test_struct_optional():
876
876
  )
877
877
 
878
878
 
879
+ def test_struct_optional_trailing_only():
880
+ class TestStruct(t.Struct):
881
+ foo: t.uint8_t
882
+ bar: t.uint8_t = t.StructField(optional=True)
883
+ baz: t.uint16_t = t.StructField(optional=True)
884
+ asd: t.uint8_t = t.StructField(requires=lambda s: s.foo == 1, optional=True)
885
+
886
+ # Optional fields can be omitted from the end
887
+ assert TestStruct(foo=0).serialize() == b"\x00"
888
+ assert TestStruct(foo=0, bar=1).serialize() == b"\x00\x01"
889
+ assert TestStruct(foo=0, bar=1, baz=2).serialize() == b"\x00\x01\x02\x00"
890
+ assert TestStruct.deserialize(b"\x00\x01") == (TestStruct(foo=0, bar=1), b"")
891
+
892
+ # A field that isn't required doesn't count as being set
893
+ assert TestStruct(foo=0, bar=1, asd=3).serialize() == b"\x00\x01"
894
+
895
+ # But not from the middle: `baz` would be deserialized as `bar`
896
+ with pytest.raises(ValueError, match="'bar' cannot be omitted when field 'baz'"):
897
+ TestStruct(foo=0, baz=2).serialize()
898
+
899
+ with pytest.raises(ValueError, match="'bar' cannot be omitted when field 'asd'"):
900
+ TestStruct(foo=1, asd=3).serialize()
901
+
902
+
879
903
  def test_struct_field_repr():
880
904
  class TestStruct(t.Struct):
881
905
  foo: t.uint8_t = t.StructField(repr=lambda v: v + 1)
@@ -421,6 +421,63 @@ def test_limited_char_string():
421
421
  t.LimitedCharString(5)("123456").serialize()
422
422
 
423
423
 
424
+ @pytest.mark.parametrize(
425
+ ("string_type", "prefix_length"),
426
+ [
427
+ (t.CharacterString, 1),
428
+ (t.LongCharacterString, 2),
429
+ (t.LimitedCharString(16), 1),
430
+ ],
431
+ )
432
+ def test_char_string_multibyte(string_type, prefix_length):
433
+ """The length prefix counts UTF-8 octets, not characters."""
434
+ text = "é1€😀" # 2 + 1 + 3 + 4 octets in 4 characters
435
+ encoded = b"\xc3\xa91\xe2\x82\xac\xf0\x9f\x98\x80"
436
+ serialized = (10).to_bytes(prefix_length, "little") + encoded
437
+
438
+ assert string_type(text).serialize() == serialized
439
+
440
+ d, rest = string_type.deserialize(serialized + b"rest")
441
+ assert d == text
442
+ assert d.raw == encoded
443
+ assert rest == b"rest"
444
+ assert d.serialize() == serialized
445
+
446
+
447
+ def test_char_string_multibyte_too_long():
448
+ # 127 characters, but 254 octets: the largest valid character string
449
+ assert len(t.CharacterString("é" * 127).serialize()) == 1 + 254
450
+
451
+ # 128 characters, but 256 octets
452
+ with pytest.raises(ValueError, match="too long"):
453
+ t.CharacterString("é" * 128).serialize()
454
+
455
+
456
+ def test_limited_char_string_multibyte():
457
+ # The limit is in characters (e.g. "0 to 16 characters" for scene names), while
458
+ # the length prefix counts octets
459
+ assert t.LimitedCharString(2)("éé").serialize() == b"\x04\xc3\xa9\xc3\xa9"
460
+
461
+ with pytest.raises(ValueError, match="too long"):
462
+ t.LimitedCharString(2)("ééé").serialize()
463
+
464
+
465
+ def test_long_char_string_invalid():
466
+ # The non-value of a long character string is 0xFFFF, not 0x00FF
467
+ r, rest = t.LongCharacterString.deserialize(b"\xff\xffabcd")
468
+ assert r == ""
469
+ assert r.invalid is True
470
+ assert rest == b"abcd"
471
+
472
+ assert r.serialize() == b"\xff\xff"
473
+
474
+ # A 255-octet long character string is a regular value
475
+ r, rest = t.LongCharacterString.deserialize(b"\xff\x00" + b"a" * 255 + b"rest")
476
+ assert r == "a" * 255
477
+ assert r.invalid is False
478
+ assert rest == b"rest"
479
+
480
+
424
481
  def test_lvlist():
425
482
  d, r = t.LVList[t.uint8_t, t.uint8_t].deserialize(b"\x0412345")
426
483
  assert r == b"5"
@@ -583,6 +583,8 @@ async def test_write_attributes_wrong_type(cluster):
583
583
  [
584
584
  (0, "zcl_version", 0xAA, b"\x00\x00\x20\xaa"),
585
585
  (0, "model", "model x", b"\x05\x00\x42\x07model x"),
586
+ # The length prefix counts octets, not characters
587
+ (0, "location_desc", "Küche", b"\x10\x00\x42\x06K\xc3\xbcche"),
586
588
  (0, "device_enabled", True, b"\x12\x00\x10\x01"),
587
589
  (0, "alarm_mask", 0x55, b"\x13\x00\x18\x55"),
588
590
  (0x0202, "fan_mode", 0xDE, b"\x00\x00\x30\xde"),
@@ -816,6 +818,18 @@ async def test_invalid_arguments_cluster_client_command(client_cluster):
816
818
  )
817
819
 
818
820
 
821
+ async def test_cluster_command_optional_field_gap(cluster_by_id):
822
+ """Test an omitted optional field can't be followed by a set one."""
823
+ cluster = cluster_by_id(0x0008)
824
+
825
+ # `options_override` would otherwise be deserialized as the `options_mask`
826
+ with pytest.raises(ValueError, match="'options_mask' cannot be omitted"):
827
+ await cluster.move_to_level(level=10, transition_time=0, options_override=1)
828
+
829
+ # Nothing is sent
830
+ assert cluster._endpoint.request.call_count == 0
831
+
832
+
819
833
  def test_name(cluster):
820
834
  assert cluster.name == "Basic"
821
835