Skip to content

Commit d2a0cfb

Browse files
Add max_message_size configuration option (#227)
* Add `max_message_size` configuration option * Let's proactively increase the default value to better catch some errors, I think there is no harm in that --------- Co-authored-by: Lukas <[email protected]>
1 parent 9c2a025 commit d2a0cfb

File tree

8 files changed

+44
-28
lines changed

8 files changed

+44
-28
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ access_token:
168168
description: Access token of the remote instance, if set.
169169
required: false
170170
type: string
171+
max_message_size:
172+
description: Maximum message size, you can expand size limit in case of an error.
173+
required: false
174+
type: int
171175
entity_prefix:
172176
description: Prefix for all entities of the remote instance.
173177
required: false

custom_components/remote_homeassistant/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
CONF_SUBSCRIBE_EVENTS = "subscribe_events"
5454
CONF_ENTITY_PREFIX = "entity_prefix"
5555
CONF_FILTER = "filter"
56+
CONF_MAX_MSG_SIZE = "max_message_size"
5657

5758
STATE_INIT = "initializing"
5859
STATE_CONNECTING = "connecting"
@@ -71,6 +72,7 @@
7172
vol.Optional(CONF_SECURE, default=False): cv.boolean,
7273
vol.Optional(CONF_VERIFY_SSL, default=True): cv.boolean,
7374
vol.Required(CONF_ACCESS_TOKEN): cv.string,
75+
vol.Optional(CONF_MAX_MSG_SIZE, default=16*1024*1024): vol.Coerce(int),
7476
vol.Optional(CONF_EXCLUDE, default={}): vol.Schema(
7577
{
7678
vol.Optional(CONF_ENTITIES, default=[]): cv.entity_ids,
@@ -300,6 +302,7 @@ def __init__(self, hass, config_entry):
300302
self._secure = config_entry.data.get(CONF_SECURE, False)
301303
self._verify_ssl = config_entry.data.get(CONF_VERIFY_SSL, False)
302304
self._access_token = config_entry.data.get(CONF_ACCESS_TOKEN)
305+
self._max_msg_size = config_entry.data.get(CONF_MAX_MSG_SIZE)
303306

304307
# see homeassistant/components/influxdb/__init__.py
305308
# for include/exclude logic
@@ -416,7 +419,7 @@ def _async_instance_id_match(info):
416419

417420
try:
418421
_LOGGER.info("Connecting to %s", url)
419-
self._connection = await session.ws_connect(url)
422+
self._connection = await session.ws_connect(url, max_msg_size = self._max_msg_size)
420423
except aiohttp.client_exceptions.ClientError:
421424
_LOGGER.error("Could not connect to %s, retry in 10 seconds...", url)
422425
self.set_connection_state(STATE_RECONNECTING)
@@ -528,6 +531,8 @@ async def _recv(self):
528531

529532
if data.type == aiohttp.WSMsgType.ERROR:
530533
_LOGGER.error("websocket connection had an error")
534+
if data.data.code == aiohttp.WSCloseCode.MESSAGE_TOO_BIG:
535+
_LOGGER.error(f"please consider increasing message size with `{CONF_MAX_MSG_SIZE}`")
531536
break
532537

533538
try:

custom_components/remote_homeassistant/config_flow.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
CONF_EXCLUDE_DOMAINS, CONF_EXCLUDE_ENTITIES, CONF_FILTER,
2020
CONF_INCLUDE_DOMAINS, CONF_INCLUDE_ENTITIES,
2121
CONF_LOAD_COMPONENTS, CONF_MAIN, CONF_OPTIONS, CONF_REMOTE, CONF_REMOTE_CONNECTION,
22-
CONF_SECURE, CONF_SERVICE_PREFIX, CONF_SERVICES,
22+
CONF_SECURE, CONF_SERVICE_PREFIX, CONF_SERVICES, CONF_MAX_MSG_SIZE,
2323
CONF_SUBSCRIBE_EVENTS, DOMAIN, REMOTE_ID)
2424
from .rest_api import (ApiProblem, CannotConnect, EndpointMissing, InvalidAuth,
2525
UnsupportedVersion, async_get_discovery_info)
@@ -71,7 +71,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
7171

7272
def __init__(self):
7373
"""Initialize a new ConfigFlow."""
74-
self.prefill = {CONF_PORT: 8123, CONF_SECURE: True}
74+
self.prefill = {CONF_PORT: 8123, CONF_SECURE: True, CONF_MAX_MSG_SIZE: 16*1024*1024}
7575

7676
@staticmethod
7777
@callback
@@ -133,13 +133,15 @@ async def async_step_connection_details(self, user_input=None):
133133
host = user_input.get(CONF_HOST, self.prefill.get(CONF_HOST) or vol.UNDEFINED)
134134
port = user_input.get(CONF_PORT, self.prefill.get(CONF_PORT) or vol.UNDEFINED)
135135
secure = user_input.get(CONF_SECURE, self.prefill.get(CONF_SECURE) or vol.UNDEFINED)
136+
max_msg_size = user_input.get(CONF_MAX_MSG_SIZE, self.prefill.get(CONF_MAX_MSG_SIZE) or vol.UNDEFINED)
136137
return self.async_show_form(
137138
step_id="connection_details",
138139
data_schema=vol.Schema(
139140
{
140141
vol.Required(CONF_HOST, default=host): str,
141142
vol.Required(CONF_PORT, default=port): int,
142143
vol.Required(CONF_ACCESS_TOKEN, default=user_input.get(CONF_ACCESS_TOKEN, vol.UNDEFINED)): str,
144+
vol.Required(CONF_MAX_MSG_SIZE, default=max_msg_size): int,
143145
vol.Optional(CONF_SECURE, default=secure): bool,
144146
vol.Optional(CONF_VERIFY_SSL, default=user_input.get(CONF_VERIFY_SSL, True)): bool,
145147
}

custom_components/remote_homeassistant/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
CONF_API_PASSWORD = "api_password"
1414
CONF_SUBSCRIBE_EVENTS = "subscribe_events"
1515
CONF_ENTITY_PREFIX = "entity_prefix"
16+
CONF_MAX_MSG_SIZE = "max_message_size"
1617

1718
CONF_INCLUDE_DOMAINS = "include_domains"
1819
CONF_INCLUDE_ENTITIES = "include_entities"

custom_components/remote_homeassistant/sensor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from homeassistant.helpers.dispatcher import async_dispatcher_connect
44
from homeassistant.helpers.entity import DeviceInfo, Entity
55

6-
from .const import DOMAIN, CONF_ENTITY_PREFIX, CONF_SECURE
6+
from .const import DOMAIN, CONF_ENTITY_PREFIX, CONF_SECURE, CONF_MAX_MSG_SIZE
77

88

99
async def async_setup_entry(hass, config_entry, async_add_entities):
@@ -44,6 +44,7 @@ def extra_state_attributes(self):
4444
"port": self._entry.data[CONF_PORT],
4545
"secure": self._entry.data.get(CONF_SECURE, False),
4646
"verify_ssl": self._entry.data.get(CONF_VERIFY_SSL, False),
47+
"max_msg_size": self._entry.data[CONF_MAX_MSG_SIZE],
4748
"entity_prefix": self._entry.options.get(CONF_ENTITY_PREFIX, ""),
4849
"uuid": self.unique_id,
4950
}

custom_components/remote_homeassistant/translations/de.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"port": "Port",
1414
"secure": "Sicher",
1515
"verify_ssl": "SSL verifizieren",
16-
"access_token": "Verbindungstoken"
16+
"access_token": "Verbindungstoken",
17+
"max_message_size": "Maximale Nachrichtengröße"
1718
}
1819
}
1920
},
@@ -44,10 +45,10 @@
4445
"init": {
4546
"title": "Basis-Einstellungen (Schritt 1/4)",
4647
"data": {
47-
"entity_prefix": "Entitätspräfix (optional)",
48-
"load_components": "Komponente laden (wenn nicht geladen)",
49-
"service_prefix": "Servicepräfix",
50-
"services": "Remote Services"
48+
"entity_prefix": "Entitätspräfix (optional)",
49+
"load_components": "Komponente laden (wenn nicht geladen)",
50+
"service_prefix": "Servicepräfix",
51+
"services": "Remote Services"
5152
}
5253
},
5354
"domain_entity_filters": {
@@ -74,10 +75,10 @@
7475
"title": "Abonnierte Events (Schritt 4/4)",
7576
"description": "Fügen Sie neue abonnierte Events hinzu, indem Sie ihren Namen in „Neue Events hinzufügen“ eingeben und auf „Absenden“ klicken. Deaktivieren Sie vorhandene Events, indem Sie sie unter „Events“ entfernen.\n\nLassen Sie „Neue Events hinzufügen“ leer und klicken Sie auf „Absenden“, um keine weiteren Änderungen vorzunehmen.",
7677
"data": {
77-
"subscribe_events": "Events",
78-
"add_new_event": "Neue Events hinzufügen"
78+
"subscribe_events": "Events",
79+
"add_new_event": "Neue Events hinzufügen"
7980
}
8081
}
8182
}
8283
}
83-
}
84+
}

custom_components/remote_homeassistant/translations/en.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"port": "Port",
1414
"secure": "Secure",
1515
"verify_ssl": "Verify SSL",
16-
"access_token": "Access token"
16+
"access_token": "Access token",
17+
"max_message_size": "Maximum Message Size"
1718
}
1819
}
1920
},
@@ -44,10 +45,10 @@
4445
"init": {
4546
"title": "Basic Options (step 1/4)",
4647
"data": {
47-
"entity_prefix": "Entity prefix (optional)",
48-
"load_components": "Load component (if not loaded)",
49-
"service_prefix": "Service prefix",
50-
"services": "Remote Services"
48+
"entity_prefix": "Entity prefix (optional)",
49+
"load_components": "Load component (if not loaded)",
50+
"service_prefix": "Service prefix",
51+
"services": "Remote Services"
5152
}
5253
},
5354
"domain_entity_filters": {
@@ -74,13 +75,13 @@
7475
"title": "Subscribed events (step 4/4)",
7576
"description": "Add a new subscribed event by entering its name in `Add new event` and press `Submit`. Remove existing events by unticking them in `Events`.\n\nLeave `Add new event` and press `Submit` to make no further changes.",
7677
"data": {
77-
"subscribe_events": "Events",
78-
"add_new_event": "Add new event"
78+
"subscribe_events": "Events",
79+
"add_new_event": "Add new event"
7980
}
8081
}
8182
},
8283
"abort": {
8384
"not_supported": "No configuration options supported for a remote node"
8485
}
8586
}
86-
}
87+
}

custom_components/remote_homeassistant/translations/pt-BR.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"port": "Porta",
1414
"secure": "Protegido",
1515
"verify_ssl": "Verificar SSL",
16-
"access_token": "Token de acesso"
16+
"access_token": "Token de acesso",
17+
"max_message_size": "Tamanho máximo da mensagem"
1718
}
1819
}
1920
},
@@ -44,10 +45,10 @@
4445
"init": {
4546
"title": "Opções básicas (passo 1/4)",
4647
"data": {
47-
"entity_prefix": "Prefixo da entidade (opcional)",
48-
"load_components": "Carregar componente (se não estiver carregado)",
49-
"service_prefix": "Prefixo do serviço",
50-
"services": "Serviços remotos"
48+
"entity_prefix": "Prefixo da entidade (opcional)",
49+
"load_components": "Carregar componente (se não estiver carregado)",
50+
"service_prefix": "Prefixo do serviço",
51+
"services": "Serviços remotos"
5152
}
5253
},
5354
"domain_entity_filters": {
@@ -74,10 +75,10 @@
7475
"title": "Eventos inscritos (passo 4/4)",
7576
"description": "Adicione um novo evento inscrito digitando seu nome em `Adicionar novo evento` e pressione `Enviar`. Remova os eventos existentes desmarcando-os em `Eventos`.\n\nDeixe `Adicionar novo evento` e pressione `Enviar` para não fazer mais alterações.",
7677
"data": {
77-
"subscribe_events": "Eventos",
78-
"add_new_event": "Adicionar novo evento"
78+
"subscribe_events": "Eventos",
79+
"add_new_event": "Adicionar novo evento"
7980
}
8081
}
8182
}
8283
}
83-
}
84+
}

0 commit comments

Comments
 (0)