Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions tests/test_siren.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
8 changes: 6 additions & 2 deletions zha/application/platforms/siren.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
Loading