Apple TV power management (#51520)

This commit is contained in:
itairaz1 2021-10-23 00:13:14 +03:00 committed by GitHub
parent a9ccd70e71
commit 8d46802558
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,6 +6,7 @@ from pyatv.const import (
FeatureName, FeatureName,
FeatureState, FeatureState,
MediaType, MediaType,
PowerState,
RepeatState, RepeatState,
ShuffleState, ShuffleState,
) )
@ -87,12 +88,14 @@ class AppleTvMediaPlayer(AppleTVEntity, MediaPlayerEntity):
"""Handle when connection is made to device.""" """Handle when connection is made to device."""
self.atv.push_updater.listener = self self.atv.push_updater.listener = self
self.atv.push_updater.start() self.atv.push_updater.start()
self.atv.power.listener = self
@callback @callback
def async_device_disconnected(self): def async_device_disconnected(self):
"""Handle when connection was lost to device.""" """Handle when connection was lost to device."""
self.atv.push_updater.stop() self.atv.push_updater.stop()
self.atv.push_updater.listener = None self.atv.push_updater.listener = None
self.atv.power.listener = None
@property @property
def state(self): def state(self):
@ -101,6 +104,11 @@ class AppleTvMediaPlayer(AppleTVEntity, MediaPlayerEntity):
return None return None
if self.atv is None: if self.atv is None:
return STATE_OFF return STATE_OFF
if (
self._is_feature_available(FeatureName.PowerState)
and self.atv.power.power_state == PowerState.Off
):
return STATE_STANDBY
if self._playing: if self._playing:
state = self._playing.device_state state = self._playing.device_state
if state in (DeviceState.Idle, DeviceState.Loading): if state in (DeviceState.Idle, DeviceState.Loading):
@ -125,6 +133,11 @@ class AppleTvMediaPlayer(AppleTVEntity, MediaPlayerEntity):
self._playing = None self._playing = None
self.async_write_ha_state() self.async_write_ha_state()
@callback
def powerstate_update(self, old_state: PowerState, new_state: PowerState):
"""Update power state when it changes."""
self.async_write_ha_state()
@property @property
def app_id(self): def app_id(self):
"""ID of the current running app.""" """ID of the current running app."""
@ -239,12 +252,16 @@ class AppleTvMediaPlayer(AppleTVEntity, MediaPlayerEntity):
async def async_turn_on(self): async def async_turn_on(self):
"""Turn the media player on.""" """Turn the media player on."""
await self.manager.connect() if self._is_feature_available(FeatureName.TurnOn):
await self.atv.power.turn_on()
async def async_turn_off(self): async def async_turn_off(self):
"""Turn the media player off.""" """Turn the media player off."""
self._playing = None if (self._is_feature_available(FeatureName.TurnOff)) and (
await self.manager.disconnect() not self._is_feature_available(FeatureName.PowerState)
or self.atv.power.power_state == PowerState.On
):
await self.atv.power.turn_off()
async def async_media_play_pause(self): async def async_media_play_pause(self):
"""Pause media on media player.""" """Pause media on media player."""