2020-01-30 18:21:51 +00:00
|
|
|
"""The Mikrotik component."""
|
2022-05-30 13:34:28 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-08-19 08:39:14 +00:00
|
|
|
from homeassistant.const import Platform
|
2022-01-02 15:28:14 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-10-02 03:37:24 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
2022-05-17 19:22:15 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv, device_registry as dr
|
2019-12-09 13:20:40 +00:00
|
|
|
|
2022-08-19 08:39:14 +00:00
|
|
|
from .const import ATTR_MANUFACTURER, DOMAIN
|
2022-07-05 07:16:38 +00:00
|
|
|
from .errors import CannotConnect, LoginError
|
|
|
|
from .hub import MikrotikDataUpdateCoordinator, get_api
|
2019-08-08 11:58:13 +00:00
|
|
|
|
2022-05-30 13:34:28 +00:00
|
|
|
CONFIG_SCHEMA = cv.removed(DOMAIN, raise_if_present=False)
|
2020-01-30 18:21:51 +00:00
|
|
|
|
2022-08-19 08:39:14 +00:00
|
|
|
PLATFORMS = [Platform.DEVICE_TRACKER]
|
|
|
|
|
2020-01-30 18:21:51 +00:00
|
|
|
|
2022-01-02 15:28:14 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
2020-01-30 18:21:51 +00:00
|
|
|
"""Set up the Mikrotik component."""
|
2022-07-05 07:16:38 +00:00
|
|
|
try:
|
|
|
|
api = await hass.async_add_executor_job(get_api, dict(config_entry.data))
|
|
|
|
except CannotConnect as api_error:
|
|
|
|
raise ConfigEntryNotReady from api_error
|
2022-10-02 03:37:24 +00:00
|
|
|
except LoginError as err:
|
|
|
|
raise ConfigEntryAuthFailed from err
|
2020-01-30 18:21:51 +00:00
|
|
|
|
2022-07-05 07:16:38 +00:00
|
|
|
coordinator = MikrotikDataUpdateCoordinator(hass, config_entry, api)
|
|
|
|
await hass.async_add_executor_job(coordinator.api.get_hub_details)
|
|
|
|
await coordinator.async_config_entry_first_refresh()
|
2022-06-29 13:32:29 +00:00
|
|
|
|
2022-07-05 07:16:38 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})[config_entry.entry_id] = coordinator
|
2022-06-29 13:32:29 +00:00
|
|
|
|
2022-08-19 08:39:14 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
2022-06-29 13:32:29 +00:00
|
|
|
|
2022-05-17 19:22:15 +00:00
|
|
|
device_registry = dr.async_get(hass)
|
2020-01-30 18:21:51 +00:00
|
|
|
device_registry.async_get_or_create(
|
|
|
|
config_entry_id=config_entry.entry_id,
|
2022-07-05 07:16:38 +00:00
|
|
|
connections={(DOMAIN, coordinator.serial_num)},
|
2020-01-30 18:21:51 +00:00
|
|
|
manufacturer=ATTR_MANUFACTURER,
|
2022-07-05 07:16:38 +00:00
|
|
|
model=coordinator.model,
|
|
|
|
name=coordinator.hostname,
|
|
|
|
sw_version=coordinator.firmware,
|
2020-01-30 18:21:51 +00:00
|
|
|
)
|
|
|
|
|
2019-08-08 11:58:13 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-01-02 15:28:14 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
2020-01-30 18:21:51 +00:00
|
|
|
"""Unload a config entry."""
|
2022-07-05 07:16:38 +00:00
|
|
|
if unload_ok := await hass.config_entries.async_unload_platforms(
|
2021-04-27 16:49:13 +00:00
|
|
|
config_entry, PLATFORMS
|
2022-07-05 07:16:38 +00:00
|
|
|
):
|
|
|
|
hass.data[DOMAIN].pop(config_entry.entry_id)
|
2020-01-30 18:21:51 +00:00
|
|
|
|
2021-04-27 16:49:13 +00:00
|
|
|
return unload_ok
|