22
33from __future__ import annotations
44
5+ import logging
6+
57from homeassistant .config_entries import SOURCE_IMPORT
68from homeassistant .const import Platform
79from homeassistant .core import HomeAssistant
810from homeassistant .helpers import config_validation as cv
11+ from homeassistant .helpers .entity_component import EntityComponent
912from homeassistant .helpers .typing import ConfigType
1013
1114# The sensor platform is pre-imported here to ensure
2326
2427CONFIG_SCHEMA = cv .empty_config_schema (DOMAIN )
2528
29+ _LOGGER = logging .getLogger (__name__ )
30+
2631
2732async 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
4348async 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
0 commit comments