diff --git a/homeassistant/components/zone/trigger.py b/homeassistant/components/zone/trigger.py index 373727e3f4d..a008a30007a 100644 --- a/homeassistant/components/zone/trigger.py +++ b/homeassistant/components/zone/trigger.py @@ -1,4 +1,6 @@ """Offer zone automation rules.""" +import logging + import voluptuous as vol from homeassistant.const import ( @@ -25,6 +27,8 @@ EVENT_ENTER = "enter" EVENT_LEAVE = "leave" DEFAULT_EVENT = EVENT_ENTER +_LOGGER = logging.getLogger(__name__) + _EVENT_DESCRIPTION = {EVENT_ENTER: "entering", EVENT_LEAVE: "leaving"} _TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend( @@ -76,6 +80,14 @@ async def async_attach_trigger( return zone_state = hass.states.get(zone_entity_id) + if not zone_state: + _LOGGER.warning( + "Automation '%s' is referencing non-existing zone '%s' in a zone trigger", + automation_info["name"], + zone_entity_id, + ) + return + from_match = condition.zone(hass, zone_state, from_s) if from_s else False to_match = condition.zone(hass, zone_state, to_s) if to_s else False diff --git a/tests/components/zone/test_trigger.py b/tests/components/zone/test_trigger.py index ee62dd5df3a..b742032f863 100644 --- a/tests/components/zone/test_trigger.py +++ b/tests/components/zone/test_trigger.py @@ -307,3 +307,49 @@ async def test_zone_condition(hass, calls): hass.bus.async_fire("test_event") await hass.async_block_till_done() assert len(calls) == 1 + + +async def test_unknown_zone(hass, calls, caplog): + """Test for firing on zone enter.""" + context = Context() + hass.states.async_set( + "test.entity", "hello", {"latitude": 32.881011, "longitude": -117.234758} + ) + await hass.async_block_till_done() + + assert await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "alias": "My Automation", + "trigger": { + "platform": "zone", + "entity_id": "test.entity", + "zone": "zone.no_such_zone", + "event": "enter", + }, + "action": { + "service": "test.automation", + }, + } + }, + ) + + assert ( + "Automation 'My Automation' is referencing non-existing zone 'zone.no_such_zone' in a zone trigger" + not in caplog.text + ) + + hass.states.async_set( + "test.entity", + "hello", + {"latitude": 32.880586, "longitude": -117.237564}, + context=context, + ) + await hass.async_block_till_done() + + assert ( + "Automation 'My Automation' is referencing non-existing zone 'zone.no_such_zone' in a zone trigger" + in caplog.text + )