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.
ecowitt_support
SchiZzA 2026-05-11 13:49:40 +02:00
parent 202447405f
commit 1d2c1b4be3
No known key found for this signature in database
1 changed files with 46 additions and 8 deletions

View File

@ -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"