diff --git a/README.md b/README.md index bfef7a8..a974870 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ The config also takes some optional properties: `search_confidence_threshold` - the confidence threshold for the search skill to use when searching for devices. The default value is 0.5, or 50%. Must be a value between 0 and 1. `assist_only` - whether to pull down only entities exposed to Assist. Default True. `toggle_automations` - whether to allow the plugin to turn automations on and off. Default False. +`max_ws_message_size` - the maximum size of a websocket message in bytes. Default 5242880 (5 MB). Sample config: @@ -73,6 +74,7 @@ Sample config: "brightness_increment": 5, "search_confidence_threshold": 0.6, "toggle_automations": false, + "max_ws_message_size": 5242880, } } ``` diff --git a/ovos_PHAL_plugin_homeassistant/__init__.py b/ovos_PHAL_plugin_homeassistant/__init__.py index 83704a8..83775ce 100644 --- a/ovos_PHAL_plugin_homeassistant/__init__.py +++ b/ovos_PHAL_plugin_homeassistant/__init__.py @@ -166,6 +166,15 @@ def toggle_automations(self) -> bool: """ return self.config.get("toggle_automations", False) + @property + def max_ws_message_size(self) -> int: + """ Get the maximum websocket message size from the config + + Returns: + int: The maximum websocket message size, default 5242880 + """ + return self.config.get("max_ws_message_size", 5242880) + # SETUP INSTANCE SUPPORT def validate_instance_connection(self, host, api_key, assist_only): """ Validate the connection to the Home Assistant instance @@ -245,12 +254,14 @@ def init_configuration(self, message=None): configuration_host, configuration_api_key, configuration_assist_only + self.max_ws_message_size ) else: self.connector = HomeAssistantRESTConnector( configuration_host, configuration_api_key, configuration_assist_only + self.max_ws_message_size ) self.devices = self.connector.get_all_devices() self.registered_devices = [] @@ -883,6 +894,9 @@ def handle_qr_oauth_response(self, message): @param message: oauth.generate.qr.response """ qr_code_url = message.data.get("qr") + if qr_code_url is None: + self.gui.show_notification("Failed to get QR code for Home Assistant login!") + return LOG.info(f"Got qr code: {qr_code_url}") self.gui.send_event("ovos.phal.plugin.homeassistant.oauth.qr.update", { "qr": qr_code_url diff --git a/ovos_PHAL_plugin_homeassistant/logic/socketclient.py b/ovos_PHAL_plugin_homeassistant/logic/socketclient.py index 8c03055..a89a35d 100644 --- a/ovos_PHAL_plugin_homeassistant/logic/socketclient.py +++ b/ovos_PHAL_plugin_homeassistant/logic/socketclient.py @@ -9,10 +9,11 @@ class HomeAssistantClient: - def __init__(self, url, token, assist_only=True): + def __init__(self, url, token, assist_only=True, max_ws_message_size=5242880): self.url = url self.token = token self.assist_only = assist_only + self.max_ws_message_size = max_ws_message_size self.websocket = None self.loop = asyncio.new_event_loop() asyncio.set_event_loop(self.loop) @@ -29,6 +30,9 @@ def __init__(self, url, token, assist_only=True): self._area_registry = {} async def authenticate(self): + if not self.websocket: + LOG.error("WS HA Connection not established") + return await self.websocket.send(f'{{"type": "auth", "access_token": "{self.token}"}}') message = await self.websocket.recv() LOG.debug(message) @@ -46,7 +50,7 @@ async def authenticate(self): async def _connect(self): try: uri = f"{self.url}/api/websocket" - self.websocket = await websockets.connect(uri=uri, close_timeout=5, open_timeout=5) + self.websocket = await websockets.connect(uri=uri, close_timeout=5, open_timeout=5, max_size=self.max_ws_message_size) # Wait for the auth_required message message = await self.websocket.recv() diff --git a/requirements.txt b/requirements.txt index f29f2ff..10b3f82 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,7 @@ ovos-PHAL-plugin-oauth>=0.0.3 ovos-bus-client>=0.0.8 youtube-search~=2.1 pytube~=12.1 -websockets>=0.54.0,<13.0 +websockets>=0.54.0,<14.0 # 14.0 introduces breaking changes around asyncio, among other changes nested-lookup~=0.2 webcolors~=1.13 +websocket-client>=0.54.0,<2.0.0 # 1.0 drops Python 2 support only