nws_spc_outlook

Initialize the NWS SPC Outlook integration.

This module sets up and manages the SPC Outlook integration within Home Assistant. It handles configuration entry setup, creates and stores the coordinator, and forwards entry setup to supported platforms.

 1"""
 2Initialize the NWS SPC Outlook integration.
 3
 4This module sets up and manages the SPC Outlook integration within Home Assistant.
 5It handles configuration entry setup, creates and stores the coordinator, and
 6forwards entry setup to supported platforms.
 7"""
 8
 9import logging
10
11from homeassistant.config_entries import ConfigEntry
12from homeassistant.core import HomeAssistant
13
14from .const import CONF_LATITUDE, CONF_LONGITUDE, DOMAIN
15from .coordinator import NWSSPCOutlookDataCoordinator
16
17_LOGGER = logging.getLogger(__name__)
18
19
20async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
21    """
22    Set up NWS SPC Outlook from a config entry.
23
24    This function initializes the integration when a user adds it via the UI.
25    It creates a data coordinator that fetches and updates data from the
26    Storm Prediction Center (SPC) API, and sets up the sensor platform.
27
28    Args:
29        hass: The Home Assistant instance.
30        entry: The configuration entry created from user input.
31
32    Returns:
33        True if setup was successful, False otherwise.
34
35    """
36    if DOMAIN not in hass.data:
37        hass.data[DOMAIN] = {}
38
39    coordinator = NWSSPCOutlookDataCoordinator(
40        hass, entry.data[CONF_LATITUDE], entry.data[CONF_LONGITUDE]
41    )
42
43    await coordinator.async_config_entry_first_refresh()
44    hass.data[DOMAIN][entry.entry_id] = coordinator
45
46    # Forward setup to platform(s)
47    await hass.config_entries.async_forward_entry_setups(entry, ["sensor"])
48
49    # Ensure cleanup on unload
50    entry.async_on_unload(lambda: async_unload_entry(hass, entry))
51
52    return True
53
54
55async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
56    """
57    Unload NWS SPC Outlook config entry.
58
59    Cleans up resources when the integration is removed from Home Assistant.
60    Unloads the sensor platform and removes the coordinator from memory.
61
62    Args:
63        hass: The Home Assistant instance.
64        entry: The configuration entry to unload.
65
66    Returns:
67        True if unload was successful, False otherwise.
68
69    """
70    _LOGGER.debug("Unloading NWS SPC Outlook integration entry %s", entry.entry_id)
71
72    if DOMAIN not in hass.data or entry.entry_id not in hass.data[DOMAIN]:
73        return False
74
75    coordinator = hass.data[DOMAIN].pop(entry.entry_id, None)
76
77    if coordinator and hasattr(coordinator, "async_unload"):
78        await coordinator.async_unload()
79
80    # Remove DOMAIN key if no entries remain
81    if not hass.data[DOMAIN]:
82        hass.data.pop(DOMAIN)
83
84    return await hass.config_entries.async_unload_platforms(entry, ["sensor"])
async def async_setup_entry( hass: homeassistant.core.HomeAssistant, entry: homeassistant.config_entries.ConfigEntry) -> bool:
21async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
22    """
23    Set up NWS SPC Outlook from a config entry.
24
25    This function initializes the integration when a user adds it via the UI.
26    It creates a data coordinator that fetches and updates data from the
27    Storm Prediction Center (SPC) API, and sets up the sensor platform.
28
29    Args:
30        hass: The Home Assistant instance.
31        entry: The configuration entry created from user input.
32
33    Returns:
34        True if setup was successful, False otherwise.
35
36    """
37    if DOMAIN not in hass.data:
38        hass.data[DOMAIN] = {}
39
40    coordinator = NWSSPCOutlookDataCoordinator(
41        hass, entry.data[CONF_LATITUDE], entry.data[CONF_LONGITUDE]
42    )
43
44    await coordinator.async_config_entry_first_refresh()
45    hass.data[DOMAIN][entry.entry_id] = coordinator
46
47    # Forward setup to platform(s)
48    await hass.config_entries.async_forward_entry_setups(entry, ["sensor"])
49
50    # Ensure cleanup on unload
51    entry.async_on_unload(lambda: async_unload_entry(hass, entry))
52
53    return True

Set up NWS SPC Outlook from a config entry.

This function initializes the integration when a user adds it via the UI. It creates a data coordinator that fetches and updates data from the Storm Prediction Center (SPC) API, and sets up the sensor platform.

Args: hass: The Home Assistant instance. entry: The configuration entry created from user input.

Returns: True if setup was successful, False otherwise.

async def async_unload_entry( hass: homeassistant.core.HomeAssistant, entry: homeassistant.config_entries.ConfigEntry) -> bool:
56async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
57    """
58    Unload NWS SPC Outlook config entry.
59
60    Cleans up resources when the integration is removed from Home Assistant.
61    Unloads the sensor platform and removes the coordinator from memory.
62
63    Args:
64        hass: The Home Assistant instance.
65        entry: The configuration entry to unload.
66
67    Returns:
68        True if unload was successful, False otherwise.
69
70    """
71    _LOGGER.debug("Unloading NWS SPC Outlook integration entry %s", entry.entry_id)
72
73    if DOMAIN not in hass.data or entry.entry_id not in hass.data[DOMAIN]:
74        return False
75
76    coordinator = hass.data[DOMAIN].pop(entry.entry_id, None)
77
78    if coordinator and hasattr(coordinator, "async_unload"):
79        await coordinator.async_unload()
80
81    # Remove DOMAIN key if no entries remain
82    if not hass.data[DOMAIN]:
83        hass.data.pop(DOMAIN)
84
85    return await hass.config_entries.async_unload_platforms(entry, ["sensor"])

Unload NWS SPC Outlook config entry.

Cleans up resources when the integration is removed from Home Assistant. Unloads the sensor platform and removes the coordinator from memory.

Args: hass: The Home Assistant instance. entry: The configuration entry to unload.

Returns: True if unload was successful, False otherwise.