-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
config_flow.py
135 lines (104 loc) 路 4.15 KB
/
config_flow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""Sure Petcare config flow."""
from __future__ import annotations
import logging
from typing import Any
from homeassistant import config_entries, core, data_entry_flow
from homeassistant.const import CONF_PASSWORD, CONF_TOKEN, CONF_USERNAME
from homeassistant.core import callback
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from surepy import Surepy
from surepy.exceptions import SurePetcareAuthenticationError, SurePetcareError
import voluptuous as vol
# pylint: disable=relative-beyond-top-level
from .const import (
ATTR_VOLTAGE_FULL,
ATTR_VOLTAGE_LOW,
DOMAIN,
SURE_API_TIMEOUT,
SURE_BATT_VOLTAGE_FULL,
SURE_BATT_VOLTAGE_LOW,
)
_LOGGER = logging.getLogger(__name__)
DATA_SCHEMA = vol.Schema(
{vol.Required(CONF_USERNAME): str, vol.Required(CONF_PASSWORD): str}
)
async def is_valid(hass: core.HomeAssistant, user_input: dict[str, Any]) -> str | None:
"""Check if we can log in with the supplied credentials."""
try:
surepy = Surepy(
user_input[CONF_USERNAME],
user_input[CONF_PASSWORD],
auth_token=None,
api_timeout=SURE_API_TIMEOUT,
session=async_get_clientsession(hass),
)
return await surepy.sac.get_token()
except SurePetcareAuthenticationError:
_LOGGER.error("Unable to connect to surepetcare.io: Wrong credentials!")
return None
except SurePetcareError as error:
_LOGGER.error("Unable to connect to surepetcare.io: %s", error)
return None
class SurePetcareConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): # type: ignore
"""Implementation of the Sure Petcare config flow."""
VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_CLOUD_POLL
@staticmethod
@callback
def async_get_options_flow(config_entry):
"""Get the options flow for this handler."""
return SureHAOptionsFlowHandler(config_entry)
async def async_step_import(
self, import_info: dict[str, Any]
) -> data_entry_flow.FlowResult:
"""Set up entry from configuration.yaml file."""
return await self.async_step_user(import_info)
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> data_entry_flow.FlowResult:
"""Handle a flow start."""
errors: dict[str, Any] = {}
if not user_input:
data_schema = {
vol.Required("username"): str,
vol.Required("password"): str,
}
return self.async_show_form(
step_id="user", data_schema=vol.Schema(data_schema), errors=errors
)
if token := await is_valid(self.hass, user_input):
uniq_username = user_input[CONF_USERNAME].casefold()
await self.async_set_unique_id(uniq_username, raise_on_progress=False)
return self.async_create_entry(
title="Sure Petcare",
data={
CONF_USERNAME: user_input[CONF_USERNAME],
CONF_PASSWORD: user_input[CONF_PASSWORD],
CONF_TOKEN: token,
},
)
return self.async_abort(reason="authentication_failed")
class SureHAOptionsFlowHandler(config_entries.OptionsFlow):
"""Handle SureHA options."""
def __init__(self, config_entry):
"""Initialize SureHA options flow."""
self.config_entry = config_entry
async def async_step_init(self, user_input=None):
"""Manage the SureHA options."""
if user_input is not None:
return self.async_create_entry(title="SureHA Options", data=user_input)
options = {
vol.Optional(
ATTR_VOLTAGE_LOW,
default=self.config_entry.options.get(
ATTR_VOLTAGE_LOW, SURE_BATT_VOLTAGE_LOW
),
): float,
vol.Optional(
ATTR_VOLTAGE_FULL,
default=self.config_entry.options.get(
ATTR_VOLTAGE_FULL, SURE_BATT_VOLTAGE_FULL
),
): float,
}
return self.async_show_form(step_id="init", data_schema=vol.Schema(options))