Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions homeassistant/components/sensor/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from homeassistant.const import (
ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT, CONF_VALUE_TEMPLATE,
CONF_ICON_TEMPLATE, CONF_ENTITY_PICTURE_TEMPLATE, ATTR_ENTITY_ID,
CONF_SENSORS, EVENT_HOMEASSISTANT_START)
CONF_SENSORS, EVENT_HOMEASSISTANT_START, CONF_FRIENDLY_NAME_TEMPLATE)
from homeassistant.exceptions import TemplateError
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity, async_generate_entity_id
Expand All @@ -26,6 +26,7 @@
vol.Required(CONF_VALUE_TEMPLATE): cv.template,
vol.Optional(CONF_ICON_TEMPLATE): cv.template,
vol.Optional(CONF_ENTITY_PICTURE_TEMPLATE): cv.template,
vol.Optional(CONF_FRIENDLY_NAME_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
Expand All @@ -50,6 +51,7 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
entity_ids = (device_config.get(ATTR_ENTITY_ID) or
state_template.extract_entities())
friendly_name = device_config.get(ATTR_FRIENDLY_NAME, device)
friendly_name_template = device_config.get(CONF_FRIENDLY_NAME_TEMPLATE)
unit_of_measurement = device_config.get(ATTR_UNIT_OF_MEASUREMENT)

state_template.hass = hass
Expand All @@ -60,11 +62,15 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
if entity_picture_template is not None:
entity_picture_template.hass = hass

if friendly_name_template is not None:
friendly_name_template.hass = hass

sensors.append(
SensorTemplate(
hass,
device,
friendly_name,
friendly_name_template,
unit_of_measurement,
state_template,
icon_template,
Expand All @@ -82,14 +88,15 @@ 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,
def __init__(self, hass, device_id, friendly_name, friendly_name_template,
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,
hass=hass)
self._name = friendly_name
self._friendly_name_template = friendly_name_template
self._unit_of_measurement = unit_of_measurement
self._template = state_template
self._state = None
Expand Down Expand Up @@ -165,7 +172,8 @@ def async_update(self):

for property_name, template in (
('_icon', self._icon_template),
('_entity_picture', self._entity_picture_template)):
('_entity_picture', self._entity_picture_template),
('_name', self._friendly_name_template)):
if template is None:
continue

Expand Down
1 change: 1 addition & 0 deletions homeassistant/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
CONF_FOR = 'for'
CONF_FORCE_UPDATE = 'force_update'
CONF_FRIENDLY_NAME = 'friendly_name'
CONF_FRIENDLY_NAME_TEMPLATE = 'friendly_name_template'
CONF_HEADERS = 'headers'
CONF_HOST = 'host'
CONF_HOSTS = 'hosts'
Expand Down
27 changes: 27 additions & 0 deletions tests/components/sensor/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,33 @@ def test_entity_picture_template(self):
state = self.hass.states.get('sensor.test_template_sensor')
assert state.attributes['entity_picture'] == '/local/sensor.png'

def test_friendly_name_template(self):
"""Test friendly_name template."""
with assert_setup_component(1):
assert setup_component(self.hass, 'sensor', {
'sensor': {
'platform': 'template',
'sensors': {
'test_template_sensor': {
'value_template': "State",
'friendly_name_template':
"It {{ states.sensor.test_state.state }}."
}
}
}
})

self.hass.start()
self.hass.block_till_done()

state = self.hass.states.get('sensor.test_template_sensor')
assert state.attributes.get('friendly_name') == 'It .'

self.hass.states.set('sensor.test_state', 'Works')
self.hass.block_till_done()
state = self.hass.states.get('sensor.test_template_sensor')
assert state.attributes['friendly_name'] == 'It Works.'

def test_template_syntax_error(self):
"""Test templating syntax error."""
with assert_setup_component(0):
Expand Down