diff --git a/homeassistant/components/geo_location/trigger.py b/homeassistant/components/geo_location/trigger.py index 3cdefccfeab..4410d39c0a6 100644 --- a/homeassistant/components/geo_location/trigger.py +++ b/homeassistant/components/geo_location/trigger.py @@ -1,4 +1,6 @@ """Offer geolocation automation rules.""" +import logging + import voluptuous as vol from homeassistant.components.geo_location import DOMAIN @@ -10,6 +12,8 @@ from homeassistant.helpers.event import TrackStates, async_track_state_change_fi # mypy: allow-untyped-defs, no-check-untyped-defs +_LOGGER = logging.getLogger(__name__) + EVENT_ENTER = "enter" EVENT_LEAVE = "leave" DEFAULT_EVENT = EVENT_ENTER @@ -49,6 +53,13 @@ async def async_attach_trigger(hass, config, action, automation_info): return zone_state = hass.states.get(zone_entity_id) + if zone_state is None: + _LOGGER.warning( + "Unable to execute automation %s: Zone %s not found", + automation_info["name"], + zone_entity_id, + ) + return from_match = ( condition.zone(hass, zone_state, from_state) if from_state else False diff --git a/tests/components/geo_location/test_trigger.py b/tests/components/geo_location/test_trigger.py index e40b134e657..bc74f01f6f1 100644 --- a/tests/components/geo_location/test_trigger.py +++ b/tests/components/geo_location/test_trigger.py @@ -1,4 +1,6 @@ """The tests for the geolocation trigger.""" +import logging + import pytest from homeassistant.components import automation, zone @@ -318,3 +320,46 @@ async def test_if_fires_on_zone_disappear(hass, calls): assert ( calls[0].data["some"] == "geo_location - geo_location.entity - hello - - test" ) + + +async def test_zone_undefined(hass, calls, caplog): + """Test for undefined zone.""" + hass.states.async_set( + "geo_location.entity", + "hello", + {"latitude": 32.880586, "longitude": -117.237564, "source": "test_source"}, + ) + await hass.async_block_till_done() + + caplog.set_level(logging.WARNING) + + zone_does_not_exist = "zone.does_not_exist" + assert await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "trigger": { + "platform": "geo_location", + "source": "test_source", + "zone": zone_does_not_exist, + "event": "leave", + }, + "action": {"service": "test.automation"}, + } + }, + ) + + hass.states.async_set( + "geo_location.entity", + "hello", + {"latitude": 32.881011, "longitude": -117.234758, "source": "test_source"}, + ) + await hass.async_block_till_done() + + assert len(calls) == 0 + + assert ( + f"Unable to execute automation automation 0: Zone {zone_does_not_exist} not found" + in caplog.text + )