[tv.samsung] Reset the websocket connection in case of command error.

This commit is contained in:
Fabio Manganiello 2024-07-18 00:43:12 +02:00
parent 4d39791569
commit 392d64d03b
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -56,6 +56,7 @@ class TvSamsungWsPlugin(Plugin):
) -> SamsungTVWS: ) -> SamsungTVWS:
host, port = self._get_host_and_port(host, port) host, port = self._get_host_and_port(host, port)
if (host, port) not in self._connections: if (host, port) not in self._connections:
self.logger.info('Connecting to %s:%s', host, port)
self._connections[(host, port)] = SamsungTVWS( self._connections[(host, port)] = SamsungTVWS(
host=host, host=host,
port=port, port=port,
@ -66,6 +67,19 @@ class TvSamsungWsPlugin(Plugin):
return self._connections[(host, port)] return self._connections[(host, port)]
def disconnect(
self, host: Optional[str] = None, port: Optional[int] = None
) -> None:
host, port = self._get_host_and_port(host, port)
if (host, port) in self._connections:
self.logger.info('Disconnecting from %s:%s', host, port)
try:
self._connections[(host, port)].close()
except Exception as e:
self.logger.warning('Connection close error: %s: %s', type(e), str(e))
finally:
self._connections.pop((host, port), None)
def exec( def exec(
self, self,
func: Callable[[SamsungTVWS], Any], func: Callable[[SamsungTVWS], Any],
@ -78,6 +92,7 @@ class TvSamsungWsPlugin(Plugin):
try: try:
return func(tv) return func(tv)
except Exception as e: except Exception as e:
self.disconnect(host, port)
self.logger.warning(str(e)) self.logger.warning(str(e))
if n_tries <= 0: if n_tries <= 0:
raise e raise e