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
parent
e158546425
commit
b3deb476ef
|
@ -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:
|
||||
|
|
|
@ -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."""
|
||||
|
|
Loading…
Reference in New Issue