From a70cf62af4eed22bfc788510c140f67c126975ad Mon Sep 17 00:00:00 2001 From: autinerd <27780930+autinerd@users.noreply.github.com> Date: Tue, 9 Jan 2024 18:46:29 +0100 Subject: [PATCH 1/4] handle deep standby and poweroff of enigma2 devices gracefully --- .../components/enigma2/media_player.py | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/enigma2/media_player.py b/homeassistant/components/enigma2/media_player.py index aa1b5270557dfa..ccacf12adcea44 100644 --- a/homeassistant/components/enigma2/media_player.py +++ b/homeassistant/components/enigma2/media_player.py @@ -1,7 +1,9 @@ """Support for Enigma2 media players.""" from __future__ import annotations -from aiohttp.client_exceptions import ClientConnectorError +from logging import getLogger + +from aiohttp.client_exceptions import ClientConnectorError, ServerDisconnectedError from openwebif.api import OpenWebIfDevice from openwebif.enums import RemoteControlCodes, SetVolumeOption import voluptuous as vol @@ -48,6 +50,8 @@ ATTR_MEDIA_END_TIME = "media_end_time" ATTR_MEDIA_START_TIME = "media_start_time" +_LOGGER = getLogger(__name__) + PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( { vol.Required(CONF_HOST): cv.string, @@ -136,7 +140,15 @@ def __init__(self, name: str, device: OpenWebIfDevice, about: dict) -> None: async def async_turn_off(self) -> None: """Turn off media player.""" - await self._device.turn_off() + try: + await self._device.turn_off() + except ServerDisconnectedError as err: + _LOGGER.warning( + "%s went into deep standby. Error: %s", + self._device.base.host, + str(err), + ) + self._attr_available = False async def async_turn_on(self) -> None: """Turn the media player on.""" @@ -184,8 +196,18 @@ async def async_select_source(self, source: str) -> None: async def async_update(self) -> None: """Update state of the media_player.""" - await self._device.update() - self._attr_available = not self._device.is_offline + try: + await self._device.update() + if not self._attr_available: + _LOGGER.debug("%s is available", self._device.base.host) + self._attr_available = True + except ClientConnectorError as err: + if self._attr_available: + _LOGGER.warning( + "%s is unavailable. Error: %s", self._device.base.host, str(err) + ) + self._attr_available = False + return if not self._device.status.in_standby: self._attr_extra_state_attributes = { From 685c700db87ef866d8286a56b34aa3c60529affa Mon Sep 17 00:00:00 2001 From: autinerd <27780930+autinerd@users.noreply.github.com> Date: Sat, 17 Feb 2024 11:21:42 +0100 Subject: [PATCH 2/4] address review comments --- .../components/enigma2/media_player.py | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/homeassistant/components/enigma2/media_player.py b/homeassistant/components/enigma2/media_player.py index d5f6b92df6ad0c..59e255bc3ad572 100644 --- a/homeassistant/components/enigma2/media_player.py +++ b/homeassistant/components/enigma2/media_player.py @@ -5,7 +5,7 @@ from aiohttp.client_exceptions import ClientConnectorError, ServerDisconnectedError from openwebif.api import OpenWebIfDevice -from openwebif.enums import RemoteControlCodes, SetVolumeOption +from openwebif.enums import PowerState, RemoteControlCodes, SetVolumeOption import voluptuous as vol from yarl import URL @@ -147,15 +147,17 @@ def __init__(self, name: str, device: OpenWebIfDevice, about: dict) -> None: async def async_turn_off(self) -> None: """Turn off media player.""" - try: - await self._device.turn_off() - except ServerDisconnectedError as err: - _LOGGER.warning( - "%s went into deep standby. Error: %s", - self._device.base.host, - str(err), - ) - self._attr_available = False + if self._device.turn_off_to_deep: + try: + await self._device.set_powerstate(PowerState.DEEP_STANDBY) + except ServerDisconnectedError: + _LOGGER.warning( + "%s went into deep standby", + self._device.base.host, + ) + self._attr_available = False + else: + await self._device.set_powerstate(PowerState.STANDBY) async def async_turn_on(self) -> None: """Turn the media player on.""" @@ -205,17 +207,18 @@ async def async_update(self) -> None: """Update state of the media_player.""" try: await self._device.update() - if not self._attr_available: - _LOGGER.debug("%s is available", self._device.base.host) - self._attr_available = True except ClientConnectorError as err: if self._attr_available: _LOGGER.warning( - "%s is unavailable. Error: %s", self._device.base.host, str(err) + "%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: self._attr_extra_state_attributes = { ATTR_MEDIA_CURRENTLY_RECORDING: self._device.status.is_recording, From 90e00ba6956692e9fd18f98c39b73c74ab34aca0 Mon Sep 17 00:00:00 2001 From: autinerd <27780930+autinerd@users.noreply.github.com> Date: Sat, 17 Feb 2024 11:42:47 +0100 Subject: [PATCH 3/4] remove warning on deep standby --- homeassistant/components/enigma2/media_player.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/homeassistant/components/enigma2/media_player.py b/homeassistant/components/enigma2/media_player.py index 59e255bc3ad572..a2fa578b57ac62 100644 --- a/homeassistant/components/enigma2/media_player.py +++ b/homeassistant/components/enigma2/media_player.py @@ -151,10 +151,6 @@ async def async_turn_off(self) -> None: try: await self._device.set_powerstate(PowerState.DEEP_STANDBY) except ServerDisconnectedError: - _LOGGER.warning( - "%s went into deep standby", - self._device.base.host, - ) self._attr_available = False else: await self._device.set_powerstate(PowerState.STANDBY) From eaff4badc0d8b17c249b630e9b9394685ae95725 Mon Sep 17 00:00:00 2001 From: autinerd <27780930+autinerd@users.noreply.github.com> Date: Sat, 17 Feb 2024 12:06:12 +0100 Subject: [PATCH 4/4] use contextlib.suppress --- homeassistant/components/enigma2/media_player.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/enigma2/media_player.py b/homeassistant/components/enigma2/media_player.py index a2fa578b57ac62..b73f8b51c4d58f 100644 --- a/homeassistant/components/enigma2/media_player.py +++ b/homeassistant/components/enigma2/media_player.py @@ -1,6 +1,7 @@ """Support for Enigma2 media players.""" from __future__ import annotations +import contextlib from logging import getLogger from aiohttp.client_exceptions import ClientConnectorError, ServerDisconnectedError @@ -148,10 +149,9 @@ def __init__(self, name: str, device: OpenWebIfDevice, about: dict) -> None: async def async_turn_off(self) -> None: """Turn off media player.""" if self._device.turn_off_to_deep: - try: + with contextlib.suppress(ServerDisconnectedError): await self._device.set_powerstate(PowerState.DEEP_STANDBY) - except ServerDisconnectedError: - self._attr_available = False + self._attr_available = False else: await self._device.set_powerstate(PowerState.STANDBY)