From b214273fb57148243c842c4dd818f73ffcd9dacb Mon Sep 17 00:00:00 2001 From: zigpy-review-bot <286747149+zigpy-review-bot@users.noreply.github.com> Date: Tue, 24 Nov 2026 08:09:15 +0100 Subject: [PATCH] Clamp the siren duration to the Start warning field's range Warning Duration is a uint16 (ZCL R8 8.4.2.2.1.1), but Home Assistant's `siren.turn_on` accepts any non-negative duration. A duration above 65535 seconds failed to serialize with a ValueError, so the siren never turned on. Clamp it to 65535 seconds, for the command and the entity's off timer. --- tests/test_siren.py | 23 +++++++++++++++++++++++ zha/application/platforms/siren.py | 8 ++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/tests/test_siren.py b/tests/test_siren.py index 0f7f16239..b24c58b68 100644 --- a/tests/test_siren.py +++ b/tests/test_siren.py @@ -3,6 +3,7 @@ import asyncio from unittest.mock import patch +import pytest from zigpy.const import SIG_EP_PROFILE from zigpy.profiles import zha from zigpy.zcl.clusters import general, security @@ -262,3 +263,25 @@ async def test_siren_squawk(zha_gateway: Gateway) -> None: strobe=security.SquawkStrobe.Strobe, level=security.SquawkLevel.Very_high_level_sound, ) + + +@pytest.mark.parametrize("basic", [False, True]) +async def test_siren_duration_above_uint16(zha_gateway: Gateway, basic: bool) -> None: + """Test a duration beyond the command's uint16 field is clamped.""" + zha_device, cluster = await siren_mock(zha_gateway, basic=basic) + entity = get_entity(zha_device, platform=Platform.SIREN) + cluster.endpoint.request.reset_mock() + + # 24 hours is a valid duration for Home Assistant's `siren.turn_on`, the + # command is serialized for real here + await entity.async_turn_on(duration=86400) + assert entity.state.is_on is True + + data = cluster.endpoint.request.call_args.kwargs["data"] + hdr, payload = zcl_f.ZCLHeader.deserialize(data) + assert hdr.command_id == security.IasWd.ServerCommandDefs.start_warning.id + args, _ = security.IasWd.ServerCommandDefs.start_warning.schema.deserialize(payload) + assert args.warning_duration == 0xFFFF + + await asyncio.sleep(0xFFFF + 1) + assert entity.state.is_on is False diff --git a/zha/application/platforms/siren.py b/zha/application/platforms/siren.py index bdecae026..d3b59081a 100644 --- a/zha/application/platforms/siren.py +++ b/zha/application/platforms/siren.py @@ -39,6 +39,8 @@ DEFAULT_DURATION = 5 # seconds +# Start warning's Warning Duration: uint16, in seconds (ZCL R8 8.4.2.2.1.1, .5) +MAX_DURATION = 0xFFFF ATTR_AVAILABLE_TONES: Final[str] = "available_tones" ATTR_DURATION: Final[str] = "duration" @@ -292,7 +294,7 @@ async def async_turn_on( ) siren_duration = DEFAULT_DURATION if duration is not None: - siren_duration = duration + siren_duration = min(duration, MAX_DURATION) if tone is not None: siren_tone = tone if volume_level is not None: @@ -366,7 +368,9 @@ async def async_turn_on( ) -> None: """Turn on siren with fixed tone, level, and strobe.""" self._cancel_off_listener() - siren_duration = duration if duration is not None else DEFAULT_DURATION + siren_duration = ( + min(duration, MAX_DURATION) if duration is not None else DEFAULT_DURATION + ) warning = WarningType( # some Frient sensors send INVALID_VALUE for EMERGENCY