Store runtime data inside the config entry in Efergy (#119551)
* Store runtime data inside the config entry in Efergy * store laterpull/119751/head
parent
b405e2f03e
commit
410ef8ce14
|
@ -16,9 +16,10 @@ from homeassistant.helpers.entity import Entity
|
|||
from .const import DEFAULT_NAME, DOMAIN
|
||||
|
||||
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."""
|
||||
api = Efergy(
|
||||
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"
|
||||
) from ex
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = api
|
||||
entry.runtime_data = api
|
||||
|
||||
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: EfergyConfigEntry) -> bool:
|
||||
"""Unload a config entry."""
|
||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
||||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
||||
|
||||
class EfergyEntity(Entity):
|
||||
|
|
|
@ -15,14 +15,13 @@ from homeassistant.components.sensor import (
|
|||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import UnitOfEnergy, UnitOfPower
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import StateType
|
||||
|
||||
from . import EfergyEntity
|
||||
from .const import CONF_CURRENT_VALUES, DOMAIN, LOGGER
|
||||
from . import EfergyConfigEntry, EfergyEntity
|
||||
from .const import CONF_CURRENT_VALUES, LOGGER
|
||||
|
||||
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||
SensorEntityDescription(
|
||||
|
@ -106,10 +105,12 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||
hass: HomeAssistant,
|
||||
entry: EfergyConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up Efergy sensors."""
|
||||
api: Efergy = hass.data[DOMAIN][entry.entry_id]
|
||||
api = entry.runtime_data
|
||||
sensors = []
|
||||
for description in SENSOR_TYPES:
|
||||
if description.key != CONF_CURRENT_VALUES:
|
||||
|
|
Loading…
Reference in New Issue