2019-04-03 15:40:03 +00:00
|
|
|
"""Allows the creation of a sensor that breaks out state_attributes."""
|
2016-01-21 16:31:23 +00:00
|
|
|
import logging
|
2018-05-01 18:32:44 +00:00
|
|
|
from typing import Optional
|
2019-09-01 16:12:55 +00:00
|
|
|
from itertools import chain
|
2016-09-08 14:26:54 +00:00
|
|
|
|
2016-08-22 08:11:16 +00:00
|
|
|
import voluptuous as vol
|
2016-01-21 16:31:23 +00:00
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
from homeassistant.core import callback
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
ENTITY_ID_FORMAT,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
DEVICE_CLASSES_SCHEMA,
|
|
|
|
)
|
2016-01-21 16:31:23 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_FRIENDLY_NAME,
|
|
|
|
ATTR_UNIT_OF_MEASUREMENT,
|
|
|
|
CONF_VALUE_TEMPLATE,
|
|
|
|
CONF_ICON_TEMPLATE,
|
|
|
|
CONF_ENTITY_PICTURE_TEMPLATE,
|
|
|
|
ATTR_ENTITY_ID,
|
|
|
|
CONF_SENSORS,
|
|
|
|
EVENT_HOMEASSISTANT_START,
|
|
|
|
CONF_FRIENDLY_NAME_TEMPLATE,
|
|
|
|
MATCH_ALL,
|
|
|
|
CONF_DEVICE_CLASS,
|
|
|
|
)
|
2019-09-24 14:05:19 +00:00
|
|
|
|
2016-01-21 23:17:19 +00:00
|
|
|
from homeassistant.exceptions import TemplateError
|
2017-03-02 05:38:19 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-10-17 05:00:55 +00:00
|
|
|
from homeassistant.helpers.entity import Entity, async_generate_entity_id
|
|
|
|
from homeassistant.helpers.event import async_track_state_change
|
2019-09-24 14:05:19 +00:00
|
|
|
from .const import CONF_AVAILABILITY_TEMPLATE
|
2016-02-01 17:45:18 +00:00
|
|
|
|
2019-09-01 16:12:55 +00:00
|
|
|
CONF_ATTRIBUTE_TEMPLATES = "attribute_templates"
|
|
|
|
|
2016-01-21 16:31:23 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-01-22 16:30:02 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
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_FRIENDLY_NAME_TEMPLATE): cv.template,
|
2019-09-24 14:05:19 +00:00
|
|
|
vol.Optional(CONF_AVAILABILITY_TEMPLATE): cv.template,
|
2019-09-01 16:12:55 +00:00
|
|
|
vol.Optional(CONF_ATTRIBUTE_TEMPLATES, default={}): vol.Schema(
|
|
|
|
{cv.string: cv.template}
|
|
|
|
),
|
2019-07-31 19:25:30 +00:00
|
|
|
vol.Optional(ATTR_FRIENDLY_NAME): cv.string,
|
|
|
|
vol.Optional(ATTR_UNIT_OF_MEASUREMENT): cv.string,
|
|
|
|
vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
|
|
|
|
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{vol.Required(CONF_SENSORS): cv.schema_with_slug_keys(SENSOR_SCHEMA)}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the template sensors."""
|
2016-01-21 16:31:23 +00:00
|
|
|
sensors = []
|
|
|
|
|
2016-01-21 23:17:19 +00:00
|
|
|
for device, device_config in config[CONF_SENSORS].items():
|
2016-08-23 06:56:39 +00:00
|
|
|
state_template = device_config[CONF_VALUE_TEMPLATE]
|
2017-02-07 09:51:44 +00:00
|
|
|
icon_template = device_config.get(CONF_ICON_TEMPLATE)
|
2019-07-31 19:25:30 +00:00
|
|
|
entity_picture_template = device_config.get(CONF_ENTITY_PICTURE_TEMPLATE)
|
2019-09-24 14:05:19 +00:00
|
|
|
availability_template = device_config.get(CONF_AVAILABILITY_TEMPLATE)
|
2016-01-21 16:31:23 +00:00
|
|
|
friendly_name = device_config.get(ATTR_FRIENDLY_NAME, device)
|
2018-02-11 20:12:30 +00:00
|
|
|
friendly_name_template = device_config.get(CONF_FRIENDLY_NAME_TEMPLATE)
|
2016-01-21 16:31:23 +00:00
|
|
|
unit_of_measurement = device_config.get(ATTR_UNIT_OF_MEASUREMENT)
|
2018-05-01 18:32:44 +00:00
|
|
|
device_class = device_config.get(CONF_DEVICE_CLASS)
|
2019-09-01 16:12:55 +00:00
|
|
|
attribute_templates = device_config[CONF_ATTRIBUTE_TEMPLATES]
|
2016-05-29 21:34:21 +00:00
|
|
|
|
2018-03-11 04:45:32 +00:00
|
|
|
entity_ids = set()
|
|
|
|
manual_entity_ids = device_config.get(ATTR_ENTITY_ID)
|
2018-10-10 11:49:15 +00:00
|
|
|
invalid_templates = []
|
|
|
|
|
2019-09-01 16:12:55 +00:00
|
|
|
templates = {
|
|
|
|
CONF_VALUE_TEMPLATE: state_template,
|
|
|
|
CONF_ICON_TEMPLATE: icon_template,
|
|
|
|
CONF_ENTITY_PICTURE_TEMPLATE: entity_picture_template,
|
|
|
|
CONF_FRIENDLY_NAME_TEMPLATE: friendly_name_template,
|
2019-09-24 14:05:19 +00:00
|
|
|
CONF_AVAILABILITY_TEMPLATE: availability_template,
|
2019-09-01 16:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for tpl_name, template in chain(templates.items(), attribute_templates.items()):
|
2018-03-11 04:45:32 +00:00
|
|
|
if template is None:
|
|
|
|
continue
|
|
|
|
template.hass = hass
|
2017-02-07 09:51:44 +00:00
|
|
|
|
2018-10-10 11:49:15 +00:00
|
|
|
if manual_entity_ids is not None:
|
2018-03-11 04:45:32 +00:00
|
|
|
continue
|
2017-10-30 16:28:37 +00:00
|
|
|
|
2018-03-11 04:45:32 +00:00
|
|
|
template_entity_ids = template.extract_entities()
|
|
|
|
if template_entity_ids == MATCH_ALL:
|
|
|
|
entity_ids = MATCH_ALL
|
2018-10-10 11:49:15 +00:00
|
|
|
# Cut off _template from name
|
2019-09-01 16:12:55 +00:00
|
|
|
invalid_templates.append(tpl_name.replace("_template", ""))
|
2018-10-10 11:49:15 +00:00
|
|
|
elif entity_ids != MATCH_ALL:
|
2018-03-11 04:45:32 +00:00
|
|
|
entity_ids |= set(template_entity_ids)
|
|
|
|
|
2018-10-10 11:49:15 +00:00
|
|
|
if invalid_templates:
|
|
|
|
_LOGGER.warning(
|
2019-07-31 19:25:30 +00:00
|
|
|
"Template sensor %s has no entity ids configured to track nor"
|
|
|
|
" were we able to extract the entities to track from the %s "
|
|
|
|
"template(s). This entity will only be able to be updated "
|
|
|
|
"manually.",
|
|
|
|
device,
|
|
|
|
", ".join(invalid_templates),
|
|
|
|
)
|
2018-10-10 11:49:15 +00:00
|
|
|
|
2018-03-11 04:45:32 +00:00
|
|
|
if manual_entity_ids is not None:
|
|
|
|
entity_ids = manual_entity_ids
|
|
|
|
elif entity_ids != MATCH_ALL:
|
|
|
|
entity_ids = list(entity_ids)
|
2018-02-11 20:12:30 +00:00
|
|
|
|
2016-01-21 16:31:23 +00:00
|
|
|
sensors.append(
|
|
|
|
SensorTemplate(
|
|
|
|
hass,
|
2016-02-01 17:45:18 +00:00
|
|
|
device,
|
2016-01-21 16:31:23 +00:00
|
|
|
friendly_name,
|
2018-02-11 20:12:30 +00:00
|
|
|
friendly_name_template,
|
2016-01-21 16:31:23 +00:00
|
|
|
unit_of_measurement,
|
2016-05-29 21:34:21 +00:00
|
|
|
state_template,
|
2017-02-07 09:51:44 +00:00
|
|
|
icon_template,
|
2017-10-30 16:28:37 +00:00
|
|
|
entity_picture_template,
|
2019-09-24 14:05:19 +00:00
|
|
|
availability_template,
|
2018-05-01 18:32:44 +00:00
|
|
|
entity_ids,
|
2019-07-31 19:25:30 +00:00
|
|
|
device_class,
|
2019-09-01 16:12:55 +00:00
|
|
|
attribute_templates,
|
2016-01-21 16:31:23 +00:00
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(sensors)
|
2016-01-21 16:31:23 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class SensorTemplate(Entity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Representation of a Template Sensor."""
|
2016-01-21 16:31:23 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
hass,
|
|
|
|
device_id,
|
|
|
|
friendly_name,
|
|
|
|
friendly_name_template,
|
|
|
|
unit_of_measurement,
|
|
|
|
state_template,
|
|
|
|
icon_template,
|
|
|
|
entity_picture_template,
|
2019-09-24 14:05:19 +00:00
|
|
|
availability_template,
|
2019-07-31 19:25:30 +00:00
|
|
|
entity_ids,
|
|
|
|
device_class,
|
2019-09-01 16:12:55 +00:00
|
|
|
attribute_templates,
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2016-03-25 06:07:19 +00:00
|
|
|
self.hass = hass
|
2019-07-31 19:25:30 +00:00
|
|
|
self.entity_id = async_generate_entity_id(
|
|
|
|
ENTITY_ID_FORMAT, device_id, hass=hass
|
|
|
|
)
|
2016-01-21 23:17:19 +00:00
|
|
|
self._name = friendly_name
|
2018-02-11 20:12:30 +00:00
|
|
|
self._friendly_name_template = friendly_name_template
|
2016-01-21 16:31:23 +00:00
|
|
|
self._unit_of_measurement = unit_of_measurement
|
2016-09-28 04:29:55 +00:00
|
|
|
self._template = state_template
|
2016-03-25 06:07:19 +00:00
|
|
|
self._state = None
|
2017-02-07 09:51:44 +00:00
|
|
|
self._icon_template = icon_template
|
2017-10-30 16:28:37 +00:00
|
|
|
self._entity_picture_template = entity_picture_template
|
2019-09-24 14:05:19 +00:00
|
|
|
self._availability_template = availability_template
|
2017-02-07 09:51:44 +00:00
|
|
|
self._icon = None
|
2017-10-30 16:28:37 +00:00
|
|
|
self._entity_picture = None
|
2017-03-02 05:38:19 +00:00
|
|
|
self._entities = entity_ids
|
2018-05-01 18:32:44 +00:00
|
|
|
self._device_class = device_class
|
2019-09-24 14:05:19 +00:00
|
|
|
self._available = True
|
2019-09-01 16:12:55 +00:00
|
|
|
self._attribute_templates = attribute_templates
|
|
|
|
self._attributes = {}
|
2017-03-02 05:38:19 +00:00
|
|
|
|
2018-10-01 06:55:43 +00:00
|
|
|
async def async_added_to_hass(self):
|
2017-03-02 05:38:19 +00:00
|
|
|
"""Register callbacks."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
@callback
|
2016-05-30 17:19:12 +00:00
|
|
|
def template_sensor_state_listener(entity, old_state, new_state):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Handle device state changes."""
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state(True)
|
2017-03-02 05:38:19 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def template_sensor_startup(event):
|
|
|
|
"""Update template on startup."""
|
2018-10-11 09:38:35 +00:00
|
|
|
if self._entities != MATCH_ALL:
|
|
|
|
# Track state change only for valid templates
|
|
|
|
async_track_state_change(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass, self._entities, template_sensor_state_listener
|
|
|
|
)
|
2017-03-02 05:38:19 +00:00
|
|
|
|
2017-09-12 08:01:03 +00:00
|
|
|
self.async_schedule_update_ha_state(True)
|
2016-03-25 06:07:19 +00:00
|
|
|
|
2017-03-02 05:38:19 +00:00
|
|
|
self.hass.bus.async_listen_once(
|
2019-07-31 19:25:30 +00:00
|
|
|
EVENT_HOMEASSISTANT_START, template_sensor_startup
|
|
|
|
)
|
2016-01-21 16:31:23 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the name of the sensor."""
|
2016-01-21 16:31:23 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the state of the sensor."""
|
2016-01-21 16:31:23 +00:00
|
|
|
return self._state
|
|
|
|
|
2017-02-07 09:51:44 +00:00
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Return the icon to use in the frontend, if any."""
|
|
|
|
return self._icon
|
|
|
|
|
2018-05-01 18:32:44 +00:00
|
|
|
@property
|
|
|
|
def device_class(self) -> Optional[str]:
|
|
|
|
"""Return the device class of the sensor."""
|
|
|
|
return self._device_class
|
|
|
|
|
2017-10-30 16:28:37 +00:00
|
|
|
@property
|
|
|
|
def entity_picture(self):
|
|
|
|
"""Return the entity_picture to use in the frontend, if any."""
|
|
|
|
return self._entity_picture
|
|
|
|
|
2016-01-21 16:31:23 +00:00
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the unit_of_measurement of the device."""
|
2016-01-21 16:31:23 +00:00
|
|
|
return self._unit_of_measurement
|
|
|
|
|
2019-09-24 14:05:19 +00:00
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return if the device is available."""
|
|
|
|
return self._available
|
|
|
|
|
2019-09-01 16:12:55 +00:00
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
|
|
|
return self._attributes
|
|
|
|
|
2016-01-21 16:31:23 +00:00
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2016-02-23 05:21:49 +00:00
|
|
|
"""No polling needed."""
|
2016-01-21 16:31:23 +00:00
|
|
|
return False
|
|
|
|
|
2018-10-01 06:55:43 +00:00
|
|
|
async def async_update(self):
|
2016-10-17 05:00:55 +00:00
|
|
|
"""Update the state from the template."""
|
2016-01-21 23:17:19 +00:00
|
|
|
try:
|
2016-10-01 04:38:39 +00:00
|
|
|
self._state = self._template.async_render()
|
2019-09-24 14:05:19 +00:00
|
|
|
self._available = True
|
2016-01-22 16:30:02 +00:00
|
|
|
except TemplateError as ex:
|
2019-09-24 14:05:19 +00:00
|
|
|
self._available = False
|
2016-02-02 14:14:29 +00:00
|
|
|
if ex.args and ex.args[0].startswith(
|
2019-07-31 19:25:30 +00:00
|
|
|
"UndefinedError: 'None' has no attribute"
|
|
|
|
):
|
2016-02-02 16:26:17 +00:00
|
|
|
# Common during HA startup - so just a warning
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.warning(
|
|
|
|
"Could not render template %s," " the state is unknown.", self._name
|
|
|
|
)
|
2018-03-11 04:45:32 +00:00
|
|
|
else:
|
|
|
|
self._state = None
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("Could not render template %s: %s", self._name, ex)
|
2019-09-01 16:12:55 +00:00
|
|
|
|
|
|
|
attrs = {}
|
|
|
|
for key, value in self._attribute_templates.items():
|
|
|
|
try:
|
|
|
|
attrs[key] = value.async_render()
|
|
|
|
except TemplateError as err:
|
|
|
|
_LOGGER.error("Error rendering attribute %s: %s", key, err)
|
|
|
|
|
|
|
|
self._attributes = attrs
|
|
|
|
|
2019-09-24 14:05:19 +00:00
|
|
|
templates = {
|
|
|
|
"_icon": self._icon_template,
|
|
|
|
"_entity_picture": self._entity_picture_template,
|
|
|
|
"_name": self._friendly_name_template,
|
|
|
|
"_available": self._availability_template,
|
|
|
|
}
|
|
|
|
|
2019-09-01 16:12:55 +00:00
|
|
|
for property_name, template in templates.items():
|
2017-10-30 16:28:37 +00:00
|
|
|
if template is None:
|
|
|
|
continue
|
|
|
|
|
2017-02-07 09:51:44 +00:00
|
|
|
try:
|
2019-09-24 14:05:19 +00:00
|
|
|
value = template.async_render()
|
|
|
|
if property_name == "_available":
|
|
|
|
value = value.lower() == "true"
|
|
|
|
setattr(self, property_name, value)
|
2017-02-07 09:51:44 +00:00
|
|
|
except TemplateError as ex:
|
2019-07-31 19:25:30 +00:00
|
|
|
friendly_property_name = property_name[1:].replace("_", " ")
|
2017-02-07 09:51:44 +00:00
|
|
|
if ex.args and ex.args[0].startswith(
|
2019-07-31 19:25:30 +00:00
|
|
|
"UndefinedError: 'None' has no attribute"
|
|
|
|
):
|
2017-02-07 09:51:44 +00:00
|
|
|
# Common during HA startup - so just a warning
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.warning(
|
|
|
|
"Could not render %s template %s," " the state is unknown.",
|
|
|
|
friendly_property_name,
|
|
|
|
self._name,
|
|
|
|
)
|
2018-03-11 04:45:32 +00:00
|
|
|
continue
|
2017-10-30 16:28:37 +00:00
|
|
|
|
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
setattr(self, property_name, getattr(super(), property_name))
|
2017-10-30 16:28:37 +00:00
|
|
|
except AttributeError:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Could not render %s template %s: %s",
|
|
|
|
friendly_property_name,
|
|
|
|
self._name,
|
|
|
|
ex,
|
|
|
|
)
|