mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 12:47:08 +00:00
Update _TrackStateChangeFiltered entity listener with new entities (#64909)
This commit is contained in:
parent
55d3de3411
commit
037621b796
@ -536,6 +536,7 @@ class _TrackStateChangeFiltered:
|
|||||||
"""Handle removal / refresh of tracker init."""
|
"""Handle removal / refresh of tracker init."""
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
self._action = action
|
self._action = action
|
||||||
|
self._action_as_hassjob = HassJob(action)
|
||||||
self._listeners: dict[str, Callable[[], None]] = {}
|
self._listeners: dict[str, Callable[[], None]] = {}
|
||||||
self._last_track_states: TrackStates = track_states
|
self._last_track_states: TrackStates = track_states
|
||||||
|
|
||||||
@ -631,13 +632,21 @@ class _TrackStateChangeFiltered:
|
|||||||
self.hass, entities, self._action
|
self.hass, entities, self._action
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _state_added(self, event: Event) -> None:
|
||||||
|
self._cancel_listener(_ENTITIES_LISTENER)
|
||||||
|
self._setup_entities_listener(
|
||||||
|
self._last_track_states.domains, self._last_track_states.entities
|
||||||
|
)
|
||||||
|
self.hass.async_run_hass_job(self._action_as_hassjob, event)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _setup_domains_listener(self, domains: set[str]) -> None:
|
def _setup_domains_listener(self, domains: set[str]) -> None:
|
||||||
if not domains:
|
if not domains:
|
||||||
return
|
return
|
||||||
|
|
||||||
self._listeners[_DOMAINS_LISTENER] = async_track_state_added_domain(
|
self._listeners[_DOMAINS_LISTENER] = async_track_state_added_domain(
|
||||||
self.hass, domains, self._action
|
self.hass, domains, self._state_added
|
||||||
)
|
)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
|
@ -273,6 +273,64 @@ async def test_if_fires_on_zone_appear(hass, calls):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_if_fires_on_zone_appear_2(hass, calls):
|
||||||
|
"""Test for firing if entity appears in zone."""
|
||||||
|
assert await async_setup_component(
|
||||||
|
hass,
|
||||||
|
automation.DOMAIN,
|
||||||
|
{
|
||||||
|
automation.DOMAIN: {
|
||||||
|
"trigger": {
|
||||||
|
"platform": "geo_location",
|
||||||
|
"source": "test_source",
|
||||||
|
"zone": "zone.test",
|
||||||
|
"event": "enter",
|
||||||
|
},
|
||||||
|
"action": {
|
||||||
|
"service": "test.automation",
|
||||||
|
"data_template": {
|
||||||
|
"some": "{{ trigger.%s }}"
|
||||||
|
% "}} - {{ trigger.".join(
|
||||||
|
(
|
||||||
|
"platform",
|
||||||
|
"entity_id",
|
||||||
|
"from_state.state",
|
||||||
|
"to_state.state",
|
||||||
|
"zone.name",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
# Entity appears in zone without previously existing outside the zone.
|
||||||
|
context = Context()
|
||||||
|
hass.states.async_set(
|
||||||
|
"geo_location.entity",
|
||||||
|
"goodbye",
|
||||||
|
{"latitude": 32.881011, "longitude": -117.234758, "source": "test_source"},
|
||||||
|
context=context,
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
hass.states.async_set(
|
||||||
|
"geo_location.entity",
|
||||||
|
"hello",
|
||||||
|
{"latitude": 32.880586, "longitude": -117.237564, "source": "test_source"},
|
||||||
|
context=context,
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
assert len(calls) == 1
|
||||||
|
assert calls[0].context.parent_id == context.id
|
||||||
|
assert (
|
||||||
|
calls[0].data["some"]
|
||||||
|
== "geo_location - geo_location.entity - goodbye - hello - test"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def test_if_fires_on_zone_disappear(hass, calls):
|
async def test_if_fires_on_zone_disappear(hass, calls):
|
||||||
"""Test for firing if entity disappears from zone."""
|
"""Test for firing if entity disappears from zone."""
|
||||||
hass.states.async_set(
|
hass.states.async_set(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user