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 (
|
2019-07-31 19:25:30 +00:00
|
|
|
STATE_ON,
|
|
|
|
SERVICE_TURN_ON,
|
|
|
|
SERVICE_TURN_OFF,
|
|
|
|
SERVICE_TOGGLE,
|
|
|
|
)
|
2016-12-01 20:48:08 +00:00
|
|
|
from homeassistant.components import group
|
2019-11-16 09:22:07 +00:00
|
|
|
from homeassistant.helpers.config_validation import ( # noqa: F401
|
2019-12-03 00:23:12 +00:00
|
|
|
make_entity_service_schema,
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
PLATFORM_SCHEMA_BASE,
|
|
|
|
)
|
2016-12-01 20:48:08 +00:00
|
|
|
|
2019-08-12 03:38:18 +00:00
|
|
|
|
|
|
|
# mypy: allow-untyped-defs, no-check-untyped-defs
|
|
|
|
|
2016-12-03 19:46:04 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_ACTIVITY = "activity"
|
|
|
|
ATTR_COMMAND = "command"
|
|
|
|
ATTR_DEVICE = "device"
|
|
|
|
ATTR_NUM_REPEATS = "num_repeats"
|
|
|
|
ATTR_DELAY_SECS = "delay_secs"
|
|
|
|
ATTR_HOLD_SECS = "hold_secs"
|
|
|
|
ATTR_ALTERNATIVE = "alternative"
|
|
|
|
ATTR_TIMEOUT = "timeout"
|
2016-12-01 20:48:08 +00:00
|
|
|
|
2019-07-31 19:25:30 +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
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ENTITY_ID_ALL_REMOTES = group.ENTITY_ID_FORMAT.format("all_remotes")
|
|
|
|
ENTITY_ID_FORMAT = DOMAIN + ".{}"
|
2016-12-01 20:48:08 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
GROUP_NAME_ALL_REMOTES = "all remotes"
|
2016-12-03 19:46:04 +00:00
|
|
|
|
2016-12-01 20:48:08 +00:00
|
|
|
MIN_TIME_BETWEEN_SCANS = timedelta(seconds=10)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SERVICE_SEND_COMMAND = "send_command"
|
|
|
|
SERVICE_LEARN_COMMAND = "learn_command"
|
|
|
|
SERVICE_SYNC = "sync"
|
2016-12-03 19:46:04 +00:00
|
|
|
|
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
|
|
|
|
2019-06-05 09:32:59 +00:00
|
|
|
SUPPORT_LEARN_COMMAND = 1
|
|
|
|
|
2019-12-03 00:23:12 +00:00
|
|
|
REMOTE_SERVICE_ACTIVITY_SCHEMA = make_entity_service_schema(
|
2019-07-31 19:25:30 +00:00
|
|
|
{vol.Optional(ATTR_ACTIVITY): cv.string}
|
|
|
|
)
|
|
|
|
|
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(
|
2019-07-31 19:25:30 +00:00
|
|
|
_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(
|
2019-07-31 19:25:30 +00:00
|
|
|
SERVICE_TURN_OFF, REMOTE_SERVICE_ACTIVITY_SCHEMA, "async_turn_off"
|
2018-08-16 07:50:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
component.async_register_entity_service(
|
2019-07-31 19:25:30 +00:00
|
|
|
SERVICE_TURN_ON, REMOTE_SERVICE_ACTIVITY_SCHEMA, "async_turn_on"
|
2018-08-16 07:50:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
component.async_register_entity_service(
|
2019-07-31 19:25:30 +00:00
|
|
|
SERVICE_TOGGLE, REMOTE_SERVICE_ACTIVITY_SCHEMA, "async_toggle"
|
2018-08-16 07:50:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
component.async_register_entity_service(
|
2019-12-03 00:23:12 +00:00
|
|
|
SERVICE_SEND_COMMAND,
|
|
|
|
{
|
|
|
|
vol.Required(ATTR_COMMAND): vol.All(cv.ensure_list, [cv.string]),
|
|
|
|
vol.Optional(ATTR_DEVICE): cv.string,
|
|
|
|
vol.Optional(
|
|
|
|
ATTR_NUM_REPEATS, default=DEFAULT_NUM_REPEATS
|
|
|
|
): cv.positive_int,
|
|
|
|
vol.Optional(ATTR_DELAY_SECS): vol.Coerce(float),
|
|
|
|
vol.Optional(ATTR_HOLD_SECS, default=DEFAULT_HOLD_SECS): vol.Coerce(float),
|
|
|
|
},
|
|
|
|
"async_send_command",
|
2018-08-16 07:50:11 +00:00
|
|
|
)
|
2016-12-01 20:48:08 +00:00
|
|
|
|
2019-06-05 09:32:59 +00:00
|
|
|
component.async_register_entity_service(
|
2019-07-31 19:25:30 +00:00
|
|
|
SERVICE_LEARN_COMMAND,
|
2019-12-03 00:23:12 +00:00
|
|
|
{
|
|
|
|
vol.Optional(ATTR_DEVICE): cv.string,
|
|
|
|
vol.Optional(ATTR_COMMAND): vol.All(cv.ensure_list, [cv.string]),
|
|
|
|
vol.Optional(ATTR_ALTERNATIVE): cv.boolean,
|
|
|
|
vol.Optional(ATTR_TIMEOUT): cv.positive_int,
|
|
|
|
},
|
2019-07-31 19:25:30 +00:00
|
|
|
"async_learn_command",
|
2019-06-05 09:32:59 +00:00
|
|
|
)
|
|
|
|
|
2016-12-01 20:48:08 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class RemoteDevice(ToggleEntity):
|
|
|
|
"""Representation of a remote."""
|
|
|
|
|
2019-06-05 09:32:59 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
return 0
|
|
|
|
|
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.
|
|
|
|
"""
|
2019-06-05 09:32:59 +00:00
|
|
|
return self.hass.async_add_executor_job(
|
2019-07-31 19:25:30 +00:00
|
|
|
ft.partial(self.send_command, command, **kwargs)
|
|
|
|
)
|
2019-06-05 09:32:59 +00:00
|
|
|
|
|
|
|
def learn_command(self, **kwargs):
|
|
|
|
"""Learn a command from a device."""
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
def async_learn_command(self, **kwargs):
|
|
|
|
"""Learn a command from a device.
|
|
|
|
|
|
|
|
This method must be run in the event loop and returns a coroutine.
|
|
|
|
"""
|
|
|
|
return self.hass.async_add_executor_job(
|
2019-07-31 19:25:30 +00:00
|
|
|
ft.partial(self.learn_command, **kwargs)
|
|
|
|
)
|