From 5beee1f93483f55ae4dfe8cd5973b45bf75d9569 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Mon, 22 Jan 2024 17:55:26 +0100 Subject: [PATCH] Unregister HomeAssistantController on instance conflict (#798) --- pychromecast/controllers/homeassistant.py | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pychromecast/controllers/homeassistant.py b/pychromecast/controllers/homeassistant.py index 95a721bc8..11612360f 100644 --- a/pychromecast/controllers/homeassistant.py +++ b/pychromecast/controllers/homeassistant.py @@ -13,16 +13,28 @@ APP_NAMESPACE = "urn:x-cast:com.nabucasa.hast" DEFAULT_HASS_CONNECT_TIMEOUT = 30 +# Error codes sent in receiver_error messages +ERR_CONNECTION_FAILED = 1 +ERR_AUTHENTICATION_FAILED = 2 +ERR_CONNECTION_LOST = 3 +ERR_HASS_URL_MISSING = 4 +ERR_NO_HTTPS = 5 +ERR_WRONG_INSTANCE = 20 +ERR_NOT_CONNECTED = 21 +ERR_FETCH_CONFIG_FAILED = 22 + class HomeAssistantController(BaseController): """Controller to interact with Home Assistant.""" def __init__( self, + *, hass_url, hass_uuid, client_id, refresh_token, + unregister, app_namespace=APP_NAMESPACE, app_id=APP_HOMEASSISTANT_LOVELACE, hass_connect_timeout=DEFAULT_HASS_CONNECT_TIMEOUT, @@ -32,6 +44,7 @@ def __init__( self.hass_uuid = hass_uuid self.client_id = client_id self.refresh_token = refresh_token + self.unregister = unregister self.hass_connect_timeout = hass_connect_timeout # { # connected: boolean; @@ -68,6 +81,11 @@ def channel_disconnected(self): def receive_message(self, _message, data: dict): """Called when a message is received.""" if data.get("type") == "receiver_status": + if data["hassUrl"] != self.hass_url or data["hassUUID"] != self.hass_uuid: + self.logger.info("Received status for another instance") + self.unregister() + return True + was_connected = self.hass_connected self.status = data @@ -79,6 +97,12 @@ def receive_message(self, _message, data: dict): self._call_on_connect_callbacks(True) return True + if data.get("type") == "receiver_error": + if data.get("error_code") == ERR_WRONG_INSTANCE: + self.logger.info("Received ERR_WRONG_INSTANCE") + self.unregister() + return True + return False def _call_on_connect_callbacks(self, msg_sent: bool) -> None: