Refresh now() templates on second=0 (#42225)

This commit is contained in:
Anders Melchiorsen 2020-10-23 00:11:02 +02:00 committed by GitHub
parent 4e917cf62c
commit cb0c937b3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 11 deletions

View File

@ -792,11 +792,13 @@ class _TrackTemplateResultInfo:
@callback @callback
def _setup_time_listener(self, template: Template, has_time: bool) -> None: def _setup_time_listener(self, template: Template, has_time: bool) -> None:
if template in self._time_listeners:
self._time_listeners.pop(template)()
# now() or utcnow() has left the scope of the template
if not has_time: if not has_time:
if template in self._time_listeners:
# now() or utcnow() has left the scope of the template
self._time_listeners.pop(template)()
return
if template in self._time_listeners:
return return
track_templates = [ track_templates = [
@ -809,8 +811,8 @@ class _TrackTemplateResultInfo:
def _refresh_from_time(now: datetime) -> None: def _refresh_from_time(now: datetime) -> None:
self._refresh(None, track_templates=track_templates) self._refresh(None, track_templates=track_templates)
self._time_listeners[template] = async_call_later( self._time_listeners[template] = async_track_utc_time_change(
self.hass, 60.45, _refresh_from_time self.hass, _refresh_from_time, second=0
) )
@callback @callback

View File

@ -2172,15 +2172,17 @@ async def test_track_template_with_time_that_leaves_scope(hass):
"time": True, "time": True,
} }
# Verify we do not update a second time # Verify we do not update before the minute rolls over
# if the state change happens
callback_count_before_time_change = len(specific_runs) callback_count_before_time_change = len(specific_runs)
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=59)) test_time = dt_util.utcnow().replace(second=1)
async_fire_time_changed(hass, test_time)
await hass.async_block_till_done()
async_fire_time_changed(hass, test_time + timedelta(seconds=58))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == callback_count_before_time_change assert len(specific_runs) == callback_count_before_time_change
# Verify we do update on the next time change # Verify we do update on the next change of minute
async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=61)) async_fire_time_changed(hass, test_time + timedelta(seconds=59))
await hass.async_block_till_done() await hass.async_block_till_done()
assert len(specific_runs) == callback_count_before_time_change + 1 assert len(specific_runs) == callback_count_before_time_change + 1