Use runtime_data in freebox (#144326)

This commit is contained in:
epenet 2025-05-06 13:09:56 +02:00 committed by GitHub
parent 687c74ee4c
commit 5475d7ef58
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 35 additions and 48 deletions

View File

@ -4,19 +4,18 @@ from datetime import timedelta
from freebox_api.exceptions import HttpRequestError from freebox_api.exceptions import HttpRequestError
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_PORT, EVENT_HOMEASSISTANT_STOP from homeassistant.const import CONF_HOST, CONF_PORT, EVENT_HOMEASSISTANT_STOP
from homeassistant.core import Event, HomeAssistant from homeassistant.core import Event, HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.event import async_track_time_interval from homeassistant.helpers.event import async_track_time_interval
from .const import DOMAIN, PLATFORMS from .const import PLATFORMS
from .router import FreeboxRouter, get_api from .router import FreeboxConfigEntry, FreeboxRouter, get_api
SCAN_INTERVAL = timedelta(seconds=30) SCAN_INTERVAL = timedelta(seconds=30)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: FreeboxConfigEntry) -> bool:
"""Set up Freebox entry.""" """Set up Freebox entry."""
api = await get_api(hass, entry.data[CONF_HOST]) api = await get_api(hass, entry.data[CONF_HOST])
try: try:
@ -32,8 +31,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async_track_time_interval(hass, router.update_all, SCAN_INTERVAL) async_track_time_interval(hass, router.update_all, SCAN_INTERVAL)
) )
hass.data.setdefault(DOMAIN, {}) entry.runtime_data = router
hass.data[DOMAIN][entry.unique_id] = router
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
@ -44,15 +42,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
entry.async_on_unload( entry.async_on_unload(
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_close_connection) hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_close_connection)
) )
entry.async_on_unload(router.close)
return True return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: FreeboxConfigEntry) -> bool:
"""Unload a config entry.""" """Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
router: FreeboxRouter = hass.data[DOMAIN].pop(entry.unique_id)
await router.close()
return unload_ok

View File

@ -7,13 +7,12 @@ from homeassistant.components.alarm_control_panel import (
AlarmControlPanelEntityFeature, AlarmControlPanelEntityFeature,
AlarmControlPanelState, AlarmControlPanelState,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from .const import DOMAIN, FreeboxHomeCategory from .const import FreeboxHomeCategory
from .entity import FreeboxHomeEntity from .entity import FreeboxHomeEntity
from .router import FreeboxRouter from .router import FreeboxConfigEntry, FreeboxRouter
FREEBOX_TO_STATUS = { FREEBOX_TO_STATUS = {
"alarm1_arming": AlarmControlPanelState.ARMING, "alarm1_arming": AlarmControlPanelState.ARMING,
@ -29,11 +28,11 @@ FREEBOX_TO_STATUS = {
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: FreeboxConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback, async_add_entities: AddConfigEntryEntitiesCallback,
) -> None: ) -> None:
"""Set up alarm panel.""" """Set up alarm panel."""
router: FreeboxRouter = hass.data[DOMAIN][entry.unique_id] router = entry.runtime_data
async_add_entities( async_add_entities(
( (

View File

@ -10,15 +10,14 @@ from homeassistant.components.binary_sensor import (
BinarySensorEntity, BinarySensorEntity,
BinarySensorEntityDescription, BinarySensorEntityDescription,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from .const import DOMAIN, FreeboxHomeCategory from .const import FreeboxHomeCategory
from .entity import FreeboxHomeEntity from .entity import FreeboxHomeEntity
from .router import FreeboxRouter from .router import FreeboxConfigEntry, FreeboxRouter
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -35,11 +34,11 @@ RAID_SENSORS: tuple[BinarySensorEntityDescription, ...] = (
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: FreeboxConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback, async_add_entities: AddConfigEntryEntitiesCallback,
) -> None: ) -> None:
"""Set up binary sensors.""" """Set up binary sensors."""
router: FreeboxRouter = hass.data[DOMAIN][entry.unique_id] router = entry.runtime_data
_LOGGER.debug("%s - %s - %s raid(s)", router.name, router.mac, len(router.raids)) _LOGGER.debug("%s - %s - %s raid(s)", router.name, router.mac, len(router.raids))

View File

@ -10,13 +10,11 @@ from homeassistant.components.button import (
ButtonEntity, ButtonEntity,
ButtonEntityDescription, ButtonEntityDescription,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from .const import DOMAIN from .router import FreeboxConfigEntry, FreeboxRouter
from .router import FreeboxRouter
@dataclass(frozen=True, kw_only=True) @dataclass(frozen=True, kw_only=True)
@ -45,11 +43,11 @@ BUTTON_DESCRIPTIONS: tuple[FreeboxButtonEntityDescription, ...] = (
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: FreeboxConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback, async_add_entities: AddConfigEntryEntitiesCallback,
) -> None: ) -> None:
"""Set up the buttons.""" """Set up the buttons."""
router: FreeboxRouter = hass.data[DOMAIN][entry.unique_id] router = entry.runtime_data
entities = [ entities = [
FreeboxButton(router, description) for description in BUTTON_DESCRIPTIONS FreeboxButton(router, description) for description in BUTTON_DESCRIPTIONS
] ]

View File

@ -12,27 +12,26 @@ from homeassistant.components.ffmpeg.camera import (
DEFAULT_ARGUMENTS, DEFAULT_ARGUMENTS,
FFmpegCamera, FFmpegCamera,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_platform from homeassistant.helpers import entity_platform
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from .const import ATTR_DETECTION, DOMAIN, FreeboxHomeCategory from .const import ATTR_DETECTION, FreeboxHomeCategory
from .entity import FreeboxHomeEntity from .entity import FreeboxHomeEntity
from .router import FreeboxRouter from .router import FreeboxConfigEntry, FreeboxRouter
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: FreeboxConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback, async_add_entities: AddConfigEntryEntitiesCallback,
) -> None: ) -> None:
"""Set up cameras.""" """Set up cameras."""
router: FreeboxRouter = hass.data[DOMAIN][entry.unique_id] router = entry.runtime_data
tracked: set[str] = set() tracked: set[str] = set()
@callback @callback

View File

@ -6,22 +6,21 @@ from datetime import datetime
from typing import Any from typing import Any
from homeassistant.components.device_tracker import ScannerEntity from homeassistant.components.device_tracker import ScannerEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from .const import DEFAULT_DEVICE_NAME, DEVICE_ICONS, DOMAIN from .const import DEFAULT_DEVICE_NAME, DEVICE_ICONS
from .router import FreeboxRouter from .router import FreeboxConfigEntry, FreeboxRouter
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: FreeboxConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback, async_add_entities: AddConfigEntryEntitiesCallback,
) -> None: ) -> None:
"""Set up device tracker for Freebox component.""" """Set up device tracker for Freebox component."""
router: FreeboxRouter = hass.data[DOMAIN][entry.unique_id] router = entry.runtime_data
tracked: set[str] = set() tracked: set[str] = set()
@callback @callback

View File

@ -38,6 +38,8 @@ from .const import (
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
type FreeboxConfigEntry = ConfigEntry[FreeboxRouter]
def is_json(json_str: str) -> bool: def is_json(json_str: str) -> bool:
"""Validate if a String is a JSON value or not.""" """Validate if a String is a JSON value or not."""
@ -102,7 +104,7 @@ class FreeboxRouter:
def __init__( def __init__(
self, self,
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: FreeboxConfigEntry,
api: Freepybox, api: Freepybox,
freebox_config: Mapping[str, Any], freebox_config: Mapping[str, Any],
) -> None: ) -> None:

View File

@ -10,7 +10,6 @@ from homeassistant.components.sensor import (
SensorEntity, SensorEntity,
SensorEntityDescription, SensorEntityDescription,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE, UnitOfDataRate, UnitOfTemperature from homeassistant.const import PERCENTAGE, UnitOfDataRate, UnitOfTemperature
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.device_registry import DeviceInfo
@ -20,7 +19,7 @@ from homeassistant.util import dt as dt_util
from .const import DOMAIN from .const import DOMAIN
from .entity import FreeboxHomeEntity from .entity import FreeboxHomeEntity
from .router import FreeboxRouter from .router import FreeboxConfigEntry, FreeboxRouter
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -61,11 +60,11 @@ DISK_PARTITION_SENSORS: tuple[SensorEntityDescription, ...] = (
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: FreeboxConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback, async_add_entities: AddConfigEntryEntitiesCallback,
) -> None: ) -> None:
"""Set up the sensors.""" """Set up the sensors."""
router: FreeboxRouter = hass.data[DOMAIN][entry.unique_id] router = entry.runtime_data
entities: list[SensorEntity] = [] entities: list[SensorEntity] = []
_LOGGER.debug( _LOGGER.debug(

View File

@ -8,13 +8,11 @@ from typing import Any
from freebox_api.exceptions import InsufficientPermissionsError from freebox_api.exceptions import InsufficientPermissionsError
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from .const import DOMAIN from .router import FreeboxConfigEntry, FreeboxRouter
from .router import FreeboxRouter
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -30,11 +28,11 @@ SWITCH_DESCRIPTIONS = [
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: FreeboxConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback, async_add_entities: AddConfigEntryEntitiesCallback,
) -> None: ) -> None:
"""Set up the switch.""" """Set up the switch."""
router: FreeboxRouter = hass.data[DOMAIN][entry.unique_id] router = entry.runtime_data
entities = [ entities = [
FreeboxSwitch(router, entity_description) FreeboxSwitch(router, entity_description)
for entity_description in SWITCH_DESCRIPTIONS for entity_description in SWITCH_DESCRIPTIONS