99 lines
3.3 KiB
Python
99 lines
3.3 KiB
Python
"""The ninlight integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from homeassistant.const import CONF_HOST, CONF_TOKEN, Platform
|
|
from homeassistant.core import HomeAssistant, callback
|
|
from homeassistant.helpers import (
|
|
config_validation as cv,
|
|
)
|
|
from homeassistant.helpers import (
|
|
device_registry as dr,
|
|
)
|
|
from homeassistant.helpers import (
|
|
issue_registry as ir,
|
|
)
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
from homeassistant.helpers.device_registry import format_mac
|
|
from homeassistant.helpers.typing import ConfigType
|
|
|
|
from .api import NinlightClient
|
|
from .const import DEFAULT_TOKEN, DOMAIN
|
|
from .coordinator import NinlightConfigEntry, NinlightCoordinator
|
|
from .shared import async_get_shared, async_start_catalog, async_stop_catalog
|
|
|
|
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
|
|
|
|
PLATFORMS: list[Platform] = [
|
|
Platform.BUTTON,
|
|
Platform.LIGHT,
|
|
Platform.NUMBER,
|
|
Platform.SELECT,
|
|
Platform.SENSOR,
|
|
Platform.SWITCH,
|
|
Platform.UPDATE,
|
|
]
|
|
|
|
|
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
|
async_get_shared(hass)
|
|
return True
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: NinlightConfigEntry) -> bool:
|
|
client = NinlightClient(
|
|
async_get_clientsession(hass), entry.data[CONF_HOST], entry.data[CONF_TOKEN]
|
|
)
|
|
coordinator = NinlightCoordinator(hass, entry, client)
|
|
await coordinator.async_config_entry_first_refresh()
|
|
entry.runtime_data = coordinator
|
|
_async_update_default_token_issue(hass, entry)
|
|
|
|
@callback
|
|
def _sync_firmware_version() -> None:
|
|
info = coordinator.data.info
|
|
registry = dr.async_get(hass)
|
|
device = registry.async_get_device_by_identifier(
|
|
(DOMAIN, format_mac(info["mac"])), entry.entry_id
|
|
)
|
|
if device is not None and device.sw_version != info["fw_version"]:
|
|
registry.async_update_device(device.id, sw_version=info["fw_version"])
|
|
|
|
entry.async_on_unload(coordinator.async_add_listener(_sync_firmware_version))
|
|
await async_start_catalog(hass, async_get_shared(hass))
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
|
return True
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: NinlightConfigEntry) -> bool:
|
|
unloaded = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
|
if unloaded and not [
|
|
other
|
|
for other in hass.config_entries.async_loaded_entries(DOMAIN)
|
|
if other.entry_id != entry.entry_id
|
|
]:
|
|
async_stop_catalog(async_get_shared(hass))
|
|
return unloaded
|
|
|
|
|
|
async def async_remove_entry(hass: HomeAssistant, entry: NinlightConfigEntry) -> None:
|
|
ir.async_delete_issue(hass, DOMAIN, f"default_token_{entry.entry_id}")
|
|
ir.async_delete_issue(hass, DOMAIN, f"wifi_guard_{entry.entry_id}")
|
|
|
|
|
|
@callback
|
|
def _async_update_default_token_issue(hass: HomeAssistant, entry: NinlightConfigEntry) -> None:
|
|
issue_id = f"default_token_{entry.entry_id}"
|
|
if entry.data[CONF_TOKEN] == DEFAULT_TOKEN:
|
|
ir.async_create_issue(
|
|
hass,
|
|
DOMAIN,
|
|
issue_id,
|
|
is_fixable=False,
|
|
severity=ir.IssueSeverity.WARNING,
|
|
translation_key="default_token",
|
|
translation_placeholders={"name": entry.title},
|
|
)
|
|
else:
|
|
ir.async_delete_issue(hass, DOMAIN, issue_id)
|