Store runtime data inside the config entry in Sun (#116808)

* store runtime data inside the config entry

* move to entry.async_on_unload()
This commit is contained in:
Michael 2024-05-04 23:39:12 +02:00 committed by GitHub
parent 90a3c2e357
commit 910c991a58
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 11 deletions

View File

@ -2,7 +2,7 @@
from __future__ import annotations
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.config_entries import SOURCE_IMPORT
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv
@ -19,7 +19,7 @@ from .const import ( # noqa: F401 # noqa: F401
STATE_ABOVE_HORIZON,
STATE_BELOW_HORIZON,
)
from .entity import Sun
from .entity import Sun, SunConfigEntry
CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
@ -40,19 +40,19 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: SunConfigEntry) -> bool:
"""Set up from a config entry."""
hass.data[DOMAIN] = Sun(hass)
entry.runtime_data = sun = Sun(hass)
entry.async_on_unload(sun.remove_listeners)
await hass.config_entries.async_forward_entry_setups(entry, [Platform.SENSOR])
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
async def async_unload_entry(hass: HomeAssistant, entry: SunConfigEntry) -> bool:
"""Unload a config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(
entry, [Platform.SENSOR]
):
sun: Sun = hass.data.pop(DOMAIN)
sun.remove_listeners()
sun = entry.runtime_data
hass.states.async_remove(sun.entity_id)
return unload_ok

View File

@ -8,6 +8,7 @@ from typing import Any
from astral.location import Elevation, Location
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
EVENT_CORE_CONFIG_UPDATE,
SUN_EVENT_SUNRISE,
@ -30,6 +31,8 @@ from .const import (
STATE_BELOW_HORIZON,
)
SunConfigEntry = ConfigEntry["Sun"]
_LOGGER = logging.getLogger(__name__)
ENTITY_ID = "sun.sun"

View File

@ -13,7 +13,6 @@ from homeassistant.components.sensor import (
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import DEGREE, EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
@ -22,7 +21,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from .const import DOMAIN, SIGNAL_EVENTS_CHANGED, SIGNAL_POSITION_CHANGED
from .entity import Sun
from .entity import Sun, SunConfigEntry
ENTITY_ID_SENSOR_FORMAT = SENSOR_DOMAIN + ".sun_{}"
@ -107,11 +106,11 @@ SENSOR_TYPES: tuple[SunSensorEntityDescription, ...] = (
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
hass: HomeAssistant, entry: SunConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up Sun sensor platform."""
sun: Sun = hass.data[DOMAIN]
sun = entry.runtime_data
async_add_entities(
[SunSensor(sun, description, entry.entry_id) for description in SENSOR_TYPES]