Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Solar-Log integration."""
2 
3 import logging
4 
5 from homeassistant.config_entries import ConfigEntry
6 from homeassistant.const import Platform
7 from homeassistant.core import HomeAssistant
8 from homeassistant.helpers import entity_registry as er
9 
10 from .const import CONF_HAS_PWD
11 from .coordinator import SolarLogCoordinator
12 
13 _LOGGER = logging.getLogger(__name__)
14 
15 PLATFORMS = [Platform.SENSOR]
16 type SolarlogConfigEntry = ConfigEntry[SolarLogCoordinator]
17 
18 
19 async def async_setup_entry(hass: HomeAssistant, entry: SolarlogConfigEntry) -> bool:
20  """Set up a config entry for solarlog."""
21  coordinator = SolarLogCoordinator(hass, entry)
22  await coordinator.async_config_entry_first_refresh()
23  entry.runtime_data = coordinator
24  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
25  return True
26 
27 
28 async def async_unload_entry(hass: HomeAssistant, entry: SolarlogConfigEntry) -> bool:
29  """Unload a config entry."""
30  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
31 
32 
34  hass: HomeAssistant, config_entry: SolarlogConfigEntry
35 ) -> bool:
36  """Migrate old entry."""
37  _LOGGER.debug("Migrating from version %s", config_entry.version)
38 
39  if config_entry.version > 1:
40  # This means the user has downgraded from a future version
41  return False
42 
43  if config_entry.version == 1:
44  if config_entry.minor_version < 2:
45  # migrate old entity unique id
46  entity_reg = er.async_get(hass)
47  entities: list[er.RegistryEntry] = er.async_entries_for_config_entry(
48  entity_reg, config_entry.entry_id
49  )
50 
51  for entity in entities:
52  if "time" in entity.unique_id:
53  new_uid = entity.unique_id.replace("time", "last_updated")
54  _LOGGER.debug(
55  "migrate unique id '%s' to '%s'", entity.unique_id, new_uid
56  )
57  entity_reg.async_update_entity(
58  entity.entity_id, new_unique_id=new_uid
59  )
60 
61  if config_entry.minor_version < 3:
62  # migrate config_entry
63  new = {**config_entry.data}
64  new[CONF_HAS_PWD] = False
65 
66  hass.config_entries.async_update_entry(
67  config_entry, data=new, minor_version=3, version=1
68  )
69 
70  _LOGGER.debug(
71  "Migration to version %s.%s successful",
72  config_entry.version,
73  config_entry.minor_version,
74  )
75 
76  return True
bool async_setup_entry(HomeAssistant hass, SolarlogConfigEntry entry)
Definition: __init__.py:19
bool async_migrate_entry(HomeAssistant hass, SolarlogConfigEntry config_entry)
Definition: __init__.py:35
bool async_unload_entry(HomeAssistant hass, SolarlogConfigEntry entry)
Definition: __init__.py:28