Migrate Ecowitt to runtime_data (#120675)

This commit is contained in:
Robert Resch 2024-06-30 15:29:00 +02:00 committed by GitHub
parent b375f5227b
commit 05ffd637f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 21 additions and 22 deletions

View File

@ -14,10 +14,12 @@ from .const import DOMAIN
PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR] PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.SENSOR]
type EcowittConfigEntry = ConfigEntry[EcoWittListener]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: EcowittConfigEntry) -> bool:
"""Set up the Ecowitt component from UI.""" """Set up the Ecowitt component from UI."""
ecowitt = hass.data.setdefault(DOMAIN, {})[entry.entry_id] = EcoWittListener() ecowitt = entry.runtime_data = EcoWittListener()
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
@ -43,11 +45,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
return True return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: EcowittConfigEntry) -> bool:
"""Unload a config entry.""" """Unload a config entry."""
webhook.async_unregister(hass, entry.data[CONF_WEBHOOK_ID]) webhook.async_unregister(hass, entry.data[CONF_WEBHOOK_ID])
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS): return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok

View File

@ -3,19 +3,18 @@
import dataclasses import dataclasses
from typing import Final from typing import Final
from aioecowitt import EcoWittListener, EcoWittSensor, EcoWittSensorTypes from aioecowitt import EcoWittSensor, EcoWittSensorTypes
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass, BinarySensorDeviceClass,
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 from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN from . import EcowittConfigEntry
from .entity import EcowittEntity from .entity import EcowittEntity
ECOWITT_BINARYSENSORS_MAPPING: Final = { ECOWITT_BINARYSENSORS_MAPPING: Final = {
@ -31,10 +30,12 @@ ECOWITT_BINARYSENSORS_MAPPING: Final = {
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant,
entry: EcowittConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Add sensors if new.""" """Add sensors if new."""
ecowitt: EcoWittListener = hass.data[DOMAIN][entry.entry_id] ecowitt = entry.runtime_data
def _new_sensor(sensor: EcoWittSensor) -> None: def _new_sensor(sensor: EcoWittSensor) -> None:
"""Add new sensor.""" """Add new sensor."""

View File

@ -4,20 +4,18 @@ from __future__ import annotations
from typing import Any from typing import Any
from aioecowitt import EcoWittListener
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntry from homeassistant.helpers.device_registry import DeviceEntry
from . import EcowittConfigEntry
from .const import DOMAIN from .const import DOMAIN
async def async_get_device_diagnostics( async def async_get_device_diagnostics(
hass: HomeAssistant, entry: ConfigEntry, device: DeviceEntry hass: HomeAssistant, entry: EcowittConfigEntry, device: DeviceEntry
) -> dict[str, Any]: ) -> dict[str, Any]:
"""Return diagnostics for a device entry.""" """Return diagnostics for a device entry."""
ecowitt: EcoWittListener = hass.data[DOMAIN][entry.entry_id] ecowitt = entry.runtime_data
station_id = next(item[1] for item in device.identifiers if item[0] == DOMAIN) station_id = next(item[1] for item in device.identifiers if item[0] == DOMAIN)
station = ecowitt.stations[station_id] station = ecowitt.stations[station_id]

View File

@ -6,7 +6,7 @@ import dataclasses
from datetime import datetime from datetime import datetime
from typing import Final from typing import Final
from aioecowitt import EcoWittListener, EcoWittSensor, EcoWittSensorTypes from aioecowitt import EcoWittSensor, EcoWittSensorTypes
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
SensorDeviceClass, SensorDeviceClass,
@ -14,7 +14,6 @@ from homeassistant.components.sensor import (
SensorEntityDescription, SensorEntityDescription,
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER, CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
CONCENTRATION_PARTS_PER_MILLION, CONCENTRATION_PARTS_PER_MILLION,
@ -37,7 +36,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType from homeassistant.helpers.typing import StateType
from homeassistant.util.unit_system import METRIC_SYSTEM, US_CUSTOMARY_SYSTEM from homeassistant.util.unit_system import METRIC_SYSTEM, US_CUSTOMARY_SYSTEM
from .const import DOMAIN from . import EcowittConfigEntry
from .entity import EcowittEntity from .entity import EcowittEntity
_METRIC: Final = ( _METRIC: Final = (
@ -217,10 +216,12 @@ ECOWITT_SENSORS_MAPPING: Final = {
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant,
entry: EcowittConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Add sensors if new.""" """Add sensors if new."""
ecowitt: EcoWittListener = hass.data[DOMAIN][entry.entry_id] ecowitt = entry.runtime_data
def _new_sensor(sensor: EcoWittSensor) -> None: def _new_sensor(sensor: EcoWittSensor) -> None:
"""Add new sensor.""" """Add new sensor."""