53 lines
1.4 KiB
Python
53 lines
1.4 KiB
Python
from dataclasses import dataclass
|
|
from flickerstrip_py import Flickerstrip
|
|
import logging
|
|
|
|
from homeassistant.config_entries import ConfigEntry, ConfigEntryError
|
|
from homeassistant.const import CONF_HOST, CONF_PORT
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from .const import (
|
|
DOMAIN,
|
|
PLATFORMS,
|
|
)
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
"""Setup flickerstrip from config entry."""
|
|
|
|
strip = Flickerstrip(
|
|
host=entry.data[CONF_HOST],
|
|
port=entry.data[CONF_PORT],
|
|
)
|
|
|
|
if not await strip.force_update():
|
|
_LOGGER.error("Unable to connect to flickerstrip")
|
|
raise ConfigEntryError("Unable to connect to flickerstrip")
|
|
|
|
hass.data.setdefault(DOMAIN, {})
|
|
hass.data[DOMAIN][entry.entry_id] = FlickerstripData(strip=strip)
|
|
|
|
for platform in PLATFORMS:
|
|
hass.async_create_task(
|
|
hass.config_entries.async_forward_entry_setup(entry, platform)
|
|
)
|
|
|
|
entry.async_on_unload(entry.add_update_listener(update_listener))
|
|
|
|
return True
|
|
|
|
|
|
async def update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
|
|
"""Update when config_entry options update."""
|
|
await hass.config_entries.async_reload(entry.entry_id)
|
|
|
|
|
|
@dataclass
|
|
class FlickerstripData:
|
|
"""Data for the Flickerstrip integration."""
|
|
|
|
strip: Flickerstrip
|