Async reduce coro (#5001)

* Asyncio coro reduce

* Replace comments
This commit is contained in:
Pascal Vizeli 2016-12-26 14:10:23 +01:00 committed by Paulus Schoutsen
parent 22d1bf0acd
commit 244cdf43d0
6 changed files with 47 additions and 36 deletions

View File

@ -152,40 +152,48 @@ class AlarmControlPanel(Entity):
"""Send disarm command.""" """Send disarm command."""
raise NotImplementedError() raise NotImplementedError()
@asyncio.coroutine
def async_alarm_disarm(self, code=None): def async_alarm_disarm(self, code=None):
"""Send disarm command.""" """Send disarm command.
yield from self.hass.loop.run_in_executor(
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) None, self.alarm_disarm, code)
def alarm_arm_home(self, code=None): def alarm_arm_home(self, code=None):
"""Send arm home command.""" """Send arm home command."""
raise NotImplementedError() raise NotImplementedError()
@asyncio.coroutine
def async_alarm_arm_home(self, code=None): def async_alarm_arm_home(self, code=None):
"""Send arm home command.""" """Send arm home command.
yield from self.hass.loop.run_in_executor(
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) None, self.alarm_arm_home, code)
def alarm_arm_away(self, code=None): def alarm_arm_away(self, code=None):
"""Send arm away command.""" """Send arm away command."""
raise NotImplementedError() raise NotImplementedError()
@asyncio.coroutine
def async_alarm_arm_away(self, code=None): def async_alarm_arm_away(self, code=None):
"""Send arm away command.""" """Send arm away command.
yield from self.hass.loop.run_in_executor(
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) None, self.alarm_arm_away, code)
def alarm_trigger(self, code=None): def alarm_trigger(self, code=None):
"""Send alarm trigger command.""" """Send alarm trigger command."""
raise NotImplementedError() raise NotImplementedError()
@asyncio.coroutine
def async_alarm_trigger(self, code=None): def async_alarm_trigger(self, code=None):
"""Send alarm trigger command.""" """Send alarm trigger command.
yield from self.hass.loop.run_in_executor(
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) None, self.alarm_trigger, code)
@property @property

View File

@ -81,15 +81,12 @@ class Camera(Entity):
"""Return bytes of camera image.""" """Return bytes of camera image."""
raise NotImplementedError() raise NotImplementedError()
@asyncio.coroutine
def async_camera_image(self): def async_camera_image(self):
"""Return bytes of camera image. """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( return self.hass.loop.run_in_executor(None, self.camera_image)
None, self.camera_image)
return image
@asyncio.coroutine @asyncio.coroutine
def handle_async_mjpeg_stream(self, request): def handle_async_mjpeg_stream(self, request):

View File

@ -149,6 +149,9 @@ class RemoteDevice(ToggleEntity):
raise NotImplementedError() raise NotImplementedError()
def async_send_command(self, **kwargs): def async_send_command(self, **kwargs):
"""Send a command to a device.""" """Send a command to a device.
yield from self.hass.loop.run_in_executor(
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)) None, ft.partial(self.send_command, **kwargs))

View File

@ -96,10 +96,9 @@ class Scene(Entity):
"""Activate scene. Try to get entities into requested state.""" """Activate scene. Try to get entities into requested state."""
raise NotImplementedError() raise NotImplementedError()
@asyncio.coroutine
def async_activate(self): def async_activate(self):
"""Activate scene. Try to get entities into requested state. """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)

View File

@ -384,17 +384,15 @@ class Provider(object):
"""Load tts audio file from provider.""" """Load tts audio file from provider."""
raise NotImplementedError() raise NotImplementedError()
@asyncio.coroutine
def async_get_tts_audio(self, message): def async_get_tts_audio(self, message):
"""Load tts audio file from provider. """Load tts audio file from provider.
Return a tuple of file extension and data as bytes. 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) None, self.get_tts_audio, message)
return (extension, data)
class TextToSpeechView(HomeAssistantView): class TextToSpeechView(HomeAssistantView):

View File

@ -346,20 +346,24 @@ class ToggleEntity(Entity):
"""Turn the entity on.""" """Turn the entity on."""
raise NotImplementedError() raise NotImplementedError()
@asyncio.coroutine
def async_turn_on(self, **kwargs): def async_turn_on(self, **kwargs):
"""Turn the entity on.""" """Turn the entity on.
yield from self.hass.loop.run_in_executor(
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)) None, ft.partial(self.turn_on, **kwargs))
def turn_off(self, **kwargs) -> None: def turn_off(self, **kwargs) -> None:
"""Turn the entity off.""" """Turn the entity off."""
raise NotImplementedError() raise NotImplementedError()
@asyncio.coroutine
def async_turn_off(self, **kwargs): def async_turn_off(self, **kwargs):
"""Turn the entity off.""" """Turn the entity off.
yield from self.hass.loop.run_in_executor(
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)) None, ft.partial(self.turn_off, **kwargs))
def toggle(self) -> None: def toggle(self) -> None:
@ -369,10 +373,12 @@ class ToggleEntity(Entity):
else: else:
self.turn_on() self.turn_on()
@asyncio.coroutine
def async_toggle(self): 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: if self.is_on:
yield from self.async_turn_off() return self.async_turn_off()
else: else:
yield from self.async_turn_on() return self.async_turn_on()