Fix Besen config flow schema serialization (#181223)

This commit is contained in:
moryoav
2026-09-05 10:33:16 +00:00
committed by Franck Nijhof
parent 83aa8bf3b3
commit 99cdc3172c
2 changed files with 27 additions and 18 deletions
@@ -34,13 +34,10 @@ def _normalize_address(address: str) -> str:
return address.strip().upper()
PIN_SCHEMA = vol.All(
selector.TextSelector(
selector.TextSelectorConfig(
type=selector.TextSelectorType.PASSWORD,
)
),
vol.Match(r"^\d{6}$"),
PIN_SCHEMA = selector.TextSelector(
selector.TextSelectorConfig(
type=selector.TextSelectorType.PASSWORD,
)
)
PIN_ONLY_SCHEMA = vol.Schema(
@@ -77,6 +74,9 @@ async def _async_validate_input(
) -> str:
"""Validate setup by logging into the charger."""
if len(pin) != 6 or not pin.isdecimal():
raise InvalidAuth("PIN must be exactly 6 digits")
def _ble_device_provider() -> BLEDevice | None:
return bluetooth.async_ble_device_from_address(
hass,
+20 -11
View File
@@ -3,8 +3,8 @@
from unittest.mock import Mock
from besen.exceptions import CannotConnect, InvalidAuth
from probatio import to_field_list
import pytest
import voluptuous as vol
from homeassistant.components.besen.const import DOMAIN
from homeassistant.components.bluetooth import BluetoothServiceInfoBleak
@@ -12,6 +12,7 @@ from homeassistant.config_entries import SOURCE_BLUETOOTH, SOURCE_USER
from homeassistant.const import CONF_ADDRESS, CONF_NAME, CONF_PIN
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from homeassistant.helpers import config_validation as cv
from .conftest import (
FIXTURE_ADDRESS,
@@ -91,6 +92,7 @@ async def test_bluetooth_step_sets_discovered_context(
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "bluetooth_confirm"
assert to_field_list(result["data_schema"], custom_serializer=cv.custom_serializer)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
@@ -243,25 +245,32 @@ async def test_user_step_success(
_assert_create_entry(result)
@pytest.mark.usefixtures("mock_besen_client", "mock_setup_entry")
@pytest.mark.parametrize("invalid_pin", ["12345", "¹²³⁴⁵⁶"])
@pytest.mark.usefixtures("mock_setup_entry")
async def test_user_step_rejects_invalid_pin(
hass: HomeAssistant,
mock_besen_client: Mock,
invalid_pin: str,
) -> None:
"""Test the user step PIN schema rejects invalid values."""
"""Test the user step rejects invalid PIN values."""
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_USER},
)
with pytest.raises(vol.Invalid):
await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_ADDRESS: FIXTURE_ADDRESS,
CONF_PIN: "12345",
},
)
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{
CONF_ADDRESS: FIXTURE_ADDRESS,
CONF_PIN: invalid_pin,
},
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
assert result["errors"] == {"base": "invalid_auth"}
mock_besen_client.async_start.assert_not_awaited()
result = await hass.config_entries.flow.async_configure(
result["flow_id"],