mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
Migrate oncue to use entry.runtime_data (#122307)
This commit is contained in:
parent
7f852d0f73
commit
272f0bc21c
@ -7,21 +7,21 @@ import logging
|
|||||||
|
|
||||||
from aiooncue import LoginFailedException, Oncue, OncueDevice
|
from aiooncue import LoginFailedException, Oncue, OncueDevice
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
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]
|
PLATFORMS: list[str] = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_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."""
|
"""Set up Oncue from a config entry."""
|
||||||
data = entry.data
|
data = entry.data
|
||||||
websession = async_get_clientsession(hass)
|
websession = async_get_clientsession(hass)
|
||||||
@ -40,7 +40,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
except LoginFailedException as ex:
|
except LoginFailedException as ex:
|
||||||
raise ConfigEntryAuthFailed from ex
|
raise ConfigEntryAuthFailed from ex
|
||||||
|
|
||||||
coordinator = DataUpdateCoordinator(
|
coordinator = DataUpdateCoordinator[dict[str, OncueDevice]](
|
||||||
hass,
|
hass,
|
||||||
_LOGGER,
|
_LOGGER,
|
||||||
name=f"Oncue {entry.data[CONF_USERNAME]}",
|
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()
|
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)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
return True
|
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."""
|
"""Unload a config entry."""
|
||||||
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
|
|
||||||
|
@ -2,21 +2,17 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from aiooncue import OncueDevice
|
|
||||||
|
|
||||||
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 homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
|
||||||
|
|
||||||
from .const import DOMAIN
|
|
||||||
from .entity import OncueEntity
|
from .entity import OncueEntity
|
||||||
|
from .types import OncueConfigEntry
|
||||||
|
|
||||||
SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = (
|
SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = (
|
||||||
BinarySensorEntityDescription(
|
BinarySensorEntityDescription(
|
||||||
@ -31,25 +27,18 @@ SENSOR_MAP = {description.key: description for description in SENSOR_TYPES}
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: OncueConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up sensors."""
|
"""Set up binary sensors."""
|
||||||
coordinator: DataUpdateCoordinator[dict[str, OncueDevice]] = hass.data[DOMAIN][
|
coordinator = config_entry.runtime_data
|
||||||
config_entry.entry_id
|
|
||||||
]
|
|
||||||
entities: list[OncueBinarySensorEntity] = []
|
|
||||||
devices = coordinator.data
|
devices = coordinator.data
|
||||||
for device_id, device in devices.items():
|
async_add_entities(
|
||||||
entities.extend(
|
OncueBinarySensorEntity(coordinator, device_id, device, sensor, SENSOR_MAP[key])
|
||||||
OncueBinarySensorEntity(
|
for device_id, device in devices.items()
|
||||||
coordinator, device_id, device, sensor, SENSOR_MAP[key]
|
for key, sensor in device.sensors.items()
|
||||||
)
|
if key in SENSOR_MAP
|
||||||
for key, sensor in device.sensors.items()
|
)
|
||||||
if key in SENSOR_MAP
|
|
||||||
)
|
|
||||||
|
|
||||||
async_add_entities(entities)
|
|
||||||
|
|
||||||
|
|
||||||
class OncueBinarySensorEntity(OncueEntity, BinarySensorEntity):
|
class OncueBinarySensorEntity(OncueEntity, BinarySensorEntity):
|
||||||
|
@ -10,7 +10,6 @@ from homeassistant.components.sensor import (
|
|||||||
SensorEntityDescription,
|
SensorEntityDescription,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
PERCENTAGE,
|
PERCENTAGE,
|
||||||
EntityCategory,
|
EntityCategory,
|
||||||
@ -26,8 +25,8 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||||
|
|
||||||
from .const import DOMAIN
|
|
||||||
from .entity import OncueEntity
|
from .entity import OncueEntity
|
||||||
|
from .types import OncueConfigEntry
|
||||||
|
|
||||||
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||||
SensorEntityDescription(
|
SensorEntityDescription(
|
||||||
@ -180,23 +179,18 @@ UNIT_MAPPINGS = {
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: OncueConfigEntry,
|
||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up sensors."""
|
"""Set up sensors."""
|
||||||
coordinator: DataUpdateCoordinator[dict[str, OncueDevice]] = hass.data[DOMAIN][
|
coordinator = config_entry.runtime_data
|
||||||
config_entry.entry_id
|
|
||||||
]
|
|
||||||
entities: list[OncueSensorEntity] = []
|
|
||||||
devices = coordinator.data
|
devices = coordinator.data
|
||||||
for device_id, device in devices.items():
|
async_add_entities(
|
||||||
entities.extend(
|
OncueSensorEntity(coordinator, device_id, device, sensor, SENSOR_MAP[key])
|
||||||
OncueSensorEntity(coordinator, device_id, device, sensor, SENSOR_MAP[key])
|
for device_id, device in devices.items()
|
||||||
for key, sensor in device.sensors.items()
|
for key, sensor in device.sensors.items()
|
||||||
if key in SENSOR_MAP
|
if key in SENSOR_MAP
|
||||||
)
|
)
|
||||||
|
|
||||||
async_add_entities(entities)
|
|
||||||
|
|
||||||
|
|
||||||
class OncueSensorEntity(OncueEntity, SensorEntity):
|
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…
x
Reference in New Issue
Block a user