Improve log message when zone missing in geolocation trigger (#51522)

* log warning message if zone cannot be found

* improve log message

* add test case
pull/51542/head
Malte Franken 2021-06-07 04:03:56 +10:00 committed by GitHub
parent f221deef2d
commit fcb8ab23ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 0 deletions

View File

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

View File

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