Improve warnings when a zone trigger is referencing a none-existing zone (#61763)

This commit is contained in:
Erik Montnemery 2021-12-14 20:14:50 +01:00 committed by GitHub
parent d9105b071a
commit cf09d1b604
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 0 deletions

View file

@ -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

View file

@ -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
)