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

+39 -12
View File
@@ -13,8 +13,9 @@ from homeassistant.core import callback
from homeassistant.components.switch import (
ENTITY_ID_FORMAT, SwitchDevice, PLATFORM_SCHEMA)
from homeassistant.const import (
ATTR_FRIENDLY_NAME, CONF_VALUE_TEMPLATE, CONF_ICON_TEMPLATE, STATE_OFF,
STATE_ON, ATTR_ENTITY_ID, CONF_SWITCHES, EVENT_HOMEASSISTANT_START)
ATTR_FRIENDLY_NAME, CONF_VALUE_TEMPLATE, CONF_ICON_TEMPLATE,
CONF_ENTITY_PICTURE_TEMPLATE, STATE_OFF, STATE_ON, ATTR_ENTITY_ID,
CONF_SWITCHES, EVENT_HOMEASSISTANT_START)
from homeassistant.exceptions import TemplateError
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import async_generate_entity_id
@@ -30,6 +31,7 @@ OFF_ACTION = 'turn_off'
SWITCH_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.Required(ON_ACTION): cv.SCRIPT_SCHEMA,
vol.Required(OFF_ACTION): cv.SCRIPT_SCHEMA,
vol.Optional(ATTR_FRIENDLY_NAME): cv.string,
@@ -51,6 +53,8 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
friendly_name = device_config.get(ATTR_FRIENDLY_NAME, device)
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)
on_action = device_config[ON_ACTION]
off_action = device_config[OFF_ACTION]
entity_ids = (device_config.get(ATTR_ENTITY_ID) or
@@ -61,10 +65,14 @@ 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
switches.append(
SwitchTemplate(
hass, device, friendly_name, state_template, icon_template,
on_action, off_action, entity_ids)
hass, device, friendly_name, state_template,
icon_template, entity_picture_template, on_action,
off_action, entity_ids)
)
if not switches:
_LOGGER.error("No switches added")
@@ -78,7 +86,8 @@ class SwitchTemplate(SwitchDevice):
"""Representation of a Template switch."""
def __init__(self, hass, device_id, friendly_name, state_template,
icon_template, on_action, off_action, entity_ids):
icon_template, entity_picture_template, on_action,
off_action, entity_ids):
"""Initialize the Template switch."""
self.hass = hass
self.entity_id = async_generate_entity_id(
@@ -89,7 +98,9 @@ class SwitchTemplate(SwitchDevice):
self._off_script = Script(hass, off_action)
self._state = False
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
@@ -136,6 +147,11 @@ class SwitchTemplate(SwitchDevice):
"""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
def turn_on(self, **kwargs):
"""Fire the on action."""
self._on_script.run()
@@ -162,16 +178,27 @@ class SwitchTemplate(SwitchDevice):
_LOGGER.error(ex)
self._state = None
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)