From 3ff5c170096d7f257c36b82d67d79a55f5b12419 Mon Sep 17 00:00:00 2001 From: Thomas Delaet Date: Fri, 24 Jul 2020 15:48:07 +0200 Subject: [PATCH] Support unavailable state in template fan (#38114) Co-authored-by: Martin Hjelmare --- homeassistant/components/template/fan.py | 9 ++-- tests/components/template/test_fan.py | 58 ++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/template/fan.py b/homeassistant/components/template/fan.py index 037cc6c40e1..1f5c433bf89 100644 --- a/homeassistant/components/template/fan.py +++ b/homeassistant/components/template/fan.py @@ -26,6 +26,7 @@ from homeassistant.const import ( MATCH_ALL, STATE_OFF, STATE_ON, + STATE_UNAVAILABLE, STATE_UNKNOWN, ) from homeassistant.core import callback @@ -344,7 +345,7 @@ class TemplateFan(FanEntity): # Validate state if state in _VALID_STATES: self._state = state - elif state == STATE_UNKNOWN: + elif state in [STATE_UNAVAILABLE, STATE_UNKNOWN]: self._state = None else: _LOGGER.error( @@ -366,7 +367,7 @@ class TemplateFan(FanEntity): # Validate speed if speed in self._speed_list: self._speed = speed - elif speed == STATE_UNKNOWN: + elif speed in [STATE_UNAVAILABLE, STATE_UNKNOWN]: self._speed = None else: _LOGGER.error( @@ -388,7 +389,7 @@ class TemplateFan(FanEntity): self._oscillating = True elif oscillating == "False" or oscillating is False: self._oscillating = False - elif oscillating == STATE_UNKNOWN: + elif oscillating in [STATE_UNAVAILABLE, STATE_UNKNOWN]: self._oscillating = None else: _LOGGER.error( @@ -409,7 +410,7 @@ class TemplateFan(FanEntity): # Validate speed if direction in _VALID_DIRECTIONS: self._direction = direction - elif direction == STATE_UNKNOWN: + elif direction in [STATE_UNAVAILABLE, STATE_UNKNOWN]: self._direction = None else: _LOGGER.error( diff --git a/tests/components/template/test_fan.py b/tests/components/template/test_fan.py index 2e44ec6f0ca..58fa80c10d5 100644 --- a/tests/components/template/test_fan.py +++ b/tests/components/template/test_fan.py @@ -222,6 +222,64 @@ async def test_templates_with_entities(hass, calls): _verify(hass, STATE_ON, SPEED_MEDIUM, True, DIRECTION_FORWARD) +async def test_template_with_unavailable_entities(hass, calls): + """Test unavailability with value_template.""" + + with assert_setup_component(1, "fan"): + assert await setup.async_setup_component( + hass, + "fan", + { + "fan": { + "platform": "template", + "fans": { + "test_fan": { + "value_template": "{{ 'unavailable' }}", + "turn_on": {"service": "script.fan_on"}, + "turn_off": {"service": "script.fan_off"}, + } + }, + } + }, + ) + + await hass.async_block_till_done() + await hass.async_start() + await hass.async_block_till_done() + assert hass.states.get(_TEST_FAN).state == STATE_OFF + + +async def test_template_with_unavailable_parameters(hass, calls): + """Test unavailability of speed, direction and oscillating parameters.""" + + with assert_setup_component(1, "fan"): + assert await setup.async_setup_component( + hass, + "fan", + { + "fan": { + "platform": "template", + "fans": { + "test_fan": { + "value_template": "{{ 'on' }}", + "speed_template": "{{ 'unavailable' }}", + "oscillating_template": "{{ 'unavailable' }}", + "direction_template": "{{ 'unavailable' }}", + "turn_on": {"service": "script.fan_on"}, + "turn_off": {"service": "script.fan_off"}, + } + }, + } + }, + ) + + await hass.async_block_till_done() + await hass.async_start() + await hass.async_block_till_done() + + _verify(hass, STATE_ON, None, None, None) + + async def test_availability_template_with_entities(hass, calls): """Test availability tempalates with values from other entities."""