Fix Shelly initialization if device runs large script (#142487)

* Don't check the whole script to see if it generates events

* Fix tests

---------

Co-authored-by: Shay Levy <levyshay1@gmail.com>
This commit is contained in:
Maciej Bieniek 2025-04-09 08:53:44 +02:00 committed by GitHub
parent d4f47bfc6b
commit 06a2de4d1c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 12 additions and 3 deletions

View File

@ -277,3 +277,7 @@ ROLE_TO_DEVICE_CLASS_MAP = {
"current_humidity": SensorDeviceClass.HUMIDITY,
"current_temperature": SensorDeviceClass.TEMPERATURE,
}
# We want to check only the first 5 KB of the script if it contains emitEvent()
# so that the integration startup remains fast.
MAX_SCRIPT_SIZE = 5120

View File

@ -58,6 +58,7 @@ from .const import (
GEN2_BETA_RELEASE_URL,
GEN2_RELEASE_URL,
LOGGER,
MAX_SCRIPT_SIZE,
RPC_INPUTS_EVENTS_TYPES,
SHAIR_MAX_WORK_HOURS,
SHBTN_INPUTS_EVENTS_TYPES,
@ -642,7 +643,7 @@ def get_rpc_ws_url(hass: HomeAssistant) -> str | None:
async def get_rpc_script_event_types(device: RpcDevice, id: int) -> list[str]:
"""Return a list of event types for a specific script."""
code_response = await device.script_getcode(id)
code_response = await device.script_getcode(id, bytes_to_read=MAX_SCRIPT_SIZE)
matches = SHELLY_EMIT_EVENT_PATTERN.finditer(code_response["data"])
return sorted([*{str(event_type.group(1)) for event_type in matches}])

View File

@ -492,7 +492,9 @@ def _mock_rpc_device(version: str | None = None):
initialized=True,
connected=True,
script_getcode=AsyncMock(
side_effect=lambda script_id: {"data": MOCK_SCRIPTS[script_id - 1]}
side_effect=lambda script_id, bytes_to_read: {
"data": MOCK_SCRIPTS[script_id - 1]
}
),
xmod_info={},
)
@ -514,7 +516,9 @@ def _mock_blu_rtv_device(version: str | None = None):
initialized=True,
connected=True,
script_getcode=AsyncMock(
side_effect=lambda script_id: {"data": MOCK_SCRIPTS[script_id - 1]}
side_effect=lambda script_id, bytes_to_read: {
"data": MOCK_SCRIPTS[script_id - 1]
}
),
xmod_info={},
)