2019-07-29 07:21:26 +00:00
|
|
|
"""Config flow for the Velbus platform."""
|
|
|
|
import velbus
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import config_entries
|
|
|
|
from homeassistant.const import CONF_PORT, CONF_NAME
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.util import slugify
|
|
|
|
|
|
|
|
from .const import DOMAIN
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def velbus_entries(hass: HomeAssistant):
|
|
|
|
"""Return connections for Velbus domain."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return set(
|
|
|
|
(entry.data[CONF_PORT]) for entry in hass.config_entries.async_entries(DOMAIN)
|
|
|
|
)
|
2019-07-29 07:21:26 +00:00
|
|
|
|
|
|
|
|
2019-08-29 06:45:01 +00:00
|
|
|
class VelbusConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
2019-07-29 07:21:26 +00:00
|
|
|
"""Handle a config flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_PUSH
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
"""Initialize the velbus config flow."""
|
|
|
|
self._errors = {}
|
|
|
|
|
|
|
|
def _create_device(self, name: str, prt: str):
|
|
|
|
"""Create an antry async."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_create_entry(title=name, data={CONF_PORT: prt})
|
2019-07-29 07:21:26 +00:00
|
|
|
|
|
|
|
def _test_connection(self, prt):
|
|
|
|
"""Try to connect to the velbus with the port specified."""
|
|
|
|
try:
|
|
|
|
controller = velbus.Controller(prt)
|
|
|
|
except Exception: # pylint: disable=broad-except
|
2019-07-31 19:25:30 +00:00
|
|
|
self._errors[CONF_PORT] = "connection_failed"
|
2019-07-29 07:21:26 +00:00
|
|
|
return False
|
|
|
|
controller.stop()
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _prt_in_configuration_exists(self, prt: str) -> bool:
|
|
|
|
"""Return True if port exists in configuration."""
|
|
|
|
if prt in velbus_entries(self.hass):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
async def async_step_user(self, user_input=None):
|
|
|
|
"""Step when user intializes a integration."""
|
|
|
|
self._errors = {}
|
|
|
|
if user_input is not None:
|
|
|
|
name = slugify(user_input[CONF_NAME])
|
|
|
|
prt = user_input[CONF_PORT]
|
|
|
|
if not self._prt_in_configuration_exists(prt):
|
|
|
|
if self._test_connection(prt):
|
|
|
|
return self._create_device(name, prt)
|
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
self._errors[CONF_PORT] = "port_exists"
|
2019-07-29 07:21:26 +00:00
|
|
|
else:
|
|
|
|
user_input = {}
|
2019-07-31 19:25:30 +00:00
|
|
|
user_input[CONF_NAME] = ""
|
|
|
|
user_input[CONF_PORT] = ""
|
2019-07-29 07:21:26 +00:00
|
|
|
|
|
|
|
return self.async_show_form(
|
2019-07-31 19:25:30 +00:00
|
|
|
step_id="user",
|
|
|
|
data_schema=vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_NAME, default=user_input[CONF_NAME]): str,
|
|
|
|
vol.Required(CONF_PORT, default=user_input[CONF_PORT]): str,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
errors=self._errors,
|
2019-07-29 07:21:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
async def async_step_import(self, user_input=None):
|
|
|
|
"""Import a config entry."""
|
2019-07-31 19:25:30 +00:00
|
|
|
user_input[CONF_NAME] = "Velbus Import"
|
2019-07-29 07:21:26 +00:00
|
|
|
prt = user_input[CONF_PORT]
|
|
|
|
if self._prt_in_configuration_exists(prt):
|
|
|
|
# if the velbus import is already in the config
|
|
|
|
# we should not proceed the import
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.async_abort(reason="port_exists")
|
2019-07-29 07:21:26 +00:00
|
|
|
return await self.async_step_user(user_input)
|