diff --git a/pychromecast/error.py b/pychromecast/error.py index ae03613e5..fbaa880d8 100644 --- a/pychromecast/error.py +++ b/pychromecast/error.py @@ -11,6 +11,10 @@ class ChromecastConnectionError(PyChromecastError): """When a connection error occurs within PyChromecast.""" +class ChromecastConnectionClosed(PyChromecastError): + """When a connection was closed by remote device.""" + + class PyChromecastStopped(PyChromecastError): """Raised when a command is invoked while the Chromecast's socket_client is stopped. diff --git a/pychromecast/socket_client.py b/pychromecast/socket_client.py index 5cbcfe91c..17545003a 100644 --- a/pychromecast/socket_client.py +++ b/pychromecast/socket_client.py @@ -33,6 +33,7 @@ from .dial import get_host_from_service from .error import ( ChromecastConnectionError, + ChromecastConnectionClosed, ControllerNotRegistered, NotConnected, PyChromecastStopped, @@ -619,6 +620,15 @@ def _run_once(self) -> int: if self.stop.is_set(): return 1 raise + except ChromecastConnectionClosed as exc: + self._force_recon = True + self.logger.debug( + "[%s(%s):%s] %s", + self.fn or "", + self.host, + self.port, + exc, + ) except socket.error as exc: self._force_recon = True self.logger.error( @@ -817,7 +827,7 @@ def _read_bytes_from_socket(self, msglen: int) -> bytes: try: chunk = self.socket.recv(min(msglen - bytes_recd, 2048)) if chunk == b"": - raise socket.error("socket connection broken") + raise ChromecastConnectionClosed("Connection was closed by remote") chunks.append(chunk) bytes_recd += len(chunk) except TimeoutError: