mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 16:27:08 +00:00
Use runtime_data in flo (#138307)
This commit is contained in:
parent
14e1b55b5a
commit
8e7f35aa7d
@ -6,27 +6,23 @@ import logging
|
|||||||
from aioflo import async_get_api
|
from aioflo import async_get_api
|
||||||
from aioflo.errors import RequestError
|
from aioflo.errors import RequestError
|
||||||
|
|
||||||
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 ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
|
|
||||||
from .const import CLIENT, DOMAIN
|
from .coordinator import FloConfigEntry, FloDeviceDataUpdateCoordinator, FloRuntimeData
|
||||||
from .coordinator import FloDeviceDataUpdateCoordinator
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR, Platform.SWITCH]
|
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR, Platform.SWITCH]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: FloConfigEntry) -> bool:
|
||||||
"""Set up flo from a config entry."""
|
"""Set up flo from a config entry."""
|
||||||
session = async_get_clientsession(hass)
|
session = async_get_clientsession(hass)
|
||||||
hass.data.setdefault(DOMAIN, {})
|
|
||||||
hass.data[DOMAIN][entry.entry_id] = {}
|
|
||||||
try:
|
try:
|
||||||
hass.data[DOMAIN][entry.entry_id][CLIENT] = client = await async_get_api(
|
client = await async_get_api(
|
||||||
entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD], session=session
|
entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD], session=session
|
||||||
)
|
)
|
||||||
except RequestError as err:
|
except RequestError as err:
|
||||||
@ -36,7 +32,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
|
|
||||||
_LOGGER.debug("Flo user information with locations: %s", user_info)
|
_LOGGER.debug("Flo user information with locations: %s", user_info)
|
||||||
|
|
||||||
hass.data[DOMAIN][entry.entry_id]["devices"] = devices = [
|
devices = [
|
||||||
FloDeviceDataUpdateCoordinator(
|
FloDeviceDataUpdateCoordinator(
|
||||||
hass, entry, client, location["id"], device["id"]
|
hass, entry, client, location["id"], device["id"]
|
||||||
)
|
)
|
||||||
@ -47,14 +43,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
tasks = [device.async_refresh() for device in devices]
|
tasks = [device.async_refresh() for device in devices]
|
||||||
await asyncio.gather(*tasks)
|
await asyncio.gather(*tasks)
|
||||||
|
|
||||||
|
entry.runtime_data = FloRuntimeData(client=client, devices=devices)
|
||||||
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: FloConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
if unload_ok:
|
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
|
||||||
return unload_ok
|
|
||||||
|
@ -6,24 +6,20 @@ from homeassistant.components.binary_sensor import (
|
|||||||
BinarySensorDeviceClass,
|
BinarySensorDeviceClass,
|
||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
|
|
||||||
from .const import DOMAIN as FLO_DOMAIN
|
from .coordinator import FloConfigEntry
|
||||||
from .coordinator import FloDeviceDataUpdateCoordinator
|
|
||||||
from .entity import FloEntity
|
from .entity import FloEntity
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: FloConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Flo sensors from config entry."""
|
"""Set up the Flo sensors from config entry."""
|
||||||
devices: list[FloDeviceDataUpdateCoordinator] = hass.data[FLO_DOMAIN][
|
devices = config_entry.runtime_data.devices
|
||||||
config_entry.entry_id
|
|
||||||
]["devices"]
|
|
||||||
entities: list[BinarySensorEntity] = []
|
entities: list[BinarySensorEntity] = []
|
||||||
for device in devices:
|
for device in devices:
|
||||||
if device.device_type == "puck_oem":
|
if device.device_type == "puck_oem":
|
||||||
|
@ -4,7 +4,6 @@ import logging
|
|||||||
|
|
||||||
LOGGER = logging.getLogger(__package__)
|
LOGGER = logging.getLogger(__package__)
|
||||||
|
|
||||||
CLIENT = "client"
|
|
||||||
DOMAIN = "flo"
|
DOMAIN = "flo"
|
||||||
FLO_HOME = "home"
|
FLO_HOME = "home"
|
||||||
FLO_AWAY = "away"
|
FLO_AWAY = "away"
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from dataclasses import dataclass
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
@ -17,6 +18,16 @@ from homeassistant.util import dt as dt_util
|
|||||||
|
|
||||||
from .const import DOMAIN as FLO_DOMAIN, LOGGER
|
from .const import DOMAIN as FLO_DOMAIN, LOGGER
|
||||||
|
|
||||||
|
type FloConfigEntry = ConfigEntry[FloRuntimeData]
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class FloRuntimeData:
|
||||||
|
"""Flo runtime data."""
|
||||||
|
|
||||||
|
client: API
|
||||||
|
devices: list[FloDeviceDataUpdateCoordinator]
|
||||||
|
|
||||||
|
|
||||||
class FloDeviceDataUpdateCoordinator(DataUpdateCoordinator):
|
class FloDeviceDataUpdateCoordinator(DataUpdateCoordinator):
|
||||||
"""Flo device object."""
|
"""Flo device object."""
|
||||||
|
@ -7,7 +7,6 @@ from homeassistant.components.sensor import (
|
|||||||
SensorEntity,
|
SensorEntity,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
PERCENTAGE,
|
PERCENTAGE,
|
||||||
UnitOfPressure,
|
UnitOfPressure,
|
||||||
@ -18,20 +17,17 @@ from homeassistant.const import (
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
|
|
||||||
from .const import DOMAIN as FLO_DOMAIN
|
from .coordinator import FloConfigEntry
|
||||||
from .coordinator import FloDeviceDataUpdateCoordinator
|
|
||||||
from .entity import FloEntity
|
from .entity import FloEntity
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: FloConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Flo sensors from config entry."""
|
"""Set up the Flo sensors from config entry."""
|
||||||
devices: list[FloDeviceDataUpdateCoordinator] = hass.data[FLO_DOMAIN][
|
devices = config_entry.runtime_data.devices
|
||||||
config_entry.entry_id
|
|
||||||
]["devices"]
|
|
||||||
entities = []
|
entities = []
|
||||||
for device in devices:
|
for device in devices:
|
||||||
if device.device_type == "puck_oem":
|
if device.device_type == "puck_oem":
|
||||||
|
@ -8,13 +8,11 @@ from aioflo.location import SLEEP_MINUTE_OPTIONS, SYSTEM_MODE_HOME, SYSTEM_REVER
|
|||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.switch import SwitchEntity
|
from homeassistant.components.switch import SwitchEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers import entity_platform
|
from homeassistant.helpers import entity_platform
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
|
|
||||||
from .const import DOMAIN as FLO_DOMAIN
|
from .coordinator import FloConfigEntry, FloDeviceDataUpdateCoordinator
|
||||||
from .coordinator import FloDeviceDataUpdateCoordinator
|
|
||||||
from .entity import FloEntity
|
from .entity import FloEntity
|
||||||
|
|
||||||
ATTR_REVERT_TO_MODE = "revert_to_mode"
|
ATTR_REVERT_TO_MODE = "revert_to_mode"
|
||||||
@ -27,13 +25,11 @@ SERVICE_RUN_HEALTH_TEST = "run_health_test"
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: FloConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Flo switches from config entry."""
|
"""Set up the Flo switches from config entry."""
|
||||||
devices: list[FloDeviceDataUpdateCoordinator] = hass.data[FLO_DOMAIN][
|
devices = config_entry.runtime_data.devices
|
||||||
config_entry.entry_id
|
|
||||||
]["devices"]
|
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
[FloSwitch(device) for device in devices if device.device_type != "puck_oem"]
|
[FloSwitch(device) for device in devices if device.device_type != "puck_oem"]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user