From d99118f6ba5e6a65437430a4cbe9ee68646d2cb6 Mon Sep 17 00:00:00 2001 From: Yannik Ache Eicher Date: Wed, 27 Jan 2021 10:07:33 +0100 Subject: [PATCH] Optimized state handling of Panasonic Viera TVs (#42913) --- .../components/panasonic_viera/__init__.py | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/panasonic_viera/__init__.py b/homeassistant/components/panasonic_viera/__init__.py index f51c6d9d372..3305c935890 100644 --- a/homeassistant/components/panasonic_viera/__init__.py +++ b/homeassistant/components/panasonic_viera/__init__.py @@ -66,7 +66,6 @@ async def async_setup(hass, config): async def async_setup_entry(hass, config_entry): """Set up Panasonic Viera from a config entry.""" - panasonic_viera_data = hass.data.setdefault(DOMAIN, {}) config = config_entry.data @@ -162,7 +161,6 @@ class Remote: async def async_create_remote_control(self, during_setup=False): """Create remote control.""" - control_existed = self._control is not None try: params = {} if self._app_id and self._encryption_key: @@ -173,21 +171,18 @@ class Remote: partial(RemoteControl, self._host, self._port, **params) ) - self.state = STATE_ON - self.available = True + if during_setup: + await self.async_update() except (TimeoutError, URLError, SOAPError, OSError) as err: - if control_existed or during_setup: - _LOGGER.debug("Could not establish remote connection: %s", err) - + _LOGGER.debug("Could not establish remote connection: %s", err) self._control = None self.state = STATE_OFF self.available = self._on_action is not None except Exception as err: # pylint: disable=broad-except - if control_existed or during_setup: - _LOGGER.exception("An unknown error occurred: %s", err) - self._control = None - self.state = STATE_OFF - self.available = self._on_action is not None + _LOGGER.exception("An unknown error occurred: %s", err) + self._control = None + self.state = STATE_OFF + self.available = self._on_action is not None async def async_update(self): """Update device data.""" @@ -215,16 +210,15 @@ class Remote: """Turn on the TV.""" if self._on_action is not None: await self._on_action.async_run(context=context) - self.state = STATE_ON + await self.async_update() elif self.state != STATE_ON: await self.async_send_key(Keys.power) - self.state = STATE_ON + await self.async_update() async def async_turn_off(self): """Turn off the TV.""" if self.state != STATE_OFF: await self.async_send_key(Keys.power) - self.state = STATE_OFF await self.async_update() async def async_set_mute(self, enable): @@ -252,12 +246,20 @@ class Remote: async def _handle_errors(self, func, *args): """Handle errors from func, set available and reconnect if needed.""" try: - return await self._hass.async_add_executor_job(func, *args) + result = await self._hass.async_add_executor_job(func, *args) + self.state = STATE_ON + self.available = True + return result except EncryptionRequired: _LOGGER.error( "The connection couldn't be encrypted. Please reconfigure your TV" ) - except (TimeoutError, URLError, SOAPError, OSError): + self.available = False + except (SOAPError): + self.state = STATE_OFF + self.available = True + await self.async_create_remote_control() + except (TimeoutError, URLError, OSError): self.state = STATE_OFF self.available = self._on_action is not None await self.async_create_remote_control()