2019-12-02 01:13:28 +01:00
|
|
|
"""Config flow to configure Mikrotik Router."""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import voluptuous as vol
|
|
|
|
from homeassistant import config_entries
|
|
|
|
from homeassistant.core import callback
|
|
|
|
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 01:13:28 +01:00
|
|
|
)
|
|
|
|
|
2019-12-02 17:44:36 +01:00
|
|
|
from .const import (
|
2019-12-02 18:13:55 +01:00
|
|
|
DOMAIN,
|
|
|
|
CONF_TRACK_ARP,
|
|
|
|
DEFAULT_TRACK_ARP,
|
|
|
|
CONF_SCAN_INTERVAL,
|
|
|
|
DEFAULT_SCAN_INTERVAL,
|
2019-12-02 01:13:28 +01:00
|
|
|
)
|
|
|
|
|
2019-12-02 04:19:40 +01:00
|
|
|
from .mikrotikapi import MikrotikAPI
|
2019-12-02 03:19:07 +01:00
|
|
|
|
2019-12-02 17:59:49 +01:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
# ---------------------------
|
2019-12-02 01:13:28 +01:00
|
|
|
# configured_instances
|
2019-12-02 17:59:49 +01:00
|
|
|
# ---------------------------
|
2019-12-02 01:13:28 +01:00
|
|
|
@callback
|
|
|
|
def configured_instances(hass):
|
2019-12-02 18:13:55 +01:00
|
|
|
"""Return a set of configured instances."""
|
|
|
|
return set(
|
|
|
|
entry.data[CONF_NAME] for entry in hass.config_entries.async_entries(DOMAIN)
|
|
|
|
)
|
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
|
|
|
# MikrotikControllerConfigFlow
|
2019-12-02 17:59:49 +01:00
|
|
|
# ---------------------------
|
2019-12-02 01:13:28 +01:00
|
|
|
class MikrotikControllerConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
2019-12-02 18:13:55 +01:00
|
|
|
def __init__(self):
|
|
|
|
"""Initialize."""
|
|
|
|
return
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@callback
|
|
|
|
def async_get_options_flow(config_entry):
|
|
|
|
"""Get the options flow for this handler."""
|
|
|
|
return MikrotikControllerOptionsFlowHandler(config_entry)
|
|
|
|
|
|
|
|
async def async_step_import(self, user_input=None):
|
|
|
|
"""Occurs when a previously entry setup fails and is re-initiated."""
|
|
|
|
return await self.async_step_user(user_input)
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
|
|
"""Handle a flow initialized by the user."""
|
|
|
|
errors = {}
|
|
|
|
if user_input is not None:
|
|
|
|
# Check if instance with this name already exists
|
|
|
|
if user_input[CONF_NAME] in configured_instances(self.hass):
|
|
|
|
errors["base"] = "name_exists"
|
|
|
|
|
|
|
|
# Test connection
|
2019-12-02 19:22:34 +01:00
|
|
|
api = MikrotikAPI(host=user_input["host"],
|
|
|
|
username=user_input["username"],
|
|
|
|
password=user_input["password"],
|
|
|
|
port=user_input["port"],
|
|
|
|
use_ssl=user_input["ssl"]
|
|
|
|
)
|
2019-12-02 18:13:55 +01:00
|
|
|
if not api.connect():
|
|
|
|
errors[CONF_HOST] = api.error
|
|
|
|
|
|
|
|
# Save instance
|
|
|
|
if not errors:
|
|
|
|
return self.async_create_entry(
|
|
|
|
title=user_input[CONF_NAME],
|
|
|
|
data=user_input
|
|
|
|
)
|
|
|
|
|
2019-12-02 19:22:34 +01:00
|
|
|
return self._show_config_form(host=user_input["host"],
|
|
|
|
username=user_input["username"],
|
|
|
|
password=user_input["password"],
|
|
|
|
port=user_input["port"],
|
|
|
|
name=user_input["name"],
|
|
|
|
use_ssl=user_input["ssl"],
|
|
|
|
errors=errors
|
|
|
|
)
|
2019-12-02 18:13:55 +01:00
|
|
|
|
|
|
|
return self._show_config_form(errors=errors)
|
|
|
|
|
|
|
|
# ---------------------------
|
|
|
|
# _show_config_form
|
|
|
|
# ---------------------------
|
|
|
|
def _show_config_form(self, host='10.0.0.1', username='admin', password='admin', port=0, name='Mikrotik', use_ssl=False, errors=None):
|
|
|
|
"""Show the configuration form to edit data."""
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id='user',
|
|
|
|
data_schema=vol.Schema({
|
|
|
|
vol.Required(CONF_HOST, default=host): str,
|
|
|
|
vol.Required(CONF_USERNAME, default=username): str,
|
|
|
|
vol.Required(CONF_PASSWORD, default=password): str,
|
|
|
|
vol.Optional(CONF_PORT, default=port): int,
|
|
|
|
vol.Optional(CONF_NAME, default=name): str,
|
|
|
|
vol.Optional(CONF_SSL, default=use_ssl): bool,
|
|
|
|
}),
|
|
|
|
errors=errors,
|
|
|
|
)
|
2019-12-02 01:13:28 +01:00
|
|
|
|
|
|
|
|
2019-12-02 17:59:49 +01:00
|
|
|
# ---------------------------
|
2019-12-02 01:13:28 +01:00
|
|
|
# MikrotikControllerOptionsFlowHandler
|
2019-12-02 17:59:49 +01:00
|
|
|
# ---------------------------
|
2019-12-02 01:13:28 +01:00
|
|
|
class MikrotikControllerOptionsFlowHandler(config_entries.OptionsFlow):
|
2019-12-02 18:13:55 +01:00
|
|
|
"""Handle options."""
|
|
|
|
|
|
|
|
def __init__(self, config_entry):
|
|
|
|
"""Initialize options flow."""
|
|
|
|
self.config_entry = config_entry
|
|
|
|
self.options = dict(config_entry.options)
|
|
|
|
|
|
|
|
async def async_step_init(self, user_input=None):
|
|
|
|
"""Manage the options."""
|
|
|
|
return await self.async_step_device_tracker(user_input)
|
|
|
|
|
|
|
|
async def async_step_device_tracker(self, user_input=None):
|
|
|
|
"""Manage the device tracker options."""
|
|
|
|
if user_input is not None:
|
|
|
|
self.options.update(user_input)
|
|
|
|
return self.async_create_entry(title="", data=self.options)
|
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="device_tracker",
|
|
|
|
data_schema=vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(
|
|
|
|
CONF_TRACK_ARP,
|
|
|
|
default=self.config_entry.options.get(
|
|
|
|
CONF_TRACK_ARP, DEFAULT_TRACK_ARP
|
|
|
|
),
|
|
|
|
): bool,
|
|
|
|
vol.Optional(
|
|
|
|
CONF_SCAN_INTERVAL,
|
|
|
|
default=self.config_entry.options.get(
|
|
|
|
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
|
|
|
|
),
|
|
|
|
): int,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|