# Buggy code
async def request_configuration(hass, config_entry, login):
    """Request configuration steps from the user using the configurator."""

    async def configuration_callback(callback_data):
        """Handle the submitted configuration."""
        await hass.async_add_job(
            setup_platform_callback, hass, config_entry, login, callback_data
        )

    configurator = hass.components.configurator
    status = login.status
    email = login.email
    # links = ""
    footer = ""
    if "error_message" in status and status["error_message"]:
        footer = (
            "\n<b>NOTE: Actual Amazon error message in red below. "
            "Remember password will be provided automatically"
            " and Amazon error message normally appears first!</b>"
        )
    # if login.links:
    #     links = '\n\nGo to link with link# (e.g. link0)\n' + login.links
    # Get Captcha
    if (
        status
        and "captcha_image_url" in status
        and status["captcha_image_url"] is not None
    ):
        config_id = configurator.async_request_config(
            "Alexa Media Player - Captcha - {}".format(email),
            configuration_callback,
            description=(
                "Please enter the text for the captcha."
                " Please hit confirm to reload image." + footer
                # + links
            ),
            description_image=status["captcha_image_url"],
            submit_caption="Confirm",
            fields=[{"id": "captcha", "name": "Captcha"}],
        )
    elif (
        status and "securitycode_required" in status and status["securitycode_required"]
    ):  # Get 2FA code
        config_id = configurator.async_request_config(
            "Alexa Media Player - 2FA - {}".format(email),
            configuration_callback,
            description=(
                "Please enter your Two-Factor Security code."
                # + links
                + footer
            ),
            submit_caption="Confirm",
            fields=[{"id": "securitycode", "name": "Security Code"}],
        )
    elif (
        status and "claimspicker_required" in status and status["claimspicker_required"]
    ):  # Get picker method
        options = status["claimspicker_message"]
        if options:
            config_id = configurator.async_request_config(
                "Alexa Media Player - Verification Method - {}".format(email),
                configuration_callback,
                description=(
                    "Please select the verification method by number. "
                    "(e.g., `0` or `1`).\n{}".format(options)
                    # + links
                    + footer
                ),
                submit_caption="Confirm",
                fields=[{"id": "claimsoption", "name": "Option"}],
            )
        else:
            await configuration_callback({})
    elif (
        status and "authselect_required" in status and status["authselect_required"]
    ):  # Get picker method
        options = status["authselect_message"]
        if options:
            config_id = configurator.async_request_config(
                "Alexa Media Player - OTP Method - {}".format(email),
                configuration_callback,
                description=(
                    "Please select the OTP method by number. "
                    "(e.g., `0`, `1`).<br />{}".format(options)
                    # + links
                    + footer
                ),
                submit_caption="Confirm",
                fields=[{"id": "authselectoption", "name": "Option"}],
            )
        else:
            await configuration_callback({})
    elif (
        status
        and "verificationcode_required" in status
        and status["verificationcode_required"]
    ):  # Get picker method
        config_id = configurator.async_request_config(
            "Alexa Media Player - Verification Code - {}".format(email),
            configuration_callback,
            description=(
                "Please enter received verification code."
                # + links
                + footer
            ),
            submit_caption="Confirm",
            fields=[{"id": "verificationcode", "name": "Verification Code"}],
        )
    else:  # Check login
        config_id = configurator.async_request_config(
            "Alexa Media Player - Begin - {}".format(email),
            configuration_callback,
            description=("Please hit confirm to begin login attempt."),
            submit_caption="Confirm",
            fields=[],
        )
    if config_id:
        hass.data[DATA_ALEXAMEDIA]["accounts"][email]["configurator"].append(config_id)
    if "error_message" in status and status["error_message"]:
        configurator.async_notify_errors(config_id, status["error_message"])
    if len(hass.data[DATA_ALEXAMEDIA]["accounts"][email]["configurator"]) > 1:
        configurator.async_request_done(
            (hass.data[DATA_ALEXAMEDIA]["accounts"][email]["configurator"]).pop(0)
        )


# Traceback (UnboundLocalError)
2020-05-25 19:16:50 DEBUG (MainThread) [alexapy.alexalogin] Processing https://www.amazon.es/ap/cvf/verify
2020-05-25 19:16:50 DEBUG (MainThread) [alexapy.alexalogin] Verification captcha code requested:
2020-05-25 19:16:50 DEBUG (MainThread) [alexapy.alexalogin] Found form to process:
2020-05-25 19:16:50 DEBUG (MainThread) [alexapy.alexalogin] Found post url to verify; converting to https://www.amazon.es/ap/cvf/verify
2020-05-25 19:16:50 DEBUG (MainThread) [custom_components.alexa_media.configurator] Testing login status: {'captcha_required': True, 'captcha_image_url': 'https://opfcaptcha-prod.s3.amazonaws.com/xxxxxxxxxxxxxxxxxxx.jpg?AWSAccessKeyId=xxxxxxxxxxxxxxx&amp;Expires=1590427309&amp;Signature=xxxxxxxxxxxx%3D', 'verification_captcha_required': True}
2020-05-25 19:16:50 DEBUG (MainThread) [custom_components.alexa_media.configurator] Creating configurator to request captcha

Logger: homeassistant.components.websocket_api.http.connection.140211693798864
Source: custom_components/alexa_media/configurator.py:143
Integration: Home Assistant WebSocket API (documentation, issues)
First occurred: 19:16:50 (1 occurrences)
Last logged: 19:16:50

local variable 'config_id' referenced before assignment
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 130, in handle_call_service
connection.context(msg),
File "/usr/src/homeassistant/homeassistant/core.py", line 1253, in async_call
task.result()
File "/usr/src/homeassistant/homeassistant/core.py", line 1288, in _execute_service
await handler.func(service_call)
File "/usr/src/homeassistant/homeassistant/components/configurator/__init__.py", line 235, in async_handle_service_call
await self.hass.async_add_job(callback, call.data.get(ATTR_FIELDS, {}))
File "/config/custom_components/alexa_media/configurator.py", line 28, in configuration_callback
setup_platform_callback, hass, config_entry, login, callback_data
File "/config/custom_components/alexa_media/configurator.py", line 177, in setup_platform_callback
await test_login_status(hass, config_entry, login, alexa_setup_callback)
File "/config/custom_components/alexa_media/configurator.py", line 206, in test_login_status
await hass.async_add_job(request_configuration, hass, config_entry, login)
File "/config/custom_components/alexa_media/configurator.py", line 143, in request_configuration
if config_id:
UnboundLocalError: local variable 'config_id' referenced before assignment

# Fixed code
async def request_configuration(hass, config_entry, login):
    """Request configuration steps from the user using the configurator."""

    async def configuration_callback(callback_data):
        """Handle the submitted configuration."""
        await hass.async_add_job(
            setup_platform_callback, hass, config_entry, login, callback_data
        )

    configurator = hass.components.configurator
    status = login.status
    email = login.email
    # links = ""
    footer = ""
    config_id = None
    if "error_message" in status and status["error_message"]:
        footer = (
            "\n<b>NOTE: Actual Amazon error message in red below. "
            "Remember password will be provided automatically"
            " and Amazon error message normally appears first!</b>"
        )
    # if login.links:
    #     links = '\n\nGo to link with link# (e.g. link0)\n' + login.links
    # Get Captcha
    if (
        status
        and "captcha_image_url" in status
        and status["captcha_image_url"] is not None
    ):
        config_id = configurator.async_request_config(
            "Alexa Media Player - Captcha - {}".format(email),
            configuration_callback,
            description=(
                "Please enter the text for the captcha."
                " Please hit confirm to reload image." + footer
                # + links
            ),
            description_image=status["captcha_image_url"],
            submit_caption="Confirm",
            fields=[{"id": "captcha", "name": "Captcha"}],
        )
    elif (
        status and "securitycode_required" in status and status["securitycode_required"]
    ):  # Get 2FA code
        config_id = configurator.async_request_config(
            "Alexa Media Player - 2FA - {}".format(email),
            configuration_callback,
            description=(
                "Please enter your Two-Factor Security code."
                # + links
                + footer
            ),
            submit_caption="Confirm",
            fields=[{"id": "securitycode", "name": "Security Code"}],
        )
    elif (
        status and "claimspicker_required" in status and status["claimspicker_required"]
    ):  # Get picker method
        options = status["claimspicker_message"]
        if options:
            config_id = configurator.async_request_config(
                "Alexa Media Player - Verification Method - {}".format(email),
                configuration_callback,
                description=(
                    "Please select the verification method by number. "
                    "(e.g., `0` or `1`).\n{}".format(options)
                    # + links
                    + footer
                ),
                submit_caption="Confirm",
                fields=[{"id": "claimsoption", "name": "Option"}],
            )
        else:
            await configuration_callback({})
    elif (
        status and "authselect_required" in status and status["authselect_required"]
    ):  # Get picker method
        options = status["authselect_message"]
        if options:
            config_id = configurator.async_request_config(
                "Alexa Media Player - OTP Method - {}".format(email),
                configuration_callback,
                description=(
                    "Please select the OTP method by number. "
                    "(e.g., `0`, `1`).<br />{}".format(options)
                    # + links
                    + footer
                ),
                submit_caption="Confirm",
                fields=[{"id": "authselectoption", "name": "Option"}],
            )
        else:
            await configuration_callback({})
    elif (
        status
        and "verificationcode_required" in status
        and status["verificationcode_required"]
    ):  # Get picker method
        config_id = configurator.async_request_config(
            "Alexa Media Player - Verification Code - {}".format(email),
            configuration_callback,
            description=(
                "Please enter received verification code."
                # + links
                + footer
            ),
            submit_caption="Confirm",
            fields=[{"id": "verificationcode", "name": "Verification Code"}],
        )
    else:  # Check login
        config_id = configurator.async_request_config(
            "Alexa Media Player - Begin - {}".format(email),
            configuration_callback,
            description=("Please hit confirm to begin login attempt."),
            submit_caption="Confirm",
            fields=[],
        )
    if config_id:
        hass.data[DATA_ALEXAMEDIA]["accounts"][email]["configurator"].append(config_id)
    if "error_message" in status and status["error_message"]:
        configurator.async_notify_errors(config_id, status["error_message"])
    if len(hass.data[DATA_ALEXAMEDIA]["accounts"][email]["configurator"]) > 1:
        configurator.async_request_done(
            (hass.data[DATA_ALEXAMEDIA]["accounts"][email]["configurator"]).pop(0)
        )