Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Forecast.Solar integration."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.config_entries import ConfigEntry
6 from homeassistant.const import Platform
7 from homeassistant.core import HomeAssistant
8 
9 from .const import (
10  CONF_DAMPING,
11  CONF_DAMPING_EVENING,
12  CONF_DAMPING_MORNING,
13  CONF_MODULES_POWER,
14 )
15 from .coordinator import ForecastSolarDataUpdateCoordinator
16 
17 PLATFORMS = [Platform.SENSOR]
18 
19 type ForecastSolarConfigEntry = ConfigEntry[ForecastSolarDataUpdateCoordinator]
20 
21 
22 async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
23  """Migrate old config entry."""
24 
25  if entry.version == 1:
26  new_options = entry.options.copy()
27  new_options |= {
28  CONF_MODULES_POWER: new_options.pop("modules power"),
29  CONF_DAMPING_MORNING: new_options.get(CONF_DAMPING, 0.0),
30  CONF_DAMPING_EVENING: new_options.pop(CONF_DAMPING, 0.0),
31  }
32 
33  hass.config_entries.async_update_entry(
34  entry, data=entry.data, options=new_options, version=2
35  )
36 
37  return True
38 
39 
41  hass: HomeAssistant, entry: ForecastSolarConfigEntry
42 ) -> bool:
43  """Set up Forecast.Solar from a config entry."""
44  coordinator = ForecastSolarDataUpdateCoordinator(hass, entry)
45  await coordinator.async_config_entry_first_refresh()
46 
47  entry.runtime_data = coordinator
48 
49  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
50 
51  entry.async_on_unload(entry.add_update_listener(async_update_options))
52 
53  return True
54 
55 
56 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
57  """Unload a config entry."""
58  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
59 
60 
61 async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None:
62  """Update options."""
63  await hass.config_entries.async_reload(entry.entry_id)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:56
bool async_setup_entry(HomeAssistant hass, ForecastSolarConfigEntry entry)
Definition: __init__.py:42
None async_update_options(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:61
bool async_migrate_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:22