Skip to content

Commit 1a1e9e9

Browse files
authored
Add test for combining state change and state report listeners (home-assistant#148721)
1 parent 254f766 commit 1a1e9e9

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

tests/helpers/test_event.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4946,6 +4946,37 @@ def single_run_callback(event: Event[EventStateReportedData]) -> None:
49464946
unsub()
49474947

49484948

4949+
async def test_async_track_state_report_change_event(hass: HomeAssistant) -> None:
4950+
"""Test listen for both state change and state report events."""
4951+
tracker_called: dict[str, list[str]] = {"light.bowl": [], "light.top": []}
4952+
4953+
@ha.callback
4954+
def on_state_change(event: Event[EventStateChangedData]) -> None:
4955+
new_state = event.data["new_state"].state
4956+
tracker_called[event.data["entity_id"]].append(new_state)
4957+
4958+
@ha.callback
4959+
def on_state_report(event: Event[EventStateReportedData]) -> None:
4960+
new_state = event.data["new_state"].state
4961+
tracker_called[event.data["entity_id"]].append(new_state)
4962+
4963+
async_track_state_change_event(hass, ["light.bowl", "light.top"], on_state_change)
4964+
async_track_state_report_event(hass, ["light.bowl", "light.top"], on_state_report)
4965+
entity_ids = ["light.bowl", "light.top"]
4966+
state_sequence = ["on", "on", "off", "off"]
4967+
for state in state_sequence:
4968+
for entity_id in entity_ids:
4969+
hass.states.async_set(entity_id, state)
4970+
await hass.async_block_till_done()
4971+
4972+
# The out-of-order is a result of state change listeners scheduled with
4973+
# loop.call_soon, whereas state report listeners are called immediately.
4974+
assert tracker_called == {
4975+
"light.bowl": ["on", "off", "on", "off"],
4976+
"light.top": ["on", "off", "on", "off"],
4977+
}
4978+
4979+
49494980
async def test_async_track_template_no_hass_deprecated(
49504981
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
49514982
) -> None:

0 commit comments

Comments
 (0)