Migrate oncue to use entry.runtime_data (#122307)
This commit is contained in:
parent
7f852d0f73
commit
272f0bc21c
4 changed files with 36 additions and 45 deletions
|
@ -7,21 +7,21 @@ import logging
|
|||
|
||||
from aiooncue import LoginFailedException, Oncue, OncueDevice
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
from .const import CONNECTION_EXCEPTIONS, DOMAIN
|
||||
from .const import CONNECTION_EXCEPTIONS, DOMAIN # noqa: F401
|
||||
from .types import OncueConfigEntry
|
||||
|
||||
PLATFORMS: list[str] = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: OncueConfigEntry) -> bool:
|
||||
"""Set up Oncue from a config entry."""
|
||||
data = entry.data
|
||||
websession = async_get_clientsession(hass)
|
||||
|
@ -40,7 +40,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
except LoginFailedException as ex:
|
||||
raise ConfigEntryAuthFailed from ex
|
||||
|
||||
coordinator = DataUpdateCoordinator(
|
||||
coordinator = DataUpdateCoordinator[dict[str, OncueDevice]](
|
||||
hass,
|
||||
_LOGGER,
|
||||
name=f"Oncue {entry.data[CONF_USERNAME]}",
|
||||
|
@ -50,13 +50,11 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
)
|
||||
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: OncueConfigEntry) -> 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)
|
||||
|
|
|
@ -2,21 +2,17 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from aiooncue import OncueDevice
|
||||
|
||||
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 homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
from .const import DOMAIN
|
||||
from .entity import OncueEntity
|
||||
from .types import OncueConfigEntry
|
||||
|
||||
SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = (
|
||||
BinarySensorEntityDescription(
|
||||
|
@ -31,25 +27,18 @@ SENSOR_MAP = {description.key: description for description in SENSOR_TYPES}
|
|||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: OncueConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up sensors."""
|
||||
coordinator: DataUpdateCoordinator[dict[str, OncueDevice]] = hass.data[DOMAIN][
|
||||
config_entry.entry_id
|
||||
]
|
||||
entities: list[OncueBinarySensorEntity] = []
|
||||
"""Set up binary sensors."""
|
||||
coordinator = config_entry.runtime_data
|
||||
devices = coordinator.data
|
||||
for device_id, device in devices.items():
|
||||
entities.extend(
|
||||
OncueBinarySensorEntity(
|
||||
coordinator, device_id, device, sensor, SENSOR_MAP[key]
|
||||
)
|
||||
for key, sensor in device.sensors.items()
|
||||
if key in SENSOR_MAP
|
||||
)
|
||||
|
||||
async_add_entities(entities)
|
||||
async_add_entities(
|
||||
OncueBinarySensorEntity(coordinator, device_id, device, sensor, SENSOR_MAP[key])
|
||||
for device_id, device in devices.items()
|
||||
for key, sensor in device.sensors.items()
|
||||
if key in SENSOR_MAP
|
||||
)
|
||||
|
||||
|
||||
class OncueBinarySensorEntity(OncueEntity, BinarySensorEntity):
|
||||
|
|
|
@ -10,7 +10,6 @@ from homeassistant.components.sensor import (
|
|||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
PERCENTAGE,
|
||||
EntityCategory,
|
||||
|
@ -26,8 +25,8 @@ from homeassistant.core import HomeAssistant
|
|||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
from .const import DOMAIN
|
||||
from .entity import OncueEntity
|
||||
from .types import OncueConfigEntry
|
||||
|
||||
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||
SensorEntityDescription(
|
||||
|
@ -180,23 +179,18 @@ UNIT_MAPPINGS = {
|
|||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: OncueConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up sensors."""
|
||||
coordinator: DataUpdateCoordinator[dict[str, OncueDevice]] = hass.data[DOMAIN][
|
||||
config_entry.entry_id
|
||||
]
|
||||
entities: list[OncueSensorEntity] = []
|
||||
coordinator = config_entry.runtime_data
|
||||
devices = coordinator.data
|
||||
for device_id, device in devices.items():
|
||||
entities.extend(
|
||||
OncueSensorEntity(coordinator, device_id, device, sensor, SENSOR_MAP[key])
|
||||
for key, sensor in device.sensors.items()
|
||||
if key in SENSOR_MAP
|
||||
)
|
||||
|
||||
async_add_entities(entities)
|
||||
async_add_entities(
|
||||
OncueSensorEntity(coordinator, device_id, device, sensor, SENSOR_MAP[key])
|
||||
for device_id, device in devices.items()
|
||||
for key, sensor in device.sensors.items()
|
||||
if key in SENSOR_MAP
|
||||
)
|
||||
|
||||
|
||||
class OncueSensorEntity(OncueEntity, SensorEntity):
|
||||
|
|
10
homeassistant/components/oncue/types.py
Normal file
10
homeassistant/components/oncue/types.py
Normal file
|
@ -0,0 +1,10 @@
|
|||
"""Support for Oncue types."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from aiooncue import OncueDevice
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
type OncueConfigEntry = ConfigEntry[DataUpdateCoordinator[dict[str, OncueDevice]]]
|
Loading…
Add table
Reference in a new issue