Coverage for custom_components/bouncie/config_flow.py: 100%
35 statements
« prev ^ index » next coverage.py v7.2.4, created at 2025-01-04 23:39 +0000
« prev ^ index » next coverage.py v7.2.4, created at 2025-01-04 23:39 +0000
1"""Config flow for bouncie integration."""
2from __future__ import annotations
4from typing import Any
6from bounciepy import AsyncRESTAPIClient
7from homeassistant import config_entries
8from homeassistant.const import CONF_CLIENT_ID, CONF_CLIENT_SECRET, CONF_SCAN_INTERVAL
9from homeassistant.core import HomeAssistant
10from homeassistant.data_entry_flow import FlowResult
11from homeassistant.exceptions import HomeAssistantError
12from homeassistant.helpers.aiohttp_client import async_get_clientsession
13import voluptuous as vol
15from .const import CONF_CODE, CONF_REDIRECT_URI, DOMAIN, LOGGER
17STEP_USER_DATA_SCHEMA = vol.Schema(
18 {
19 vol.Required(CONF_CLIENT_ID): str,
20 vol.Required(CONF_CLIENT_SECRET): str,
21 vol.Required(CONF_REDIRECT_URI): str,
22 vol.Required(CONF_CODE): str,
23 vol.Optional(CONF_SCAN_INTERVAL, default=10): int,
24 }
25)
28async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, Any]:
29 """Validate the user input allows us to connect.
31 Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
32 """
33 session = async_get_clientsession(hass=hass)
34 bouncie_client = AsyncRESTAPIClient(
35 client_id=data[CONF_CLIENT_ID],
36 client_secret=data[CONF_CLIENT_SECRET],
37 redirect_url=data[CONF_REDIRECT_URI],
38 auth_code=data[CONF_CODE],
39 session=session,
40 )
41 result = await bouncie_client.get_access_token()
42 if not result:
43 raise InvalidAuth
44 return data
47class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
48 """Handle a config flow for bouncie."""
50 VERSION = 1
52 async def async_step_user(
53 self, user_input: dict[str, Any] | None = None
54 ) -> FlowResult:
55 """Handle the initial step."""
56 if user_input is None:
57 return self.async_show_form(
58 step_id="user", data_schema=STEP_USER_DATA_SCHEMA
59 )
61 errors = {}
63 try:
64 info = await validate_input(self.hass, user_input)
65 except InvalidAuth:
66 errors["base"] = "invalid_auth"
67 except Exception as ex: # pylint: disable=broad-except
68 LOGGER.exception(ex)
69 errors["base"] = "unknown"
70 else:
71 return self.async_create_entry(title="Bouncie", data=info)
73 return self.async_show_form(
74 step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
75 )
78class InvalidAuth(HomeAssistantError):
79 """Error to indicate there is invalid auth."""