From 5b3d2f823fe5342cf14805c35cc8fc2f4cb383c7 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 20 Apr 2026 13:05:51 +0200 Subject: [PATCH] Always load all platforms in sfr_box (#168594) Co-authored-by: Claude Opus 4.6 (1M context) --- homeassistant/components/sfr_box/__init__.py | 14 +++---------- .../components/sfr_box/binary_sensor.py | 3 --- homeassistant/components/sfr_box/button.py | 8 +++++--- homeassistant/components/sfr_box/const.py | 3 +-- .../components/sfr_box/coordinator.py | 1 + homeassistant/components/sfr_box/sensor.py | 3 --- .../components/sfr_box/test_binary_sensor.py | 4 ---- tests/components/sfr_box/test_button.py | 20 +++++++++++++++---- tests/components/sfr_box/test_sensor.py | 3 --- 9 files changed, 26 insertions(+), 33 deletions(-) diff --git a/homeassistant/components/sfr_box/__init__.py b/homeassistant/components/sfr_box/__init__.py index 9e57a0f25ff..03e903864db 100644 --- a/homeassistant/components/sfr_box/__init__.py +++ b/homeassistant/components/sfr_box/__init__.py @@ -3,7 +3,6 @@ from __future__ import annotations import asyncio -from typing import TYPE_CHECKING from sfrbox_api.bridge import SFRBox from sfrbox_api.exceptions import SFRBoxAuthenticationError, SFRBoxError @@ -14,14 +13,13 @@ from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.helpers import device_registry as dr from homeassistant.helpers.aiohttp_client import async_get_clientsession -from .const import DOMAIN, PLATFORMS, PLATFORMS_WITH_AUTH +from .const import DOMAIN, PLATFORMS from .coordinator import SFRConfigEntry, SFRDataUpdateCoordinator, SFRRuntimeData async def async_setup_entry(hass: HomeAssistant, entry: SFRConfigEntry) -> bool: """Set up SFR box as config entry.""" box = SFRBox(ip=entry.data[CONF_HOST], client=async_get_clientsession(hass)) - platforms = PLATFORMS has_auth = False if (username := entry.data.get(CONF_USERNAME)) and ( password := entry.data.get(CONF_PASSWORD) @@ -39,11 +37,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: SFRConfigEntry) -> bool: translation_key="unknown_error", translation_placeholders={"error": str(err)}, ) from err - platforms = PLATFORMS_WITH_AUTH has_auth = True data = SFRRuntimeData( box=box, + has_authentication=has_auth, dsl=SFRDataUpdateCoordinator( hass, entry, box, "dsl", lambda b: b.dsl_get_info() ), @@ -65,8 +63,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: SFRConfigEntry) -> bool: # Preload system information await data.system.async_config_entry_first_refresh() system_info = data.system.data - if TYPE_CHECKING: - assert system_info is not None # Preload other coordinators (based on net infrastructure) tasks = [data.wan.async_config_entry_first_refresh()] @@ -91,15 +87,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: SFRConfigEntry) -> bool: ) entry.runtime_data = data - await hass.config_entries.async_forward_entry_setups(entry, platforms) + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) return True async def async_unload_entry(hass: HomeAssistant, entry: SFRConfigEntry) -> bool: """Unload a config entry.""" - if entry.data.get(CONF_USERNAME) and entry.data.get(CONF_PASSWORD): - return await hass.config_entries.async_unload_platforms( - entry, PLATFORMS_WITH_AUTH - ) return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/homeassistant/components/sfr_box/binary_sensor.py b/homeassistant/components/sfr_box/binary_sensor.py index 1cf71db89da..10fc6daac90 100644 --- a/homeassistant/components/sfr_box/binary_sensor.py +++ b/homeassistant/components/sfr_box/binary_sensor.py @@ -4,7 +4,6 @@ from __future__ import annotations from collections.abc import Callable from dataclasses import dataclass -from typing import TYPE_CHECKING from sfrbox_api.models import DslInfo, FtthInfo, VoipInfo, WanInfo @@ -88,8 +87,6 @@ async def async_setup_entry( """Set up the sensors.""" data = entry.runtime_data system_info = data.system.data - if TYPE_CHECKING: - assert system_info is not None entities: list[SFRBoxBinarySensor] = [ SFRBoxBinarySensor(data.wan, description, system_info) diff --git a/homeassistant/components/sfr_box/button.py b/homeassistant/components/sfr_box/button.py index 350f72c68ac..5ca2350e619 100644 --- a/homeassistant/components/sfr_box/button.py +++ b/homeassistant/components/sfr_box/button.py @@ -5,7 +5,7 @@ from __future__ import annotations from collections.abc import Awaitable, Callable, Coroutine from dataclasses import dataclass from functools import wraps -from typing import TYPE_CHECKING, Any, Concatenate +from typing import Any, Concatenate from sfrbox_api.bridge import SFRBox from sfrbox_api.exceptions import SFRBoxError @@ -78,9 +78,11 @@ async def async_setup_entry( ) -> None: """Set up the buttons.""" data = entry.runtime_data + if not data.has_authentication: + # All buttons currently require authentication + return + system_info = data.system.data - if TYPE_CHECKING: - assert system_info is not None entities = [ SFRBoxButton(data.box, description, system_info) for description in BUTTON_TYPES diff --git a/homeassistant/components/sfr_box/const.py b/homeassistant/components/sfr_box/const.py index acc4e8e4941..69195289034 100644 --- a/homeassistant/components/sfr_box/const.py +++ b/homeassistant/components/sfr_box/const.py @@ -7,5 +7,4 @@ DEFAULT_USERNAME = "admin" DOMAIN = "sfr_box" -PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR] -PLATFORMS_WITH_AUTH = [*PLATFORMS, Platform.BUTTON] +PLATFORMS = [Platform.BINARY_SENSOR, Platform.BUTTON, Platform.SENSOR] diff --git a/homeassistant/components/sfr_box/coordinator.py b/homeassistant/components/sfr_box/coordinator.py index b57267cee71..cd9f8914e20 100644 --- a/homeassistant/components/sfr_box/coordinator.py +++ b/homeassistant/components/sfr_box/coordinator.py @@ -29,6 +29,7 @@ class SFRRuntimeData: """Runtime data for SFR Box.""" box: SFRBox + has_authentication: bool dsl: SFRDataUpdateCoordinator[DslInfo] ftth: SFRDataUpdateCoordinator[FtthInfo] system: SFRDataUpdateCoordinator[SystemInfo] diff --git a/homeassistant/components/sfr_box/sensor.py b/homeassistant/components/sfr_box/sensor.py index d37c5fc93cf..4884886854c 100644 --- a/homeassistant/components/sfr_box/sensor.py +++ b/homeassistant/components/sfr_box/sensor.py @@ -2,7 +2,6 @@ from collections.abc import Callable from dataclasses import dataclass -from typing import TYPE_CHECKING from sfrbox_api.models import DslInfo, SystemInfo, VoipInfo, WanInfo @@ -236,8 +235,6 @@ async def async_setup_entry( """Set up the sensors.""" data = entry.runtime_data system_info = data.system.data - if TYPE_CHECKING: - assert system_info is not None entities: list[SFRBoxSensor] = [ SFRBoxSensor(data.system, description, system_info) diff --git a/tests/components/sfr_box/test_binary_sensor.py b/tests/components/sfr_box/test_binary_sensor.py index 04818fc2936..75e23d983bd 100644 --- a/tests/components/sfr_box/test_binary_sensor.py +++ b/tests/components/sfr_box/test_binary_sensor.py @@ -24,10 +24,6 @@ def override_platforms() -> Generator[None]: """Override PLATFORMS.""" with ( patch("homeassistant.components.sfr_box.PLATFORMS", [Platform.BINARY_SENSOR]), - patch( - "homeassistant.components.sfr_box.PLATFORMS_WITH_AUTH", - [Platform.BINARY_SENSOR], - ), patch("homeassistant.components.sfr_box.coordinator.SFRBox.authenticate"), ): yield diff --git a/tests/components/sfr_box/test_button.py b/tests/components/sfr_box/test_button.py index dce4bf5a4bd..a278b3de860 100644 --- a/tests/components/sfr_box/test_button.py +++ b/tests/components/sfr_box/test_button.py @@ -23,11 +23,9 @@ pytestmark = pytest.mark.usefixtures( @pytest.fixture(autouse=True) def override_platforms() -> Generator[None]: - """Override PLATFORMS_WITH_AUTH.""" + """Override PLATFORMS.""" with ( - patch( - "homeassistant.components.sfr_box.PLATFORMS_WITH_AUTH", [Platform.BUTTON] - ), + patch("homeassistant.components.sfr_box.PLATFORMS", [Platform.BUTTON]), patch("homeassistant.components.sfr_box.coordinator.SFRBox.authenticate"), ): yield @@ -48,6 +46,20 @@ async def test_buttons( ) +async def test_buttons_no_auth( + hass: HomeAssistant, + config_entry: ConfigEntry, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Test for SFR Box buttons.""" + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + # Ensure auth-only entities are not registered + assert len(entity_registry.entities) == 0 + + async def test_reboot(hass: HomeAssistant, config_entry_with_auth: ConfigEntry) -> None: """Test for SFR Box reboot button.""" await hass.config_entries.async_setup(config_entry_with_auth.entry_id) diff --git a/tests/components/sfr_box/test_sensor.py b/tests/components/sfr_box/test_sensor.py index 7ce7c38af53..32d6ab60ead 100644 --- a/tests/components/sfr_box/test_sensor.py +++ b/tests/components/sfr_box/test_sensor.py @@ -23,9 +23,6 @@ def override_platforms() -> Generator[None]: """Override PLATFORMS.""" with ( patch("homeassistant.components.sfr_box.PLATFORMS", [Platform.SENSOR]), - patch( - "homeassistant.components.sfr_box.PLATFORMS_WITH_AUTH", [Platform.SENSOR] - ), patch("homeassistant.components.sfr_box.coordinator.SFRBox.authenticate"), ): yield