2016-12-01 20:48:08 +00:00
|
|
|
"""
|
|
|
|
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/
|
|
|
|
"""
|
2016-12-04 06:08:24 +00:00
|
|
|
import asyncio
|
2016-12-01 20:48:08 +00:00
|
|
|
from datetime import timedelta
|
2016-12-04 06:08:24 +00:00
|
|
|
import functools as ft
|
2016-12-01 20:48:08 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
2016-12-03 19:46:04 +00:00
|
|
|
|
2017-07-16 17:14:46 +00:00
|
|
|
from homeassistant.loader import bind_hass
|
2016-12-01 20:48:08 +00:00
|
|
|
from homeassistant.helpers.entity_component import EntityComponent
|
|
|
|
from homeassistant.helpers.entity import ToggleEntity
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.const import (
|
2017-07-31 17:46:12 +00:00
|
|
|
STATE_ON, SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_TOGGLE,
|
|
|
|
ATTR_ENTITY_ID)
|
2016-12-01 20:48:08 +00:00
|
|
|
from homeassistant.components import group
|
|
|
|
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA # noqa
|
|
|
|
|
2016-12-03 19:46:04 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-12-01 20:48:08 +00:00
|
|
|
ATTR_ACTIVITY = 'activity'
|
2016-12-03 19:46:04 +00:00
|
|
|
ATTR_COMMAND = 'command'
|
|
|
|
ATTR_DEVICE = 'device'
|
2017-05-24 00:00:52 +00:00
|
|
|
ATTR_NUM_REPEATS = 'num_repeats'
|
|
|
|
ATTR_DELAY_SECS = 'delay_secs'
|
2016-12-01 20:48:08 +00:00
|
|
|
|
|
|
|
DOMAIN = 'remote'
|
2017-06-15 22:52:28 +00:00
|
|
|
DEPENDENCIES = ['group']
|
|
|
|
SCAN_INTERVAL = timedelta(seconds=30)
|
2016-12-01 20:48:08 +00:00
|
|
|
|
|
|
|
ENTITY_ID_ALL_REMOTES = group.ENTITY_ID_FORMAT.format('all_remotes')
|
|
|
|
ENTITY_ID_FORMAT = DOMAIN + '.{}'
|
|
|
|
|
2016-12-03 19:46:04 +00:00
|
|
|
GROUP_NAME_ALL_REMOTES = 'all remotes'
|
|
|
|
|
2016-12-01 20:48:08 +00:00
|
|
|
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
|
|
|
|
|
2016-12-03 19:46:04 +00:00
|
|
|
SERVICE_SEND_COMMAND = 'send_command'
|
|
|
|
SERVICE_SYNC = 'sync'
|
|
|
|
|
2017-08-01 03:52:39 +00:00
|
|
|
DEFAULT_NUM_REPEATS = 1
|
|
|
|
DEFAULT_DELAY_SECS = 0.4
|
2017-05-24 00:00:52 +00:00
|
|
|
|
2016-12-01 20:48:08 +00:00
|
|
|
REMOTE_SERVICE_SCHEMA = vol.Schema({
|
2017-08-01 03:52:39 +00:00
|
|
|
vol.Optional(ATTR_ENTITY_ID): cv.entity_ids,
|
2016-12-01 20:48:08 +00:00
|
|
|
})
|
|
|
|
|
2017-07-31 17:46:12 +00:00
|
|
|
REMOTE_SERVICE_ACTIVITY_SCHEMA = REMOTE_SERVICE_SCHEMA.extend({
|
2016-12-01 20:48:08 +00:00
|
|
|
vol.Optional(ATTR_ACTIVITY): cv.string
|
|
|
|
})
|
|
|
|
|
|
|
|
REMOTE_SERVICE_SEND_COMMAND_SCHEMA = REMOTE_SERVICE_SCHEMA.extend({
|
2017-05-24 00:00:52 +00:00
|
|
|
vol.Required(ATTR_COMMAND): vol.All(cv.ensure_list, [cv.string]),
|
2017-08-01 03:52:39 +00:00
|
|
|
vol.Optional(ATTR_DEVICE): cv.string,
|
|
|
|
vol.Optional(
|
|
|
|
ATTR_NUM_REPEATS, default=DEFAULT_NUM_REPEATS): cv.positive_int,
|
2017-11-09 16:57:41 +00:00
|
|
|
vol.Optional(ATTR_DELAY_SECS): vol.Coerce(float),
|
2016-12-01 20:48:08 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
2017-07-16 17:14:46 +00:00
|
|
|
@bind_hass
|
2016-12-01 20:48:08 +00:00
|
|
|
def is_on(hass, entity_id=None):
|
|
|
|
"""Return if the remote is on based on the statemachine."""
|
|
|
|
entity_id = entity_id or ENTITY_ID_ALL_REMOTES
|
|
|
|
return hass.states.is_state(entity_id, STATE_ON)
|
|
|
|
|
|
|
|
|
2017-07-16 17:14:46 +00:00
|
|
|
@bind_hass
|
2016-12-01 20:48:08 +00:00
|
|
|
def turn_on(hass, activity=None, entity_id=None):
|
|
|
|
"""Turn all or specified remote on."""
|
2017-08-01 03:52:39 +00:00
|
|
|
data = {
|
|
|
|
key: value for key, value in [
|
|
|
|
(ATTR_ACTIVITY, activity),
|
|
|
|
(ATTR_ENTITY_ID, entity_id),
|
|
|
|
] if value is not None}
|
2016-12-01 20:48:08 +00:00
|
|
|
hass.services.call(DOMAIN, SERVICE_TURN_ON, data)
|
|
|
|
|
|
|
|
|
2017-07-16 17:14:46 +00:00
|
|
|
@bind_hass
|
2017-07-31 17:46:12 +00:00
|
|
|
def turn_off(hass, activity=None, entity_id=None):
|
2016-12-01 20:48:08 +00:00
|
|
|
"""Turn all or specified remote off."""
|
2017-07-31 17:46:12 +00:00
|
|
|
data = {}
|
|
|
|
if activity:
|
|
|
|
data[ATTR_ACTIVITY] = activity
|
|
|
|
|
|
|
|
if entity_id:
|
|
|
|
data[ATTR_ENTITY_ID] = entity_id
|
|
|
|
|
2016-12-01 20:48:08 +00:00
|
|
|
hass.services.call(DOMAIN, SERVICE_TURN_OFF, data)
|
|
|
|
|
|
|
|
|
2017-07-31 17:46:12 +00:00
|
|
|
@bind_hass
|
|
|
|
def toggle(hass, activity=None, entity_id=None):
|
|
|
|
"""Toggle all or specified remote."""
|
|
|
|
data = {}
|
|
|
|
if activity:
|
|
|
|
data[ATTR_ACTIVITY] = activity
|
|
|
|
|
|
|
|
if entity_id:
|
|
|
|
data[ATTR_ENTITY_ID] = entity_id
|
|
|
|
|
|
|
|
hass.services.call(DOMAIN, SERVICE_TOGGLE, data)
|
|
|
|
|
|
|
|
|
2017-07-16 17:14:46 +00:00
|
|
|
@bind_hass
|
2017-08-01 03:52:39 +00:00
|
|
|
def send_command(hass, command, entity_id=None, device=None,
|
2017-05-24 00:00:52 +00:00
|
|
|
num_repeats=None, delay_secs=None):
|
2016-12-01 20:48:08 +00:00
|
|
|
"""Send a command to a device."""
|
2017-08-01 03:52:39 +00:00
|
|
|
data = {ATTR_COMMAND: command}
|
2016-12-01 20:48:08 +00:00
|
|
|
if entity_id:
|
|
|
|
data[ATTR_ENTITY_ID] = entity_id
|
2017-05-24 00:00:52 +00:00
|
|
|
|
2017-08-01 03:52:39 +00:00
|
|
|
if device:
|
|
|
|
data[ATTR_DEVICE] = device
|
|
|
|
|
2017-05-24 00:00:52 +00:00
|
|
|
if num_repeats:
|
|
|
|
data[ATTR_NUM_REPEATS] = num_repeats
|
|
|
|
|
|
|
|
if delay_secs:
|
|
|
|
data[ATTR_DELAY_SECS] = delay_secs
|
|
|
|
|
2016-12-01 20:48:08 +00:00
|
|
|
hass.services.call(DOMAIN, SERVICE_SEND_COMMAND, data)
|
|
|
|
|
|
|
|
|
2016-12-04 06:08:24 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_setup(hass, config):
|
2016-12-01 20:48:08 +00:00
|
|
|
"""Track states and offer events for remotes."""
|
|
|
|
component = EntityComponent(
|
|
|
|
_LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_REMOTES)
|
2016-12-04 06:08:24 +00:00
|
|
|
yield from component.async_setup(config)
|
2016-12-01 20:48:08 +00:00
|
|
|
|
2016-12-04 06:08:24 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_handle_remote_service(service):
|
2016-12-01 20:48:08 +00:00
|
|
|
"""Handle calls to the remote services."""
|
2016-12-04 06:08:24 +00:00
|
|
|
target_remotes = component.async_extract_from_service(service)
|
2017-11-09 16:57:41 +00:00
|
|
|
kwargs = service.data.copy()
|
2016-12-01 20:48:08 +00:00
|
|
|
|
2017-10-19 08:56:25 +00:00
|
|
|
update_tasks = []
|
2016-12-01 20:48:08 +00:00
|
|
|
for remote in target_remotes:
|
|
|
|
if service.service == SERVICE_TURN_ON:
|
2017-11-09 16:57:41 +00:00
|
|
|
yield from remote.async_turn_on(**kwargs)
|
2017-07-31 17:46:12 +00:00
|
|
|
elif service.service == SERVICE_TOGGLE:
|
2017-11-09 16:57:41 +00:00
|
|
|
yield from remote.async_toggle(**kwargs)
|
2016-12-01 20:48:08 +00:00
|
|
|
elif service.service == SERVICE_SEND_COMMAND:
|
2017-11-09 16:57:41 +00:00
|
|
|
yield from remote.async_send_command(**kwargs)
|
2016-12-01 20:48:08 +00:00
|
|
|
else:
|
2017-11-09 16:57:41 +00:00
|
|
|
yield from remote.async_turn_off(**kwargs)
|
2016-12-01 20:48:08 +00:00
|
|
|
|
2016-12-07 06:30:47 +00:00
|
|
|
if not remote.should_poll:
|
|
|
|
continue
|
2017-10-19 08:56:25 +00:00
|
|
|
update_tasks.append(remote.async_update_ha_state(True))
|
2016-12-04 06:08:24 +00:00
|
|
|
|
|
|
|
if update_tasks:
|
|
|
|
yield from asyncio.wait(update_tasks, loop=hass.loop)
|
|
|
|
|
|
|
|
hass.services.async_register(
|
|
|
|
DOMAIN, SERVICE_TURN_OFF, async_handle_remote_service,
|
2017-07-31 17:46:12 +00:00
|
|
|
schema=REMOTE_SERVICE_ACTIVITY_SCHEMA)
|
2016-12-04 06:08:24 +00:00
|
|
|
hass.services.async_register(
|
|
|
|
DOMAIN, SERVICE_TURN_ON, async_handle_remote_service,
|
2017-07-31 17:46:12 +00:00
|
|
|
schema=REMOTE_SERVICE_ACTIVITY_SCHEMA)
|
|
|
|
hass.services.async_register(
|
|
|
|
DOMAIN, SERVICE_TOGGLE, async_handle_remote_service,
|
|
|
|
schema=REMOTE_SERVICE_ACTIVITY_SCHEMA)
|
2016-12-04 06:08:24 +00:00
|
|
|
hass.services.async_register(
|
|
|
|
DOMAIN, SERVICE_SEND_COMMAND, async_handle_remote_service,
|
|
|
|
schema=REMOTE_SERVICE_SEND_COMMAND_SCHEMA)
|
2016-12-01 20:48:08 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class RemoteDevice(ToggleEntity):
|
|
|
|
"""Representation of a remote."""
|
|
|
|
|
2017-08-01 03:52:39 +00:00
|
|
|
def send_command(self, command, **kwargs):
|
2016-12-01 20:48:08 +00:00
|
|
|
"""Send a command to a device."""
|
|
|
|
raise NotImplementedError()
|
2016-12-04 06:08:24 +00:00
|
|
|
|
2017-08-01 03:52:39 +00:00
|
|
|
def async_send_command(self, command, **kwargs):
|
2016-12-26 13:10:23 +00:00
|
|
|
"""Send a command to a device.
|
|
|
|
|
|
|
|
This method must be run in the event loop and returns a coroutine.
|
|
|
|
"""
|
2017-08-01 03:52:39 +00:00
|
|
|
return self.hass.async_add_job(ft.partial(
|
|
|
|
self.send_command, command, **kwargs))
|