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

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