core/homeassistant/components/sensor/template.py

123 lines
3.9 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 asyncio
2016-01-21 16:31:23 +00:00
import logging
import voluptuous as vol
2016-01-21 16:31:23 +00:00
from homeassistant.core import callback
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, CONF_SENSORS)
2016-01-21 23:17:19 +00:00
from homeassistant.exceptions import TemplateError
from homeassistant.helpers.entity import Entity, async_generate_entity_id
from homeassistant.helpers.event import async_track_state_change
import homeassistant.helpers.config_validation as cv
2016-01-21 16:31:23 +00:00
_LOGGER = logging.getLogger(__name__)
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
2016-10-18 05:27:32 +00:00
@asyncio.coroutine
2016-01-21 16:31:23 +00:00
# pylint: disable=unused-argument
def async_setup_platform(hass, config, async_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]
entity_ids = (device_config.get(ATTR_ENTITY_ID) or
state_template.extract_entities())
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.hass = hass
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
yield from async_add_devices(sensors, True)
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
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
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
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
@callback
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."""
hass.async_add_job(self.async_update_ha_state, True)
2016-03-25 06:07:19 +00:00
async_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
@asyncio.coroutine
def async_update(self):
"""Update the state from the template."""
2016-01-21 23:17:19 +00:00
try:
self._state = self._template.async_render()
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)