Remember Opower utility and username on config flow errors (#148097)

This commit is contained in:
tronikos 2025-07-05 01:26:15 -07:00 committed by GitHub
parent f1698cdb75
commit fea7dc7eba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 3 deletions

View File

@ -26,6 +26,7 @@ from .const import CONF_TOTP_SECRET, CONF_UTILITY, DOMAIN
_LOGGER = logging.getLogger(__name__)
STEP_USER_DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_UTILITY): vol.In(get_supported_utility_names()),
@ -88,9 +89,15 @@ class OpowerConfigFlow(ConfigFlow, domain=DOMAIN):
errors = await _validate_login(self.hass, user_input)
if not errors:
return self._async_create_opower_entry(user_input)
else:
user_input = {}
user_input.pop(CONF_PASSWORD, None)
return self.async_show_form(
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
step_id="user",
data_schema=self.add_suggested_values_to_schema(
STEP_USER_DATA_SCHEMA, user_input
),
errors=errors,
)
async def async_step_mfa(

View File

@ -13,7 +13,7 @@ from homeassistant.config_entries import ConfigEntryState
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType
from tests.common import MockConfigEntry
from tests.common import MockConfigEntry, get_schema_suggested_value
@pytest.fixture(autouse=True, name="mock_setup_entry")
@ -203,6 +203,15 @@ async def test_form_exceptions(
assert result2["type"] is FlowResultType.FORM
assert result2["errors"] == {"base": expected_error}
# On error, the form should have the previous user input, except password,
# as suggested values.
data_schema = result2["data_schema"].schema
assert (
get_schema_suggested_value(data_schema, "utility")
== "Pacific Gas and Electric Company (PG&E)"
)
assert get_schema_suggested_value(data_schema, "username") == "test-username"
assert get_schema_suggested_value(data_schema, "password") is None
assert mock_login.call_count == 1