mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Load sun via entity component (#132598)
* Load sun via entity component * Remove unique id * Remove entity registry
This commit is contained in:
parent
2da7a93139
commit
40182fc197
@ -2,10 +2,13 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
from homeassistant.config_entries import SOURCE_IMPORT
|
from homeassistant.config_entries import SOURCE_IMPORT
|
||||||
from homeassistant.const import Platform
|
from homeassistant.const import Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import config_validation as cv
|
from homeassistant.helpers import config_validation as cv
|
||||||
|
from homeassistant.helpers.entity_component import EntityComponent
|
||||||
from homeassistant.helpers.typing import ConfigType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
|
|
||||||
# The sensor platform is pre-imported here to ensure
|
# 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)
|
CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
"""Track the state of the sun."""
|
"""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:
|
async def async_setup_entry(hass: HomeAssistant, entry: SunConfigEntry) -> bool:
|
||||||
"""Set up from a config entry."""
|
"""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)
|
entry.async_on_unload(sun.remove_listeners)
|
||||||
await hass.config_entries.async_forward_entry_setups(entry, [Platform.SENSOR])
|
await hass.config_entries.async_forward_entry_setups(entry, [Platform.SENSOR])
|
||||||
return True
|
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(
|
if unload_ok := await hass.config_entries.async_unload_platforms(
|
||||||
entry, [Platform.SENSOR]
|
entry, [Platform.SENSOR]
|
||||||
):
|
):
|
||||||
sun = entry.runtime_data
|
await entry.runtime_data.async_remove()
|
||||||
hass.states.async_remove(sun.entity_id)
|
|
||||||
return unload_ok
|
return unload_ok
|
||||||
|
@ -100,9 +100,6 @@ class Sun(Entity):
|
|||||||
|
|
||||||
_attr_name = "Sun"
|
_attr_name = "Sun"
|
||||||
entity_id = ENTITY_ID
|
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
|
location: Location
|
||||||
elevation: Elevation
|
elevation: Elevation
|
||||||
@ -122,18 +119,16 @@ class Sun(Entity):
|
|||||||
self.hass = hass
|
self.hass = hass
|
||||||
self.phase: str | None = None
|
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._config_listener: CALLBACK_TYPE | None = None
|
||||||
self._update_events_listener: CALLBACK_TYPE | None = None
|
self._update_events_listener: CALLBACK_TYPE | None = None
|
||||||
self._update_sun_position_listener: CALLBACK_TYPE | None = None
|
self._update_sun_position_listener: CALLBACK_TYPE | None = None
|
||||||
self._config_listener = self.hass.bus.async_listen(
|
self._config_listener = self.hass.bus.async_listen(
|
||||||
EVENT_CORE_CONFIG_UPDATE, self.update_location
|
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)
|
self.update_location(initial=True)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
|
Loading…
x
Reference in New Issue
Block a user