Correctly detect is not home (#53279)

This commit is contained in:
Paulus Schoutsen 2021-07-21 10:45:07 -07:00 committed by GitHub
parent 217c625c9b
commit ba00c786b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 8 deletions

View file

@ -11,7 +11,6 @@ from homeassistant.const import (
CONF_ENTITY_ID,
CONF_TYPE,
STATE_HOME,
STATE_NOT_HOME,
)
from homeassistant.core import HomeAssistant, callback
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."""
if config_validation:
config = CONDITION_SCHEMA(config)
if config[CONF_TYPE] == "is_home":
state = STATE_HOME
else:
state = STATE_NOT_HOME
reverse = config[CONF_TYPE] == "is_not_home"
@callback
def test_is_state(hass: HomeAssistant, variables: TemplateVarsType) -> bool:
"""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

View file

@ -3,7 +3,7 @@ import pytest
import homeassistant.components.automation as automation
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.setup import async_setup_component
@ -119,7 +119,7 @@ async def test_if_state(hass, calls):
assert len(calls) == 1
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_event2")
await hass.async_block_till_done()