2020-03-21 19:02:28 +03:00
|
|
|
"""Mikrotik Router integration."""
|
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
|
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
|
2022-06-25 13:56:39 +02:00
|
|
|
from homeassistant.helpers import device_registry
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-13 13:26:46 +01:00
|
|
|
|
2020-03-21 19:02:28 +03:00
|
|
|
from .const import (
|
2022-01-24 12:47:19 +01:00
|
|
|
PLATFORMS,
|
2020-03-21 19:02:28 +03:00
|
|
|
DOMAIN,
|
2020-04-20 11:05:49 +02:00
|
|
|
RUN_SCRIPT_COMMAND,
|
2020-03-21 19:02:28 +03:00
|
|
|
)
|
2023-08-08 00:50:09 +02:00
|
|
|
from .coordinator import MikrotikCoordinator
|
2020-03-21 19:02:28 +03:00
|
|
|
|
2020-04-20 11:46:23 +02:00
|
|
|
SCRIPT_SCHEMA = vol.Schema(
|
|
|
|
{vol.Required("router"): cv.string, vol.Required("script"): cv.string}
|
|
|
|
)
|
2020-04-20 11:05:49 +02:00
|
|
|
|
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()
|
|
|
|
hass.data.setdefault(DOMAIN, {})[config_entry.entry_id] = coordinator
|
2020-03-21 19:02:28 +03:00
|
|
|
|
2022-08-18 10:45:31 +02: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-04-20 11:05:49 +02:00
|
|
|
hass.services.async_register(
|
2023-08-08 00:50:09 +02:00
|
|
|
DOMAIN, RUN_SCRIPT_COMMAND, coordinator.run_script, schema=SCRIPT_SCHEMA
|
2020-04-20 11:05:49 +02: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(
|
2022-01-24 12:47:19 +01:00
|
|
|
config_entry, PLATFORMS
|
2023-08-08 00:50:09 +02:00
|
|
|
):
|
2022-01-24 13:38:32 +01:00
|
|
|
hass.services.async_remove(DOMAIN, RUN_SCRIPT_COMMAND)
|
2022-02-18 08:06:10 +01:00
|
|
|
hass.data[DOMAIN].pop(config_entry.entry_id)
|
2022-01-24 12:47:19 +01:00
|
|
|
|
|
|
|
return unload_ok
|
2022-05-26 10:16:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
# async_remove_config_entry_device
|
|
|
|
# ---------------------------
|
2022-06-25 13:56:39 +02:00
|
|
|
async def async_remove_config_entry_device(
|
|
|
|
hass, config_entry: ConfigEntry, device_entry: device_registry.DeviceEntry
|
|
|
|
) -> bool:
|
2022-05-26 10:16:58 +02:00
|
|
|
"""Remove a config entry from a device."""
|
|
|
|
return True
|