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

Commit 9a3bb17

Browse files
committed
feat: make max websocket message size configurable
1 parent 91f4585 commit 9a3bb17

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
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: 11 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 = []

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, max_size=5242880)
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()

0 commit comments

Comments
 (0)