From 244cdf43d04317aea0e0a9f2a50f7cc24b9d63a2 Mon Sep 17 00:00:00 2001 From: Pascal Vizeli <pascal.vizeli@syshack.ch> Date: Mon, 26 Dec 2016 14:10:23 +0100 Subject: [PATCH] Async reduce coro (#5001) * Asyncio coro reduce * Replace comments --- .../alarm_control_panel/__init__.py | 32 ++++++++++++------- homeassistant/components/camera/__init__.py | 7 ++-- homeassistant/components/remote/__init__.py | 7 ++-- homeassistant/components/scene/__init__.py | 5 ++- homeassistant/components/tts/__init__.py | 6 ++-- homeassistant/helpers/entity.py | 26 +++++++++------ 6 files changed, 47 insertions(+), 36 deletions(-) diff --git a/homeassistant/components/alarm_control_panel/__init__.py b/homeassistant/components/alarm_control_panel/__init__.py index 54be6aa4d0b..ea7727cea33 100644 --- a/homeassistant/components/alarm_control_panel/__init__.py +++ b/homeassistant/components/alarm_control_panel/__init__.py @@ -152,40 +152,48 @@ class AlarmControlPanel(Entity): """Send disarm command.""" raise NotImplementedError() - @asyncio.coroutine def async_alarm_disarm(self, code=None): - """Send disarm command.""" - yield from self.hass.loop.run_in_executor( + """Send disarm command. + + This method must be run in the event loop and returns a coroutine. + """ + return self.hass.loop.run_in_executor( None, self.alarm_disarm, code) def alarm_arm_home(self, code=None): """Send arm home command.""" raise NotImplementedError() - @asyncio.coroutine def async_alarm_arm_home(self, code=None): - """Send arm home command.""" - yield from self.hass.loop.run_in_executor( + """Send arm home command. + + This method must be run in the event loop and returns a coroutine. + """ + return self.hass.loop.run_in_executor( None, self.alarm_arm_home, code) def alarm_arm_away(self, code=None): """Send arm away command.""" raise NotImplementedError() - @asyncio.coroutine def async_alarm_arm_away(self, code=None): - """Send arm away command.""" - yield from self.hass.loop.run_in_executor( + """Send arm away command. + + This method must be run in the event loop and returns a coroutine. + """ + return self.hass.loop.run_in_executor( None, self.alarm_arm_away, code) def alarm_trigger(self, code=None): """Send alarm trigger command.""" raise NotImplementedError() - @asyncio.coroutine def async_alarm_trigger(self, code=None): - """Send alarm trigger command.""" - yield from self.hass.loop.run_in_executor( + """Send alarm trigger command. + + This method must be run in the event loop and returns a coroutine. + """ + return self.hass.loop.run_in_executor( None, self.alarm_trigger, code) @property diff --git a/homeassistant/components/camera/__init__.py b/homeassistant/components/camera/__init__.py index 427d4535ef6..8a114cb627d 100644 --- a/homeassistant/components/camera/__init__.py +++ b/homeassistant/components/camera/__init__.py @@ -81,15 +81,12 @@ class Camera(Entity): """Return bytes of camera image.""" raise NotImplementedError() - @asyncio.coroutine def async_camera_image(self): """Return bytes of camera image. - This method must be run in the event loop. + This method must be run in the event loop and returns a coroutine. """ - image = yield from self.hass.loop.run_in_executor( - None, self.camera_image) - return image + return self.hass.loop.run_in_executor(None, self.camera_image) @asyncio.coroutine def handle_async_mjpeg_stream(self, request): diff --git a/homeassistant/components/remote/__init__.py b/homeassistant/components/remote/__init__.py index 2baef2011fc..118a160c305 100755 --- a/homeassistant/components/remote/__init__.py +++ b/homeassistant/components/remote/__init__.py @@ -149,6 +149,9 @@ class RemoteDevice(ToggleEntity): raise NotImplementedError() def async_send_command(self, **kwargs): - """Send a command to a device.""" - yield from self.hass.loop.run_in_executor( + """Send a command to a device. + + This method must be run in the event loop and returns a coroutine. + """ + return self.hass.loop.run_in_executor( None, ft.partial(self.send_command, **kwargs)) diff --git a/homeassistant/components/scene/__init__.py b/homeassistant/components/scene/__init__.py index 3f532a33151..7e20338f4ab 100644 --- a/homeassistant/components/scene/__init__.py +++ b/homeassistant/components/scene/__init__.py @@ -96,10 +96,9 @@ class Scene(Entity): """Activate scene. Try to get entities into requested state.""" raise NotImplementedError() - @asyncio.coroutine def async_activate(self): """Activate scene. Try to get entities into requested state. - This method is a coroutine. + This method must be run in the event loop and returns a coroutine. """ - yield from self.hass.loop.run_in_executor(None, self.activate) + return self.hass.loop.run_in_executor(None, self.activate) diff --git a/homeassistant/components/tts/__init__.py b/homeassistant/components/tts/__init__.py index 32cbbaa265b..bd19de52a98 100644 --- a/homeassistant/components/tts/__init__.py +++ b/homeassistant/components/tts/__init__.py @@ -384,17 +384,15 @@ class Provider(object): """Load tts audio file from provider.""" raise NotImplementedError() - @asyncio.coroutine def async_get_tts_audio(self, message): """Load tts audio file from provider. Return a tuple of file extension and data as bytes. - This method is a coroutine. + This method must be run in the event loop and returns a coroutine. """ - extension, data = yield from self.hass.loop.run_in_executor( + return self.hass.loop.run_in_executor( None, self.get_tts_audio, message) - return (extension, data) class TextToSpeechView(HomeAssistantView): diff --git a/homeassistant/helpers/entity.py b/homeassistant/helpers/entity.py index 4137a31b8b6..0d2f56f1807 100644 --- a/homeassistant/helpers/entity.py +++ b/homeassistant/helpers/entity.py @@ -346,20 +346,24 @@ class ToggleEntity(Entity): """Turn the entity on.""" raise NotImplementedError() - @asyncio.coroutine def async_turn_on(self, **kwargs): - """Turn the entity on.""" - yield from self.hass.loop.run_in_executor( + """Turn the entity on. + + This method must be run in the event loop and returns a coroutine. + """ + return self.hass.loop.run_in_executor( None, ft.partial(self.turn_on, **kwargs)) def turn_off(self, **kwargs) -> None: """Turn the entity off.""" raise NotImplementedError() - @asyncio.coroutine def async_turn_off(self, **kwargs): - """Turn the entity off.""" - yield from self.hass.loop.run_in_executor( + """Turn the entity off. + + This method must be run in the event loop and returns a coroutine. + """ + return self.hass.loop.run_in_executor( None, ft.partial(self.turn_off, **kwargs)) def toggle(self) -> None: @@ -369,10 +373,12 @@ class ToggleEntity(Entity): else: self.turn_on() - @asyncio.coroutine def async_toggle(self): - """Toggle the entity.""" + """Toggle the entity. + + This method must be run in the event loop and returns a coroutine. + """ if self.is_on: - yield from self.async_turn_off() + return self.async_turn_off() else: - yield from self.async_turn_on() + return self.async_turn_on() -- GitLab