Skip to content

Commit 40182fc

Browse files
Load sun via entity component (#132598)
* Load sun via entity component * Remove unique id * Remove entity registry
1 parent 2da7a93 commit 40182fc

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

homeassistant/components/sun/__init__.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
from __future__ import annotations
44

5+
import logging
6+
57
from homeassistant.config_entries import SOURCE_IMPORT
68
from homeassistant.const import Platform
79
from homeassistant.core import HomeAssistant
810
from homeassistant.helpers import config_validation as cv
11+
from homeassistant.helpers.entity_component import EntityComponent
912
from homeassistant.helpers.typing import ConfigType
1013

1114
# The sensor platform is pre-imported here to ensure
@@ -23,6 +26,8 @@
2326

2427
CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
2528

29+
_LOGGER = logging.getLogger(__name__)
30+
2631

2732
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
2833
"""Track the state of the sun."""
@@ -42,7 +47,10 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
4247

4348
async def async_setup_entry(hass: HomeAssistant, entry: SunConfigEntry) -> bool:
4449
"""Set up from a config entry."""
45-
entry.runtime_data = sun = Sun(hass)
50+
sun = Sun(hass)
51+
component = EntityComponent[Sun](_LOGGER, DOMAIN, hass)
52+
await component.async_add_entities([sun])
53+
entry.runtime_data = sun
4654
entry.async_on_unload(sun.remove_listeners)
4755
await hass.config_entries.async_forward_entry_setups(entry, [Platform.SENSOR])
4856
return True
@@ -53,6 +61,5 @@ async def async_unload_entry(hass: HomeAssistant, entry: SunConfigEntry) -> bool
5361
if unload_ok := await hass.config_entries.async_unload_platforms(
5462
entry, [Platform.SENSOR]
5563
):
56-
sun = entry.runtime_data
57-
hass.states.async_remove(sun.entity_id)
64+
await entry.runtime_data.async_remove()
5865
return unload_ok

homeassistant/components/sun/entity.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,6 @@ class Sun(Entity):
100100

101101
_attr_name = "Sun"
102102
entity_id = ENTITY_ID
103-
# This entity is legacy and does not have a platform.
104-
# We can't fix this easily without breaking changes.
105-
_no_platform_reported = True
106103

107104
location: Location
108105
elevation: Elevation
@@ -122,18 +119,16 @@ def __init__(self, hass: HomeAssistant) -> None:
122119
self.hass = hass
123120
self.phase: str | None = None
124121

125-
# This is normally done by async_internal_added_to_hass which is not called
126-
# for sun because sun has no platform
127-
self._state_info = {
128-
"unrecorded_attributes": self._Entity__combined_unrecorded_attributes # type: ignore[attr-defined]
129-
}
130-
131122
self._config_listener: CALLBACK_TYPE | None = None
132123
self._update_events_listener: CALLBACK_TYPE | None = None
133124
self._update_sun_position_listener: CALLBACK_TYPE | None = None
134125
self._config_listener = self.hass.bus.async_listen(
135126
EVENT_CORE_CONFIG_UPDATE, self.update_location
136127
)
128+
129+
async def async_added_to_hass(self) -> None:
130+
"""Update after entity has been added."""
131+
await super().async_added_to_hass()
137132
self.update_location(initial=True)
138133

139134
@callback

0 commit comments

Comments
 (0)