add friendly_name_template to template sensor (#12268)

* add friendly_name_template to template sensor.
If set, overrides friendly_name setting.

* Add test
This commit is contained in:
NovapaX authored and Paulus Schoutsen committed 2018-02-11 12:12:30 -08:00
1 parent 47bfef9640
commit 219ed7331c
3 files changed
+39 -3

No files matched your search

+11 -3
View File
@@ -14,7 +14,7 @@ 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, 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
@@ -26,6 +26,7 @@ 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(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
@@ -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
@@ -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,
@@ -82,7 +88,7 @@ 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."""
@@ -90,6 +96,7 @@ class SensorTemplate(Entity):
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
@@ -165,7 +172,8 @@ class SensorTemplate(Entity):
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