Skip to content
This repository was archived by the owner on Apr 11, 2025. It is now read-only.

Commit d3c58b3

Browse files
authored
fix: handle missing QR code (#65)
2 parents c63a179 + 9a3bb17 commit d3c58b3

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ The config also takes some optional properties:
6262
`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.
6363
`assist_only` - whether to pull down only entities exposed to Assist. Default True.
6464
`toggle_automations` - whether to allow the plugin to turn automations on and off. Default False.
65+
`max_ws_message_size` - the maximum size of a websocket message in bytes. Default 5242880 (5 MB).
6566

6667
Sample config:
6768

@@ -73,6 +74,7 @@ Sample config:
7374
"brightness_increment": 5,
7475
"search_confidence_threshold": 0.6,
7576
"toggle_automations": false,
77+
"max_ws_message_size": 5242880,
7678
}
7779
}
7880
```

ovos_PHAL_plugin_homeassistant/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,15 @@ def toggle_automations(self) -> bool:
166166
"""
167167
return self.config.get("toggle_automations", False)
168168

169+
@property
170+
def max_ws_message_size(self) -> int:
171+
""" Get the maximum websocket message size from the config
172+
173+
Returns:
174+
int: The maximum websocket message size, default 5242880
175+
"""
176+
return self.config.get("max_ws_message_size", 5242880)
177+
169178
# SETUP INSTANCE SUPPORT
170179
def validate_instance_connection(self, host, api_key, assist_only):
171180
""" Validate the connection to the Home Assistant instance
@@ -245,12 +254,14 @@ def init_configuration(self, message=None):
245254
configuration_host,
246255
configuration_api_key,
247256
configuration_assist_only
257+
self.max_ws_message_size
248258
)
249259
else:
250260
self.connector = HomeAssistantRESTConnector(
251261
configuration_host,
252262
configuration_api_key,
253263
configuration_assist_only
264+
self.max_ws_message_size
254265
)
255266
self.devices = self.connector.get_all_devices()
256267
self.registered_devices = []
@@ -883,6 +894,9 @@ def handle_qr_oauth_response(self, message):
883894
@param message: oauth.generate.qr.response
884895
"""
885896
qr_code_url = message.data.get("qr")
897+
if qr_code_url is None:
898+
self.gui.show_notification("Failed to get QR code for Home Assistant login!")
899+
return
886900
LOG.info(f"Got qr code: {qr_code_url}")
887901
self.gui.send_event("ovos.phal.plugin.homeassistant.oauth.qr.update", {
888902
"qr": qr_code_url

ovos_PHAL_plugin_homeassistant/logic/socketclient.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99

1010

1111
class HomeAssistantClient:
12-
def __init__(self, url, token, assist_only=True):
12+
def __init__(self, url, token, assist_only=True, max_ws_message_size=5242880):
1313
self.url = url
1414
self.token = token
1515
self.assist_only = assist_only
16+
self.max_ws_message_size = max_ws_message_size
1617
self.websocket = None
1718
self.loop = asyncio.new_event_loop()
1819
asyncio.set_event_loop(self.loop)
@@ -29,6 +30,9 @@ def __init__(self, url, token, assist_only=True):
2930
self._area_registry = {}
3031

3132
async def authenticate(self):
33+
if not self.websocket:
34+
LOG.error("WS HA Connection not established")
35+
return
3236
await self.websocket.send(f'{{"type": "auth", "access_token": "{self.token}"}}')
3337
message = await self.websocket.recv()
3438
LOG.debug(message)
@@ -46,7 +50,7 @@ async def authenticate(self):
4650
async def _connect(self):
4751
try:
4852
uri = f"{self.url}/api/websocket"
49-
self.websocket = await websockets.connect(uri=uri, close_timeout=5, open_timeout=5)
53+
self.websocket = await websockets.connect(uri=uri, close_timeout=5, open_timeout=5, max_size=self.max_ws_message_size)
5054

5155
# Wait for the auth_required message
5256
message = await self.websocket.recv()

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ ovos-PHAL-plugin-oauth>=0.0.3
55
ovos-bus-client>=0.0.8
66
youtube-search~=2.1
77
pytube~=12.1
8-
websockets>=0.54.0,<13.0
8+
websockets>=0.54.0,<14.0 # 14.0 introduces breaking changes around asyncio, among other changes
99
nested-lookup~=0.2
1010
webcolors~=1.13
11+
websocket-client>=0.54.0,<2.0.0 # 1.0 drops Python 2 support only

0 commit comments

Comments
 (0)