2020-03-21 19:02:28 +03:00
|
|
|
"""Mikrotik Router integration."""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
|
|
|
|
|
|
|
from .const import (
|
|
|
|
DOMAIN,
|
|
|
|
DATA_CLIENT,
|
|
|
|
)
|
|
|
|
from .mikrotik_controller import MikrotikControllerData
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
# 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(
|
|
|
|
hass.config_entries.async_forward_entry_setup(config_entry,
|
|
|
|
"binary_sensor")
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(config_entry,
|
|
|
|
"device_tracker")
|
|
|
|
)
|
|
|
|
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(config_entry, "switch")
|
|
|
|
)
|
|
|
|
|
|
|
|
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")
|
|
|
|
await hass.config_entries.async_forward_entry_unload(config_entry,
|
|
|
|
"binary_sensor")
|
|
|
|
await hass.config_entries.async_forward_entry_unload(config_entry,
|
|
|
|
"device_tracker")
|
|
|
|
await hass.config_entries.async_forward_entry_unload(config_entry, "switch")
|
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
|