Migrate remote to async (#4678)

* Migrate remote to async

* add coro

* remove sync from init since only used in harmony

* import ATTR from remote

* remove unused sync stuff from tests
This commit is contained in:
Pascal Vizeli 2016-12-04 07:08:24 +01:00 committed by Paulus Schoutsen
parent ca63e44227
commit e8c9dcf0fe
4 changed files with 43 additions and 58 deletions

View file

@ -4,7 +4,9 @@ Component to interface with universal remote control devices.
For more details about this component, please refer to the documentation For more details about this component, please refer to the documentation
at https://home-assistant.io/components/remote/ at https://home-assistant.io/components/remote/
""" """
import asyncio
from datetime import timedelta from datetime import timedelta
import functools as ft
import logging import logging
import os import os
@ -80,50 +82,57 @@ def send_command(hass, device, command, entity_id=None):
hass.services.call(DOMAIN, SERVICE_SEND_COMMAND, data) hass.services.call(DOMAIN, SERVICE_SEND_COMMAND, data)
def sync(hass, entity_id=None): @asyncio.coroutine
"""Sync remote device.""" def async_setup(hass, config):
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
hass.services.call(DOMAIN, SERVICE_SYNC, data)
def setup(hass, config):
"""Track states and offer events for remotes.""" """Track states and offer events for remotes."""
component = EntityComponent( component = EntityComponent(
_LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_REMOTES) _LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_REMOTES)
component.setup(config) yield from component.async_setup(config)
def handle_remote_service(service): @asyncio.coroutine
def async_handle_remote_service(service):
"""Handle calls to the remote services.""" """Handle calls to the remote services."""
target_remotes = component.extract_from_service(service) target_remotes = component.async_extract_from_service(service)
activity_id = service.data.get(ATTR_ACTIVITY) activity_id = service.data.get(ATTR_ACTIVITY)
device = service.data.get(ATTR_DEVICE) device = service.data.get(ATTR_DEVICE)
command = service.data.get(ATTR_COMMAND) command = service.data.get(ATTR_COMMAND)
update_tasks = []
for remote in target_remotes: for remote in target_remotes:
if service.service == SERVICE_TURN_ON: if service.service == SERVICE_TURN_ON:
remote.turn_on(activity=activity_id) yield from remote.async_turn_on(activity=activity_id)
elif service.service == SERVICE_SEND_COMMAND: elif service.service == SERVICE_SEND_COMMAND:
remote.send_command(device=device, command=command) yield from remote.async_send_command(
elif service.service == SERVICE_SYNC: device=device, command=command)
remote.sync()
else: else:
remote.turn_off() yield from remote.async_turn_off()
if remote.should_poll: if remote.should_poll:
remote.update_ha_state(True) update_coro = remote.async_update_ha_state(True)
if hasattr(remote, 'async_update'):
update_tasks.append(hass.loop.create_task(update_coro))
else:
yield from update_coro
descriptions = load_yaml_config_file( if update_tasks:
os.path.join(os.path.dirname(__file__), 'services.yaml')) yield from asyncio.wait(update_tasks, loop=hass.loop)
hass.services.register(DOMAIN, SERVICE_TURN_OFF, handle_remote_service,
descriptions.get(SERVICE_TURN_OFF), descriptions = yield from hass.loop.run_in_executor(
schema=REMOTE_SERVICE_SCHEMA) None, load_yaml_config_file, os.path.join(
hass.services.register(DOMAIN, SERVICE_TURN_ON, handle_remote_service, os.path.dirname(__file__), 'services.yaml'))
descriptions.get(SERVICE_TURN_ON), hass.services.async_register(
schema=REMOTE_SERVICE_TURN_ON_SCHEMA) DOMAIN, SERVICE_TURN_OFF, async_handle_remote_service,
hass.services.register(DOMAIN, SERVICE_SEND_COMMAND, handle_remote_service, descriptions.get(SERVICE_TURN_OFF),
descriptions.get(SERVICE_SEND_COMMAND), schema=REMOTE_SERVICE_SCHEMA)
schema=REMOTE_SERVICE_SEND_COMMAND_SCHEMA) hass.services.async_register(
DOMAIN, SERVICE_TURN_ON, async_handle_remote_service,
descriptions.get(SERVICE_TURN_ON),
schema=REMOTE_SERVICE_TURN_ON_SCHEMA)
hass.services.async_register(
DOMAIN, SERVICE_SEND_COMMAND, async_handle_remote_service,
descriptions.get(SERVICE_SEND_COMMAND),
schema=REMOTE_SERVICE_SEND_COMMAND_SCHEMA)
return True return True
@ -131,14 +140,11 @@ def setup(hass, config):
class RemoteDevice(ToggleEntity): class RemoteDevice(ToggleEntity):
"""Representation of a remote.""" """Representation of a remote."""
def turn_on(self, **kwargs):
"""Turn a device on with the remote."""
raise NotImplementedError()
def turn_off(self, **kwargs):
"""Turn a device off with the remote."""
raise NotImplementedError()
def send_command(self, **kwargs): def send_command(self, **kwargs):
"""Send a command to a device.""" """Send a command to a device."""
raise NotImplementedError() raise NotImplementedError()
def async_send_command(self, **kwargs):
"""Send a command to a device."""
yield from self.hass.loop.run_in_executor(
None, ft.partial(self.send_command, **kwargs))

View file

@ -14,7 +14,8 @@ import homeassistant.components.remote as remote
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.const import ( from homeassistant.const import (
CONF_NAME, CONF_HOST, CONF_PORT, ATTR_ENTITY_ID) CONF_NAME, CONF_HOST, CONF_PORT, ATTR_ENTITY_ID)
from homeassistant.components.remote import PLATFORM_SCHEMA, DOMAIN from homeassistant.components.remote import (
PLATFORM_SCHEMA, DOMAIN, ATTR_DEVICE, ATTR_COMMAND, ATTR_ACTIVITY)
from homeassistant.util import slugify from homeassistant.util import slugify
from homeassistant.config import load_yaml_config_file from homeassistant.config import load_yaml_config_file
@ -22,10 +23,6 @@ REQUIREMENTS = ['pyharmony==1.0.12']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
ATTR_DEVICE = 'device'
ATTR_COMMAND = 'command'
ATTR_ACTIVITY = 'activity'
DEFAULT_PORT = 5222 DEFAULT_PORT = 5222
DEVICES = [] DEVICES = []

View file

@ -9,7 +9,6 @@ from homeassistant.const import (
SERVICE_TURN_ON, SERVICE_TURN_OFF) SERVICE_TURN_ON, SERVICE_TURN_OFF)
from tests.common import get_test_home_assistant, mock_service from tests.common import get_test_home_assistant, mock_service
SERVICE_SYNC = 'sync'
SERVICE_SEND_COMMAND = 'send_command' SERVICE_SEND_COMMAND = 'send_command'
@ -83,22 +82,6 @@ class TestDemoRemote(unittest.TestCase):
self.assertEqual(SERVICE_TURN_OFF, call.service) self.assertEqual(SERVICE_TURN_OFF, call.service)
self.assertEqual('entity_id_val', call.data[ATTR_ENTITY_ID]) self.assertEqual('entity_id_val', call.data[ATTR_ENTITY_ID])
# Test sync
sync_calls = mock_service(
self.hass, remote.DOMAIN, SERVICE_SYNC)
remote.sync(
self.hass, entity_id='entity_id_val')
self.hass.block_till_done()
self.assertEqual(1, len(sync_calls))
call = sync_calls[-1]
self.assertEqual(remote.DOMAIN, call.domain)
self.assertEqual(SERVICE_SYNC, call.service)
self.assertEqual('entity_id_val', call.data[ATTR_ENTITY_ID])
# Test send_command # Test send_command
send_command_calls = mock_service( send_command_calls = mock_service(
self.hass, remote.DOMAIN, SERVICE_SEND_COMMAND) self.hass, remote.DOMAIN, SERVICE_SEND_COMMAND)

View file

@ -11,7 +11,6 @@ import homeassistant.components.remote as remote
from tests.common import mock_service, get_test_home_assistant from tests.common import mock_service, get_test_home_assistant
TEST_PLATFORM = {remote.DOMAIN: {CONF_PLATFORM: 'test'}} TEST_PLATFORM = {remote.DOMAIN: {CONF_PLATFORM: 'test'}}
SERVICE_SYNC = 'sync'
SERVICE_SEND_COMMAND = 'send_command' SERVICE_SEND_COMMAND = 'send_command'