Migrate myuplink to runtime_data (#118960)

pull/118887/head
Åke Strandberg 2024-06-06 13:31:47 +02:00 committed by GitHub
parent 2a4f7439a2
commit 3d8fc96592
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 27 additions and 40 deletions

View File

@ -30,10 +30,13 @@ PLATFORMS: list[Platform] = [
Platform.UPDATE,
]
type MyUplinkConfigEntry = ConfigEntry[MyUplinkDataCoordinator]
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
async def async_setup_entry(
hass: HomeAssistant, config_entry: MyUplinkConfigEntry
) -> bool:
"""Set up myUplink from a config entry."""
hass.data.setdefault(DOMAIN, {})
implementation = (
await config_entry_oauth2_flow.async_get_config_entry_implementation(
@ -59,7 +62,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
api = MyUplinkAPI(auth)
coordinator = MyUplinkDataCoordinator(hass, api)
await coordinator.async_config_entry_first_refresh()
hass.data[DOMAIN][config_entry.entry_id] = coordinator
config_entry.runtime_data = coordinator
# Update device registry
create_devices(hass, config_entry, coordinator)
@ -71,10 +74,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
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)
@callback
@ -100,11 +100,11 @@ def create_devices(
async def async_remove_config_entry_device(
hass: HomeAssistant, config_entry: ConfigEntry, device_entry: DeviceEntry
hass: HomeAssistant, config_entry: MyUplinkConfigEntry, device_entry: DeviceEntry
) -> bool:
"""Remove myuplink config entry from a device."""
myuplink_data: MyUplinkDataCoordinator = hass.data[DOMAIN][config_entry.entry_id]
myuplink_data = config_entry.runtime_data
return not device_entry.identifiers.intersection(
(DOMAIN, device_id) for device_id in myuplink_data.data.devices
)

View File

@ -7,13 +7,11 @@ from homeassistant.components.binary_sensor import (
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import MyUplinkDataCoordinator
from .const import DOMAIN
from . import MyUplinkConfigEntry, MyUplinkDataCoordinator
from .entity import MyUplinkEntity, MyUplinkSystemEntity
from .helpers import find_matching_platform
@ -51,12 +49,12 @@ def get_description(device_point: DevicePoint) -> BinarySensorEntityDescription
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: MyUplinkConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up myUplink binary_sensor."""
entities: list[BinarySensorEntity] = []
coordinator: MyUplinkDataCoordinator = hass.data[DOMAIN][config_entry.entry_id]
coordinator = config_entry.runtime_data
# Setup device point bound sensors
for device_id, point_data in coordinator.data.points.items():

View File

@ -4,25 +4,22 @@ from __future__ import annotations
from typing import Any
from myuplink import MyUplinkAPI
from homeassistant.components.diagnostics import async_redact_data
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from .const import DOMAIN
from . import MyUplinkConfigEntry
TO_REDACT = {"access_token", "refresh_token", "serialNumber"}
async def async_get_config_entry_diagnostics(
hass: HomeAssistant, config_entry: ConfigEntry
hass: HomeAssistant, config_entry: MyUplinkConfigEntry
) -> dict[str, Any]:
"""Return diagnostics for a config entry.
Pick up fresh data from API and dump it.
"""
api: MyUplinkAPI = hass.data[DOMAIN][config_entry.entry_id].api
api = config_entry.runtime_data.api
myuplink_data = {}
myuplink_data["my_systems"] = await api.async_get_systems_json()
myuplink_data["my_systems"]["devices"] = []

View File

@ -4,14 +4,12 @@ from aiohttp import ClientError
from myuplink import DevicePoint
from homeassistant.components.number import NumberEntity, NumberEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import MyUplinkDataCoordinator
from .const import DOMAIN
from . import MyUplinkConfigEntry, MyUplinkDataCoordinator
from .entity import MyUplinkEntity
from .helpers import find_matching_platform, skip_entity
@ -55,12 +53,12 @@ def get_description(device_point: DevicePoint) -> NumberEntityDescription | None
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: MyUplinkConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up myUplink number."""
entities: list[NumberEntity] = []
coordinator: MyUplinkDataCoordinator = hass.data[DOMAIN][config_entry.entry_id]
coordinator = config_entry.runtime_data
# Setup device point number entities
for device_id, point_data in coordinator.data.points.items():

View File

@ -8,7 +8,6 @@ from homeassistant.components.sensor import (
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
REVOLUTIONS_PER_MINUTE,
Platform,
@ -25,8 +24,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from . import MyUplinkDataCoordinator
from .const import DOMAIN
from . import MyUplinkConfigEntry, MyUplinkDataCoordinator
from .entity import MyUplinkEntity
from .helpers import find_matching_platform, skip_entity
@ -187,13 +185,13 @@ def get_description(device_point: DevicePoint) -> SensorEntityDescription | None
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: MyUplinkConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up myUplink sensor."""
entities: list[SensorEntity] = []
coordinator: MyUplinkDataCoordinator = hass.data[DOMAIN][config_entry.entry_id]
coordinator = config_entry.runtime_data
# Setup device point sensors
for device_id, point_data in coordinator.data.points.items():

View File

@ -6,14 +6,12 @@ import aiohttp
from myuplink import DevicePoint
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import MyUplinkDataCoordinator
from .const import DOMAIN
from . import MyUplinkConfigEntry, MyUplinkDataCoordinator
from .entity import MyUplinkEntity
from .helpers import find_matching_platform, skip_entity
@ -44,12 +42,12 @@ def get_description(device_point: DevicePoint) -> SwitchEntityDescription | None
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: MyUplinkConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up myUplink switch."""
entities: list[SwitchEntity] = []
coordinator: MyUplinkDataCoordinator = hass.data[DOMAIN][config_entry.entry_id]
coordinator = config_entry.runtime_data
# Setup device point switches
for device_id, point_data in coordinator.data.points.items():

View File

@ -5,12 +5,10 @@ from homeassistant.components.update import (
UpdateEntity,
UpdateEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import MyUplinkDataCoordinator
from .const import DOMAIN
from . import MyUplinkConfigEntry, MyUplinkDataCoordinator
from .entity import MyUplinkEntity
UPDATE_DESCRIPTION = UpdateEntityDescription(
@ -21,11 +19,11 @@ UPDATE_DESCRIPTION = UpdateEntityDescription(
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
config_entry: MyUplinkConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up update entity."""
coordinator: MyUplinkDataCoordinator = hass.data[DOMAIN][config_entry.entry_id]
coordinator = config_entry.runtime_data
async_add_entities(
MyUplinkDeviceUpdate(