1 """Support to interface with universal remote control devices."""
3 from __future__
import annotations
5 from collections.abc
import Iterable
6 from datetime
import timedelta
7 from enum
import IntFlag
10 from typing
import Any, final
12 from propcache
import cached_property
13 import voluptuous
as vol
26 DeprecatedConstantEnum,
27 all_with_deprecated_constants,
28 check_if_deprecated_constant,
29 dir_with_deprecated_constants,
37 _LOGGER = logging.getLogger(__name__)
40 DATA_COMPONENT: HassKey[EntityComponent[RemoteEntity]] =
HassKey(DOMAIN)
41 ENTITY_ID_FORMAT = DOMAIN +
".{}"
42 PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA
43 PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE
46 ATTR_ACTIVITY =
"activity"
47 ATTR_ACTIVITY_LIST =
"activity_list"
48 ATTR_CURRENT_ACTIVITY =
"current_activity"
49 ATTR_COMMAND_TYPE =
"command_type"
50 ATTR_DEVICE =
"device"
51 ATTR_NUM_REPEATS =
"num_repeats"
52 ATTR_DELAY_SECS =
"delay_secs"
53 ATTR_HOLD_SECS =
"hold_secs"
54 ATTR_ALTERNATIVE =
"alternative"
55 ATTR_TIMEOUT =
"timeout"
59 SERVICE_SEND_COMMAND =
"send_command"
60 SERVICE_LEARN_COMMAND =
"learn_command"
61 SERVICE_DELETE_COMMAND =
"delete_command"
64 DEFAULT_NUM_REPEATS = 1
65 DEFAULT_DELAY_SECS = 0.4
70 """Supported features of the remote entity."""
80 RemoteEntityFeature.LEARN_COMMAND,
"2025.1"
83 RemoteEntityFeature.DELETE_COMMAND,
"2025.1"
86 RemoteEntityFeature.ACTIVITY,
"2025.1"
90 REMOTE_SERVICE_ACTIVITY_SCHEMA = cv.make_entity_service_schema(
91 {vol.Optional(ATTR_ACTIVITY): cv.string}
96 def is_on(hass: HomeAssistant, entity_id: str) -> bool:
97 """Return if the remote is on based on the statemachine."""
98 return hass.states.is_state(entity_id, STATE_ON)
101 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
102 """Track states and offer events for remotes."""
103 component = hass.data[DATA_COMPONENT] = EntityComponent[RemoteEntity](
104 _LOGGER, DOMAIN, hass, SCAN_INTERVAL
106 await component.async_setup(config)
108 component.async_register_entity_service(
109 SERVICE_TURN_OFF, REMOTE_SERVICE_ACTIVITY_SCHEMA,
"async_turn_off"
112 component.async_register_entity_service(
113 SERVICE_TURN_ON, REMOTE_SERVICE_ACTIVITY_SCHEMA,
"async_turn_on"
116 component.async_register_entity_service(
117 SERVICE_TOGGLE, REMOTE_SERVICE_ACTIVITY_SCHEMA,
"async_toggle"
120 component.async_register_entity_service(
121 SERVICE_SEND_COMMAND,
123 vol.Required(ATTR_COMMAND): vol.All(cv.ensure_list, [cv.string]),
124 vol.Optional(ATTR_DEVICE): cv.string,
126 ATTR_NUM_REPEATS, default=DEFAULT_NUM_REPEATS
128 vol.Optional(ATTR_DELAY_SECS): vol.Coerce(float),
129 vol.Optional(ATTR_HOLD_SECS, default=DEFAULT_HOLD_SECS): vol.Coerce(float),
131 "async_send_command",
134 component.async_register_entity_service(
135 SERVICE_LEARN_COMMAND,
137 vol.Optional(ATTR_DEVICE): cv.string,
138 vol.Optional(ATTR_COMMAND): vol.All(cv.ensure_list, [cv.string]),
139 vol.Optional(ATTR_COMMAND_TYPE): cv.string,
140 vol.Optional(ATTR_ALTERNATIVE): cv.boolean,
141 vol.Optional(ATTR_TIMEOUT): cv.positive_int,
143 "async_learn_command",
146 component.async_register_entity_service(
147 SERVICE_DELETE_COMMAND,
149 vol.Required(ATTR_COMMAND): vol.All(cv.ensure_list, [cv.string]),
150 vol.Optional(ATTR_DEVICE): cv.string,
152 "async_delete_command",
159 """Set up a config entry."""
164 """Unload a config entry."""
169 """A class that describes remote entities."""
172 CACHED_PROPERTIES_WITH_ATTR_ = {
173 "supported_features",
180 """Base class for remote entities."""
182 entity_description: RemoteEntityDescription
183 _attr_activity_list: list[str] |
None =
None
184 _attr_current_activity: str |
None =
None
189 """Flag supported features."""
190 return self._attr_supported_features
194 """Return the supported features as RemoteEntityFeature.
196 Remove this compatibility shim in 2025.1 or later.
199 if type(features)
is int:
207 """Active activity."""
208 return self._attr_current_activity
212 """List of available activities."""
213 return self._attr_activity_list
218 """Return optional state attributes."""
228 """Send commands to a device."""
229 raise NotImplementedError
232 """Send commands to a device."""
233 await self.
hasshass.async_add_executor_job(
234 ft.partial(self.
send_commandsend_command, command, **kwargs)
238 """Learn a command from a device."""
239 raise NotImplementedError
242 """Learn a command from a device."""
243 await self.
hasshass.async_add_executor_job(ft.partial(self.
learn_commandlearn_command, **kwargs))
246 """Delete commands from the database."""
247 raise NotImplementedError
250 """Delete commands from the database."""
251 await self.
hasshass.async_add_executor_job(
257 __getattr__ = ft.partial(check_if_deprecated_constant, module_globals=globals())
258 __dir__ = ft.partial(
259 dir_with_deprecated_constants, module_globals_keys=[*globals().keys()]
None delete_command(self, **Any kwargs)
RemoteEntityFeature supported_features(self)
None async_delete_command(self, **Any kwargs)
dict[str, Any]|None state_attributes(self)
None learn_command(self, **Any kwargs)
str|None current_activity(self)
None send_command(self, Iterable[str] command, **Any kwargs)
None async_learn_command(self, **Any kwargs)
list[str]|None activity_list(self)
None async_send_command(self, Iterable[str] command, **Any kwargs)
RemoteEntityFeature supported_features_compat(self)
None _report_deprecated_supported_features_values(self, IntFlag replacement)
int|None supported_features(self)
bool async_setup(HomeAssistant hass, ConfigType config)
bool is_on(HomeAssistant hass, str entity_id)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
list[str] all_with_deprecated_constants(dict[str, Any] module_globals)