2020-01-30 18:21:51 +00:00
|
|
|
"""The Mikrotik component."""
|
2024-03-08 14:01:29 +00:00
|
|
|
|
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
|
2024-08-05 15:44:14 +00:00
|
|
|
from homeassistant.helpers import 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
|
2024-05-15 10:02:33 +00:00
|
|
|
from .coordinator import MikrotikDataUpdateCoordinator, get_api
|
2022-07-05 07:16:38 +00:00
|
|
|
from .errors import CannotConnect, LoginError
|
2019-08-08 11:58:13 +00:00
|
|
|
|
2022-08-19 08:39:14 +00:00
|
|
|
PLATFORMS = [Platform.DEVICE_TRACKER]
|
|
|
|
|
2024-06-13 09:53:32 +00:00
|
|
|
type MikrotikConfigEntry = ConfigEntry[MikrotikDataUpdateCoordinator]
|
2020-01-30 18:21:51 +00:00
|
|
|
|
2024-06-13 09:53:32 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, config_entry: MikrotikConfigEntry
|
|
|
|
) -> 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
|
|
|
|
2024-06-13 09:53:32 +00:00
|
|
|
config_entry.runtime_data = 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."""
|
2024-06-13 09:53:32 +00:00
|
|
|
return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
|