Repository: home-assistant/core
File: homeassistant/components/teltonika/config_flow.py (Python)

Code before:
                errors["base"] = "invalid_auth"
            except Exception:
                _LOGGER.exception("Unexpected exception during reauth")
                errors["base"] = "unknown"
            else:
                # Verify reauth is for the same device
                await self.async_set_unique_id(info["device_id"])
                self._abort_if_unique_id_mismatch(reason="wrong_account")

                return self.async_update_reload_and_abort(
                    reauth_entry,
                    data_updates={
                        CONF_USERNAME: user_input[CONF_USERNAME],
                        CONF_PASSWORD: user_input[CONF_PASSWORD],
                    },
                )

        return self.async_show_form(
            step_id="reauth_confirm",
            data_schema=vol.Schema(
                {
                    vol.Required(
                        CONF_USERNAME, default=reauth_entry.data[CONF_USERNAME]
                    ): str,
                    vol.Required(CONF_PASSWORD): str,
                }
            ),
            errors=errors,
            description_placeholders={
                "name": reauth_entry.title,
                "host": reauth_entry.data[CONF_HOST],
            },
        )

    async def async_step_dhcp(
        self, discovery_info: DhcpServiceInfo
    ) -> ConfigFlowResult:
        """Handle DHCP discovery."""
        host = discovery_info.ip

        # Store discovered host for later use
        self._discovered_host = host

        # Try to get device info without authentication to get device identifier and name
        session = async_get_clientsession(self.hass)

        for base_url in get_url_variants(host):
            client = Teltasync(
                base_url=f"{base_url}/api",
                username="",  # No credentials yet
                password="",

Review comment (bug):
IMO it's better here to `self.add_suggested_values_to_schema` with both reauth entry data and user input, that way if you enter your password wrong on the first attempt you don't have to enter the user again and I also like adding both user and password there

Code after:
                errors["base"] = "invalid_auth"
            except Exception:
                _LOGGER.exception("Unexpected exception during reauth")
                errors["base"] = "unknown"
            else:
                # Verify reauth is for the same device
                await self.async_set_unique_id(info["device_id"])
                self._abort_if_unique_id_mismatch(reason="wrong_account")

                return self.async_update_reload_and_abort(
                    reauth_entry,
                    data_updates=user_input,
                )

        reauth_schema = vol.Schema(
            {
                vol.Required(CONF_USERNAME): str,
                vol.Required(CONF_PASSWORD): str,
            }
        )

        suggested = {**reauth_entry.data, **(user_input or {})}

        return self.async_show_form(
            step_id="reauth_confirm",
            data_schema=self.add_suggested_values_to_schema(reauth_schema, suggested),
            errors=errors,
            description_placeholders={
                "name": reauth_entry.title,
                "host": reauth_entry.data[CONF_HOST],
            },
        )

    async def async_step_dhcp(
        self, discovery_info: DhcpServiceInfo
    ) -> ConfigFlowResult:
        """Handle DHCP discovery."""
        host = discovery_info.ip

        # Store discovered host for later use
        self._discovered_host = host

        # Try to get device info without authentication to get device identifier and name
        session = async_get_clientsession(self.hass)

        for base_url in get_url_variants(host):
            client = Teltasync(
                base_url=f"{base_url}/api",
                username="",  # No credentials yet
                password="",
                session=session,