core/homeassistant/components/sensor/template.py

118 lines
3.8 KiB
Python
Raw Normal View History

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
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
2016-01-21 16:31:23 +00:00
from homeassistant.components.sensor import ENTITY_ID_FORMAT, PLATFORM_SCHEMA
2016-01-21 16:31:23 +00:00
from homeassistant.const import (
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
from homeassistant.helpers.event import track_state_change
2016-01-21 16:31:23 +00:00
_LOGGER = logging.getLogger(__name__)
CONF_SENSORS = 'sensors'
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,
2016-08-23 06:56:39 +00:00
vol.Optional(ATTR_ENTITY_ID, default=MATCH_ALL): 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-08-23 06:56:39 +00:00
state_template = device_config[CONF_VALUE_TEMPLATE]
2016-08-23 07:14:45 +00:00
entity_ids = device_config[ATTR_ENTITY_ID]
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)
2016-01-21 16:31:23 +00:00
sensors.append(
SensorTemplate(
hass,
device,
2016-01-21 16:31:23 +00:00
friendly_name,
unit_of_measurement,
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,
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)
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)
except TemplateError as ex:
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)
return
2016-03-25 06:07:19 +00:00
self._state = None
_LOGGER.error(ex)