2020-03-21 19:02:28 +03:00
|
|
|
"""Mikrotik Router integration."""
|
|
|
|
|
|
|
|
import logging
|
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
|
2020-03-21 19:02:28 +03:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2021-12-13 13:26:46 +01:00
|
|
|
|
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_NAME,
|
|
|
|
CONF_HOST,
|
|
|
|
)
|
|
|
|
|
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,
|
|
|
|
DATA_CLIENT,
|
2020-04-20 11:05:49 +02:00
|
|
|
RUN_SCRIPT_COMMAND,
|
2020-03-21 19:02:28 +03:00
|
|
|
)
|
|
|
|
from .mikrotik_controller import MikrotikControllerData
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
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] = {}
|
|
|
|
hass.data[DOMAIN][DATA_CLIENT] = {}
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
# async_setup_entry
|
|
|
|
# ---------------------------
|
|
|
|
async def async_setup_entry(hass, config_entry):
|
|
|
|
"""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():
|
|
|
|
raise ConfigEntryNotReady(f"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()
|
|
|
|
hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id] = controller
|
2020-03-21 19:02:28 +03:00
|
|
|
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(config_entry, "sensor")
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.async_create_task(
|
2020-04-11 05:45:36 +02:00
|
|
|
hass.config_entries.async_forward_entry_setup(config_entry, "binary_sensor")
|
2020-03-21 19:02:28 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
hass.async_create_task(
|
2020-04-11 05:45:36 +02:00
|
|
|
hass.config_entries.async_forward_entry_setup(config_entry, "device_tracker")
|
2020-03-21 19:02:28 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(config_entry, "switch")
|
|
|
|
)
|
|
|
|
|
2022-01-08 21:05:50 +01:00
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(config_entry, "button")
|
|
|
|
)
|
|
|
|
|
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
|
|
|
device_registry = await hass.helpers.device_registry.async_get_registry()
|
|
|
|
device_registry.async_get_or_create(
|
|
|
|
config_entry_id=config_entry.entry_id,
|
2022-01-21 15:50:25 +01:00
|
|
|
connections={(DOMAIN, f"{controller.data['routerboard']['serial-number']}")},
|
2020-04-11 05:35:40 +02:00
|
|
|
manufacturer=controller.data["resource"]["platform"],
|
|
|
|
model=controller.data["routerboard"]["model"],
|
2021-12-13 13:26:46 +01:00
|
|
|
name=f"{config_entry.data[CONF_NAME]} {controller.data['routerboard']['model']}",
|
2020-04-11 05:35:40 +02:00
|
|
|
sw_version=controller.data["resource"]["version"],
|
2021-12-13 13:26:46 +01:00
|
|
|
configuration_url=f"http://{config_entry.data[CONF_HOST]}",
|
|
|
|
identifiers={
|
|
|
|
DOMAIN,
|
|
|
|
"serial-number",
|
2022-01-21 15:50:25 +01:00
|
|
|
f"{controller.data['routerboard']['serial-number']}",
|
2021-12-13 13:26:46 +01:00
|
|
|
"sensor",
|
|
|
|
f"{config_entry.data[CONF_NAME]} {controller.data['routerboard']['model']}",
|
2021-12-13 14:02:25 +01:00
|
|
|
},
|
2020-03-21 19:02:28 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
# async_unload_entry
|
|
|
|
# ---------------------------
|
|
|
|
async def async_unload_entry(hass, config_entry):
|
|
|
|
"""Unload a config entry."""
|
2020-04-11 05:35:40 +02:00
|
|
|
controller = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
|
|
|
|
await controller.async_reset()
|
2022-01-24 12:47:19 +01:00
|
|
|
hass.services.async_remove(DOMAIN, RUN_SCRIPT_COMMAND)
|
2020-03-21 19:02:28 +03:00
|
|
|
hass.data[DOMAIN][DATA_CLIENT].pop(config_entry.entry_id)
|
2022-01-24 12:47:19 +01:00
|
|
|
|
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(
|
|
|
|
config_entry, PLATFORMS
|
|
|
|
)
|
|
|
|
|
|
|
|
return unload_ok
|