1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 21:06:19 +00:00

Hello Python 3.5 (#12610)

* Hello Python 3.5

* Fix test

* Fix tests

* Fix never awaited block till done warnings
This commit is contained in:
Paulus Schoutsen
2018-02-22 23:22:27 -08:00
committed by GitHub
parent 156206dfee
commit 6ee3c1b3e5
10 changed files with 36 additions and 56 deletions

View File

@@ -164,8 +164,7 @@ class HomeAssistant(object):
finally:
self.loop.close()
@asyncio.coroutine
def async_start(self):
async def async_start(self):
"""Finalize startup from inside the event loop.
This method is a coroutine.
@@ -181,7 +180,7 @@ class HomeAssistant(object):
# Only block for EVENT_HOMEASSISTANT_START listener
self.async_stop_track_tasks()
with timeout(TIMEOUT_EVENT_START, loop=self.loop):
yield from self.async_block_till_done()
await self.async_block_till_done()
except asyncio.TimeoutError:
_LOGGER.warning(
'Something is blocking Home Assistant from wrapping up the '
@@ -190,7 +189,7 @@ class HomeAssistant(object):
', '.join(self.config.components))
# Allow automations to set up the start triggers before changing state
yield from asyncio.sleep(0, loop=self.loop)
await asyncio.sleep(0, loop=self.loop)
self.state = CoreState.running
_async_create_timer(self)
@@ -259,27 +258,25 @@ class HomeAssistant(object):
run_coroutine_threadsafe(
self.async_block_till_done(), loop=self.loop).result()
@asyncio.coroutine
def async_block_till_done(self):
async def async_block_till_done(self):
"""Block till all pending work is done."""
# To flush out any call_soon_threadsafe
yield from asyncio.sleep(0, loop=self.loop)
await asyncio.sleep(0, loop=self.loop)
while self._pending_tasks:
pending = [task for task in self._pending_tasks
if not task.done()]
self._pending_tasks.clear()
if pending:
yield from asyncio.wait(pending, loop=self.loop)
await asyncio.wait(pending, loop=self.loop)
else:
yield from asyncio.sleep(0, loop=self.loop)
await asyncio.sleep(0, loop=self.loop)
def stop(self) -> None:
"""Stop Home Assistant and shuts down all threads."""
fire_coroutine_threadsafe(self.async_stop(), self.loop)
@asyncio.coroutine
def async_stop(self, exit_code=0) -> None:
async def async_stop(self, exit_code=0) -> None:
"""Stop Home Assistant and shuts down all threads.
This method is a coroutine.
@@ -288,12 +285,12 @@ class HomeAssistant(object):
self.state = CoreState.stopping
self.async_track_tasks()
self.bus.async_fire(EVENT_HOMEASSISTANT_STOP)
yield from self.async_block_till_done()
await self.async_block_till_done()
# stage 2
self.state = CoreState.not_running
self.bus.async_fire(EVENT_HOMEASSISTANT_CLOSE)
yield from self.async_block_till_done()
await self.async_block_till_done()
self.executor.shutdown()
self.exit_code = exit_code
@@ -912,8 +909,8 @@ class ServiceRegistry(object):
self._hass.loop
).result()
@asyncio.coroutine
def async_call(self, domain, service, service_data=None, blocking=False):
async def async_call(self, domain, service, service_data=None,
blocking=False):
"""
Call a service.
@@ -956,14 +953,13 @@ class ServiceRegistry(object):
self._hass.bus.async_fire(EVENT_CALL_SERVICE, event_data)
if blocking:
done, _ = yield from asyncio.wait(
done, _ = await asyncio.wait(
[fut], loop=self._hass.loop, timeout=SERVICE_CALL_LIMIT)
success = bool(done)
unsub()
return success
@asyncio.coroutine
def _event_to_service_call(self, event):
async def _event_to_service_call(self, event):
"""Handle the SERVICE_CALLED events from the EventBus."""
service_data = event.data.get(ATTR_SERVICE_DATA) or {}
domain = event.data.get(ATTR_DOMAIN).lower()
@@ -1007,7 +1003,7 @@ class ServiceRegistry(object):
service_handler.func(service_call)
fire_service_executed()
elif service_handler.is_coroutinefunction:
yield from service_handler.func(service_call)
await service_handler.func(service_call)
fire_service_executed()
else:
def execute_service():
@@ -1015,7 +1011,7 @@ class ServiceRegistry(object):
service_handler.func(service_call)
fire_service_executed()
yield from self._hass.async_add_job(execute_service)
await self._hass.async_add_job(execute_service)
except Exception: # pylint: disable=broad-except
_LOGGER.exception('Error executing service %s', service_call)