mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 00:37:53 +00:00
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:
parent
ca63e44227
commit
e8c9dcf0fe
@ -4,7 +4,9 @@ Component to interface with universal remote control devices.
|
||||
For more details about this component, please refer to the documentation
|
||||
at https://home-assistant.io/components/remote/
|
||||
"""
|
||||
import asyncio
|
||||
from datetime import timedelta
|
||||
import functools as ft
|
||||
import logging
|
||||
import os
|
||||
|
||||
@ -80,50 +82,57 @@ def send_command(hass, device, command, entity_id=None):
|
||||
hass.services.call(DOMAIN, SERVICE_SEND_COMMAND, data)
|
||||
|
||||
|
||||
def sync(hass, entity_id=None):
|
||||
"""Sync remote device."""
|
||||
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
|
||||
hass.services.call(DOMAIN, SERVICE_SYNC, data)
|
||||
|
||||
|
||||
def setup(hass, config):
|
||||
@asyncio.coroutine
|
||||
def async_setup(hass, config):
|
||||
"""Track states and offer events for remotes."""
|
||||
component = EntityComponent(
|
||||
_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."""
|
||||
target_remotes = component.extract_from_service(service)
|
||||
target_remotes = component.async_extract_from_service(service)
|
||||
|
||||
activity_id = service.data.get(ATTR_ACTIVITY)
|
||||
device = service.data.get(ATTR_DEVICE)
|
||||
command = service.data.get(ATTR_COMMAND)
|
||||
|
||||
update_tasks = []
|
||||
for remote in target_remotes:
|
||||
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:
|
||||
remote.send_command(device=device, command=command)
|
||||
elif service.service == SERVICE_SYNC:
|
||||
remote.sync()
|
||||
yield from remote.async_send_command(
|
||||
device=device, command=command)
|
||||
else:
|
||||
remote.turn_off()
|
||||
yield from remote.async_turn_off()
|
||||
|
||||
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(
|
||||
os.path.join(os.path.dirname(__file__), 'services.yaml'))
|
||||
hass.services.register(DOMAIN, SERVICE_TURN_OFF, handle_remote_service,
|
||||
descriptions.get(SERVICE_TURN_OFF),
|
||||
schema=REMOTE_SERVICE_SCHEMA)
|
||||
hass.services.register(DOMAIN, SERVICE_TURN_ON, handle_remote_service,
|
||||
descriptions.get(SERVICE_TURN_ON),
|
||||
schema=REMOTE_SERVICE_TURN_ON_SCHEMA)
|
||||
hass.services.register(DOMAIN, SERVICE_SEND_COMMAND, handle_remote_service,
|
||||
descriptions.get(SERVICE_SEND_COMMAND),
|
||||
schema=REMOTE_SERVICE_SEND_COMMAND_SCHEMA)
|
||||
if update_tasks:
|
||||
yield from asyncio.wait(update_tasks, loop=hass.loop)
|
||||
|
||||
descriptions = yield from hass.loop.run_in_executor(
|
||||
None, load_yaml_config_file, os.path.join(
|
||||
os.path.dirname(__file__), 'services.yaml'))
|
||||
hass.services.async_register(
|
||||
DOMAIN, SERVICE_TURN_OFF, async_handle_remote_service,
|
||||
descriptions.get(SERVICE_TURN_OFF),
|
||||
schema=REMOTE_SERVICE_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
|
||||
|
||||
@ -131,14 +140,11 @@ def setup(hass, config):
|
||||
class RemoteDevice(ToggleEntity):
|
||||
"""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):
|
||||
"""Send a command to a device."""
|
||||
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))
|
||||
|
@ -14,7 +14,8 @@ import homeassistant.components.remote as remote
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.const import (
|
||||
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.config import load_yaml_config_file
|
||||
|
||||
@ -22,10 +23,6 @@ REQUIREMENTS = ['pyharmony==1.0.12']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
ATTR_DEVICE = 'device'
|
||||
ATTR_COMMAND = 'command'
|
||||
ATTR_ACTIVITY = 'activity'
|
||||
|
||||
DEFAULT_PORT = 5222
|
||||
DEVICES = []
|
||||
|
||||
|
@ -9,7 +9,6 @@ from homeassistant.const import (
|
||||
SERVICE_TURN_ON, SERVICE_TURN_OFF)
|
||||
from tests.common import get_test_home_assistant, mock_service
|
||||
|
||||
SERVICE_SYNC = 'sync'
|
||||
SERVICE_SEND_COMMAND = 'send_command'
|
||||
|
||||
|
||||
@ -83,22 +82,6 @@ class TestDemoRemote(unittest.TestCase):
|
||||
self.assertEqual(SERVICE_TURN_OFF, call.service)
|
||||
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
|
||||
send_command_calls = mock_service(
|
||||
self.hass, remote.DOMAIN, SERVICE_SEND_COMMAND)
|
||||
|
@ -11,7 +11,6 @@ import homeassistant.components.remote as remote
|
||||
|
||||
from tests.common import mock_service, get_test_home_assistant
|
||||
TEST_PLATFORM = {remote.DOMAIN: {CONF_PLATFORM: 'test'}}
|
||||
SERVICE_SYNC = 'sync'
|
||||
SERVICE_SEND_COMMAND = 'send_command'
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user