* init support for config flow for lupusec * correctly iterate over BinarySensorDeviceClass values for device class * bump lupupy to 0.3.2 * Updated device info for lupusec * revert bump lupupy for separate pr * fixed lupusec test-cases * Change setup to async_setup * remove redundant check for hass.data.setdefault * init support for config flow for lupusec * correctly iterate over BinarySensorDeviceClass values for device class * bump lupupy to 0.3.2 * Updated device info for lupusec * revert bump lupupy for separate pr * fixed lupusec test-cases * Change setup to async_setup * remove redundant check for hass.data.setdefault * resolve merge error lupupy * connection check when setting up config entry * removed unique_id and device_info for separate pr * changed name to friendly name * renamed LUPUSEC_PLATFORMS to PLATFORMS * preparation for code review * necessary changes for pr * changed config access * duplicate entry check * types added for setup_entry and test_host_connection * removed name for lupusec system * removed config entry from LupusecDevice * fixes for sensors * added else block for try * added integration warning * pass config to config_flow * fix test cases for new config flow * added error strings * changed async_create_entry invocation * added tests for exception handling * use parametrize * use parametrize for tests * recover test * test unique id * import from yaml tests * import error test cases * Update tests/components/lupusec/test_config_flow.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * fixed test case * removed superfluous test cases * self._async_abort_entries_match added * lib patching call * _async_abort_entries_match * patch lupupy lib instead of test connection * removed statements * test_flow_source_import_already_configured * Update homeassistant/components/lupusec/config_flow.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * removed unique_id from mockentry * added __init__.py to .coveragerc --------- Co-authored-by: suaveolent <suaveolent@users.noreply.github.com> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
110 lines
3.3 KiB
Python
110 lines
3.3 KiB
Python
""""Config flow for Lupusec integration."""
|
|
|
|
import logging
|
|
from typing import Any
|
|
|
|
import lupupy
|
|
import voluptuous as vol
|
|
|
|
from homeassistant import config_entries
|
|
from homeassistant.const import (
|
|
CONF_HOST,
|
|
CONF_IP_ADDRESS,
|
|
CONF_NAME,
|
|
CONF_PASSWORD,
|
|
CONF_USERNAME,
|
|
)
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.data_entry_flow import FlowResult
|
|
from homeassistant.exceptions import HomeAssistantError
|
|
|
|
from .const import DOMAIN
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
DATA_SCHEMA = vol.Schema(
|
|
{
|
|
vol.Required(CONF_HOST): str,
|
|
vol.Required(CONF_USERNAME): str,
|
|
vol.Required(CONF_PASSWORD): str,
|
|
}
|
|
)
|
|
|
|
|
|
class LupusecConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|
"""Lupusec config flow."""
|
|
|
|
async def async_step_user(
|
|
self, user_input: dict[str, Any] | None = None
|
|
) -> FlowResult:
|
|
"""Handle a flow initiated by the user."""
|
|
errors = {}
|
|
|
|
if user_input is not None:
|
|
self._async_abort_entries_match(user_input)
|
|
host = user_input[CONF_HOST]
|
|
username = user_input[CONF_USERNAME]
|
|
password = user_input[CONF_PASSWORD]
|
|
|
|
try:
|
|
await test_host_connection(self.hass, host, username, password)
|
|
except CannotConnect:
|
|
errors["base"] = "cannot_connect"
|
|
except Exception: # pylint: disable=broad-except
|
|
_LOGGER.exception("Unexpected exception")
|
|
errors["base"] = "unknown"
|
|
|
|
else:
|
|
return self.async_create_entry(
|
|
title=host,
|
|
data=user_input,
|
|
)
|
|
|
|
return self.async_show_form(
|
|
step_id="user", data_schema=DATA_SCHEMA, errors=errors
|
|
)
|
|
|
|
async def async_step_import(self, user_input: dict[str, Any]) -> FlowResult:
|
|
"""Import the yaml config."""
|
|
self._async_abort_entries_match(
|
|
{
|
|
CONF_HOST: user_input[CONF_IP_ADDRESS],
|
|
CONF_USERNAME: user_input[CONF_USERNAME],
|
|
CONF_PASSWORD: user_input[CONF_PASSWORD],
|
|
}
|
|
)
|
|
host = user_input[CONF_IP_ADDRESS]
|
|
username = user_input[CONF_USERNAME]
|
|
password = user_input[CONF_PASSWORD]
|
|
try:
|
|
await test_host_connection(self.hass, host, username, password)
|
|
except CannotConnect:
|
|
return self.async_abort(reason="cannot_connect")
|
|
except Exception: # pylint: disable=broad-except
|
|
_LOGGER.exception("Unexpected exception")
|
|
return self.async_abort(reason="unknown")
|
|
|
|
return self.async_create_entry(
|
|
title=user_input.get(CONF_NAME, host),
|
|
data={
|
|
CONF_HOST: host,
|
|
CONF_USERNAME: username,
|
|
CONF_PASSWORD: password,
|
|
},
|
|
)
|
|
|
|
|
|
async def test_host_connection(
|
|
hass: HomeAssistant, host: str, username: str, password: str
|
|
):
|
|
"""Test if the host is reachable and is actually a Lupusec device."""
|
|
|
|
try:
|
|
await hass.async_add_executor_job(lupupy.Lupusec, username, password, host)
|
|
except lupupy.LupusecException:
|
|
_LOGGER.error("Failed to connect to Lupusec device at %s", host)
|
|
raise CannotConnect
|
|
|
|
|
|
class CannotConnect(HomeAssistantError):
|
|
"""Error to indicate we cannot connect."""
|