mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
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:
parent
47bfef9640
commit
219ed7331c
@ -14,7 +14,7 @@ from homeassistant.components.sensor import ENTITY_ID_FORMAT, PLATFORM_SCHEMA
|
|||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT, CONF_VALUE_TEMPLATE,
|
ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT, CONF_VALUE_TEMPLATE,
|
||||||
CONF_ICON_TEMPLATE, CONF_ENTITY_PICTURE_TEMPLATE, ATTR_ENTITY_ID,
|
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
|
from homeassistant.exceptions import TemplateError
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity import Entity, async_generate_entity_id
|
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.Required(CONF_VALUE_TEMPLATE): cv.template,
|
||||||
vol.Optional(CONF_ICON_TEMPLATE): cv.template,
|
vol.Optional(CONF_ICON_TEMPLATE): cv.template,
|
||||||
vol.Optional(CONF_ENTITY_PICTURE_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_FRIENDLY_NAME): cv.string,
|
||||||
vol.Optional(ATTR_UNIT_OF_MEASUREMENT): cv.string,
|
vol.Optional(ATTR_UNIT_OF_MEASUREMENT): cv.string,
|
||||||
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids
|
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
|
entity_ids = (device_config.get(ATTR_ENTITY_ID) or
|
||||||
state_template.extract_entities())
|
state_template.extract_entities())
|
||||||
friendly_name = device_config.get(ATTR_FRIENDLY_NAME, device)
|
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)
|
unit_of_measurement = device_config.get(ATTR_UNIT_OF_MEASUREMENT)
|
||||||
|
|
||||||
state_template.hass = hass
|
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:
|
if entity_picture_template is not None:
|
||||||
entity_picture_template.hass = hass
|
entity_picture_template.hass = hass
|
||||||
|
|
||||||
|
if friendly_name_template is not None:
|
||||||
|
friendly_name_template.hass = hass
|
||||||
|
|
||||||
sensors.append(
|
sensors.append(
|
||||||
SensorTemplate(
|
SensorTemplate(
|
||||||
hass,
|
hass,
|
||||||
device,
|
device,
|
||||||
friendly_name,
|
friendly_name,
|
||||||
|
friendly_name_template,
|
||||||
unit_of_measurement,
|
unit_of_measurement,
|
||||||
state_template,
|
state_template,
|
||||||
icon_template,
|
icon_template,
|
||||||
@ -82,7 +88,7 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
|||||||
class SensorTemplate(Entity):
|
class SensorTemplate(Entity):
|
||||||
"""Representation of a Template Sensor."""
|
"""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,
|
unit_of_measurement, state_template, icon_template,
|
||||||
entity_picture_template, entity_ids):
|
entity_picture_template, entity_ids):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
@ -90,6 +96,7 @@ class SensorTemplate(Entity):
|
|||||||
self.entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, device_id,
|
self.entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, device_id,
|
||||||
hass=hass)
|
hass=hass)
|
||||||
self._name = friendly_name
|
self._name = friendly_name
|
||||||
|
self._friendly_name_template = friendly_name_template
|
||||||
self._unit_of_measurement = unit_of_measurement
|
self._unit_of_measurement = unit_of_measurement
|
||||||
self._template = state_template
|
self._template = state_template
|
||||||
self._state = None
|
self._state = None
|
||||||
@ -165,7 +172,8 @@ class SensorTemplate(Entity):
|
|||||||
|
|
||||||
for property_name, template in (
|
for property_name, template in (
|
||||||
('_icon', self._icon_template),
|
('_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:
|
if template is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
@ -76,6 +76,7 @@ CONF_FILENAME = 'filename'
|
|||||||
CONF_FOR = 'for'
|
CONF_FOR = 'for'
|
||||||
CONF_FORCE_UPDATE = 'force_update'
|
CONF_FORCE_UPDATE = 'force_update'
|
||||||
CONF_FRIENDLY_NAME = 'friendly_name'
|
CONF_FRIENDLY_NAME = 'friendly_name'
|
||||||
|
CONF_FRIENDLY_NAME_TEMPLATE = 'friendly_name_template'
|
||||||
CONF_HEADERS = 'headers'
|
CONF_HEADERS = 'headers'
|
||||||
CONF_HOST = 'host'
|
CONF_HOST = 'host'
|
||||||
CONF_HOSTS = 'hosts'
|
CONF_HOSTS = 'hosts'
|
||||||
|
@ -104,6 +104,33 @@ class TestTemplateSensor:
|
|||||||
state = self.hass.states.get('sensor.test_template_sensor')
|
state = self.hass.states.get('sensor.test_template_sensor')
|
||||||
assert state.attributes['entity_picture'] == '/local/sensor.png'
|
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):
|
def test_template_syntax_error(self):
|
||||||
"""Test templating syntax error."""
|
"""Test templating syntax error."""
|
||||||
with assert_setup_component(0):
|
with assert_setup_component(0):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user