Store runtime data inside the config entry in Efergy (#119551)

* Store runtime data inside the config entry in Efergy

* store later
pull/119751/head
Robert Hillis 2024-06-15 12:14:34 -04:00 committed by GitHub
parent b405e2f03e
commit 410ef8ce14
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 11 deletions

View File

@ -16,9 +16,10 @@ from homeassistant.helpers.entity import Entity
from .const import DEFAULT_NAME, DOMAIN from .const import DEFAULT_NAME, DOMAIN
PLATFORMS = [Platform.SENSOR] PLATFORMS = [Platform.SENSOR]
type EfergyConfigEntry = ConfigEntry[Efergy]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: EfergyConfigEntry) -> bool:
"""Set up Efergy from a config entry.""" """Set up Efergy from a config entry."""
api = Efergy( api = Efergy(
entry.data[CONF_API_KEY], entry.data[CONF_API_KEY],
@ -36,17 +37,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"API Key is no longer valid. Please reauthenticate" "API Key is no longer valid. Please reauthenticate"
) from ex ) from ex
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = api entry.runtime_data = api
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: EfergyConfigEntry) -> 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
class EfergyEntity(Entity): class EfergyEntity(Entity):

View File

@ -15,14 +15,13 @@ from homeassistant.components.sensor import (
SensorEntityDescription, SensorEntityDescription,
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import UnitOfEnergy, UnitOfPower from homeassistant.const import UnitOfEnergy, UnitOfPower
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.typing import StateType from homeassistant.helpers.typing import StateType
from . import EfergyEntity from . import EfergyConfigEntry, EfergyEntity
from .const import CONF_CURRENT_VALUES, DOMAIN, LOGGER from .const import CONF_CURRENT_VALUES, LOGGER
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription( SensorEntityDescription(
@ -106,10 +105,12 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback hass: HomeAssistant,
entry: EfergyConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up Efergy sensors.""" """Set up Efergy sensors."""
api: Efergy = hass.data[DOMAIN][entry.entry_id] api = entry.runtime_data
sensors = [] sensors = []
for description in SENSOR_TYPES: for description in SENSOR_TYPES:
if description.key != CONF_CURRENT_VALUES: if description.key != CONF_CURRENT_VALUES: