diff --git a/homeassistant/components/opower/config_flow.py b/homeassistant/components/opower/config_flow.py index 4753a77894e..e7f2534e1ad 100644 --- a/homeassistant/components/opower/config_flow.py +++ b/homeassistant/components/opower/config_flow.py @@ -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( diff --git a/tests/components/opower/test_config_flow.py b/tests/components/opower/test_config_flow.py index 8134539b0a5..c9edfc6808f 100644 --- a/tests/components/opower/test_config_flow.py +++ b/tests/components/opower/test_config_flow.py @@ -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