tomaae.homeassistant-mikrot.../custom_components/mikrotik_router/__init__.py

99 lines
3.1 KiB
Python
Raw Permalink Normal View History

2020-03-21 19:02:28 +03:00
"""Mikrotik Router integration."""
2025-04-25 21:49:43 +02:00
2023-08-08 00:50:09 +02:00
from __future__ import annotations
2020-03-21 19:02:28 +03:00
2020-04-20 11:05:49 +02:00
import voluptuous as vol
import logging
2020-03-21 19:02:28 +03:00
2023-08-08 00:50:09 +02:00
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers import device_registry
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_VERIFY_SSL
from .const import PLATFORMS, DOMAIN, DEFAULT_VERIFY_SSL
2023-08-09 23:00:00 +02:00
from .coordinator import MikrotikData, MikrotikCoordinator, MikrotikTrackerCoordinator
2020-03-21 19:02:28 +03:00
SCRIPT_SCHEMA = vol.Schema(
{vol.Required("router"): cv.string, vol.Required("script"): cv.string}
)
2020-04-20 11:05:49 +02:00
_LOGGER = logging.getLogger(__name__)
2020-03-21 19:02:28 +03:00
# ---------------------------
# async_setup_entry
# ---------------------------
2023-08-08 00:50:09 +02:00
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Set up a config entry."""
coordinator = MikrotikCoordinator(hass, config_entry)
await coordinator.async_config_entry_first_refresh()
2023-08-09 23:00:00 +02:00
coordinatorTracker = MikrotikTrackerCoordinator(hass, config_entry, coordinator)
await coordinatorTracker.async_config_entry_first_refresh()
hass.data.setdefault(DOMAIN, {})[config_entry.entry_id] = MikrotikData(
data_coordinator=coordinator,
tracker_coordinator=coordinatorTracker,
)
2020-03-21 19:02:28 +03:00
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
2023-08-08 00:50:09 +02:00
config_entry.async_on_unload(config_entry.add_update_listener(async_reload_entry))
2022-01-08 21:05:50 +01:00
2020-03-21 19:02:28 +03:00
return True
2023-08-08 00:50:09 +02:00
# ---------------------------
# async_reload_entry
# ---------------------------
async def async_reload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
"""Reload the config entry when it changed."""
await hass.config_entries.async_reload(config_entry.entry_id)
2020-03-21 19:02:28 +03:00
# ---------------------------
# async_unload_entry
# ---------------------------
2023-08-08 00:50:09 +02:00
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
2020-03-21 19:02:28 +03:00
"""Unload a config entry."""
2023-08-08 00:50:09 +02:00
if unload_ok := await hass.config_entries.async_unload_platforms(
config_entry, PLATFORMS
2023-08-08 00:50:09 +02:00
):
2022-02-18 08:06:10 +01:00
hass.data[DOMAIN].pop(config_entry.entry_id)
return unload_ok
# ---------------------------
# async_remove_config_entry_device
# ---------------------------
async def async_remove_config_entry_device(
hass, config_entry: ConfigEntry, device_entry: device_registry.DeviceEntry
) -> bool:
"""Remove a config entry from a device."""
return True
# ---------------------------
# async_migrate_entry
# ---------------------------
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry):
_LOGGER.debug(
"Migrating configuration from version %s.%s",
config_entry.version,
config_entry.minor_version,
)
if config_entry.version < 2:
new_data = {**config_entry.data}
new_data[CONF_VERIFY_SSL] = DEFAULT_VERIFY_SSL
hass.config_entries.async_update_entry(config_entry, data=new_data, version=2)
_LOGGER.debug(
"Migration to configuration version %s.%s successful",
config_entry.version,
config_entry.minor_version,
)
return True