Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for WLED."""
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 from homeassistant.helpers import config_validation as cv
9 from homeassistant.helpers.typing import ConfigType
10 from homeassistant.util.hass_dict import HassKey
11 
12 from .const import DOMAIN
13 from .coordinator import WLEDDataUpdateCoordinator, WLEDReleasesDataUpdateCoordinator
14 
15 PLATFORMS = (
16  Platform.BUTTON,
17  Platform.LIGHT,
18  Platform.NUMBER,
19  Platform.SELECT,
20  Platform.SENSOR,
21  Platform.SWITCH,
22  Platform.UPDATE,
23 )
24 
25 type WLEDConfigEntry = ConfigEntry[WLEDDataUpdateCoordinator]
26 
27 WLED_KEY: HassKey[WLEDReleasesDataUpdateCoordinator] = HassKey(DOMAIN)
28 CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
29 
30 
31 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
32  """Set up the WLED integration.
33 
34  We set up a single coordinator for fetching WLED releases, which
35  is used across all WLED devices (and config entries) to avoid
36  fetching the same data multiple times for each.
37  """
38  hass.data[WLED_KEY] = WLEDReleasesDataUpdateCoordinator(hass)
39  await hass.data[WLED_KEY].async_request_refresh()
40  return True
41 
42 
43 async def async_setup_entry(hass: HomeAssistant, entry: WLEDConfigEntry) -> bool:
44  """Set up WLED from a config entry."""
45  entry.runtime_data = WLEDDataUpdateCoordinator(hass, entry=entry)
46  await entry.runtime_data.async_config_entry_first_refresh()
47 
48  # Set up all platforms for this device/entry.
49  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
50 
51  # Reload entry when its updated.
52  entry.async_on_unload(entry.add_update_listener(async_reload_entry))
53 
54  return True
55 
56 
57 async def async_unload_entry(hass: HomeAssistant, entry: WLEDConfigEntry) -> bool:
58  """Unload WLED config entry."""
59  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
60  coordinator = entry.runtime_data
61 
62  # Ensure disconnected and cleanup stop sub
63  await coordinator.wled.disconnect()
64  if coordinator.unsub:
65  coordinator.unsub()
66 
67  return unload_ok
68 
69 
70 async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
71  """Reload the config entry when it changed."""
72  await hass.config_entries.async_reload(entry.entry_id)
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:31
None async_reload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:70
bool async_unload_entry(HomeAssistant hass, WLEDConfigEntry entry)
Definition: __init__.py:57
bool async_setup_entry(HomeAssistant hass, WLEDConfigEntry entry)
Definition: __init__.py:43