From ebff253cc9e8d2bd613e0652c2db12bc6f8029e2 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 11 Oct 2018 11:38:35 +0200 Subject: [PATCH] still update sensor on startup (#17319) --- homeassistant/components/sensor/template.py | 10 ++++------ tests/components/sensor/test_template.py | 21 ++++++++++++++++----- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/sensor/template.py b/homeassistant/components/sensor/template.py index dbe92c2b3ba..3fa45935617 100644 --- a/homeassistant/components/sensor/template.py +++ b/homeassistant/components/sensor/template.py @@ -137,10 +137,6 @@ class SensorTemplate(Entity): async def async_added_to_hass(self): """Register callbacks.""" - # We don't render on every update - if self._entities == MATCH_ALL: - return - @callback def template_sensor_state_listener(entity, old_state, new_state): """Handle device state changes.""" @@ -149,8 +145,10 @@ class SensorTemplate(Entity): @callback def template_sensor_startup(event): """Update template on startup.""" - async_track_state_change( - self.hass, self._entities, template_sensor_state_listener) + if self._entities != MATCH_ALL: + # Track state change only for valid templates + async_track_state_change( + self.hass, self._entities, template_sensor_state_listener) self.async_schedule_update_ha_state(True) diff --git a/tests/components/sensor/test_template.py b/tests/components/sensor/test_template.py index 159a5c34d37..b0b1ea22285 100644 --- a/tests/components/sensor/test_template.py +++ b/tests/components/sensor/test_template.py @@ -1,4 +1,5 @@ """The test for the Template sensor platform.""" +from homeassistant.const import EVENT_HOMEASSISTANT_START from homeassistant.setup import setup_component, async_setup_component from tests.common import get_test_home_assistant, assert_setup_component @@ -316,6 +317,8 @@ class TestTemplateSensor: async def test_no_template_match_all(hass, caplog): """Test that we do not allow sensors that match on all.""" + hass.states.async_set('sensor.test_sensor', 'startup') + await async_setup_component(hass, 'sensor', { 'sensor': { 'platform': 'template', @@ -342,7 +345,7 @@ async def test_no_template_match_all(hass, caplog): } }) await hass.async_block_till_done() - assert len(hass.states.async_all()) == 4 + assert len(hass.states.async_all()) == 5 assert ('Template sensor invalid_state has no entity ids ' 'configured to track nor were we able to extract the entities to ' 'track from the value template') in caplog.text @@ -361,13 +364,21 @@ async def test_no_template_match_all(hass, caplog): assert hass.states.get('sensor.invalid_entity_picture').state == 'unknown' assert hass.states.get('sensor.invalid_friendly_name').state == 'unknown' + hass.bus.async_fire(EVENT_HOMEASSISTANT_START) + await hass.async_block_till_done() + + assert hass.states.get('sensor.invalid_state').state == '2' + assert hass.states.get('sensor.invalid_icon').state == 'startup' + assert hass.states.get('sensor.invalid_entity_picture').state == 'startup' + assert hass.states.get('sensor.invalid_friendly_name').state == 'startup' + hass.states.async_set('sensor.test_sensor', 'hello') await hass.async_block_till_done() - assert hass.states.get('sensor.invalid_state').state == 'unknown' - assert hass.states.get('sensor.invalid_icon').state == 'unknown' - assert hass.states.get('sensor.invalid_entity_picture').state == 'unknown' - assert hass.states.get('sensor.invalid_friendly_name').state == 'unknown' + assert hass.states.get('sensor.invalid_state').state == '2' + assert hass.states.get('sensor.invalid_icon').state == 'startup' + assert hass.states.get('sensor.invalid_entity_picture').state == 'startup' + assert hass.states.get('sensor.invalid_friendly_name').state == 'startup' await hass.helpers.entity_component.async_update_entity( 'sensor.invalid_state')