Raise ConditionError for state errors (#46244)
parent
6a62ebb6a4
commit
f27066e773
|
@ -356,7 +356,12 @@ class BayesianBinarySensor(BinarySensorEntity):
|
|||
"""Return True if state conditions are met."""
|
||||
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
|
||||
def name(self):
|
||||
|
|
|
@ -314,11 +314,22 @@ def state(
|
|||
|
||||
Async friendly.
|
||||
"""
|
||||
if entity is None:
|
||||
raise ConditionError("No entity specified")
|
||||
|
||||
if isinstance(entity, str):
|
||||
entity_id = entity
|
||||
entity = hass.states.get(entity)
|
||||
|
||||
if entity is None or (attribute is not None and attribute not in entity.attributes):
|
||||
return False
|
||||
if entity is None:
|
||||
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)
|
||||
|
||||
|
|
|
@ -359,6 +359,37 @@ async def test_if_numeric_state_raises_on_unavailable(hass, caplog):
|
|||
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):
|
||||
"""Test with multiple entities in condition."""
|
||||
test = await condition.async_from_config(
|
||||
|
@ -466,7 +497,8 @@ async def test_state_attribute_boolean(hass):
|
|||
assert not test(hass)
|
||||
|
||||
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})
|
||||
assert test(hass)
|
||||
|
@ -567,7 +599,7 @@ async def test_numeric_state_raises(hass):
|
|||
},
|
||||
)
|
||||
|
||||
assert test(hass)
|
||||
test(hass)
|
||||
|
||||
# Unknown attribute
|
||||
with pytest.raises(ConditionError, match=r"Attribute .* does not exist"):
|
||||
|
|
Loading…
Reference in New Issue