2019-02-13 20:21:14 +00:00
|
|
|
"""Remote control support for Apple TV."""
|
2017-07-05 04:37:18 +00:00
|
|
|
from homeassistant.components import remote
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_NAME
|
|
|
|
|
|
|
|
from . import ATTR_ATV, ATTR_POWER, DATA_APPLE_TV
|
2017-07-05 04:37:18 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
2017-07-05 04:37:18 +00:00
|
|
|
"""Set up the Apple TV remote platform."""
|
|
|
|
if not discovery_info:
|
|
|
|
return
|
|
|
|
|
|
|
|
name = discovery_info[CONF_NAME]
|
|
|
|
host = discovery_info[CONF_HOST]
|
|
|
|
atv = hass.data[DATA_APPLE_TV][host][ATTR_ATV]
|
|
|
|
power = hass.data[DATA_APPLE_TV][host][ATTR_POWER]
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities([AppleTVRemote(atv, power, name)])
|
2017-07-05 04:37:18 +00:00
|
|
|
|
|
|
|
|
2020-04-26 00:12:36 +00:00
|
|
|
class AppleTVRemote(remote.RemoteEntity):
|
2017-07-05 04:37:18 +00:00
|
|
|
"""Device that sends commands to an Apple TV."""
|
|
|
|
|
|
|
|
def __init__(self, atv, power, name):
|
|
|
|
"""Initialize device."""
|
|
|
|
self._atv = atv
|
|
|
|
self._name = name
|
|
|
|
self._power = power
|
|
|
|
self._power.listeners.append(self)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the device."""
|
|
|
|
return self._name
|
|
|
|
|
2017-07-18 18:19:36 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
2018-03-03 18:23:55 +00:00
|
|
|
"""Return a unique ID."""
|
2017-07-18 18:19:36 +00:00
|
|
|
return self._atv.metadata.device_id
|
|
|
|
|
2017-07-05 04:37:18 +00:00
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if device is on."""
|
|
|
|
return self._power.turned_on
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""No polling needed for Apple TV."""
|
|
|
|
return False
|
|
|
|
|
2018-10-01 06:56:50 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
2017-07-05 04:37:18 +00:00
|
|
|
"""Turn the device on.
|
|
|
|
|
|
|
|
This method is a coroutine.
|
|
|
|
"""
|
|
|
|
self._power.set_power_on(True)
|
|
|
|
|
2018-10-01 06:56:50 +00:00
|
|
|
async def async_turn_off(self, **kwargs):
|
2017-07-05 04:37:18 +00:00
|
|
|
"""Turn the device off.
|
|
|
|
|
|
|
|
This method is a coroutine.
|
|
|
|
"""
|
|
|
|
self._power.set_power_on(False)
|
|
|
|
|
2020-01-29 21:59:45 +00:00
|
|
|
async def async_send_command(self, command, **kwargs):
|
|
|
|
"""Send a command to one device."""
|
|
|
|
for single_command in command:
|
|
|
|
if not hasattr(self._atv.remote_control, single_command):
|
|
|
|
continue
|
2017-07-05 04:37:18 +00:00
|
|
|
|
2020-01-29 21:59:45 +00:00
|
|
|
await getattr(self._atv.remote_control, single_command)()
|