mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Remove EVENT_TIME_CHANGED and EVENT_TIMER_OUT_OF_SYNC (#69643)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
@@ -13,8 +13,6 @@ import voluptuous as vol
|
||||
|
||||
from homeassistant.const import (
|
||||
ATTR_FRIENDLY_NAME,
|
||||
ATTR_NOW,
|
||||
ATTR_SECONDS,
|
||||
CONF_UNIT_SYSTEM,
|
||||
EVENT_CALL_SERVICE,
|
||||
EVENT_CORE_CONFIG_UPDATE,
|
||||
@@ -26,8 +24,6 @@ from homeassistant.const import (
|
||||
EVENT_SERVICE_REGISTERED,
|
||||
EVENT_SERVICE_REMOVED,
|
||||
EVENT_STATE_CHANGED,
|
||||
EVENT_TIME_CHANGED,
|
||||
EVENT_TIMER_OUT_OF_SYNC,
|
||||
MATCH_ALL,
|
||||
__version__,
|
||||
)
|
||||
@@ -1061,129 +1057,6 @@ async def test_bad_timezone_raises_value_error(hass):
|
||||
await hass.config.async_update(time_zone="not_a_timezone")
|
||||
|
||||
|
||||
@patch("homeassistant.core.monotonic")
|
||||
def test_create_timer(mock_monotonic, loop):
|
||||
"""Test create timer."""
|
||||
hass = MagicMock()
|
||||
funcs = []
|
||||
orig_callback = ha.callback
|
||||
|
||||
def mock_callback(func):
|
||||
funcs.append(func)
|
||||
return orig_callback(func)
|
||||
|
||||
mock_monotonic.side_effect = 10.2, 10.8, 11.3
|
||||
|
||||
with patch.object(ha, "callback", mock_callback), patch(
|
||||
"homeassistant.core.dt_util.utcnow",
|
||||
return_value=datetime(2018, 12, 31, 3, 4, 5, 333333),
|
||||
):
|
||||
ha._async_create_timer(hass)
|
||||
|
||||
assert len(funcs) == 2
|
||||
fire_time_event, stop_timer = funcs
|
||||
|
||||
assert len(hass.loop.call_later.mock_calls) == 1
|
||||
delay, callback, target = hass.loop.call_later.mock_calls[0][1]
|
||||
assert abs(delay - 0.666667) < 0.001
|
||||
assert callback is fire_time_event
|
||||
assert abs(target - 10.866667) < 0.001
|
||||
|
||||
with patch(
|
||||
"homeassistant.core.dt_util.utcnow",
|
||||
return_value=datetime(2018, 12, 31, 3, 4, 6, 100000),
|
||||
):
|
||||
callback(target)
|
||||
|
||||
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) == 2
|
||||
|
||||
event_type, callback = hass.bus.async_listen_once.mock_calls[0][1]
|
||||
assert event_type == EVENT_HOMEASSISTANT_STOP
|
||||
assert callback is stop_timer
|
||||
|
||||
delay, callback, target = hass.loop.call_later.mock_calls[1][1]
|
||||
assert abs(delay - 0.9) < 0.001
|
||||
assert callback is fire_time_event
|
||||
assert abs(target - 12.2) < 0.001
|
||||
|
||||
event_type, event_data = hass.bus.async_fire.mock_calls[0][1]
|
||||
assert event_type == EVENT_TIME_CHANGED
|
||||
assert event_data[ATTR_NOW] == datetime(2018, 12, 31, 3, 4, 6, 100000)
|
||||
|
||||
|
||||
@patch("homeassistant.core.monotonic")
|
||||
def test_timer_out_of_sync(mock_monotonic, loop):
|
||||
"""Test create timer."""
|
||||
hass = MagicMock()
|
||||
funcs = []
|
||||
orig_callback = ha.callback
|
||||
|
||||
def mock_callback(func):
|
||||
funcs.append(func)
|
||||
return orig_callback(func)
|
||||
|
||||
mock_monotonic.side_effect = 10.2, 13.3, 13.4
|
||||
|
||||
with patch.object(ha, "callback", mock_callback), patch(
|
||||
"homeassistant.core.dt_util.utcnow",
|
||||
return_value=datetime(2018, 12, 31, 3, 4, 5, 333333),
|
||||
):
|
||||
ha._async_create_timer(hass)
|
||||
|
||||
delay, callback, target = hass.loop.call_later.mock_calls[0][1]
|
||||
|
||||
with patch(
|
||||
"homeassistant.core.dt_util.utcnow",
|
||||
return_value=datetime(2018, 12, 31, 3, 4, 8, 200000),
|
||||
):
|
||||
callback(target)
|
||||
|
||||
_, event_0_args, event_0_kwargs = hass.bus.async_fire.mock_calls[0]
|
||||
event_context_0 = event_0_kwargs["context"]
|
||||
|
||||
event_type_0, _ = event_0_args
|
||||
assert event_type_0 == EVENT_TIME_CHANGED
|
||||
|
||||
_, event_1_args, event_1_kwargs = hass.bus.async_fire.mock_calls[1]
|
||||
event_type_1, event_data_1 = event_1_args
|
||||
event_context_1 = event_1_kwargs["context"]
|
||||
|
||||
assert event_type_1 == EVENT_TIMER_OUT_OF_SYNC
|
||||
assert abs(event_data_1[ATTR_SECONDS] - 2.433333) < 0.001
|
||||
|
||||
assert event_context_0 == event_context_1
|
||||
|
||||
assert len(funcs) == 2
|
||||
fire_time_event, _ = funcs
|
||||
|
||||
assert len(hass.loop.call_later.mock_calls) == 2
|
||||
|
||||
delay, callback, target = hass.loop.call_later.mock_calls[1][1]
|
||||
assert abs(delay - 0.8) < 0.001
|
||||
assert callback is fire_time_event
|
||||
assert abs(target - 14.2) < 0.001
|
||||
|
||||
|
||||
async def test_hass_start_starts_the_timer(loop):
|
||||
"""Test when hass starts, it starts the timer."""
|
||||
hass = ha.HomeAssistant()
|
||||
|
||||
try:
|
||||
with patch("homeassistant.core._async_create_timer") as mock_timer:
|
||||
await hass.async_start()
|
||||
|
||||
assert hass.state == ha.CoreState.running
|
||||
assert not hass._track_task
|
||||
assert len(mock_timer.mock_calls) == 1
|
||||
assert mock_timer.mock_calls[0][1][0] is hass
|
||||
|
||||
finally:
|
||||
await hass.async_stop()
|
||||
assert hass.state == ha.CoreState.stopped
|
||||
|
||||
|
||||
async def test_start_taking_too_long(loop, caplog):
|
||||
"""Test when async_start takes too long."""
|
||||
hass = ha.HomeAssistant()
|
||||
@@ -1192,12 +1065,10 @@ async def test_start_taking_too_long(loop, caplog):
|
||||
try:
|
||||
with patch.object(
|
||||
hass, "async_block_till_done", side_effect=asyncio.TimeoutError
|
||||
), patch("homeassistant.core._async_create_timer") as mock_timer:
|
||||
):
|
||||
await hass.async_start()
|
||||
|
||||
assert hass.state == ha.CoreState.running
|
||||
assert len(mock_timer.mock_calls) == 1
|
||||
assert mock_timer.mock_calls[0][1][0] is hass
|
||||
assert "Something is blocking Home Assistant" in caplog.text
|
||||
|
||||
finally:
|
||||
|
||||
Reference in New Issue
Block a user