From 95748a688028cfd233b53a7057800f92ef8b41ab Mon Sep 17 00:00:00 2001 From: pavoni Date: Mon, 1 Feb 2016 17:45:18 +0000 Subject: [PATCH 1/4] Generate entity id correctly, was using friendly_name. --- homeassistant/components/sensor/template.py | 22 +++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/sensor/template.py b/homeassistant/components/sensor/template.py index 4af3cae9260..e712d0cf51c 100644 --- a/homeassistant/components/sensor/template.py +++ b/homeassistant/components/sensor/template.py @@ -9,16 +9,20 @@ https://home-assistant.io/components/sensor.template/ """ 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.const import ( ATTR_FRIENDLY_NAME, CONF_VALUE_TEMPLATE, ATTR_UNIT_OF_MEASUREMENT) -from homeassistant.util import template +from homeassistant.util import template, slugify from homeassistant.exceptions import TemplateError +from homeassistant.components.sensor import DOMAIN + +ENTITY_ID_FORMAT = DOMAIN + '.{}' + _LOGGER = logging.getLogger(__name__) CONF_SENSORS = 'sensors' STATE_ERROR = 'error' @@ -34,9 +38,16 @@ def setup_platform(hass, config, add_devices, discovery_info=None): return False 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): _LOGGER.error("Missing configuration data for sensor %s", device) continue + friendly_name = device_config.get(ATTR_FRIENDLY_NAME, device) unit_of_measurement = device_config.get(ATTR_UNIT_OF_MEASUREMENT) state_template = device_config.get(CONF_VALUE_TEMPLATE) @@ -44,9 +55,11 @@ def setup_platform(hass, config, add_devices, discovery_info=None): _LOGGER.error( "Missing %s for sensor %s", CONF_VALUE_TEMPLATE, device) continue + sensors.append( SensorTemplate( hass, + device, friendly_name, unit_of_measurement, state_template) @@ -64,10 +77,15 @@ class SensorTemplate(Entity): # pylint: disable=too-many-arguments def __init__(self, hass, + device_id, friendly_name, unit_of_measurement, state_template): + self.entity_id = generate_entity_id( + ENTITY_ID_FORMAT, device_id, + hass=hass) + self.hass = hass self._name = friendly_name self._unit_of_measurement = unit_of_measurement From d54e10e54a5653d53345c59bddd3b37244b57103 Mon Sep 17 00:00:00 2001 From: pavoni Date: Mon, 1 Feb 2016 18:18:51 +0000 Subject: [PATCH 2/4] Improve test coverage of error conditions. --- tests/components/sensor/test_template.py | 43 +++++++++++++++++++++--- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/tests/components/sensor/test_template.py b/tests/components/sensor/test_template.py index 513117a8a9e..55657bc0336 100644 --- a/tests/components/sensor/test_template.py +++ b/tests/components/sensor/test_template.py @@ -4,9 +4,6 @@ tests.components.sensor.template Tests template sensor. """ -from unittest.mock import patch - -import pytest import homeassistant.core as ha import homeassistant.components.sensor as sensor @@ -56,8 +53,46 @@ class TestTemplateSensor: } }) - self.hass.states.set('sensor.test_state', 'Works') self.hass.pool.block_till_done() state = self.hass.states.get('sensor.test_template_sensor') 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_config_does_not_create(self): + assert sensor.setup(self.hass, { + 'sensor': { + 'platform': 'template', + 'sensors': { + 'test_template_sensor': {} + } + } + }) + 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() == [] From 7c1241c1f84feaeed51cecd23c835bc5165e3454 Mon Sep 17 00:00:00 2001 From: pavoni Date: Mon, 1 Feb 2016 18:30:39 +0000 Subject: [PATCH 3/4] Add another test, revise another. Improve coverage. --- tests/components/sensor/test_template.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/components/sensor/test_template.py b/tests/components/sensor/test_template.py index 55657bc0336..cc416e28f4e 100644 --- a/tests/components/sensor/test_template.py +++ b/tests/components/sensor/test_template.py @@ -72,17 +72,25 @@ class TestTemplateSensor: }) assert self.hass.states.all() == [] - def test_invalid_config_does_not_create(self): + def test_invalid_sensor_does_not_create(self): assert sensor.setup(self.hass, { 'sensor': { 'platform': 'template', 'sensors': { - 'test_template_sensor': {} + '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': { From 9c33af60f2a30e749dcbe825a8f2db0321d1d26b Mon Sep 17 00:00:00 2001 From: pavoni Date: Mon, 1 Feb 2016 18:38:11 +0000 Subject: [PATCH 4/4] Fix unreachable code! --- homeassistant/components/sensor/template.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/sensor/template.py b/homeassistant/components/sensor/template.py index e712d0cf51c..b87e26aa415 100644 --- a/homeassistant/components/sensor/template.py +++ b/homeassistant/components/sensor/template.py @@ -64,7 +64,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): unit_of_measurement, state_template) ) - if sensors is None: + if not sensors: _LOGGER.error("No sensors added") return False add_devices(sensors)