Add availability_template to Template Binary Sensor platform (#26510)
* Added availability_template to Template Binary Sensor platform * Added to test for invalid values in availability_template * black * simplified exception handler * Updated AVAILABILITY_TEMPLATE Rendering error * Moved const to package Const.py * Fix import order (pylint) * Moved availability_template rendering to common loop * 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:
1 parent
6d773198a1
commit
5c5f6a21af
2 files changed
+111
-7
No files matched your search
@@ -26,6 +26,7 @@ from homeassistant.exceptions import TemplateError
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.entity import async_generate_entity_id
|
||||
from homeassistant.helpers.event import async_track_state_change, async_track_same_state
|
||||
from .const import CONF_AVAILABILITY_TEMPLATE
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@@ -38,6 +39,7 @@ SENSOR_SCHEMA = vol.Schema(
|
||||
vol.Required(CONF_VALUE_TEMPLATE): cv.template,
|
||||
vol.Optional(CONF_ICON_TEMPLATE): cv.template,
|
||||
vol.Optional(CONF_ENTITY_PICTURE_TEMPLATE): cv.template,
|
||||
vol.Optional(CONF_AVAILABILITY_TEMPLATE): cv.template,
|
||||
vol.Optional(CONF_ATTRIBUTE_TEMPLATES): vol.Schema({cv.string: cv.template}),
|
||||
vol.Optional(ATTR_FRIENDLY_NAME): cv.string,
|
||||
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
|
||||
@@ -60,6 +62,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
||||
value_template = device_config[CONF_VALUE_TEMPLATE]
|
||||
icon_template = device_config.get(CONF_ICON_TEMPLATE)
|
||||
entity_picture_template = device_config.get(CONF_ENTITY_PICTURE_TEMPLATE)
|
||||
availability_template = device_config.get(CONF_AVAILABILITY_TEMPLATE)
|
||||
entity_ids = set()
|
||||
manual_entity_ids = device_config.get(ATTR_ENTITY_ID)
|
||||
attribute_templates = device_config.get(CONF_ATTRIBUTE_TEMPLATES, {})
|
||||
@@ -70,6 +73,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
||||
CONF_VALUE_TEMPLATE: value_template,
|
||||
CONF_ICON_TEMPLATE: icon_template,
|
||||
CONF_ENTITY_PICTURE_TEMPLATE: entity_picture_template,
|
||||
CONF_AVAILABILITY_TEMPLATE: availability_template,
|
||||
}
|
||||
|
||||
for tpl_name, template in chain(templates.items(), attribute_templates.items()):
|
||||
@@ -117,6 +121,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
||||
value_template,
|
||||
icon_template,
|
||||
entity_picture_template,
|
||||
availability_template,
|
||||
entity_ids,
|
||||
delay_on,
|
||||
delay_off,
|
||||
@@ -143,6 +148,7 @@ class BinarySensorTemplate(BinarySensorDevice):
|
||||
value_template,
|
||||
icon_template,
|
||||
entity_picture_template,
|
||||
availability_template,
|
||||
entity_ids,
|
||||
delay_on,
|
||||
delay_off,
|
||||
@@ -156,12 +162,14 @@ class BinarySensorTemplate(BinarySensorDevice):
|
||||
self._template = value_template
|
||||
self._state = None
|
||||
self._icon_template = icon_template
|
||||
self._availability_template = availability_template
|
||||
self._entity_picture_template = entity_picture_template
|
||||
self._icon = None
|
||||
self._entity_picture = None
|
||||
self._entities = entity_ids
|
||||
self._delay_on = delay_on
|
||||
self._delay_off = delay_off
|
||||
self._available = True
|
||||
self._attribute_templates = attribute_templates
|
||||
self._attributes = {}
|
||||
|
||||
@@ -223,6 +231,11 @@ class BinarySensorTemplate(BinarySensorDevice):
|
||||
"""No polling needed."""
|
||||
return False
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Availability indicator."""
|
||||
return self._available
|
||||
|
||||
@callback
|
||||
def _async_render(self):
|
||||
"""Get the state of template."""
|
||||
@@ -240,11 +253,6 @@ class BinarySensorTemplate(BinarySensorDevice):
|
||||
return
|
||||
_LOGGER.error("Could not render template %s: %s", self._name, ex)
|
||||
|
||||
templates = {
|
||||
"_icon": self._icon_template,
|
||||
"_entity_picture": self._entity_picture_template,
|
||||
}
|
||||
|
||||
attrs = {}
|
||||
if self._attribute_templates is not None:
|
||||
for key, value in self._attribute_templates.items():
|
||||
@@ -254,12 +262,21 @@ class BinarySensorTemplate(BinarySensorDevice):
|
||||
_LOGGER.error("Error rendering attribute %s: %s", key, err)
|
||||
self._attributes = attrs
|
||||
|
||||
templates = {
|
||||
"_icon": self._icon_template,
|
||||
"_entity_picture": self._entity_picture_template,
|
||||
"_available": self._availability_template,
|
||||
}
|
||||
|
||||
for property_name, template in templates.items():
|
||||
if template is None:
|
||||
continue
|
||||
|
||||
try:
|
||||
setattr(self, property_name, template.async_render())
|
||||
value = template.async_render()
|
||||
if property_name == "_available":
|
||||
value = value.lower() == "true"
|
||||
setattr(self, property_name, value)
|
||||
except TemplateError as ex:
|
||||
friendly_property_name = property_name[1:].replace("_", " ")
|
||||
if ex.args and ex.args[0].startswith(
|
||||
|
||||
Reference in new issue
Block a user