Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The met component."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.const import Platform
9 from homeassistant.core import HomeAssistant
10 from homeassistant.helpers import device_registry as dr
11 
12 from .const import (
13  CONF_TRACK_HOME,
14  DEFAULT_HOME_LATITUDE,
15  DEFAULT_HOME_LONGITUDE,
16  DOMAIN,
17 )
18 from .coordinator import MetDataUpdateCoordinator
19 
20 PLATFORMS = [Platform.WEATHER]
21 
22 _LOGGER = logging.getLogger(__name__)
23 
24 type MetWeatherConfigEntry = ConfigEntry[MetDataUpdateCoordinator]
25 
26 
28  hass: HomeAssistant, config_entry: MetWeatherConfigEntry
29 ) -> bool:
30  """Set up Met as config entry."""
31  # Don't setup if tracking home location and latitude or longitude isn't set.
32  # Also, filters out our onboarding default location.
33  if config_entry.data.get(CONF_TRACK_HOME, False) and (
34  (not hass.config.latitude and not hass.config.longitude)
35  or (
36  hass.config.latitude == DEFAULT_HOME_LATITUDE
37  and hass.config.longitude == DEFAULT_HOME_LONGITUDE
38  )
39  ):
40  _LOGGER.warning(
41  "Skip setting up met.no integration; No Home location has been set"
42  )
43  return False
44 
45  coordinator = MetDataUpdateCoordinator(hass, config_entry)
46  await coordinator.async_config_entry_first_refresh()
47 
48  if config_entry.data.get(CONF_TRACK_HOME, False):
49  coordinator.track_home()
50 
51  config_entry.runtime_data = coordinator
52 
53  config_entry.async_on_unload(config_entry.add_update_listener(async_update_entry))
54  config_entry.async_on_unload(coordinator.untrack_home)
55 
56  await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
57 
58  await cleanup_old_device(hass)
59 
60  return True
61 
62 
64  hass: HomeAssistant, config_entry: MetWeatherConfigEntry
65 ) -> bool:
66  """Unload a config entry."""
67  return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
68 
69 
70 async def async_update_entry(hass: HomeAssistant, config_entry: MetWeatherConfigEntry):
71  """Reload Met component when options changed."""
72  await hass.config_entries.async_reload(config_entry.entry_id)
73 
74 
75 async def cleanup_old_device(hass: HomeAssistant) -> None:
76  """Cleanup device without proper device identifier."""
77  device_reg = dr.async_get(hass)
78  device = device_reg.async_get_device(identifiers={(DOMAIN,)}) # type: ignore[arg-type]
79  if device:
80  _LOGGER.debug("Removing improper device %s", device.name)
81  device_reg.async_remove_device(device.id)
bool async_setup_entry(HomeAssistant hass, MetWeatherConfigEntry config_entry)
Definition: __init__.py:29
bool async_unload_entry(HomeAssistant hass, MetWeatherConfigEntry config_entry)
Definition: __init__.py:65
None cleanup_old_device(HomeAssistant hass)
Definition: __init__.py:75
def async_update_entry(HomeAssistant hass, MetWeatherConfigEntry config_entry)
Definition: __init__.py:70