2019-12-02 01:13:28 +01:00
|
|
|
"""Mikrotik Router integration."""
|
|
|
|
|
|
|
|
import logging
|
2019-12-02 03:19:07 +01:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2019-12-02 04:19:40 +01:00
|
|
|
from homeassistant.const import (
|
2019-12-02 18:13:55 +01:00
|
|
|
CONF_NAME,
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_PORT,
|
|
|
|
CONF_USERNAME,
|
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_SSL,
|
2019-12-02 04:19:40 +01:00
|
|
|
)
|
|
|
|
|
2019-12-02 17:44:36 +01:00
|
|
|
from .mikrotik_controller import MikrotikControllerData
|
2019-12-02 04:19:40 +01:00
|
|
|
|
2019-12-02 01:13:28 +01:00
|
|
|
from .const import (
|
2019-12-02 18:13:55 +01:00
|
|
|
DOMAIN,
|
|
|
|
DATA_CLIENT,
|
2019-12-02 01:13:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-12-02 03:19:07 +01:00
|
|
|
|
2019-12-02 17:59:49 +01:00
|
|
|
# ---------------------------
|
2019-12-02 01:13:28 +01:00
|
|
|
# async_setup
|
2019-12-02 17:59:49 +01:00
|
|
|
# ---------------------------
|
2019-12-02 01:13:28 +01:00
|
|
|
async def async_setup(hass, config):
|
2019-12-02 18:13:55 +01:00
|
|
|
"""Set up configured Mikrotik Controller."""
|
|
|
|
hass.data[DOMAIN] = {}
|
|
|
|
hass.data[DOMAIN][DATA_CLIENT] = {}
|
|
|
|
return True
|
2019-12-02 01:13:28 +01:00
|
|
|
|
2019-12-02 03:19:07 +01:00
|
|
|
|
2019-12-02 17:59:49 +01:00
|
|
|
# ---------------------------
|
2019-12-02 01:13:28 +01:00
|
|
|
# async_setup_entry
|
2019-12-02 17:59:49 +01:00
|
|
|
# ---------------------------
|
2019-12-02 01:13:28 +01:00
|
|
|
async def async_setup_entry(hass, config_entry):
|
2019-12-02 18:13:55 +01:00
|
|
|
"""Set up Mikrotik Router as config entry."""
|
|
|
|
name = config_entry.data[CONF_NAME]
|
|
|
|
host = config_entry.data[CONF_HOST]
|
|
|
|
port = config_entry.data[CONF_PORT]
|
|
|
|
username = config_entry.data[CONF_USERNAME]
|
|
|
|
password = config_entry.data[CONF_PASSWORD]
|
|
|
|
use_ssl = config_entry.data[CONF_SSL]
|
|
|
|
|
|
|
|
mikrotik_controller = MikrotikControllerData(hass, config_entry, name, host, port, username, password, use_ssl)
|
|
|
|
await mikrotik_controller.hwinfo_update()
|
|
|
|
await mikrotik_controller.async_update()
|
|
|
|
|
|
|
|
if not mikrotik_controller.data:
|
|
|
|
raise ConfigEntryNotReady()
|
|
|
|
|
|
|
|
hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id] = mikrotik_controller
|
|
|
|
|
|
|
|
# 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, "device_tracker")
|
|
|
|
)
|
|
|
|
|
|
|
|
device_registry = await hass.helpers.device_registry.async_get_registry()
|
|
|
|
device_registry.async_get_or_create(
|
|
|
|
config_entry_id=config_entry.entry_id,
|
|
|
|
manufacturer=mikrotik_controller.data['resource']['platform'],
|
|
|
|
model=mikrotik_controller.data['routerboard']['model'],
|
|
|
|
name=mikrotik_controller.data['routerboard']['model'],
|
|
|
|
sw_version=mikrotik_controller.data['resource']['version'],
|
|
|
|
)
|
|
|
|
|
|
|
|
return True
|
2019-12-02 01:13:28 +01:00
|
|
|
|
2019-12-02 03:19:07 +01:00
|
|
|
|
2019-12-02 17:59:49 +01:00
|
|
|
# ---------------------------
|
2019-12-02 01:13:28 +01:00
|
|
|
# async_unload_entry
|
2019-12-02 17:59:49 +01:00
|
|
|
# ---------------------------
|
2019-12-02 01:13:28 +01:00
|
|
|
async def async_unload_entry(hass, config_entry):
|
2019-12-02 18:13:55 +01:00
|
|
|
"""Unload a config entry."""
|
|
|
|
mikrotik_controller = hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id]
|
|
|
|
await hass.config_entries.async_forward_entry_unload(config_entry, "sensor")
|
|
|
|
await hass.config_entries.async_forward_entry_unload(config_entry, "device_tracker")
|
|
|
|
await mikrotik_controller.async_reset()
|
|
|
|
hass.data[DOMAIN][DATA_CLIENT].pop(config_entry.entry_id)
|
|
|
|
return True
|