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
|
2020-04-20 11:05:49 +02:00
|
|
|
from homeassistant.const import CONF_NAME
|
2020-03-21 19:02:28 +03:00
|
|
|
from .const import (
|
|
|
|
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:05:49 +02:00
|
|
|
SCRIPT_SCHEMA = vol.Schema({vol.Required(CONF_NAME): cv.string})
|
|
|
|
|
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()
|
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")
|
|
|
|
)
|
|
|
|
|
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,
|
2020-04-11 05:35:40 +02:00
|
|
|
manufacturer=controller.data["resource"]["platform"],
|
|
|
|
model=controller.data["routerboard"]["model"],
|
|
|
|
name=controller.data["routerboard"]["model"],
|
|
|
|
sw_version=controller.data["resource"]["version"],
|
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]
|
2020-03-21 19:02:28 +03:00
|
|
|
await hass.config_entries.async_forward_entry_unload(config_entry, "sensor")
|
2020-04-11 05:45:36 +02:00
|
|
|
await hass.config_entries.async_forward_entry_unload(config_entry, "binary_sensor")
|
|
|
|
await hass.config_entries.async_forward_entry_unload(config_entry, "device_tracker")
|
2020-03-21 19:02:28 +03:00
|
|
|
await hass.config_entries.async_forward_entry_unload(config_entry, "switch")
|
2020-04-20 11:05:49 +02:00
|
|
|
hass.services.async_remove(DOMAIN, RUN_SCRIPT_COMMAND)
|
2020-04-11 05:35:40 +02:00
|
|
|
await controller.async_reset()
|
2020-03-21 19:02:28 +03:00
|
|
|
hass.data[DOMAIN][DATA_CLIENT].pop(config_entry.entry_id)
|
|
|
|
return True
|