1
mirror of https://github.com/home-assistant/core synced 2024-09-15 17:29:45 +02:00
ha-core/homeassistant/components/wled/__init__.py
Franck Nijhof 40d4495ed0
Add update platform to WLED (#68454)
* Add update platform to WLED

* Copy pasta fixes

* Apply suggestions from code review

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update tests/components/wled/test_update.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Fix tests

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2022-03-21 15:38:29 +01:00

65 lines
1.9 KiB
Python

"""Support for WLED."""
from __future__ import annotations
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from .const import DOMAIN, LOGGER
from .coordinator import WLEDDataUpdateCoordinator
PLATFORMS = (
Platform.BINARY_SENSOR,
Platform.BUTTON,
Platform.LIGHT,
Platform.NUMBER,
Platform.SELECT,
Platform.SENSOR,
Platform.SWITCH,
Platform.UPDATE,
)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up WLED from a config entry."""
coordinator = WLEDDataUpdateCoordinator(hass, entry=entry)
await coordinator.async_config_entry_first_refresh()
if coordinator.data.info.leds.cct:
LOGGER.error(
"WLED device '%s' has a CCT channel, which is not supported by "
"this integration",
entry.title,
)
return False
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
# Set up all platforms for this device/entry.
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
# Reload entry when its updated.
entry.async_on_unload(entry.add_update_listener(async_reload_entry))
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload WLED config entry."""
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
coordinator: WLEDDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
# Ensure disconnected and cleanup stop sub
await coordinator.wled.disconnect()
if coordinator.unsub:
coordinator.unsub()
del hass.data[DOMAIN][entry.entry_id]
return unload_ok
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)