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."""
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

View File

@ -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):

View File

@ -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))

View File

@ -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)

View File

@ -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):

View File

@ -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()