<reponame>jcgoette/weight_gurus_homeassistant
"""weight_gurus configuration flow."""
from __future__ import annotations

from dataclasses import dataclass
from typing import Dict

import voluptuous as vol
from homeassistant import config_entries
from homeassistant.config_entries import ConfigEntry, OptionsFlow
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.aiohttp_client import async_create_clientsession

from .api import WeightGurusApiClient
from .const import DOMAIN, PLATFORMS


class WeightGurusFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
    """Handle a config flow."""

    VERSION = 1
    CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL

    def __init__(self) -> None:
        """Initialize."""
        self._errors: dict = {}

    async def async_step_user(
        self, user_input: Dict[str, str] | None = None
    ) -> FlowResult:
        """Handle a flow initiated by the user."""
        self._errors = {}

        if user_input is not None:
            valid = await self._test_credentials(
                user_input[CONF_EMAIL], user_input[CONF_PASSWORD]
            )
            if valid:
                return self.async_create_entry(
                    title=user_input[CONF_EMAIL], data=user_input
                )
            else:
                self._errors["base"] = "auth"

            return await self._show_config_form(user_input)

        user_input = {}
        # Provide defaults for form
        user_input[CONF_EMAIL] = ""
        user_input[CONF_PASSWORD] = ""

        return await self._show_config_form(user_input)

    @staticmethod
    @callback
    def async_get_options_flow(config_entry: ConfigEntry) -> OptionsFlow:
        return WeightGurusOptionsFlowHandler(config_entry)

    async def _show_config_form(
        self, user_input: Dict[str, str] | None = None
    ) -> OptionsFlow:  # pylint: disable=unused-argument
        """Show the configuration form to edit location data."""
        return self.async_show_form(
            step_id="user",
            data_schema=vol.Schema(
                {
                    vol.Required(CONF_EMAIL, default=user_input[CONF_EMAIL]): str,
                    vol.Required(CONF_PASSWORD, default=user_input[CONF_PASSWORD]): str,
                }
            ),
            errors=self._errors,
        )

    async def _test_credentials(self, email: str, password: str) -> bool:
        """Test if the credentials are valid."""
        try:
            session = async_create_clientsession(self.hass)
            client = WeightGurusApiClient(email, password, session)
            await client.async_get_data()
            return True
        except Exception:  # pylint: disable=broad-except
            pass
        return False


@dataclass
class WeightGurusOptionsFlowHandler(config_entries.OptionsFlow):
    """Handle a option flow."""

    config_entry: ConfigEntry

    def __post_init__(self) -> None:
        """Initialize."""
        self.options = dict(self.config_entry.options)

    async def async_step_init(
        self, user_input: dict | None = None
    ) -> FlowResult:  # pylint: disable=unused-argument
        """Handle options flow."""
        return await self.async_step_user()

    async def async_step_user(self, user_input: dict | None = None) -> FlowResult:
        """Handle a flow initiated by the user."""
        if user_input is not None:
            self.options.update(user_input)
            return await self._update_options()

        return self.async_show_form(
            step_id="user",
            data_schema=vol.Schema(
                {
                    vol.Required(x, default=self.options.get(x, True)): bool
                    for x in sorted(PLATFORMS)
                }
            ),
        )

    async def _update_options(self) -> FlowResult:
        """Update options."""
        return self.async_create_entry(
            title=self.config_entry.data.get(CONF_EMAIL), data=self.options
        )

"""weight_gurus configuration flow."""
from __future__ import annotations

from dataclasses import dataclass
from typing import Dict

import voluptuous as vol
from homeassistant import config_entries
from homeassistant.config_entries import ConfigEntry, OptionsFlow
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.aiohttp_client import async_create_clientsession

from .api import WeightGurusApiClient
from .const import DOMAIN, PLATFORMS


class WeightGurusFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
    """Handle a config flow."""

    VERSION = 1
    CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL

    def __init__(self) -> None:
        """Initialize."""
        self._errors: dict = {}

    async def async_step_user(
        self, user_input: Dict[str, str] | None = None
    ) -> FlowResult:
        """Handle a flow initiated by the user."""
        self._errors = {}

        if user_input is not None:
            valid = await self._test_credentials(
                user_input[CONF_EMAIL], user_input[CONF_PASSWORD]
            )
            if valid:
                return self.async_create_entry(
                    title=user_input[CONF_EMAIL], data=user_input
                )
            else:
                self._errors["base"] = "auth"

            return await self._show_config_form(user_input)

        user_input = {}
        # Provide defaults for form
        user_input[CONF_EMAIL] = ""
        user_input[CONF_PASSWORD] = ""

        return await self._show_config_form(user_input)

    @staticmethod
    @callback
    def async_get_options_flow(config_entry: ConfigEntry) -> OptionsFlow:
        return WeightGurusOptionsFlowHandler(config_entry)

    async def _show_config_form(
        self, user_input: Dict[str, str] | None = None
    ) -> OptionsFlow:  # pylint: disable=unused-argument
        """Show the configuration form to edit location data."""
        return self.async_show_form(
            step_id="user",
            data_schema=vol.Schema(
                {
                    vol.Required(CONF_EMAIL, default=user_input[CONF_EMAIL]): str,
                    vol.Required(CONF_PASSWORD, default=user_input[CONF_PASSWORD]): str,
                }
            ),
            errors=self._errors,
        )

    async def _test_credentials(self, email: str, password: str) -> bool:
        """Test if the credentials are valid."""
        try:
            session = async_create_clientsession(self.hass)
            client = WeightGurusApiClient(email, password, session)
            await client.async_get_data()
            return True
        except Exception:  # pylint: disable=broad-except
            pass
        return False


@dataclass
class WeightGurusOptionsFlowHandler(config_entries.OptionsFlow):
    """Handle a option flow."""

    config_entry: ConfigEntry

    def __post_init__(self) -> None:
        """Initialize."""
        self.options = dict(self.config_entry.options)

    async def async_step_init(
        self, user_input: dict | None = None
    ) -> FlowResult:  # pylint: disable=unused-argument
        """Handle options flow."""
        return await self.async_step_user()

    async def async_step_user(self, user_input: dict | None = None) -> FlowResult:
        """Handle a flow initiated by the user."""
        if user_input is not None:
            self.options.update(user_input)
            return await self._update_options()

        return self.async_show_form(
            step_id="user",
            data_schema=vol.Schema(
                {
                    vol.Required(x, default=self.options.get(x, True)): bool
                    for x in sorted(PLATFORMS)
                }
            ),
        )

    async def _update_options(self) -> FlowResult:
        """Update options."""
        return self.async_create_entry(
            title=self.config_entry.data.get(CONF_EMAIL), data=self.options
        )
weight_gurus configuration flow. Handle a config flow. Initialize. Handle a flow initiated by the user. # Provide defaults for form # pylint: disable=unused-argument Show the configuration form to edit location data. Test if the credentials are valid. # pylint: disable=broad-except Handle a option flow. Initialize. # pylint: disable=unused-argument Handle options flow. Handle a flow initiated by the user. Update options.