Skip to content

Commit

Permalink
fix: handle missing QR code (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejgray authored Feb 3, 2025
2 parents c63a179 + 9a3bb17 commit d3c58b3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -73,6 +74,7 @@ Sample config:
"brightness_increment": 5,
"search_confidence_threshold": 0.6,
"toggle_automations": false,
"max_ws_message_size": 5242880,
}
}
```
Expand Down
14 changes: 14 additions & 0 deletions ovos_PHAL_plugin_homeassistant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions ovos_PHAL_plugin_homeassistant/logic/socketclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d3c58b3

Please sign in to comment.