From b3deb476ef41e20e98774df3a585b9755db1bcf4 Mon Sep 17 00:00:00 2001 From: Mike Degatano Date: Wed, 30 Nov 2022 16:54:14 -0500 Subject: [PATCH] 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 --- homeassistant/helpers/template.py | 6 ++++-- tests/helpers/test_template.py | 8 ++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index 8d284f042c2..73e1400cedf 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -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: diff --git a/tests/helpers/test_template.py b/tests/helpers/test_template.py index 07463787a8b..e19daf00627 100644 --- a/tests/helpers/test_template.py +++ b/tests/helpers/test_template.py @@ -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."""