Raise ConditionError for state errors (#46244)

This commit is contained in:
Anders Melchiorsen 2021-02-09 09:46:36 +01:00 committed by GitHub
parent 6a62ebb6a4
commit f27066e773
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 5 deletions

View file

@ -356,7 +356,12 @@ class BayesianBinarySensor(BinarySensorEntity):
"""Return True if state conditions are met.""" """Return True if state conditions are met."""
entity = entity_observation["entity_id"] entity = entity_observation["entity_id"]
return condition.state(self.hass, entity, entity_observation.get("to_state")) try:
return condition.state(
self.hass, entity, entity_observation.get("to_state")
)
except ConditionError:
return False
@property @property
def name(self): def name(self):

View file

@ -314,11 +314,22 @@ def state(
Async friendly. Async friendly.
""" """
if entity is None:
raise ConditionError("No entity specified")
if isinstance(entity, str): if isinstance(entity, str):
entity_id = entity
entity = hass.states.get(entity) entity = hass.states.get(entity)
if entity is None or (attribute is not None and attribute not in entity.attributes): if entity is None:
return False raise ConditionError(f"Unknown entity {entity_id}")
else:
entity_id = entity.entity_id
if attribute is not None and attribute not in entity.attributes:
raise ConditionError(
f"Attribute '{attribute}' (of entity {entity_id}) does not exist"
)
assert isinstance(entity, State) assert isinstance(entity, State)

View file

@ -359,6 +359,37 @@ async def test_if_numeric_state_raises_on_unavailable(hass, caplog):
assert len(caplog.record_tuples) == 0 assert len(caplog.record_tuples) == 0
async def test_state_raises(hass):
"""Test that state raises ConditionError on errors."""
# Unknown entity_id
with pytest.raises(ConditionError, match="Unknown entity"):
test = await condition.async_from_config(
hass,
{
"condition": "state",
"entity_id": "sensor.door_unknown",
"state": "open",
},
)
test(hass)
# Unknown attribute
with pytest.raises(ConditionError, match=r"Attribute .* does not exist"):
test = await condition.async_from_config(
hass,
{
"condition": "state",
"entity_id": "sensor.door",
"attribute": "model",
"state": "acme",
},
)
hass.states.async_set("sensor.door", "open")
test(hass)
async def test_state_multiple_entities(hass): async def test_state_multiple_entities(hass):
"""Test with multiple entities in condition.""" """Test with multiple entities in condition."""
test = await condition.async_from_config( test = await condition.async_from_config(
@ -466,7 +497,8 @@ async def test_state_attribute_boolean(hass):
assert not test(hass) assert not test(hass)
hass.states.async_set("sensor.temperature", 100, {"no_happening": 201}) hass.states.async_set("sensor.temperature", 100, {"no_happening": 201})
assert not test(hass) with pytest.raises(ConditionError):
test(hass)
hass.states.async_set("sensor.temperature", 100, {"happening": False}) hass.states.async_set("sensor.temperature", 100, {"happening": False})
assert test(hass) assert test(hass)
@ -567,7 +599,7 @@ async def test_numeric_state_raises(hass):
}, },
) )
assert test(hass) test(hass)
# Unknown attribute # Unknown attribute
with pytest.raises(ConditionError, match=r"Attribute .* does not exist"): with pytest.raises(ConditionError, match=r"Attribute .* does not exist"):