mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Migrate luftdaten to use runtime_data (#147480)
This commit is contained in:
parent
51da1bc25a
commit
909d950b50
@ -8,17 +8,16 @@ from __future__ import annotations
|
||||
|
||||
from luftdaten import Luftdaten
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import CONF_SENSOR_ID, DOMAIN
|
||||
from .coordinator import LuftdatenDataUpdateCoordinator
|
||||
from .const import CONF_SENSOR_ID
|
||||
from .coordinator import LuftdatenConfigEntry, LuftdatenDataUpdateCoordinator
|
||||
|
||||
PLATFORMS = [Platform.SENSOR]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: LuftdatenConfigEntry) -> bool:
|
||||
"""Set up Sensor.Community as config entry."""
|
||||
|
||||
# For backwards compat, set unique ID
|
||||
@ -32,14 +31,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
coordinator = LuftdatenDataUpdateCoordinator(hass, entry, sensor_community)
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
|
||||
entry.runtime_data = coordinator
|
||||
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: LuftdatenConfigEntry) -> bool:
|
||||
"""Unload an Sensor.Community config entry."""
|
||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
||||
del hass.data[DOMAIN][entry.entry_id]
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
@ -19,16 +19,18 @@ from .const import DEFAULT_SCAN_INTERVAL, DOMAIN
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
type LuftdatenConfigEntry = ConfigEntry[LuftdatenDataUpdateCoordinator]
|
||||
|
||||
|
||||
class LuftdatenDataUpdateCoordinator(DataUpdateCoordinator[dict[str, float | int]]):
|
||||
"""Data update coordinator for Sensor.Community."""
|
||||
|
||||
config_entry: ConfigEntry
|
||||
config_entry: LuftdatenConfigEntry
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: LuftdatenConfigEntry,
|
||||
sensor_community: Luftdaten,
|
||||
) -> None:
|
||||
"""Initialize the coordinator."""
|
||||
|
@ -5,12 +5,11 @@ from __future__ import annotations
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.diagnostics import async_redact_data
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import CONF_SENSOR_ID, DOMAIN
|
||||
from .coordinator import LuftdatenDataUpdateCoordinator
|
||||
from .const import CONF_SENSOR_ID
|
||||
from .coordinator import LuftdatenConfigEntry
|
||||
|
||||
TO_REDACT = {
|
||||
CONF_LATITUDE,
|
||||
@ -20,8 +19,8 @@ TO_REDACT = {
|
||||
|
||||
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant, entry: ConfigEntry
|
||||
hass: HomeAssistant, entry: LuftdatenConfigEntry
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
coordinator: LuftdatenDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
coordinator = entry.runtime_data
|
||||
return async_redact_data(coordinator.data, TO_REDACT)
|
||||
|
@ -10,7 +10,6 @@ from homeassistant.components.sensor import (
|
||||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
ATTR_LATITUDE,
|
||||
ATTR_LONGITUDE,
|
||||
@ -26,7 +25,7 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import ATTR_SENSOR_ID, CONF_SENSOR_ID, DOMAIN
|
||||
from .coordinator import LuftdatenDataUpdateCoordinator
|
||||
from .coordinator import LuftdatenConfigEntry, LuftdatenDataUpdateCoordinator
|
||||
|
||||
SENSORS: tuple[SensorEntityDescription, ...] = (
|
||||
SensorEntityDescription(
|
||||
@ -71,11 +70,11 @@ SENSORS: tuple[SensorEntityDescription, ...] = (
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
entry: LuftdatenConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up a Sensor.Community sensor based on a config entry."""
|
||||
coordinator: LuftdatenDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
async_add_entities(
|
||||
SensorCommunitySensor(
|
||||
|
@ -5,8 +5,7 @@ from unittest.mock import MagicMock
|
||||
from luftdaten.exceptions import LuftdatenConnectionError
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.luftdaten import DOMAIN
|
||||
from homeassistant.components.luftdaten.const import CONF_SENSOR_ID
|
||||
from homeassistant.components.luftdaten.const import CONF_SENSOR_ID, DOMAIN
|
||||
from homeassistant.config_entries import SOURCE_USER
|
||||
from homeassistant.const import CONF_SHOW_ON_MAP
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
Loading…
x
Reference in New Issue
Block a user