still update sensor on startup (#17319)

This commit is contained in:
Paulus Schoutsen 2018-10-11 11:38:35 +02:00 committed by Pascal Vizeli
parent f5d3aa1826
commit ebff253cc9
2 changed files with 20 additions and 11 deletions

View File

@ -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)

View File

@ -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')