Correctly detect is not home (#53279)

This commit is contained in:
Paulus Schoutsen 2021-07-21 10:45:07 -07:00
parent a0411aab4b
commit df72eb7ebb
2 changed files with 8 additions and 8 deletions

View file

@ -11,7 +11,6 @@ from homeassistant.const import (
CONF_ENTITY_ID, CONF_ENTITY_ID,
CONF_TYPE, CONF_TYPE,
STATE_HOME, STATE_HOME,
STATE_NOT_HOME,
) )
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import condition, config_validation as cv, entity_registry from homeassistant.helpers import condition, config_validation as cv, entity_registry
@ -62,14 +61,15 @@ def async_condition_from_config(
"""Create a function to test a device condition.""" """Create a function to test a device condition."""
if config_validation: if config_validation:
config = CONDITION_SCHEMA(config) config = CONDITION_SCHEMA(config)
if config[CONF_TYPE] == "is_home":
state = STATE_HOME reverse = config[CONF_TYPE] == "is_not_home"
else:
state = STATE_NOT_HOME
@callback @callback
def test_is_state(hass: HomeAssistant, variables: TemplateVarsType) -> bool: def test_is_state(hass: HomeAssistant, variables: TemplateVarsType) -> bool:
"""Test if an entity is a certain state.""" """Test if an entity is a certain state."""
return condition.state(hass, config[ATTR_ENTITY_ID], state) result = condition.state(hass, config[ATTR_ENTITY_ID], STATE_HOME)
if reverse:
result = not result
return result
return test_is_state return test_is_state

View file

@ -3,7 +3,7 @@ import pytest
import homeassistant.components.automation as automation import homeassistant.components.automation as automation
from homeassistant.components.device_tracker import DOMAIN from homeassistant.components.device_tracker import DOMAIN
from homeassistant.const import STATE_HOME, STATE_NOT_HOME from homeassistant.const import STATE_HOME
from homeassistant.helpers import device_registry from homeassistant.helpers import device_registry
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
@ -119,7 +119,7 @@ async def test_if_state(hass, calls):
assert len(calls) == 1 assert len(calls) == 1
assert calls[0].data["some"] == "is_home - event - test_event1" assert calls[0].data["some"] == "is_home - event - test_event1"
hass.states.async_set("device_tracker.entity", STATE_NOT_HOME) hass.states.async_set("device_tracker.entity", "school")
hass.bus.async_fire("test_event1") hass.bus.async_fire("test_event1")
hass.bus.async_fire("test_event2") hass.bus.async_fire("test_event2")
await hass.async_block_till_done() await hass.async_block_till_done()