Add availability_template to Template Fan platform (#26511)

* Added availability_template to Template Fan platform

* Added to test for invalid values in availability_template

* fixed component ID in test

* Made availability_template redering erorr more concise

* Updated AVAILABILITY_TEMPLATE Rendering error

* Moved const to package Const.py

* Fix import order (pylint)

* Removed 'Magic' string

* Cleaned up const and compare lowercase result to 'true'

* reverted _available back to boolean

* Fixed tests (magic values and state checks)
This commit is contained in:
Gil Peeters 2019-09-28 21:59:40 +10:00 committed by Charles Garwood
parent 5c5f6a21af
commit 74196eaf8b
2 changed files with 108 additions and 1 deletions

View file

@ -33,6 +33,7 @@ from homeassistant.core import callback
from homeassistant.exceptions import TemplateError
from homeassistant.helpers.entity import async_generate_entity_id
from homeassistant.helpers.script import Script
from .const import CONF_AVAILABILITY_TEMPLATE
_LOGGER = logging.getLogger(__name__)
@ -58,6 +59,7 @@ FAN_SCHEMA = vol.Schema(
vol.Optional(CONF_SPEED_TEMPLATE): cv.template,
vol.Optional(CONF_OSCILLATING_TEMPLATE): cv.template,
vol.Optional(CONF_DIRECTION_TEMPLATE): cv.template,
vol.Optional(CONF_AVAILABILITY_TEMPLATE): cv.template,
vol.Required(CONF_ON_ACTION): cv.SCRIPT_SCHEMA,
vol.Required(CONF_OFF_ACTION): cv.SCRIPT_SCHEMA,
vol.Optional(CONF_SET_SPEED_ACTION): cv.SCRIPT_SCHEMA,
@ -86,6 +88,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
speed_template = device_config.get(CONF_SPEED_TEMPLATE)
oscillating_template = device_config.get(CONF_OSCILLATING_TEMPLATE)
direction_template = device_config.get(CONF_DIRECTION_TEMPLATE)
availability_template = device_config.get(CONF_AVAILABILITY_TEMPLATE)
on_action = device_config[CONF_ON_ACTION]
off_action = device_config[CONF_OFF_ACTION]
@ -103,6 +106,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
speed_template,
oscillating_template,
direction_template,
availability_template,
):
if template is None:
continue
@ -131,6 +135,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
speed_template,
oscillating_template,
direction_template,
availability_template,
on_action,
off_action,
set_speed_action,
@ -156,6 +161,7 @@ class TemplateFan(FanEntity):
speed_template,
oscillating_template,
direction_template,
availability_template,
on_action,
off_action,
set_speed_action,
@ -175,6 +181,8 @@ class TemplateFan(FanEntity):
self._speed_template = speed_template
self._oscillating_template = oscillating_template
self._direction_template = direction_template
self._availability_template = availability_template
self._available = True
self._supported_features = 0
self._on_script = Script(hass, on_action)
@ -207,6 +215,8 @@ class TemplateFan(FanEntity):
if self._direction_template:
self._direction_template.hass = self.hass
self._supported_features |= SUPPORT_DIRECTION
if self._availability_template:
self._availability_template.hass = self.hass
self._entities = entity_ids
# List of valid speeds
@ -252,6 +262,11 @@ class TemplateFan(FanEntity):
"""Return the polling state."""
return False
@property
def available(self):
"""Return availability of Device."""
return self._available
# pylint: disable=arguments-differ
async def async_turn_on(self, speed: str = None) -> None:
"""Turn on the fan."""
@ -422,3 +437,17 @@ class TemplateFan(FanEntity):
", ".join(_VALID_DIRECTIONS),
)
self._direction = None
# Update Availability if 'availability_template' is defined
if self._availability_template is not None:
try:
self._available = (
self._availability_template.async_render().lower() == "true"
)
except (TemplateError, ValueError) as ex:
_LOGGER.error(
"Could not render %s template %s: %s",
CONF_AVAILABILITY_TEMPLATE,
self._name,
ex,
)