2019-11-06 22:55:39 +00:00
|
|
|
"""Support for WLED."""
|
2021-03-18 14:08:35 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-11-06 22:55:39 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-03 08:31:17 +00:00
|
|
|
from homeassistant.const import Platform
|
2020-03-13 12:19:05 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-11-06 22:55:39 +00:00
|
|
|
|
2021-05-09 17:34:21 +00:00
|
|
|
from .const import DOMAIN
|
2021-06-09 18:15:46 +00:00
|
|
|
from .coordinator import WLEDDataUpdateCoordinator
|
2019-11-06 22:55:39 +00:00
|
|
|
|
2021-11-11 06:22:52 +00:00
|
|
|
PLATFORMS = (
|
2021-12-02 17:07:09 +00:00
|
|
|
Platform.BINARY_SENSOR,
|
|
|
|
Platform.BUTTON,
|
|
|
|
Platform.LIGHT,
|
|
|
|
Platform.NUMBER,
|
|
|
|
Platform.SELECT,
|
|
|
|
Platform.SENSOR,
|
|
|
|
Platform.SWITCH,
|
2021-11-11 06:22:52 +00:00
|
|
|
)
|
2019-11-06 22:55:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Set up WLED from a config entry."""
|
2021-06-23 21:43:24 +00:00
|
|
|
coordinator = WLEDDataUpdateCoordinator(hass, entry=entry)
|
2021-03-29 22:51:39 +00:00
|
|
|
await coordinator.async_config_entry_first_refresh()
|
2019-11-06 22:55:39 +00:00
|
|
|
|
|
|
|
hass.data.setdefault(DOMAIN, {})
|
2020-03-13 12:19:05 +00:00
|
|
|
hass.data[DOMAIN][entry.entry_id] = coordinator
|
2019-11-06 22:55:39 +00:00
|
|
|
|
2020-01-04 21:48:31 +00:00
|
|
|
# For backwards compat, set unique ID
|
|
|
|
if entry.unique_id is None:
|
|
|
|
hass.config_entries.async_update_entry(
|
2020-03-13 12:19:05 +00:00
|
|
|
entry, unique_id=coordinator.data.info.mac_address
|
2020-01-04 21:48:31 +00:00
|
|
|
)
|
|
|
|
|
2019-11-06 22:55:39 +00:00
|
|
|
# Set up all platforms for this device/entry.
|
2021-04-27 20:51:11 +00:00
|
|
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
2019-11-06 22:55:39 +00:00
|
|
|
|
2021-06-12 11:33:23 +00:00
|
|
|
# Reload entry when its updated.
|
|
|
|
entry.async_on_unload(entry.add_update_listener(async_reload_entry))
|
|
|
|
|
2019-11-06 22:55:39 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Unload WLED config entry."""
|
2021-04-27 20:51:11 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2021-01-31 17:59:39 +00:00
|
|
|
if unload_ok:
|
2021-06-11 18:55:08 +00:00
|
|
|
coordinator: WLEDDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
|
|
|
|
# Ensure disconnected and cleanup stop sub
|
|
|
|
await coordinator.wled.disconnect()
|
|
|
|
if coordinator.unsub:
|
|
|
|
coordinator.unsub()
|
|
|
|
|
2021-01-31 17:59:39 +00:00
|
|
|
del hass.data[DOMAIN][entry.entry_id]
|
2021-06-11 18:55:08 +00:00
|
|
|
|
2021-01-31 17:59:39 +00:00
|
|
|
return unload_ok
|
2021-06-12 11:33:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_reload_entry(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
|
|
|
"""Reload the config entry when it changed."""
|
|
|
|
await hass.config_entries.async_reload(entry.entry_id)
|