Allow is_state to accept a list of values (#81877)

* Allow is_state to accept a list of values

* Slightly more efficient

* Fix typing of state

Co-authored-by: Franck Nijhof <git@frenck.dev>
pull/83029/head
Mike Degatano 2022-11-30 16:54:14 -05:00 committed by GitHub
parent e158546425
commit b3deb476ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 2 deletions

View File

@ -1368,10 +1368,12 @@ def distance(hass, *args):
)
def is_state(hass: HomeAssistant, entity_id: str, state: State) -> bool:
def is_state(hass: HomeAssistant, entity_id: str, state: str | list[str]) -> bool:
"""Test if a state is a specific value."""
state_obj = _get_state(hass, entity_id)
return state_obj is not None and state_obj.state == state
return state_obj is not None and (
state_obj.state == state or isinstance(state, list) and state_obj.state in state
)
def is_state_attr(hass: HomeAssistant, entity_id: str, name: str, value: Any) -> bool:

View File

@ -1389,6 +1389,14 @@ def test_is_state(hass: HomeAssistant) -> None:
)
assert tpl.async_render() == "test.object"
tpl = template.Template(
"""
{{ is_state("test.object", ["on", "off", "available"]) }}
""",
hass,
)
assert tpl.async_render() is True
def test_is_state_attr(hass: HomeAssistant) -> None:
"""Test is_state_attr method."""