From 4b820a0204084a35abc404bf9ddbca6951e6fd08 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 9 Apr 2026 13:39:19 +0200 Subject: [PATCH] Use runtime_data in somfy_mylink integration (#167745) Co-authored-by: Claude Opus 4.6 (1M context) --- .../components/somfy_mylink/__init__.py | 34 ++++++++++++------- .../components/somfy_mylink/config_flow.py | 13 ++++--- .../components/somfy_mylink/const.py | 2 -- .../components/somfy_mylink/cover.py | 17 +++------- 4 files changed, 33 insertions(+), 33 deletions(-) diff --git a/homeassistant/components/somfy_mylink/__init__.py b/homeassistant/components/somfy_mylink/__init__.py index fdbaaf9f427..4e7028ec6c9 100644 --- a/homeassistant/components/somfy_mylink/__init__.py +++ b/homeassistant/components/somfy_mylink/__init__.py @@ -1,6 +1,8 @@ """Component for the Somfy MyLink device supporting the Synergy API.""" +from dataclasses import dataclass import logging +from typing import Any from somfy_mylink_synergy import SomfyMyLinkSynergy @@ -9,15 +11,23 @@ from homeassistant.const import CONF_HOST, CONF_PORT from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady -from .const import CONF_SYSTEM_ID, DATA_SOMFY_MYLINK, DOMAIN, MYLINK_STATUS, PLATFORMS +from .const import CONF_SYSTEM_ID, PLATFORMS _LOGGER = logging.getLogger(__name__) +type SomfyMyLinkConfigEntry = ConfigEntry[SomfyMyLinkRuntimeData] -async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: + +@dataclass +class SomfyMyLinkRuntimeData: + """Runtime data for Somfy MyLink.""" + + somfy_mylink: SomfyMyLinkSynergy + mylink_status: dict[str, Any] + + +async def async_setup_entry(hass: HomeAssistant, entry: SomfyMyLinkConfigEntry) -> bool: """Set up Somfy MyLink from a config entry.""" - hass.data.setdefault(DOMAIN, {}) - config = entry.data somfy_mylink = SomfyMyLinkSynergy( config[CONF_SYSTEM_ID], config[CONF_HOST], config[CONF_PORT] @@ -42,18 +52,18 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: if "result" not in mylink_status: raise ConfigEntryNotReady("The Somfy MyLink device returned an empty result") - hass.data[DOMAIN][entry.entry_id] = { - DATA_SOMFY_MYLINK: somfy_mylink, - MYLINK_STATUS: mylink_status, - } + entry.runtime_data = SomfyMyLinkRuntimeData( + somfy_mylink=somfy_mylink, + mylink_status=mylink_status, + ) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) return True -async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +async def async_unload_entry( + hass: HomeAssistant, entry: SomfyMyLinkConfigEntry +) -> bool: """Unload a config entry.""" - if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): - hass.data[DOMAIN].pop(entry.entry_id) - return unload_ok + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/homeassistant/components/somfy_mylink/config_flow.py b/homeassistant/components/somfy_mylink/config_flow.py index 91cfae87347..fc3cc476933 100644 --- a/homeassistant/components/somfy_mylink/config_flow.py +++ b/homeassistant/components/somfy_mylink/config_flow.py @@ -10,7 +10,6 @@ from somfy_mylink_synergy import SomfyMyLinkSynergy import voluptuous as vol from homeassistant.config_entries import ( - ConfigEntry, ConfigEntryState, ConfigFlow, ConfigFlowResult, @@ -22,6 +21,7 @@ from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.device_registry import format_mac from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo +from . import SomfyMyLinkConfigEntry from .const import ( CONF_REVERSE, CONF_REVERSED_TARGET_IDS, @@ -30,7 +30,6 @@ from .const import ( CONF_TARGET_NAME, DEFAULT_PORT, DOMAIN, - MYLINK_STATUS, ) _LOGGER = logging.getLogger(__name__) @@ -119,7 +118,7 @@ class SomfyConfigFlow(ConfigFlow, domain=DOMAIN): @staticmethod @callback def async_get_options_flow( - config_entry: ConfigEntry, + config_entry: SomfyMyLinkConfigEntry, ) -> OptionsFlowHandler: """Get the options flow for this handler.""" return OptionsFlowHandler(config_entry) @@ -128,7 +127,9 @@ class SomfyConfigFlow(ConfigFlow, domain=DOMAIN): class OptionsFlowHandler(OptionsFlowWithReload): """Handle a option flow for somfy_mylink.""" - def __init__(self, config_entry: ConfigEntry) -> None: + config_entry: SomfyMyLinkConfigEntry + + def __init__(self, config_entry: SomfyMyLinkConfigEntry) -> None: """Initialize options flow.""" self.options = deepcopy(dict(config_entry.options)) self._target_id: str | None = None @@ -136,9 +137,7 @@ class OptionsFlowHandler(OptionsFlowWithReload): @callback def _async_callback_targets(self): """Return the list of targets.""" - return self.hass.data[DOMAIN][self.config_entry.entry_id][MYLINK_STATUS][ - "result" - ] + return self.config_entry.runtime_data.mylink_status["result"] @callback def _async_get_target_name(self, target_id) -> str: diff --git a/homeassistant/components/somfy_mylink/const.py b/homeassistant/components/somfy_mylink/const.py index 8669c73fb9b..a4740ba4b55 100644 --- a/homeassistant/components/somfy_mylink/const.py +++ b/homeassistant/components/somfy_mylink/const.py @@ -10,8 +10,6 @@ CONF_TARGET_ID = "target_id" DEFAULT_PORT = 44100 -DATA_SOMFY_MYLINK = "somfy_mylink_data" -MYLINK_STATUS = "mylink_status" DOMAIN = "somfy_mylink" PLATFORMS = [Platform.COVER] diff --git a/homeassistant/components/somfy_mylink/cover.py b/homeassistant/components/somfy_mylink/cover.py index 5b888ea4b96..e731bbac698 100644 --- a/homeassistant/components/somfy_mylink/cover.py +++ b/homeassistant/components/somfy_mylink/cover.py @@ -4,19 +4,13 @@ import logging from typing import Any from homeassistant.components.cover import CoverDeviceClass, CoverEntity, CoverState -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.restore_state import RestoreEntity -from .const import ( - CONF_REVERSED_TARGET_IDS, - DATA_SOMFY_MYLINK, - DOMAIN, - MANUFACTURER, - MYLINK_STATUS, -) +from . import SomfyMyLinkConfigEntry +from .const import CONF_REVERSED_TARGET_IDS, DOMAIN, MANUFACTURER _LOGGER = logging.getLogger(__name__) @@ -28,15 +22,14 @@ MYLINK_COVER_TYPE_TO_DEVICE_CLASS = { async def async_setup_entry( hass: HomeAssistant, - config_entry: ConfigEntry, + config_entry: SomfyMyLinkConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Discover and configure Somfy covers.""" reversed_target_ids = config_entry.options.get(CONF_REVERSED_TARGET_IDS, {}) - data = hass.data[DOMAIN][config_entry.entry_id] - mylink_status = data[MYLINK_STATUS] - somfy_mylink = data[DATA_SOMFY_MYLINK] + mylink_status = config_entry.runtime_data.mylink_status + somfy_mylink = config_entry.runtime_data.somfy_mylink cover_list = [] for cover in mylink_status["result"]: