Handle deep standby and poweroffs of enigma2 devices gracefully (#107462)

* handle deep standby and poweroff of enigma2 devices gracefully

* address review comments

* remove warning on deep standby

* use contextlib.suppress
This commit is contained in:
Sid 2024-02-17 18:23:29 +01:00 committed by GitHub
parent 664285b9d4
commit 926a634ebf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,9 +1,12 @@
"""Support for Enigma2 media players.""" """Support for Enigma2 media players."""
from __future__ import annotations from __future__ import annotations
from aiohttp.client_exceptions import ClientConnectorError import contextlib
from logging import getLogger
from aiohttp.client_exceptions import ClientConnectorError, ServerDisconnectedError
from openwebif.api import OpenWebIfDevice from openwebif.api import OpenWebIfDevice
from openwebif.enums import RemoteControlCodes, SetVolumeOption from openwebif.enums import PowerState, RemoteControlCodes, SetVolumeOption
import voluptuous as vol import voluptuous as vol
from yarl import URL from yarl import URL
@ -50,6 +53,8 @@ ATTR_MEDIA_DESCRIPTION = "media_description"
ATTR_MEDIA_END_TIME = "media_end_time" ATTR_MEDIA_END_TIME = "media_end_time"
ATTR_MEDIA_START_TIME = "media_start_time" ATTR_MEDIA_START_TIME = "media_start_time"
_LOGGER = getLogger(__name__)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{ {
vol.Required(CONF_HOST): cv.string, vol.Required(CONF_HOST): cv.string,
@ -143,7 +148,12 @@ class Enigma2Device(MediaPlayerEntity):
async def async_turn_off(self) -> None: async def async_turn_off(self) -> None:
"""Turn off media player.""" """Turn off media player."""
await self._device.turn_off() if self._device.turn_off_to_deep:
with contextlib.suppress(ServerDisconnectedError):
await self._device.set_powerstate(PowerState.DEEP_STANDBY)
self._attr_available = False
else:
await self._device.set_powerstate(PowerState.STANDBY)
async def async_turn_on(self) -> None: async def async_turn_on(self) -> None:
"""Turn the media player on.""" """Turn the media player on."""
@ -191,8 +201,19 @@ class Enigma2Device(MediaPlayerEntity):
async def async_update(self) -> None: async def async_update(self) -> None:
"""Update state of the media_player.""" """Update state of the media_player."""
await self._device.update() try:
self._attr_available = not self._device.is_offline await self._device.update()
except ClientConnectorError as err:
if self._attr_available:
_LOGGER.warning(
"%s is unavailable. Error: %s", self._device.base.host, err
)
self._attr_available = False
return
if not self._attr_available:
_LOGGER.debug("%s is available", self._device.base.host)
self._attr_available = True
if not self._device.status.in_standby: if not self._device.status.in_standby:
self._attr_extra_state_attributes = { self._attr_extra_state_attributes = {