2019-02-13 20:21:14 +00:00
|
|
|
"""Support for APCUPSd via its Network Information Server (NIS)."""
|
2022-09-28 07:14:04 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-10-18 00:10:28 +00:00
|
|
|
import logging
|
2023-11-21 21:40:05 +00:00
|
|
|
from typing import Final
|
2016-02-11 07:33:53 +00:00
|
|
|
|
2022-12-13 07:52:05 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-09-28 07:14:04 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_PORT, Platform
|
2021-12-31 05:17:05 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2016-08-30 19:34:33 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2023-11-26 12:04:52 +00:00
|
|
|
|
|
|
|
from .const import DOMAIN
|
|
|
|
from .coordinator import APCUPSdCoordinator
|
2016-02-10 16:32:18 +00:00
|
|
|
|
2016-08-30 19:34:33 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-02-10 16:32:18 +00:00
|
|
|
|
2022-09-28 07:14:04 +00:00
|
|
|
PLATFORMS: Final = (Platform.BINARY_SENSOR, Platform.SENSOR)
|
2016-02-11 07:33:53 +00:00
|
|
|
|
2022-12-13 07:52:05 +00:00
|
|
|
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
2016-02-10 16:32:18 +00:00
|
|
|
|
|
|
|
|
2022-09-28 07:14:04 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
|
|
|
"""Use config values to set up a function enabling status retrieval."""
|
2023-11-21 21:40:05 +00:00
|
|
|
host, port = config_entry.data[CONF_HOST], config_entry.data[CONF_PORT]
|
|
|
|
coordinator = APCUPSdCoordinator(hass, host, port)
|
2022-09-28 07:14:04 +00:00
|
|
|
|
2023-11-21 21:40:05 +00:00
|
|
|
await coordinator.async_config_entry_first_refresh()
|
2022-09-28 07:14:04 +00:00
|
|
|
|
2023-11-21 21:40:05 +00:00
|
|
|
# Store the coordinator for later uses.
|
2022-09-28 07:14:04 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})
|
2023-11-21 21:40:05 +00:00
|
|
|
hass.data[DOMAIN][config_entry.entry_id] = coordinator
|
2022-09-28 07:14:04 +00:00
|
|
|
|
|
|
|
# Forward the config entries to the supported platforms.
|
|
|
|
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
2016-02-10 16:32:18 +00:00
|
|
|
return True
|
2016-02-11 07:33:53 +00:00
|
|
|
|
|
|
|
|
2022-09-28 07:14:04 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Unload a config entry."""
|
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2023-09-03 13:54:00 +00:00
|
|
|
if unload_ok and DOMAIN in hass.data:
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
2022-09-28 07:14:04 +00:00
|
|
|
return unload_ok
|