mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
Use runtime_data in huisbaasje (#144953)
This commit is contained in:
parent
a3aae68229
commit
177afea5ad
@ -4,19 +4,18 @@ import logging
|
|||||||
|
|
||||||
from energyflip import EnergyFlip, EnergyFlipException
|
from energyflip import EnergyFlip, EnergyFlipException
|
||||||
|
|
||||||
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 .const import DATA_COORDINATOR, DOMAIN, FETCH_TIMEOUT, SOURCE_TYPES
|
from .const import FETCH_TIMEOUT, SOURCE_TYPES
|
||||||
from .coordinator import EnergyFlipUpdateCoordinator
|
from .coordinator import EnergyFlipConfigEntry, EnergyFlipUpdateCoordinator
|
||||||
|
|
||||||
PLATFORMS = [Platform.SENSOR]
|
PLATFORMS = [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: EnergyFlipConfigEntry) -> bool:
|
||||||
"""Set up EnergyFlip from a config entry."""
|
"""Set up EnergyFlip from a config entry."""
|
||||||
# Create the EnergyFlip client
|
# Create the EnergyFlip client
|
||||||
energyflip = EnergyFlip(
|
energyflip = EnergyFlip(
|
||||||
@ -39,7 +38,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
|
|
||||||
# Load the client in the data of home assistant
|
# Load the client in the data of home assistant
|
||||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {DATA_COORDINATOR: coordinator}
|
entry.runtime_data = coordinator
|
||||||
|
|
||||||
# Offload the loading of entities to the platform
|
# Offload the loading of entities to the platform
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||||
@ -47,13 +46,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: EnergyFlipConfigEntry) -> bool:
|
||||||
"""Unload a config entry."""
|
"""Unload a config entry."""
|
||||||
# Forward the unloading of the entry to the platform
|
# Forward the unloading of the entry to the platform
|
||||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
|
|
||||||
# If successful, unload the EnergyFlip client
|
|
||||||
if unload_ok:
|
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
|
||||||
|
|
||||||
return unload_ok
|
|
||||||
|
@ -9,8 +9,6 @@ from energyflip.const import (
|
|||||||
SOURCE_TYPE_GAS,
|
SOURCE_TYPE_GAS,
|
||||||
)
|
)
|
||||||
|
|
||||||
DATA_COORDINATOR = "coordinator"
|
|
||||||
|
|
||||||
DOMAIN = "huisbaasje"
|
DOMAIN = "huisbaasje"
|
||||||
|
|
||||||
"""Interval in seconds between polls to EnergyFlip."""
|
"""Interval in seconds between polls to EnergyFlip."""
|
||||||
|
@ -27,16 +27,18 @@ PLATFORMS = [Platform.SENSOR]
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
type EnergyFlipConfigEntry = ConfigEntry[EnergyFlipUpdateCoordinator]
|
||||||
|
|
||||||
|
|
||||||
class EnergyFlipUpdateCoordinator(DataUpdateCoordinator[dict[str, dict[str, Any]]]):
|
class EnergyFlipUpdateCoordinator(DataUpdateCoordinator[dict[str, dict[str, Any]]]):
|
||||||
"""EnergyFlip data update coordinator."""
|
"""EnergyFlip data update coordinator."""
|
||||||
|
|
||||||
config_entry: ConfigEntry
|
config_entry: EnergyFlipConfigEntry
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: EnergyFlipConfigEntry,
|
||||||
energyflip: EnergyFlip,
|
energyflip: EnergyFlip,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the Huisbaasje data coordinator."""
|
"""Initialize the Huisbaasje data coordinator."""
|
||||||
|
@ -20,7 +20,6 @@ from homeassistant.components.sensor import (
|
|||||||
SensorEntityDescription,
|
SensorEntityDescription,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_ID,
|
CONF_ID,
|
||||||
UnitOfEnergy,
|
UnitOfEnergy,
|
||||||
@ -33,7 +32,6 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
|||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
DATA_COORDINATOR,
|
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
SENSOR_TYPE_RATE,
|
SENSOR_TYPE_RATE,
|
||||||
SENSOR_TYPE_THIS_DAY,
|
SENSOR_TYPE_THIS_DAY,
|
||||||
@ -41,7 +39,7 @@ from .const import (
|
|||||||
SENSOR_TYPE_THIS_WEEK,
|
SENSOR_TYPE_THIS_WEEK,
|
||||||
SENSOR_TYPE_THIS_YEAR,
|
SENSOR_TYPE_THIS_YEAR,
|
||||||
)
|
)
|
||||||
from .coordinator import EnergyFlipUpdateCoordinator
|
from .coordinator import EnergyFlipConfigEntry, EnergyFlipUpdateCoordinator
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -215,13 +213,11 @@ SENSORS_INFO = [
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config_entry: ConfigEntry,
|
config_entry: EnergyFlipConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the sensor platform."""
|
"""Set up the sensor platform."""
|
||||||
coordinator: EnergyFlipUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id][
|
coordinator = config_entry.runtime_data
|
||||||
DATA_COORDINATOR
|
|
||||||
]
|
|
||||||
user_id = config_entry.data[CONF_ID]
|
user_id = config_entry.data[CONF_ID]
|
||||||
|
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user