Add entity_picture_template options to Template Cover, Template Light, Template Sensor, and Template Switch (#9854)

* Re-adding cover

* Re-adding light

* Re-adding sensor

* Re-adding switch

* Re-added tests

* Fixing missing imports in rebased test

* Fixing broken tests

* Owner-requested changes

* Owner-requested changes

* Fixed exception
This commit is contained in:
Aaron Bach authored and Paulus Schoutsen committed 2017-10-30 09:28:37 -07:00
1 parent 05ece53ec2
commit 646c03eea1
9 files changed
+388 -41

No files matched your search

+37 -11
View File
@@ -13,8 +13,8 @@ from homeassistant.core import callback
from homeassistant.components.sensor import ENTITY_ID_FORMAT, PLATFORM_SCHEMA
from homeassistant.const import (
ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT, CONF_VALUE_TEMPLATE,
CONF_ICON_TEMPLATE, ATTR_ENTITY_ID, CONF_SENSORS,
EVENT_HOMEASSISTANT_START)
CONF_ICON_TEMPLATE, CONF_ENTITY_PICTURE_TEMPLATE, ATTR_ENTITY_ID,
CONF_SENSORS, EVENT_HOMEASSISTANT_START)
from homeassistant.exceptions import TemplateError
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity, async_generate_entity_id
@@ -25,6 +25,7 @@ _LOGGER = logging.getLogger(__name__)
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(ATTR_FRIENDLY_NAME): cv.string,
vol.Optional(ATTR_UNIT_OF_MEASUREMENT): cv.string,
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids
@@ -44,6 +45,8 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
for device, device_config in config[CONF_SENSORS].items():
state_template = device_config[CONF_VALUE_TEMPLATE]
icon_template = device_config.get(CONF_ICON_TEMPLATE)
entity_picture_template = device_config.get(
CONF_ENTITY_PICTURE_TEMPLATE)
entity_ids = (device_config.get(ATTR_ENTITY_ID) or
state_template.extract_entities())
friendly_name = device_config.get(ATTR_FRIENDLY_NAME, device)
@@ -54,6 +57,9 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
if icon_template is not None:
icon_template.hass = hass
if entity_picture_template is not None:
entity_picture_template.hass = hass
sensors.append(
SensorTemplate(
hass,
@@ -62,6 +68,7 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
unit_of_measurement,
state_template,
icon_template,
entity_picture_template,
entity_ids)
)
if not sensors:
@@ -75,8 +82,9 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
class SensorTemplate(Entity):
"""Representation of a Template Sensor."""
def __init__(self, hass, device_id, friendly_name, unit_of_measurement,
state_template, icon_template, entity_ids):
def __init__(self, hass, device_id, friendly_name,
unit_of_measurement, state_template, icon_template,
entity_picture_template, entity_ids):
"""Initialize the sensor."""
self.hass = hass
self.entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, device_id,
@@ -86,7 +94,9 @@ class SensorTemplate(Entity):
self._template = state_template
self._state = None
self._icon_template = icon_template
self._entity_picture_template = entity_picture_template
self._icon = None
self._entity_picture = None
self._entities = entity_ids
@asyncio.coroutine
@@ -123,6 +133,11 @@ class SensorTemplate(Entity):
"""Return the icon to use in the frontend, if any."""
return self._icon
@property
def entity_picture(self):
"""Return the entity_picture to use in the frontend, if any."""
return self._entity_picture
@property
def unit_of_measurement(self):
"""Return the unit_of_measurement of the device."""
@@ -148,16 +163,27 @@ class SensorTemplate(Entity):
self._state = None
_LOGGER.error('Could not render template %s: %s', self._name, ex)
if self._icon_template is not None:
for property_name, template in (
('_icon', self._icon_template),
('_entity_picture', self._entity_picture_template)):
if template is None:
continue
try:
self._icon = self._icon_template.async_render()
setattr(self, property_name, template.async_render())
except TemplateError as ex:
friendly_property_name = property_name[1:].replace('_', ' ')
if ex.args and ex.args[0].startswith(
"UndefinedError: 'None' has no attribute"):
# Common during HA startup - so just a warning
_LOGGER.warning('Could not render icon template %s,'
' the state is unknown.', self._name)
_LOGGER.warning('Could not render %s template %s,'
' the state is unknown.',
friendly_property_name, self._name)
return
self._icon = super().icon
_LOGGER.error('Could not render icon template %s: %s',
self._name, ex)
try:
setattr(self, property_name,
getattr(super(), property_name))
except AttributeError:
_LOGGER.error('Could not render %s template %s: %s',
friendly_property_name, self._name, ex)