hass-core/homeassistant/components/blue_current/config_flow.py
Floris272 8b0d19aca2
Add bluecurrent integration (#82483)
* Add bluecurrent integration

* Apply feedback

* Rename integration

* changed constants and removed strings.sensor.json

* update blue_current integration

* update bluecurrent-api to 1.0.4

* Update bluecurrent-api to 1.0.5

* Apply feedback

* Remove translation

* Apply feedback

* Use customer_id as unique id

* Apply feedback

* Add @pytest.mark.parametrize

* Replace loop.create_task with async_create_task
2023-12-22 16:34:16 +01:00

61 lines
1.9 KiB
Python

"""Config flow for Blue Current integration."""
from __future__ import annotations
from typing import Any
from bluecurrent_api import Client
from bluecurrent_api.exceptions import (
AlreadyConnected,
InvalidApiToken,
RequestLimitReached,
WebsocketError,
)
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import CONF_API_TOKEN
from homeassistant.data_entry_flow import FlowResult
from .const import DOMAIN, LOGGER
DATA_SCHEMA = vol.Schema({vol.Required(CONF_API_TOKEN): str})
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle the config flow for Blue Current."""
VERSION = 1
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle the initial step."""
errors = {}
if user_input is not None:
client = Client()
api_token = user_input[CONF_API_TOKEN]
try:
customer_id = await client.validate_api_token(api_token)
email = await client.get_email()
except WebsocketError:
errors["base"] = "cannot_connect"
except RequestLimitReached:
errors["base"] = "limit_reached"
except AlreadyConnected:
errors["base"] = "already_connected"
except InvalidApiToken:
errors["base"] = "invalid_token"
except Exception: # pylint: disable=broad-except
LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
else:
await self.async_set_unique_id(customer_id)
self._abort_if_unique_id_configured()
return self.async_create_entry(title=email, data=user_input)
return self.async_show_form(
step_id="user", data_schema=DATA_SCHEMA, errors=errors
)