mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 08:17: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.errors import RequestError
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
|
||||
from .const import CLIENT, DOMAIN
|
||||
from .coordinator import FloDeviceDataUpdateCoordinator
|
||||
from .coordinator import FloConfigEntry, FloDeviceDataUpdateCoordinator, FloRuntimeData
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
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."""
|
||||
session = async_get_clientsession(hass)
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
hass.data[DOMAIN][entry.entry_id] = {}
|
||||
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
|
||||
)
|
||||
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)
|
||||
|
||||
hass.data[DOMAIN][entry.entry_id]["devices"] = devices = [
|
||||
devices = [
|
||||
FloDeviceDataUpdateCoordinator(
|
||||
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]
|
||||
await asyncio.gather(*tasks)
|
||||
|
||||
entry.runtime_data = FloRuntimeData(client=client, devices=devices)
|
||||
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: FloConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
if unload_ok:
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
@ -6,24 +6,20 @@ from homeassistant.components.binary_sensor import (
|
||||
BinarySensorDeviceClass,
|
||||
BinarySensorEntity,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .const import DOMAIN as FLO_DOMAIN
|
||||
from .coordinator import FloDeviceDataUpdateCoordinator
|
||||
from .coordinator import FloConfigEntry
|
||||
from .entity import FloEntity
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: FloConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the Flo sensors from config entry."""
|
||||
devices: list[FloDeviceDataUpdateCoordinator] = hass.data[FLO_DOMAIN][
|
||||
config_entry.entry_id
|
||||
]["devices"]
|
||||
devices = config_entry.runtime_data.devices
|
||||
entities: list[BinarySensorEntity] = []
|
||||
for device in devices:
|
||||
if device.device_type == "puck_oem":
|
||||
|
@ -4,7 +4,6 @@ import logging
|
||||
|
||||
LOGGER = logging.getLogger(__package__)
|
||||
|
||||
CLIENT = "client"
|
||||
DOMAIN = "flo"
|
||||
FLO_HOME = "home"
|
||||
FLO_AWAY = "away"
|
||||
|
@ -3,6 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
@ -17,6 +18,16 @@ from homeassistant.util import dt as dt_util
|
||||
|
||||
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):
|
||||
"""Flo device object."""
|
||||
|
@ -7,7 +7,6 @@ from homeassistant.components.sensor import (
|
||||
SensorEntity,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
PERCENTAGE,
|
||||
UnitOfPressure,
|
||||
@ -18,20 +17,17 @@ from homeassistant.const import (
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .const import DOMAIN as FLO_DOMAIN
|
||||
from .coordinator import FloDeviceDataUpdateCoordinator
|
||||
from .coordinator import FloConfigEntry
|
||||
from .entity import FloEntity
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: FloConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the Flo sensors from config entry."""
|
||||
devices: list[FloDeviceDataUpdateCoordinator] = hass.data[FLO_DOMAIN][
|
||||
config_entry.entry_id
|
||||
]["devices"]
|
||||
devices = config_entry.runtime_data.devices
|
||||
entities = []
|
||||
for device in devices:
|
||||
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
|
||||
|
||||
from homeassistant.components.switch import SwitchEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers import entity_platform
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .const import DOMAIN as FLO_DOMAIN
|
||||
from .coordinator import FloDeviceDataUpdateCoordinator
|
||||
from .coordinator import FloConfigEntry, FloDeviceDataUpdateCoordinator
|
||||
from .entity import FloEntity
|
||||
|
||||
ATTR_REVERT_TO_MODE = "revert_to_mode"
|
||||
@ -27,13 +25,11 @@ SERVICE_RUN_HEALTH_TEST = "run_health_test"
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: FloConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the Flo switches from config entry."""
|
||||
devices: list[FloDeviceDataUpdateCoordinator] = hass.data[FLO_DOMAIN][
|
||||
config_entry.entry_id
|
||||
]["devices"]
|
||||
devices = config_entry.runtime_data.devices
|
||||
|
||||
async_add_entities(
|
||||
[FloSwitch(device) for device in devices if device.device_type != "puck_oem"]
|
||||
|
Loading…
x
Reference in New Issue
Block a user