mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Bugfix time and task coro (#6968)
* Bugfix time and task coro * fix also other create_task * fix tests * fix lint in test
This commit is contained in:
parent
2ce8c2f80e
commit
74ac160355
@ -31,7 +31,8 @@ from homeassistant.const import (
|
|||||||
from homeassistant.exceptions import (
|
from homeassistant.exceptions import (
|
||||||
HomeAssistantError, InvalidEntityFormatError)
|
HomeAssistantError, InvalidEntityFormatError)
|
||||||
from homeassistant.util.async import (
|
from homeassistant.util.async import (
|
||||||
run_coroutine_threadsafe, run_callback_threadsafe)
|
run_coroutine_threadsafe, run_callback_threadsafe,
|
||||||
|
fire_coroutine_threadsafe)
|
||||||
import homeassistant.util as util
|
import homeassistant.util as util
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
import homeassistant.util.location as location
|
import homeassistant.util.location as location
|
||||||
@ -131,7 +132,7 @@ class HomeAssistant(object):
|
|||||||
def start(self) -> None:
|
def start(self) -> None:
|
||||||
"""Start home assistant."""
|
"""Start home assistant."""
|
||||||
# Register the async start
|
# Register the async start
|
||||||
self.add_job(self.async_start())
|
fire_coroutine_threadsafe(self.async_start(), self.loop)
|
||||||
|
|
||||||
# Run forever and catch keyboard interrupt
|
# Run forever and catch keyboard interrupt
|
||||||
try:
|
try:
|
||||||
@ -140,7 +141,7 @@ class HomeAssistant(object):
|
|||||||
self.loop.run_forever()
|
self.loop.run_forever()
|
||||||
return self.exit_code
|
return self.exit_code
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
self.loop.create_task(self.async_stop())
|
fire_coroutine_threadsafe(self.async_stop(), self.loop)
|
||||||
self.loop.run_forever()
|
self.loop.run_forever()
|
||||||
finally:
|
finally:
|
||||||
self.loop.close()
|
self.loop.close()
|
||||||
@ -246,8 +247,7 @@ class HomeAssistant(object):
|
|||||||
|
|
||||||
def stop(self) -> None:
|
def stop(self) -> None:
|
||||||
"""Stop Home Assistant and shuts down all threads."""
|
"""Stop Home Assistant and shuts down all threads."""
|
||||||
self.loop.call_soon_threadsafe(
|
fire_coroutine_threadsafe(self.async_stop(), self.loop)
|
||||||
self.loop.create_task, self.async_stop())
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_stop(self, exit_code=0) -> None:
|
def async_stop(self, exit_code=0) -> None:
|
||||||
@ -1091,17 +1091,13 @@ def _async_create_timer(hass):
|
|||||||
|
|
||||||
handle = hass.loop.call_later(slp_seconds, fire_time_event, nxt)
|
handle = hass.loop.call_later(slp_seconds, fire_time_event, nxt)
|
||||||
|
|
||||||
@callback
|
|
||||||
def start_timer(event):
|
|
||||||
"""Create an async timer."""
|
|
||||||
_LOGGER.info("Timer:starting")
|
|
||||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, stop_timer)
|
|
||||||
fire_time_event(monotonic())
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def stop_timer(event):
|
def stop_timer(event):
|
||||||
"""Stop the timer."""
|
"""Stop the timer."""
|
||||||
if handle is not None:
|
if handle is not None:
|
||||||
handle.cancel()
|
handle.cancel()
|
||||||
|
|
||||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, start_timer)
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, stop_timer)
|
||||||
|
|
||||||
|
_LOGGER.info("Timer:starting")
|
||||||
|
fire_time_event(monotonic())
|
||||||
|
@ -16,8 +16,7 @@ from homeassistant.util.unit_system import (METRIC_SYSTEM)
|
|||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
__version__, EVENT_STATE_CHANGED, ATTR_FRIENDLY_NAME, CONF_UNIT_SYSTEM,
|
__version__, EVENT_STATE_CHANGED, ATTR_FRIENDLY_NAME, CONF_UNIT_SYSTEM,
|
||||||
ATTR_NOW, EVENT_TIME_CHANGED, EVENT_HOMEASSISTANT_STOP,
|
ATTR_NOW, EVENT_TIME_CHANGED, EVENT_HOMEASSISTANT_STOP,
|
||||||
EVENT_HOMEASSISTANT_CLOSE, EVENT_HOMEASSISTANT_START,
|
EVENT_HOMEASSISTANT_CLOSE, EVENT_SERVICE_REGISTERED, EVENT_SERVICE_REMOVED)
|
||||||
EVENT_SERVICE_REGISTERED, EVENT_SERVICE_REMOVED)
|
|
||||||
|
|
||||||
from tests.common import get_test_home_assistant
|
from tests.common import get_test_home_assistant
|
||||||
|
|
||||||
@ -813,28 +812,21 @@ def test_create_timer(mock_monotonic, loop):
|
|||||||
funcs.append(func)
|
funcs.append(func)
|
||||||
return orig_callback(func)
|
return orig_callback(func)
|
||||||
|
|
||||||
with patch.object(ha, 'callback', mock_callback):
|
|
||||||
ha._async_create_timer(hass)
|
|
||||||
|
|
||||||
assert len(funcs) == 3
|
|
||||||
fire_time_event, start_timer, stop_timer = funcs
|
|
||||||
|
|
||||||
assert len(hass.bus.async_listen_once.mock_calls) == 1
|
|
||||||
event_type, callback = hass.bus.async_listen_once.mock_calls[0][1]
|
|
||||||
assert event_type == EVENT_HOMEASSISTANT_START
|
|
||||||
assert callback is start_timer
|
|
||||||
|
|
||||||
mock_monotonic.side_effect = 10.2, 10.3
|
mock_monotonic.side_effect = 10.2, 10.3
|
||||||
|
|
||||||
with patch('homeassistant.core.dt_util.utcnow',
|
with patch.object(ha, 'callback', mock_callback), \
|
||||||
return_value=sentinel.mock_date):
|
patch('homeassistant.core.dt_util.utcnow',
|
||||||
start_timer(None)
|
return_value=sentinel.mock_date):
|
||||||
|
ha._async_create_timer(hass)
|
||||||
|
|
||||||
assert len(hass.bus.async_listen_once.mock_calls) == 2
|
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.bus.async_fire.mock_calls) == 1
|
||||||
assert len(hass.loop.call_later.mock_calls) == 1
|
assert len(hass.loop.call_later.mock_calls) == 1
|
||||||
|
|
||||||
event_type, callback = hass.bus.async_listen_once.mock_calls[1][1]
|
event_type, callback = hass.bus.async_listen_once.mock_calls[0][1]
|
||||||
assert event_type == EVENT_HOMEASSISTANT_STOP
|
assert event_type == EVENT_HOMEASSISTANT_STOP
|
||||||
assert callback is stop_timer
|
assert callback is stop_timer
|
||||||
|
|
||||||
@ -859,17 +851,15 @@ def test_timer_out_of_sync(mock_monotonic, loop):
|
|||||||
funcs.append(func)
|
funcs.append(func)
|
||||||
return orig_callback(func)
|
return orig_callback(func)
|
||||||
|
|
||||||
with patch.object(ha, 'callback', mock_callback):
|
|
||||||
ha._async_create_timer(hass)
|
|
||||||
|
|
||||||
assert len(funcs) == 3
|
|
||||||
fire_time_event, start_timer, stop_timer = funcs
|
|
||||||
|
|
||||||
mock_monotonic.side_effect = 10.2, 11.3, 11.3
|
mock_monotonic.side_effect = 10.2, 11.3, 11.3
|
||||||
|
|
||||||
with patch('homeassistant.core.dt_util.utcnow',
|
with patch.object(ha, 'callback', mock_callback), \
|
||||||
return_value=sentinel.mock_date):
|
patch('homeassistant.core.dt_util.utcnow',
|
||||||
start_timer(None)
|
return_value=sentinel.mock_date):
|
||||||
|
ha._async_create_timer(hass)
|
||||||
|
|
||||||
|
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) == 1
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user