1
0
mirror of https://github.com/home-assistant/core.git synced 2026-02-15 07:36:16 +00:00

Improve EnOcean config flow (#162751)

This commit is contained in:
Christopher Fenner
2026-02-11 19:14:45 +01:00
committed by GitHub
parent 80ebb34ad1
commit 9f1b4c9035
2 changed files with 11 additions and 12 deletions

View File

@@ -6,6 +6,7 @@ import voluptuous as vol
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_DEVICE
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.selector import (
SelectSelector,
SelectSelectorConfig,
@@ -15,6 +16,12 @@ from homeassistant.helpers.selector import (
from . import dongle
from .const import DOMAIN, ERROR_INVALID_DONGLE_PATH, LOGGER
MANUAL_SCHEMA = vol.Schema(
{
vol.Required(CONF_DEVICE): cv.string,
}
)
class EnOceanFlowHandler(ConfigFlow, domain=DOMAIN):
"""Handle the enOcean config flows."""
@@ -49,17 +56,14 @@ class EnOceanFlowHandler(ConfigFlow, domain=DOMAIN):
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Propose a list of detected dongles."""
errors = {}
if user_input is not None:
if user_input[CONF_DEVICE] == self.MANUAL_PATH_VALUE:
return await self.async_step_manual()
if await self.validate_enocean_conf(user_input):
return self.create_enocean_entry(user_input)
errors = {CONF_DEVICE: ERROR_INVALID_DONGLE_PATH}
return await self.async_step_manual(user_input)
devices = await self.hass.async_add_executor_job(dongle.detect)
if len(devices) == 0:
return await self.async_step_manual(user_input)
return await self.async_step_manual()
devices.append(self.MANUAL_PATH_VALUE)
return self.async_show_form(
@@ -75,26 +79,21 @@ class EnOceanFlowHandler(ConfigFlow, domain=DOMAIN):
)
}
),
errors=errors,
)
async def async_step_manual(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Request manual USB dongle path."""
default_value = None
errors = {}
if user_input is not None:
if await self.validate_enocean_conf(user_input):
return self.create_enocean_entry(user_input)
default_value = user_input[CONF_DEVICE]
errors = {CONF_DEVICE: ERROR_INVALID_DONGLE_PATH}
return self.async_show_form(
step_id="manual",
data_schema=vol.Schema(
{vol.Required(CONF_DEVICE, default=default_value): str}
),
data_schema=self.add_suggested_values_to_schema(MANUAL_SCHEMA, user_input),
errors=errors,
)

View File

@@ -106,7 +106,7 @@ async def test_detection_flow_with_invalid_path(hass: HomeAssistant) -> None:
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "detect"
assert result["step_id"] == "manual"
assert CONF_DEVICE in result["errors"]