1 """Remote control support for Apple TV."""
4 from collections.abc
import Iterable
8 from pyatv.const
import InputAction
22 from .
import AppleTvConfigEntry
23 from .entity
import AppleTVEntity
25 _LOGGER = logging.getLogger(__name__)
28 COMMAND_TO_ATTRIBUTE = {
29 "wakeup": (
"power",
"turn_on"),
30 "suspend": (
"power",
"turn_off"),
31 "turn_on": (
"power",
"turn_on"),
32 "turn_off": (
"power",
"turn_off"),
33 "volume_up": (
"audio",
"volume_up"),
34 "volume_down": (
"audio",
"volume_down"),
40 config_entry: AppleTvConfigEntry,
41 async_add_entities: AddEntitiesCallback,
43 """Load Apple TV remote based on a config entry."""
44 name: str = config_entry.data[CONF_NAME]
46 assert config_entry.unique_id
is not None
47 manager = config_entry.runtime_data
52 """Device that sends commands to an Apple TV."""
56 """Return true if device is on."""
57 return self.
atvatv
is not None
60 """Turn the device on."""
61 await self.
managermanager.connect()
64 """Turn the device off."""
65 await self.
managermanager.disconnect()
68 """Send a command to one device."""
69 num_repeats = kwargs[ATTR_NUM_REPEATS]
70 delay = kwargs.get(ATTR_DELAY_SECS, DEFAULT_DELAY_SECS)
71 hold_secs = kwargs.get(ATTR_HOLD_SECS, DEFAULT_HOLD_SECS)
74 _LOGGER.error(
"Unable to send commands, not connected to %s", self.
namename)
77 for _
in range(num_repeats):
78 for single_command
in command:
79 attr_value: Any =
None
80 if attributes := COMMAND_TO_ATTRIBUTE.get(single_command):
81 attr_value = self.
atvatv
82 for attr_name
in attributes:
83 attr_value = getattr(attr_value, attr_name,
None)
85 attr_value = getattr(self.
atvatv.remote_control, single_command,
None)
87 raise ValueError(
"Command not found. Exiting sequence")
89 _LOGGER.debug(
"Sending command %s", single_command)
92 await attr_value(action=InputAction.Hold)
96 await asyncio.sleep(delay)
None async_turn_off(self, **Any kwargs)
None async_send_command(self, Iterable[str] command, **Any kwargs)
None async_turn_on(self, **Any kwargs)
str|UndefinedType|None name(self)
None async_setup_entry(HomeAssistant hass, AppleTvConfigEntry config_entry, AddEntitiesCallback async_add_entities)