Skip to content

Commit 09dd765

Browse files
chemelli74frenck
authored andcommitted
Fix config flow reconfigure for Comelit (home-assistant#156193)
1 parent 0c8b765 commit 09dd765

File tree

1 file changed

+42
-30
lines changed

1 file changed

+42
-30
lines changed

homeassistant/components/comelit/config_flow.py

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,6 @@
3737
}
3838
)
3939
STEP_REAUTH_DATA_SCHEMA = vol.Schema({vol.Required(CONF_PIN): cv.string})
40-
STEP_RECONFIGURE = vol.Schema(
41-
{
42-
vol.Required(CONF_HOST): cv.string,
43-
vol.Required(CONF_PORT): cv.port,
44-
vol.Optional(CONF_PIN, default=DEFAULT_PIN): cv.string,
45-
}
46-
)
4740

4841

4942
async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, str]:
@@ -175,36 +168,55 @@ async def async_step_reconfigure(
175168
) -> ConfigFlowResult:
176169
"""Handle reconfiguration of the device."""
177170
reconfigure_entry = self._get_reconfigure_entry()
178-
if not user_input:
179-
return self.async_show_form(
180-
step_id="reconfigure", data_schema=STEP_RECONFIGURE
181-
)
171+
errors: dict[str, str] = {}
182172

183-
updated_host = user_input[CONF_HOST]
173+
if user_input is not None:
174+
updated_host = user_input[CONF_HOST]
184175

185-
self._async_abort_entries_match({CONF_HOST: updated_host})
176+
self._async_abort_entries_match({CONF_HOST: updated_host})
186177

187-
errors: dict[str, str] = {}
178+
try:
179+
data_to_validate = {
180+
CONF_HOST: updated_host,
181+
CONF_PORT: user_input[CONF_PORT],
182+
CONF_PIN: user_input[CONF_PIN],
183+
CONF_TYPE: reconfigure_entry.data.get(CONF_TYPE, BRIDGE),
184+
}
185+
await validate_input(self.hass, data_to_validate)
186+
except CannotConnect:
187+
errors["base"] = "cannot_connect"
188+
except InvalidAuth:
189+
errors["base"] = "invalid_auth"
190+
except InvalidPin:
191+
errors["base"] = "invalid_pin"
192+
except Exception: # noqa: BLE001
193+
_LOGGER.exception("Unexpected exception")
194+
errors["base"] = "unknown"
195+
else:
196+
data_updates = {
197+
CONF_HOST: updated_host,
198+
CONF_PORT: user_input[CONF_PORT],
199+
CONF_PIN: user_input[CONF_PIN],
200+
}
201+
return self.async_update_reload_and_abort(
202+
reconfigure_entry, data_updates=data_updates
203+
)
188204

189-
try:
190-
await validate_input(self.hass, user_input)
191-
except CannotConnect:
192-
errors["base"] = "cannot_connect"
193-
except InvalidAuth:
194-
errors["base"] = "invalid_auth"
195-
except InvalidPin:
196-
errors["base"] = "invalid_pin"
197-
except Exception: # noqa: BLE001
198-
_LOGGER.exception("Unexpected exception")
199-
errors["base"] = "unknown"
200-
else:
201-
return self.async_update_reload_and_abort(
202-
reconfigure_entry, data_updates={CONF_HOST: updated_host}
203-
)
205+
schema = vol.Schema(
206+
{
207+
vol.Required(
208+
CONF_HOST, default=reconfigure_entry.data[CONF_HOST]
209+
): cv.string,
210+
vol.Required(
211+
CONF_PORT, default=reconfigure_entry.data[CONF_PORT]
212+
): cv.port,
213+
vol.Optional(CONF_PIN): cv.string,
214+
}
215+
)
204216

205217
return self.async_show_form(
206218
step_id="reconfigure",
207-
data_schema=STEP_RECONFIGURE,
219+
data_schema=schema,
208220
errors=errors,
209221
)
210222

0 commit comments

Comments
 (0)