Ask before installing the Matter Server during onboarding on hosts with little memory (#188890)

This commit is contained in:
Raphael Hehl
2026-10-27 17:00:24 +01:00
committed by GitHub
parent 1160e85a82
commit c4e9a2121e
2 changed files with 67 additions and 1 deletions
+13 -1
View File
@@ -1,6 +1,7 @@
"""Config flow for Matter integration."""
import asyncio
import os
from typing import Any, override
from matter_server.client import MatterClient
@@ -35,6 +36,8 @@ from .const import (
ADDON_SETUP_TIMEOUT = 5
ADDON_SETUP_TIMEOUT_ROUNDS = 40
# Below the documented 2 GB, allowing for memory a Raspberry Pi reserves for the GPU.
MIN_MEMORY_FOR_ONBOARDING_ADDON = 1536 * 1024**2
DEFAULT_URL = "ws://localhost:5580/ws"
DEFAULT_TITLE = "Matter"
ON_SUPERVISOR_SCHEMA = probatio.Schema(
@@ -54,6 +57,11 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> None:
await client.connect()
def total_memory() -> int:
"""Return the physical memory of the host in bytes."""
return os.sysconf("SC_PAGE_SIZE") * os.sysconf("SC_PHYS_PAGES")
def build_ws_address(host: str, port: int) -> str:
"""Return the websocket address."""
return f"ws://{host}:{port}/ws"
@@ -240,7 +248,11 @@ class MatterConfigFlow(ConfigFlow, domain=DOMAIN):
self, discovery_info: ZeroconfServiceInfo
) -> ConfigFlowResult:
"""Handle zeroconf discovery."""
if not async_is_onboarded(self.hass) and is_hassio(self.hass):
if (
not async_is_onboarded(self.hass)
and is_hassio(self.hass)
and total_memory() >= MIN_MEMORY_FOR_ONBOARDING_ADDON
):
await self._async_handle_discovery_without_unique_id()
self._running_in_background = True
return await self.async_step_on_supervisor(
@@ -12,6 +12,9 @@ import pytest
from pytest_homeassistant.common import MockConfigEntry
from homeassistant import config_entries
from homeassistant.components.matter.config_flow import (
total_memory as real_total_memory,
)
from homeassistant.components.matter.const import ADDON_SLUG, DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
@@ -106,6 +109,16 @@ def addon_setup_time_fixture() -> Generator[int]:
yield addon_setup_time
@pytest.fixture(name="total_memory", autouse=True)
def total_memory_fixture() -> Generator[MagicMock]:
"""Mock the physical memory of the host."""
with patch(
"homeassistant.components.matter.config_flow.total_memory",
return_value=4 * 1024**3,
) as total_memory:
yield total_memory
@pytest.fixture(name="not_onboarded")
def mock_onboarded_fixture() -> Generator[MagicMock]:
"""Mock that Home Assistant is not yet onboarded."""
@@ -487,6 +500,47 @@ async def test_zeroconf_not_onboarded_not_installed(
assert setup_entry.call_count == 1
@pytest.mark.parametrize("zeroconf_info", [ZEROCONF_INFO_TCP, ZEROCONF_INFO_UDP])
@pytest.mark.usefixtures("supervisor", "addon_not_installed", "not_onboarded")
async def test_zeroconf_not_onboarded_low_memory(
hass: HomeAssistant,
total_memory: MagicMock,
install_addon: AsyncMock,
zeroconf_info: ZeroconfServiceInfo,
) -> None:
"""Test Zeroconf discovery when not onboarded asks first on a host with little memory."""
total_memory.return_value = 903 * 1024**2
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_ZEROCONF},
data=zeroconf_info,
)
await hass.async_block_till_done()
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "on_supervisor"
assert install_addon.call_count == 0
result = await hass.config_entries.flow.async_configure(
result["flow_id"], {"use_addon": True}
)
assert result["type"] is FlowResultType.SHOW_PROGRESS
assert result["step_id"] == "install_addon"
await hass.async_block_till_done()
assert install_addon.call_args == call("core_matter_server")
def test_total_memory() -> None:
"""Test the physical memory is page size times page count."""
with patch(
"homeassistant.components.matter.config_flow.os.sysconf",
side_effect=lambda name: {"SC_PAGE_SIZE": 4096, "SC_PHYS_PAGES": 2048}[name],
):
assert real_total_memory() == 8 * 1024**2
@pytest.mark.parametrize(
"discovery_info",
[