Support unavailable state in template fan (#38114)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Thomas Delaet 2020-07-24 15:48:07 +02:00 committed by GitHub
parent 5d28e109e8
commit 3ff5c17009
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 4 deletions

View file

@ -26,6 +26,7 @@ from homeassistant.const import (
MATCH_ALL, MATCH_ALL,
STATE_OFF, STATE_OFF,
STATE_ON, STATE_ON,
STATE_UNAVAILABLE,
STATE_UNKNOWN, STATE_UNKNOWN,
) )
from homeassistant.core import callback from homeassistant.core import callback
@ -344,7 +345,7 @@ class TemplateFan(FanEntity):
# Validate state # Validate state
if state in _VALID_STATES: if state in _VALID_STATES:
self._state = state self._state = state
elif state == STATE_UNKNOWN: elif state in [STATE_UNAVAILABLE, STATE_UNKNOWN]:
self._state = None self._state = None
else: else:
_LOGGER.error( _LOGGER.error(
@ -366,7 +367,7 @@ class TemplateFan(FanEntity):
# Validate speed # Validate speed
if speed in self._speed_list: if speed in self._speed_list:
self._speed = speed self._speed = speed
elif speed == STATE_UNKNOWN: elif speed in [STATE_UNAVAILABLE, STATE_UNKNOWN]:
self._speed = None self._speed = None
else: else:
_LOGGER.error( _LOGGER.error(
@ -388,7 +389,7 @@ class TemplateFan(FanEntity):
self._oscillating = True self._oscillating = True
elif oscillating == "False" or oscillating is False: elif oscillating == "False" or oscillating is False:
self._oscillating = False self._oscillating = False
elif oscillating == STATE_UNKNOWN: elif oscillating in [STATE_UNAVAILABLE, STATE_UNKNOWN]:
self._oscillating = None self._oscillating = None
else: else:
_LOGGER.error( _LOGGER.error(
@ -409,7 +410,7 @@ class TemplateFan(FanEntity):
# Validate speed # Validate speed
if direction in _VALID_DIRECTIONS: if direction in _VALID_DIRECTIONS:
self._direction = direction self._direction = direction
elif direction == STATE_UNKNOWN: elif direction in [STATE_UNAVAILABLE, STATE_UNKNOWN]:
self._direction = None self._direction = None
else: else:
_LOGGER.error( _LOGGER.error(

View file

@ -222,6 +222,64 @@ async def test_templates_with_entities(hass, calls):
_verify(hass, STATE_ON, SPEED_MEDIUM, True, DIRECTION_FORWARD) _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): async def test_availability_template_with_entities(hass, calls):
"""Test availability tempalates with values from other entities.""" """Test availability tempalates with values from other entities."""