Optimized state handling of Panasonic Viera TVs (#42913)

This commit is contained in:
Yannik Ache Eicher 2021-01-27 10:07:33 +01:00 committed by GitHub
parent 890eaf840c
commit d99118f6ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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,17 +171,14 @@ 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)
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
@ -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()