2019-02-13 20:21:14 +00:00
|
|
|
"""Support to interface with universal remote control devices."""
|
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
|
2019-02-05 05:52:19 +00:00
|
|
|
from homeassistant.helpers.config_validation import ( # noqa
|
|
|
|
PLATFORM_SCHEMA, PLATFORM_SCHEMA_BASE)
|
2016-12-01 20:48:08 +00:00
|
|
|
|
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'
|
2019-03-02 23:54:03 +00:00
|
|
|
ATTR_HOLD_SECS = 'hold_secs'
|
2016-12-01 20:48:08 +00:00
|
|
|
|
|
|
|
DOMAIN = 'remote'
|
2017-06-15 22:52:28 +00:00
|
|
|
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
|
2019-03-02 23:54:03 +00:00
|
|
|
DEFAULT_HOLD_SECS = 0
|
2017-05-24 00:00:52 +00:00
|
|
|
|
2016-12-01 20:48:08 +00:00
|
|
|
REMOTE_SERVICE_SCHEMA = vol.Schema({
|
2018-12-13 09:07:59 +00:00
|
|
|
vol.Optional(ATTR_ENTITY_ID): cv.comp_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),
|
2019-03-02 23:54:03 +00:00
|
|
|
vol.Optional(ATTR_HOLD_SECS, default=DEFAULT_HOLD_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)
|
|
|
|
|
|
|
|
|
2018-10-01 06:56:50 +00:00
|
|
|
async 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)
|
2018-10-01 06:56:50 +00:00
|
|
|
await component.async_setup(config)
|
2016-12-01 20:48:08 +00:00
|
|
|
|
2018-08-16 07:50:11 +00:00
|
|
|
component.async_register_entity_service(
|
|
|
|
SERVICE_TURN_OFF, REMOTE_SERVICE_ACTIVITY_SCHEMA,
|
|
|
|
'async_turn_off'
|
|
|
|
)
|
|
|
|
|
|
|
|
component.async_register_entity_service(
|
|
|
|
SERVICE_TURN_ON, REMOTE_SERVICE_ACTIVITY_SCHEMA,
|
|
|
|
'async_turn_on'
|
|
|
|
)
|
|
|
|
|
|
|
|
component.async_register_entity_service(
|
|
|
|
SERVICE_TOGGLE, REMOTE_SERVICE_ACTIVITY_SCHEMA,
|
|
|
|
'async_toggle'
|
|
|
|
)
|
|
|
|
|
|
|
|
component.async_register_entity_service(
|
|
|
|
SERVICE_SEND_COMMAND, REMOTE_SERVICE_SEND_COMMAND_SCHEMA,
|
|
|
|
'async_send_command'
|
|
|
|
)
|
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))
|