Add test for combining state change and state report listeners (#148721)

This commit is contained in:
Erik Montnemery 2025-07-14 20:15:39 +02:00 committed by GitHub
parent 254f766357
commit 1a1e9e9f57
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -4946,6 +4946,37 @@ async def test_async_track_state_report_event(hass: HomeAssistant) -> None:
unsub() unsub()
async def test_async_track_state_report_change_event(hass: HomeAssistant) -> None:
"""Test listen for both state change and state report events."""
tracker_called: dict[str, list[str]] = {"light.bowl": [], "light.top": []}
@ha.callback
def on_state_change(event: Event[EventStateChangedData]) -> None:
new_state = event.data["new_state"].state
tracker_called[event.data["entity_id"]].append(new_state)
@ha.callback
def on_state_report(event: Event[EventStateReportedData]) -> None:
new_state = event.data["new_state"].state
tracker_called[event.data["entity_id"]].append(new_state)
async_track_state_change_event(hass, ["light.bowl", "light.top"], on_state_change)
async_track_state_report_event(hass, ["light.bowl", "light.top"], on_state_report)
entity_ids = ["light.bowl", "light.top"]
state_sequence = ["on", "on", "off", "off"]
for state in state_sequence:
for entity_id in entity_ids:
hass.states.async_set(entity_id, state)
await hass.async_block_till_done()
# The out-of-order is a result of state change listeners scheduled with
# loop.call_soon, whereas state report listeners are called immediately.
assert tracker_called == {
"light.bowl": ["on", "off", "on", "off"],
"light.top": ["on", "off", "on", "off"],
}
async def test_async_track_template_no_hass_deprecated( async def test_async_track_template_no_hass_deprecated(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None: ) -> None: