2020-03-21 19:02:28 +03:00
|
|
|
"""Mikrotik Router integration."""
|
|
|
|
|
2020-04-20 11:05:49 +02:00
|
|
|
import voluptuous as vol
|
2020-03-21 19:02:28 +03:00
|
|
|
|
2020-04-20 11:05:49 +02:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-06-25 13:56:39 +02:00
|
|
|
from homeassistant.helpers import device_registry
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-03-21 19:02:28 +03:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
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
|
|
|
)
|
|
|
|
from .mikrotik_controller import MikrotikControllerData
|
|
|
|
|
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
|
|
|
|
# ---------------------------
|
|
|
|
async def async_setup(hass, _config):
|
|
|
|
"""Set up configured Mikrotik Controller."""
|
|
|
|
hass.data[DOMAIN] = {}
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-01-24 13:43:00 +01:00
|
|
|
# ---------------------------
|
|
|
|
# update_listener
|
|
|
|
# ---------------------------
|
|
|
|
async def update_listener(hass, config_entry) -> None:
|
|
|
|
"""Handle options update."""
|
|
|
|
await hass.config_entries.async_reload(config_entry.entry_id)
|
|
|
|
|
|
|
|
|
2020-03-21 19:02:28 +03:00
|
|
|
# ---------------------------
|
|
|
|
# async_setup_entry
|
|
|
|
# ---------------------------
|
2022-01-24 13:43:00 +01:00
|
|
|
async def async_setup_entry(hass, config_entry) -> bool:
|
2020-03-21 19:02:28 +03:00
|
|
|
"""Set up Mikrotik Router as config entry."""
|
2020-04-11 05:35:40 +02:00
|
|
|
controller = MikrotikControllerData(hass, config_entry)
|
|
|
|
await controller.async_hwinfo_update()
|
2022-01-24 11:50:58 +01:00
|
|
|
if not controller.connected():
|
2022-03-26 01:29:03 +01:00
|
|
|
raise ConfigEntryNotReady("Cannot connect to host")
|
2020-04-04 19:42:05 +02:00
|
|
|
|
2020-04-11 05:35:40 +02:00
|
|
|
await controller.async_update()
|
2020-03-21 19:02:28 +03:00
|
|
|
|
2020-04-11 05:35:40 +02:00
|
|
|
if not controller.data:
|
2020-03-21 19:02:28 +03:00
|
|
|
raise ConfigEntryNotReady()
|
|
|
|
|
2020-04-11 05:35:40 +02:00
|
|
|
await controller.async_init()
|
2022-02-18 08:06:10 +01:00
|
|
|
hass.data[DOMAIN][config_entry.entry_id] = controller
|
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)
|
2022-01-24 13:43:00 +01:00
|
|
|
config_entry.async_on_unload(config_entry.add_update_listener(update_listener))
|
2022-01-08 21:05:50 +01:00
|
|
|
|
2020-04-20 11:05:49 +02:00
|
|
|
hass.services.async_register(
|
|
|
|
DOMAIN, RUN_SCRIPT_COMMAND, controller.run_script, schema=SCRIPT_SCHEMA
|
|
|
|
)
|
|
|
|
|
2020-03-21 19:02:28 +03:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
# async_unload_entry
|
|
|
|
# ---------------------------
|
2022-01-24 13:38:32 +01:00
|
|
|
async def async_unload_entry(hass, config_entry) -> bool:
|
2020-03-21 19:02:28 +03:00
|
|
|
"""Unload a config entry."""
|
2022-01-24 12:47:19 +01:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(
|
|
|
|
config_entry, PLATFORMS
|
|
|
|
)
|
2022-01-24 13:38:32 +01:00
|
|
|
if unload_ok:
|
2022-02-18 08:06:10 +01:00
|
|
|
controller = hass.data[DOMAIN][config_entry.entry_id]
|
2022-01-24 13:38:32 +01:00
|
|
|
await controller.async_reset()
|
|
|
|
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
|