Load sun via entity component (#132598)

* Load sun via entity component

* Remove unique id

* Remove entity registry
This commit is contained in:
G Johansson 2024-12-16 21:35:55 +01:00 committed by GitHub
parent 2da7a93139
commit 40182fc197
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 12 deletions

View File

@ -2,10 +2,13 @@
from __future__ import annotations
import logging
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
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.typing import ConfigType
# The sensor platform is pre-imported here to ensure
@ -23,6 +26,8 @@ from .entity import Sun, SunConfigEntry
CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
_LOGGER = logging.getLogger(__name__)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Track the state of the sun."""
@ -42,7 +47,10 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
async def async_setup_entry(hass: HomeAssistant, entry: SunConfigEntry) -> bool:
"""Set up from a config entry."""
entry.runtime_data = sun = Sun(hass)
sun = Sun(hass)
component = EntityComponent[Sun](_LOGGER, DOMAIN, hass)
await component.async_add_entities([sun])
entry.runtime_data = sun
entry.async_on_unload(sun.remove_listeners)
await hass.config_entries.async_forward_entry_setups(entry, [Platform.SENSOR])
return True
@ -53,6 +61,5 @@ async def async_unload_entry(hass: HomeAssistant, entry: SunConfigEntry) -> bool
if unload_ok := await hass.config_entries.async_unload_platforms(
entry, [Platform.SENSOR]
):
sun = entry.runtime_data
hass.states.async_remove(sun.entity_id)
await entry.runtime_data.async_remove()
return unload_ok

View File

@ -100,9 +100,6 @@ class Sun(Entity):
_attr_name = "Sun"
entity_id = ENTITY_ID
# This entity is legacy and does not have a platform.
# We can't fix this easily without breaking changes.
_no_platform_reported = True
location: Location
elevation: Elevation
@ -122,18 +119,16 @@ class Sun(Entity):
self.hass = hass
self.phase: str | None = None
# This is normally done by async_internal_added_to_hass which is not called
# for sun because sun has no platform
self._state_info = {
"unrecorded_attributes": self._Entity__combined_unrecorded_attributes # type: ignore[attr-defined]
}
self._config_listener: CALLBACK_TYPE | None = None
self._update_events_listener: CALLBACK_TYPE | None = None
self._update_sun_position_listener: CALLBACK_TYPE | None = None
self._config_listener = self.hass.bus.async_listen(
EVENT_CORE_CONFIG_UPDATE, self.update_location
)
async def async_added_to_hass(self) -> None:
"""Update after entity has been added."""
await super().async_added_to_hass()
self.update_location(initial=True)
@callback