mirror of
https://github.com/home-assistant/core.git
synced 2025-07-27 15:17:35 +00:00
Merge pull request #1090 from balloob/fix_sensor_template_entity_id
Fix bug in sensor.template entity_id
This commit is contained in:
commit
26efaa91a3
@ -9,16 +9,20 @@ https://home-assistant.io/components/sensor.template/
|
|||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity, generate_entity_id
|
||||||
from homeassistant.core import EVENT_STATE_CHANGED
|
from homeassistant.core import EVENT_STATE_CHANGED
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_FRIENDLY_NAME,
|
ATTR_FRIENDLY_NAME,
|
||||||
CONF_VALUE_TEMPLATE,
|
CONF_VALUE_TEMPLATE,
|
||||||
ATTR_UNIT_OF_MEASUREMENT)
|
ATTR_UNIT_OF_MEASUREMENT)
|
||||||
|
|
||||||
from homeassistant.util import template
|
from homeassistant.util import template, slugify
|
||||||
from homeassistant.exceptions import TemplateError
|
from homeassistant.exceptions import TemplateError
|
||||||
|
|
||||||
|
from homeassistant.components.sensor import DOMAIN
|
||||||
|
|
||||||
|
ENTITY_ID_FORMAT = DOMAIN + '.{}'
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
CONF_SENSORS = 'sensors'
|
CONF_SENSORS = 'sensors'
|
||||||
STATE_ERROR = 'error'
|
STATE_ERROR = 'error'
|
||||||
@ -34,9 +38,16 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
for device, device_config in config[CONF_SENSORS].items():
|
for device, device_config in config[CONF_SENSORS].items():
|
||||||
|
|
||||||
|
if device != slugify(device):
|
||||||
|
_LOGGER.error("Found invalid key for sensor.template: %s. "
|
||||||
|
"Use %s instead", device, slugify(device))
|
||||||
|
continue
|
||||||
|
|
||||||
if not isinstance(device_config, dict):
|
if not isinstance(device_config, dict):
|
||||||
_LOGGER.error("Missing configuration data for sensor %s", device)
|
_LOGGER.error("Missing configuration data for sensor %s", device)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
friendly_name = device_config.get(ATTR_FRIENDLY_NAME, device)
|
friendly_name = device_config.get(ATTR_FRIENDLY_NAME, device)
|
||||||
unit_of_measurement = device_config.get(ATTR_UNIT_OF_MEASUREMENT)
|
unit_of_measurement = device_config.get(ATTR_UNIT_OF_MEASUREMENT)
|
||||||
state_template = device_config.get(CONF_VALUE_TEMPLATE)
|
state_template = device_config.get(CONF_VALUE_TEMPLATE)
|
||||||
@ -44,14 +55,16 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
"Missing %s for sensor %s", CONF_VALUE_TEMPLATE, device)
|
"Missing %s for sensor %s", CONF_VALUE_TEMPLATE, device)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
sensors.append(
|
sensors.append(
|
||||||
SensorTemplate(
|
SensorTemplate(
|
||||||
hass,
|
hass,
|
||||||
|
device,
|
||||||
friendly_name,
|
friendly_name,
|
||||||
unit_of_measurement,
|
unit_of_measurement,
|
||||||
state_template)
|
state_template)
|
||||||
)
|
)
|
||||||
if sensors is None:
|
if not sensors:
|
||||||
_LOGGER.error("No sensors added")
|
_LOGGER.error("No sensors added")
|
||||||
return False
|
return False
|
||||||
add_devices(sensors)
|
add_devices(sensors)
|
||||||
@ -64,10 +77,15 @@ class SensorTemplate(Entity):
|
|||||||
# pylint: disable=too-many-arguments
|
# pylint: disable=too-many-arguments
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
hass,
|
hass,
|
||||||
|
device_id,
|
||||||
friendly_name,
|
friendly_name,
|
||||||
unit_of_measurement,
|
unit_of_measurement,
|
||||||
state_template):
|
state_template):
|
||||||
|
|
||||||
|
self.entity_id = generate_entity_id(
|
||||||
|
ENTITY_ID_FORMAT, device_id,
|
||||||
|
hass=hass)
|
||||||
|
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
self._name = friendly_name
|
self._name = friendly_name
|
||||||
self._unit_of_measurement = unit_of_measurement
|
self._unit_of_measurement = unit_of_measurement
|
||||||
|
@ -4,9 +4,6 @@ tests.components.sensor.template
|
|||||||
|
|
||||||
Tests template sensor.
|
Tests template sensor.
|
||||||
"""
|
"""
|
||||||
from unittest.mock import patch
|
|
||||||
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
import homeassistant.core as ha
|
import homeassistant.core as ha
|
||||||
import homeassistant.components.sensor as sensor
|
import homeassistant.components.sensor as sensor
|
||||||
@ -56,8 +53,54 @@ class TestTemplateSensor:
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
self.hass.states.set('sensor.test_state', 'Works')
|
self.hass.states.set('sensor.test_state', 'Works')
|
||||||
self.hass.pool.block_till_done()
|
self.hass.pool.block_till_done()
|
||||||
state = self.hass.states.get('sensor.test_template_sensor')
|
state = self.hass.states.get('sensor.test_template_sensor')
|
||||||
assert state.state == 'error'
|
assert state.state == 'error'
|
||||||
|
|
||||||
|
def test_invalid_name_does_not_create(self):
|
||||||
|
assert sensor.setup(self.hass, {
|
||||||
|
'sensor': {
|
||||||
|
'platform': 'template',
|
||||||
|
'sensors': {
|
||||||
|
'test INVALID sensor': {
|
||||||
|
'value_template':
|
||||||
|
"{{ states.sensor.test_state.state }}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
assert self.hass.states.all() == []
|
||||||
|
|
||||||
|
def test_invalid_sensor_does_not_create(self):
|
||||||
|
assert sensor.setup(self.hass, {
|
||||||
|
'sensor': {
|
||||||
|
'platform': 'template',
|
||||||
|
'sensors': {
|
||||||
|
'test_template_sensor': 'invalid'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
assert self.hass.states.all() == []
|
||||||
|
|
||||||
|
def test_no_sensors_does_not_create(self):
|
||||||
|
assert sensor.setup(self.hass, {
|
||||||
|
'sensor': {
|
||||||
|
'platform': 'template'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
assert self.hass.states.all() == []
|
||||||
|
|
||||||
|
def test_missing_template_does_not_create(self):
|
||||||
|
assert sensor.setup(self.hass, {
|
||||||
|
'sensor': {
|
||||||
|
'platform': 'template',
|
||||||
|
'sensors': {
|
||||||
|
'test_template_sensor': {
|
||||||
|
'not_value_template':
|
||||||
|
"{{ states.sensor.test_state.state }}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
assert self.hass.states.all() == []
|
||||||
|
Loading…
x
Reference in New Issue
Block a user