Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Stookwijzer integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from stookwijzer import Stookwijzer
8 
9 from homeassistant.const import CONF_LATITUDE, CONF_LOCATION, CONF_LONGITUDE, Platform
10 from homeassistant.core import HomeAssistant, callback
11 from homeassistant.helpers import entity_registry as er, issue_registry as ir
12 from homeassistant.helpers.aiohttp_client import async_get_clientsession
13 
14 from .const import DOMAIN, LOGGER
15 from .coordinator import StookwijzerConfigEntry, StookwijzerCoordinator
16 
17 PLATFORMS = [Platform.SENSOR]
18 
19 
20 async def async_setup_entry(hass: HomeAssistant, entry: StookwijzerConfigEntry) -> bool:
21  """Set up Stookwijzer from a config entry."""
22  await er.async_migrate_entries(hass, entry.entry_id, async_migrate_entity_entry)
23 
24  coordinator = StookwijzerCoordinator(hass, entry)
25  await coordinator.async_config_entry_first_refresh()
26 
27  entry.runtime_data = coordinator
28  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
29  return True
30 
31 
33  hass: HomeAssistant, entry: StookwijzerConfigEntry
34 ) -> bool:
35  """Unload Stookwijzer config entry."""
36  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
37 
38 
40  hass: HomeAssistant, entry: StookwijzerConfigEntry
41 ) -> bool:
42  """Migrate old entry."""
43  LOGGER.debug("Migrating from version %s", entry.version)
44 
45  if entry.version == 1:
46  latitude, longitude = await Stookwijzer.async_transform_coordinates(
48  entry.data[CONF_LOCATION][CONF_LATITUDE],
49  entry.data[CONF_LOCATION][CONF_LONGITUDE],
50  )
51 
52  if not latitude or not longitude:
53  ir.async_create_issue(
54  hass,
55  DOMAIN,
56  "location_migration_failed",
57  is_fixable=False,
58  severity=ir.IssueSeverity.ERROR,
59  translation_key="location_migration_failed",
60  translation_placeholders={
61  "entry_title": entry.title,
62  },
63  )
64  return False
65 
66  hass.config_entries.async_update_entry(
67  entry,
68  version=2,
69  data={
70  CONF_LATITUDE: latitude,
71  CONF_LONGITUDE: longitude,
72  },
73  )
74 
75  LOGGER.debug("Migration to version %s successful", entry.version)
76 
77  return True
78 
79 
80 @callback
81 def async_migrate_entity_entry(entity_entry: er.RegistryEntry) -> dict[str, Any] | None:
82  """Migrate Stookwijzer entity entries.
83 
84  - Migrates unique ID for the old Stookwijzer sensors to the new unique ID.
85  """
86  if entity_entry.unique_id == entity_entry.config_entry_id:
87  return {"new_unique_id": f"{entity_entry.config_entry_id}_advice"}
88 
89  # No migration needed
90  return None
dict[str, Any]|None async_migrate_entity_entry(er.RegistryEntry entity_entry)
Definition: __init__.py:81
bool async_unload_entry(HomeAssistant hass, StookwijzerConfigEntry entry)
Definition: __init__.py:34
bool async_setup_entry(HomeAssistant hass, StookwijzerConfigEntry entry)
Definition: __init__.py:20
bool async_migrate_entry(HomeAssistant hass, StookwijzerConfigEntry entry)
Definition: __init__.py:41
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)