From 1d2c1b4be386862b18bbc67d5cbd360c16446fdd Mon Sep 17 00:00:00 2001 From: SchiZzA Date: Mon, 11 May 2026 13:49:40 +0200 Subject: [PATCH] Introduce SWSRuntimeData dataclass for typed entry.runtime_data. Adds the SWSRuntimeData dataclass and SWSConfigEntry type alias so per-entry state can move from loosely-typed hass.data[DOMAIN][entry_id] dicts to HA 2025+ typed entry.runtime_data. The legacy ENTRY_* string keys are kept in place for now so callers can be migrated incrementally. This commit is not final update. Just preparing for future complete refactor. --- custom_components/sws12500/data.py | 54 +++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/custom_components/sws12500/data.py b/custom_components/sws12500/data.py index 3985654..d4adeeb 100644 --- a/custom_components/sws12500/data.py +++ b/custom_components/sws12500/data.py @@ -1,16 +1,54 @@ -"""Shared keys for storing integration runtime state in `hass.data`. +"""Shared keys for storing integration runtime data. -This integration stores runtime state under: - - hass.data[DOMAIN][entry_id] -> dict - -Keeping keys in a dedicated module prevents subtle bugs where different modules -store different types under the same key. +HA 2025+ pattern: structured runtime state stored in entry.runtime_data +instead of loosely-typed hass.data[][] dicts. """ from __future__ import annotations -from typing import Final +from dataclasses import dataclass, field +from typing import TYPE_CHECKING, Any + +from homeassistant.config_entries import ConfigEntry +from homeassistant.core_config import Config +from homeassistant.exceptions import NoEntitySpecifiedError +from homeassistant.helpers.entity_platform import AddEntitiesCallback + +if TYPE_CHECKING: + from . import WeatherDataUpdateCoordinator + from .ecowitt import EcowittBridge + from .health_coordinator import HealthCoordinator + from .sensors_common import WeatherSensorEntityDescription + + +@dataclass +class SWSRuntimeData: + """Per-entry runtime state for SWS12500 integration. + + Stored in entry.runtime_data. Type-safe, no string key lookups, + no checked() boilerplate + """ + + # Core coordinators + coordinator: WeatherDataUpdateCoordinator + health_coordinator: HealthCoordinator + + # Sensor platform callbacks (set by sensor.async_setup_entry) + add_sensor_entities: AddEntitiesCallback | None = None + sensor_descriptions: dict[str, WeatherSensorEntityDescription] | None = None + + # Binary sensor platform callbacks + add_binary_entities: AddEntitiesCallback | None = None + binary_description: dict[str, Any] | None = None + added_binary_keys: set[str] = field(default_factory=dict) + + # Health data cache for diagnostics + health_data: dict[str, Any] | None = None + + +# Type alias for typed ConfigEntry +type SWSConfigEntry = ConfigEntry[SWSRuntimeData] + # Per-entry dict keys stored under hass.data[DOMAIN][entry_id] ENTRY_COORDINATOR: Final[str] = "coordinator"