Skip to content

Commit

Permalink
Make zone condition more robust by ignoring unavailable and unknown e…
Browse files Browse the repository at this point in the history
…ntities (home-assistant#72751)

* ignore entities with state unavailable or unknown

* test for unavailable entity
  • Loading branch information
exxamalte authored May 31, 2022
1 parent eda2be8 commit 638992f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
6 changes: 6 additions & 0 deletions homeassistant/helpers/condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,12 @@ def zone(
else:
entity_id = entity.entity_id

if entity.state in (
STATE_UNAVAILABLE,
STATE_UNKNOWN,
):
return False

latitude = entity.attributes.get(ATTR_LATITUDE)
longitude = entity.attributes.get(ATTR_LONGITUDE)

Expand Down
42 changes: 41 additions & 1 deletion tests/components/geo_location/test_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
import pytest

from homeassistant.components import automation, zone
from homeassistant.const import ATTR_ENTITY_ID, ENTITY_MATCH_ALL, SERVICE_TURN_OFF
from homeassistant.const import (
ATTR_ENTITY_ID,
ENTITY_MATCH_ALL,
SERVICE_TURN_OFF,
STATE_UNAVAILABLE,
)
from homeassistant.core import Context
from homeassistant.setup import async_setup_component

Expand Down Expand Up @@ -189,6 +194,41 @@ async def test_if_fires_on_zone_leave(hass, calls):
assert len(calls) == 1


async def test_if_fires_on_zone_leave_2(hass, calls):
"""Test for firing on zone leave for unavailable entity."""
hass.states.async_set(
"geo_location.entity",
"hello",
{"latitude": 32.880586, "longitude": -117.237564, "source": "test_source"},
)
await hass.async_block_till_done()

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"},
}
},
)

hass.states.async_set(
"geo_location.entity",
STATE_UNAVAILABLE,
{"source": "test_source"},
)
await hass.async_block_till_done()

assert len(calls) == 0


async def test_if_not_fires_for_leave_on_zone_enter(hass, calls):
"""Test for not firing on zone enter."""
hass.states.async_set(
Expand Down

0 comments on commit 638992f

Please sign in to comment.