Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for the Swedish weather institute weather service."""
2 
3 from homeassistant.config_entries import ConfigEntry
4 from homeassistant.const import (
5  CONF_LATITUDE,
6  CONF_LOCATION,
7  CONF_LONGITUDE,
8  CONF_NAME,
9  Platform,
10 )
11 from homeassistant.core import HomeAssistant
12 
13 PLATFORMS = [Platform.WEATHER]
14 
15 
16 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
17  """Set up SMHI forecast as config entry."""
18 
19  # Setting unique id where missing
20  if entry.unique_id is None:
21  unique_id = f"{entry.data[CONF_LOCATION][CONF_LATITUDE]}-{entry.data[CONF_LOCATION][CONF_LONGITUDE]}"
22  hass.config_entries.async_update_entry(entry, unique_id=unique_id)
23 
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: ConfigEntry) -> bool:
29  """Unload a config entry."""
30  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
31 
32 
33 async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
34  """Migrate old entry."""
35  if entry.version == 1:
36  new_data = {
37  CONF_NAME: entry.data[CONF_NAME],
38  CONF_LOCATION: {
39  CONF_LATITUDE: entry.data[CONF_LATITUDE],
40  CONF_LONGITUDE: entry.data[CONF_LONGITUDE],
41  },
42  }
43 
44  if not hass.config_entries.async_update_entry(entry, data=new_data, version=2):
45  return False
46 
47  return True
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:16
bool async_migrate_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:33
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:28