core/homeassistant/components/knocki/__init__.py

50 lines
1.4 KiB
Python
Raw Normal View History

"""The Knocki integration."""
from __future__ import annotations
from knocki import Event, EventType, KnockiClient
from homeassistant.const import CONF_TOKEN, Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .coordinator import KnockiConfigEntry, KnockiCoordinator
2024-06-24 09:41:33 +00:00
PLATFORMS: list[Platform] = [Platform.EVENT]
async def async_setup_entry(hass: HomeAssistant, entry: KnockiConfigEntry) -> bool:
"""Set up Knocki from a config entry."""
client = KnockiClient(
session=async_get_clientsession(hass), token=entry.data[CONF_TOKEN]
)
coordinator = KnockiCoordinator(hass, entry, client)
2024-06-24 09:41:33 +00:00
await coordinator.async_config_entry_first_refresh()
entry.async_on_unload(
client.register_listener(EventType.CREATED, coordinator.add_trigger)
)
async def _refresh_coordinator(_: Event) -> None:
await coordinator.async_refresh()
entry.async_on_unload(
client.register_listener(EventType.DELETED, _refresh_coordinator)
)
2024-06-24 09:41:33 +00:00
entry.runtime_data = coordinator
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
2024-12-04 13:56:42 +00:00
await client.start_websocket()
return True
async def async_unload_entry(hass: HomeAssistant, entry: KnockiConfigEntry) -> bool:
"""Unload a config entry."""
2024-12-04 13:56:42 +00:00
await entry.runtime_data.client.close()
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)