Migrate Ecowitt to runtime_data (#120675)
parent
b375f5227b
commit
05ffd637f5
|
@ -14,10 +14,12 @@ from .const import DOMAIN
|
|||
|
||||
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."""
|
||||
ecowitt = hass.data.setdefault(DOMAIN, {})[entry.entry_id] = EcoWittListener()
|
||||
ecowitt = entry.runtime_data = EcoWittListener()
|
||||
|
||||
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
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: EcowittConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
webhook.async_unregister(hass, entry.data[CONF_WEBHOOK_ID])
|
||||
|
||||
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)
|
||||
|
|
|
@ -3,19 +3,18 @@
|
|||
import dataclasses
|
||||
from typing import Final
|
||||
|
||||
from aioecowitt import EcoWittListener, EcoWittSensor, EcoWittSensorTypes
|
||||
from aioecowitt import EcoWittSensor, EcoWittSensorTypes
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
BinarySensorDeviceClass,
|
||||
BinarySensorEntity,
|
||||
BinarySensorEntityDescription,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import EntityCategory
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .const import DOMAIN
|
||||
from . import EcowittConfigEntry
|
||||
from .entity import EcowittEntity
|
||||
|
||||
ECOWITT_BINARYSENSORS_MAPPING: Final = {
|
||||
|
@ -31,10 +30,12 @@ ECOWITT_BINARYSENSORS_MAPPING: Final = {
|
|||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
hass: HomeAssistant,
|
||||
entry: EcowittConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Add sensors if new."""
|
||||
ecowitt: EcoWittListener = hass.data[DOMAIN][entry.entry_id]
|
||||
ecowitt = entry.runtime_data
|
||||
|
||||
def _new_sensor(sensor: EcoWittSensor) -> None:
|
||||
"""Add new sensor."""
|
||||
|
|
|
@ -4,20 +4,18 @@ from __future__ import annotations
|
|||
|
||||
from typing import Any
|
||||
|
||||
from aioecowitt import EcoWittListener
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.device_registry import DeviceEntry
|
||||
|
||||
from . import EcowittConfigEntry
|
||||
from .const import DOMAIN
|
||||
|
||||
|
||||
async def async_get_device_diagnostics(
|
||||
hass: HomeAssistant, entry: ConfigEntry, device: DeviceEntry
|
||||
hass: HomeAssistant, entry: EcowittConfigEntry, device: DeviceEntry
|
||||
) -> dict[str, Any]:
|
||||
"""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 = ecowitt.stations[station_id]
|
||||
|
|
|
@ -6,7 +6,7 @@ import dataclasses
|
|||
from datetime import datetime
|
||||
from typing import Final
|
||||
|
||||
from aioecowitt import EcoWittListener, EcoWittSensor, EcoWittSensorTypes
|
||||
from aioecowitt import EcoWittSensor, EcoWittSensorTypes
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
SensorDeviceClass,
|
||||
|
@ -14,7 +14,6 @@ from homeassistant.components.sensor import (
|
|||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
|
||||
CONCENTRATION_PARTS_PER_MILLION,
|
||||
|
@ -37,7 +36,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|||
from homeassistant.helpers.typing import StateType
|
||||
from homeassistant.util.unit_system import METRIC_SYSTEM, US_CUSTOMARY_SYSTEM
|
||||
|
||||
from .const import DOMAIN
|
||||
from . import EcowittConfigEntry
|
||||
from .entity import EcowittEntity
|
||||
|
||||
_METRIC: Final = (
|
||||
|
@ -217,10 +216,12 @@ ECOWITT_SENSORS_MAPPING: Final = {
|
|||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
hass: HomeAssistant,
|
||||
entry: EcowittConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Add sensors if new."""
|
||||
ecowitt: EcoWittListener = hass.data[DOMAIN][entry.entry_id]
|
||||
ecowitt = entry.runtime_data
|
||||
|
||||
def _new_sensor(sensor: EcoWittSensor) -> None:
|
||||
"""Add new sensor."""
|
||||
|
|
Loading…
Reference in New Issue