2016-01-21 16:31:23 +00:00
|
|
|
"""
|
2016-03-08 15:46:34 +00:00
|
|
|
Allows the creation of a sensor that breaks out state_attributes.
|
2016-01-21 16:31:23 +00:00
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/sensor.template/
|
|
|
|
"""
|
|
|
|
import logging
|
2016-08-22 08:11:16 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-01-21 16:31:23 +00:00
|
|
|
|
2016-08-22 08:11:16 +00:00
|
|
|
from homeassistant.components.sensor import ENTITY_ID_FORMAT, PLATFORM_SCHEMA
|
2016-01-21 16:31:23 +00:00
|
|
|
from homeassistant.const import (
|
2016-05-29 21:34:21 +00:00
|
|
|
ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT, CONF_VALUE_TEMPLATE,
|
|
|
|
ATTR_ENTITY_ID, MATCH_ALL)
|
2016-01-21 23:17:19 +00:00
|
|
|
from homeassistant.exceptions import TemplateError
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.helpers.entity import Entity, generate_entity_id
|
2016-02-23 20:06:50 +00:00
|
|
|
from homeassistant.helpers import template
|
2016-05-29 21:34:21 +00:00
|
|
|
from homeassistant.helpers.event import track_state_change
|
2016-02-01 17:45:18 +00:00
|
|
|
|
2016-01-21 16:31:23 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
CONF_SENSORS = 'sensors'
|
2016-01-22 16:30:02 +00:00
|
|
|
|
2016-08-22 08:11:16 +00:00
|
|
|
SENSOR_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(CONF_VALUE_TEMPLATE): cv.template,
|
|
|
|
vol.Optional(ATTR_FRIENDLY_NAME): cv.string,
|
|
|
|
vol.Optional(ATTR_UNIT_OF_MEASUREMENT): cv.string,
|
|
|
|
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids
|
|
|
|
})
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_SENSORS): vol.Schema({cv.slug: SENSOR_SCHEMA}),
|
|
|
|
})
|
|
|
|
|
2016-01-21 16:31:23 +00:00
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Setup 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-01-21 16:31:23 +00:00
|
|
|
friendly_name = device_config.get(ATTR_FRIENDLY_NAME, device)
|
|
|
|
unit_of_measurement = device_config.get(ATTR_UNIT_OF_MEASUREMENT)
|
|
|
|
state_template = device_config.get(CONF_VALUE_TEMPLATE)
|
2016-02-01 17:45:18 +00:00
|
|
|
|
2016-05-29 21:34:21 +00:00
|
|
|
entity_ids = device_config.get(ATTR_ENTITY_ID, MATCH_ALL)
|
|
|
|
|
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,
|
|
|
|
unit_of_measurement,
|
2016-05-29 21:34:21 +00:00
|
|
|
state_template,
|
|
|
|
entity_ids)
|
2016-01-21 16:31:23 +00:00
|
|
|
)
|
2016-02-01 18:38:11 +00:00
|
|
|
if not sensors:
|
2016-01-27 08:21:21 +00:00
|
|
|
_LOGGER.error("No sensors added")
|
2016-01-21 16:31:23 +00:00
|
|
|
return False
|
|
|
|
add_devices(sensors)
|
|
|
|
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
|
|
|
|
|
|
|
# pylint: disable=too-many-arguments
|
2016-03-08 15:46:34 +00:00
|
|
|
def __init__(self, hass, device_id, friendly_name, unit_of_measurement,
|
2016-05-29 21:34:21 +00:00
|
|
|
state_template, entity_ids):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2016-03-25 06:07:19 +00:00
|
|
|
self.hass = hass
|
2016-03-08 15:46:34 +00:00
|
|
|
self.entity_id = generate_entity_id(ENTITY_ID_FORMAT, device_id,
|
|
|
|
hass=hass)
|
2016-01-21 23:17:19 +00:00
|
|
|
self._name = friendly_name
|
2016-01-21 16:31:23 +00:00
|
|
|
self._unit_of_measurement = unit_of_measurement
|
|
|
|
self._template = state_template
|
2016-03-25 06:07:19 +00:00
|
|
|
self._state = None
|
|
|
|
|
2016-01-21 23:17:19 +00:00
|
|
|
self.update()
|
2016-01-21 16:31:23 +00:00
|
|
|
|
2016-05-30 17:19:12 +00:00
|
|
|
def template_sensor_state_listener(entity, old_state, new_state):
|
2016-03-25 06:07:19 +00:00
|
|
|
"""Called when the target device changes state."""
|
|
|
|
self.update_ha_state(True)
|
|
|
|
|
2016-05-29 21:34:21 +00:00
|
|
|
track_state_change(hass, entity_ids,
|
|
|
|
template_sensor_state_listener)
|
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
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
|
|
|
def update(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Get the latest data and update the states."""
|
2016-01-21 23:17:19 +00:00
|
|
|
try:
|
|
|
|
self._state = template.render(self.hass, self._template)
|
2016-01-22 16:30:02 +00:00
|
|
|
except TemplateError as ex:
|
2016-02-02 14:14:29 +00:00
|
|
|
if ex.args and ex.args[0].startswith(
|
|
|
|
"UndefinedError: 'None' has no attribute"):
|
2016-02-02 16:26:17 +00:00
|
|
|
# Common during HA startup - so just a warning
|
|
|
|
_LOGGER.warning(ex)
|
2016-02-02 14:14:29 +00:00
|
|
|
return
|
2016-03-25 06:07:19 +00:00
|
|
|
self._state = None
|
2016-01-22 16:30:02 +00:00
|
|
|
_LOGGER.error(ex)
|