Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve precision of timer ticks #16598

Merged
merged 1 commit into from
Sep 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion homeassistant/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1256,4 +1256,5 @@ def stop_timer(_: Event) -> None:
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, stop_timer)

_LOGGER.info("Timer:starting")
fire_time_event(monotonic())
slp_seconds = 1 - (dt_util.utcnow().microsecond / 10**6)
hass.loop.call_later(slp_seconds, lambda: fire_time_event(monotonic()))
26 changes: 20 additions & 6 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -862,21 +862,29 @@ def mock_callback(func):

with patch.object(ha, 'callback', mock_callback), \
patch('homeassistant.core.dt_util.utcnow',
return_value=sentinel.mock_date):
return_value=datetime(2018, 12, 31, 3, 4, 5, 333333)):
ha._async_create_timer(hass)

assert len(hass.loop.call_later.mock_calls) == 1
slp_seconds, action = hass.loop.call_later.mock_calls[0][1]
assert abs(slp_seconds - 0.666667) < 0.001

with patch('homeassistant.core.dt_util.utcnow',
return_value=sentinel.mock_date):
action()

assert len(funcs) == 2
fire_time_event, stop_timer = funcs

assert len(hass.bus.async_listen_once.mock_calls) == 1
assert len(hass.bus.async_fire.mock_calls) == 1
assert len(hass.loop.call_later.mock_calls) == 1
assert len(hass.loop.call_later.mock_calls) == 2

event_type, callback = hass.bus.async_listen_once.mock_calls[0][1]
assert event_type == EVENT_HOMEASSISTANT_STOP
assert callback is stop_timer

slp_seconds, callback, nxt = hass.loop.call_later.mock_calls[0][1]
slp_seconds, callback, nxt = hass.loop.call_later.mock_calls[1][1]
assert abs(slp_seconds - 0.9) < 0.001
assert callback is fire_time_event
assert abs(nxt - 11.2) < 0.001
Expand All @@ -901,15 +909,21 @@ def mock_callback(func):

with patch.object(ha, 'callback', mock_callback), \
patch('homeassistant.core.dt_util.utcnow',
return_value=sentinel.mock_date):
return_value=datetime(2018, 12, 31, 3, 4, 5, 333333)):
ha._async_create_timer(hass)

_, action = hass.loop.call_later.mock_calls[0][1]

with patch('homeassistant.core.dt_util.utcnow',
return_value=sentinel.mock_date):
action()

assert len(funcs) == 2
fire_time_event, stop_timer = funcs

assert len(hass.loop.call_later.mock_calls) == 1
assert len(hass.loop.call_later.mock_calls) == 2

slp_seconds, callback, nxt = hass.loop.call_later.mock_calls[0][1]
slp_seconds, callback, nxt = hass.loop.call_later.mock_calls[1][1]
assert slp_seconds == 1
assert callback is fire_time_event
assert abs(nxt - 12.3) < 0.001
Expand Down