Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for functionality to keep track of the sun."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.config_entries import SOURCE_IMPORT
6 from homeassistant.const import Platform
7 from homeassistant.core import HomeAssistant
8 from homeassistant.helpers import config_validation as cv
9 from homeassistant.helpers.typing import ConfigType
10 
11 # The sensor platform is pre-imported here to ensure
12 # it gets loaded when the base component is loaded
13 # as we will always load it and we do not want to have
14 # to wait for the import executor when its busy later
15 # in the startup process.
16 from . import sensor as sensor_pre_import # noqa: F401
17 from .const import ( # noqa: F401 # noqa: F401
18  DOMAIN,
19  STATE_ABOVE_HORIZON,
20  STATE_BELOW_HORIZON,
21 )
22 from .entity import Sun, SunConfigEntry
23 
24 CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN)
25 
26 
27 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
28  """Track the state of the sun."""
29  if not hass.config_entries.async_entries(DOMAIN):
30  # We avoid creating an import flow if its already
31  # setup since it will have to import the config_flow
32  # module.
33  hass.async_create_task(
34  hass.config_entries.flow.async_init(
35  DOMAIN,
36  context={"source": SOURCE_IMPORT},
37  data=config,
38  )
39  )
40  return True
41 
42 
43 async def async_setup_entry(hass: HomeAssistant, entry: SunConfigEntry) -> bool:
44  """Set up from a config entry."""
45  entry.runtime_data = sun = Sun(hass)
46  entry.async_on_unload(sun.remove_listeners)
47  await hass.config_entries.async_forward_entry_setups(entry, [Platform.SENSOR])
48  return True
49 
50 
51 async def async_unload_entry(hass: HomeAssistant, entry: SunConfigEntry) -> bool:
52  """Unload a config entry."""
53  if unload_ok := await hass.config_entries.async_unload_platforms(
54  entry, [Platform.SENSOR]
55  ):
56  sun = entry.runtime_data
57  hass.states.async_remove(sun.entity_id)
58  return unload_ok
bool async_setup_entry(HomeAssistant hass, SunConfigEntry entry)
Definition: __init__.py:43
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:27
bool async_unload_entry(HomeAssistant hass, SunConfigEntry entry)
Definition: __init__.py:51