Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Nina integration."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.config_entries import ConfigEntry
6 from homeassistant.const import Platform
7 from homeassistant.core import HomeAssistant
8 
9 from .const import (
10  ALL_MATCH_REGEX,
11  CONF_AREA_FILTER,
12  CONF_FILTER_CORONA,
13  CONF_HEADLINE_FILTER,
14  CONF_REGIONS,
15  DOMAIN,
16  NO_MATCH_REGEX,
17 )
18 from .coordinator import NINADataUpdateCoordinator
19 
20 PLATFORMS: list[str] = [Platform.BINARY_SENSOR]
21 
22 
23 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
24  """Set up platform from a ConfigEntry."""
25 
26  regions: dict[str, str] = entry.data[CONF_REGIONS]
27 
28  if CONF_HEADLINE_FILTER not in entry.data:
29  filter_regex = NO_MATCH_REGEX
30 
31  if entry.data[CONF_FILTER_CORONA]:
32  filter_regex = ".*corona.*"
33 
34  new_data = {**entry.data, CONF_HEADLINE_FILTER: filter_regex}
35  new_data.pop(CONF_FILTER_CORONA, None)
36  hass.config_entries.async_update_entry(entry, data=new_data)
37 
38  if CONF_AREA_FILTER not in entry.data:
39  new_data = {**entry.data, CONF_AREA_FILTER: ALL_MATCH_REGEX}
40  hass.config_entries.async_update_entry(entry, data=new_data)
41 
42  coordinator = NINADataUpdateCoordinator(
43  hass,
44  regions,
45  entry.data[CONF_HEADLINE_FILTER],
46  entry.data[CONF_AREA_FILTER],
47  )
48 
49  await coordinator.async_config_entry_first_refresh()
50 
51  entry.async_on_unload(entry.add_update_listener(_async_update_listener))
52 
53  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
54 
55  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
56 
57  return True
58 
59 
60 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
61  """Unload a config entry."""
62  return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
63 
64 
65 async def _async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
66  """Handle options update."""
67  await hass.config_entries.async_reload(entry.entry_id)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:23
None _async_update_listener(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:65
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:60