mirror of
https://github.com/home-assistant/core.git
synced 2026-05-08 17:49:37 +01:00
Always load all platforms in sfr_box (#168594)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user