-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Expand file tree
/
Copy pathconfig_flow.py
More file actions
92 lines (72 loc) · 2.97 KB
/
config_flow.py
File metadata and controls
92 lines (72 loc) · 2.97 KB
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
"""Config flow for the guntamatic integration."""
from __future__ import annotations
import logging
from typing import Any
from guntamatic.heater import Heater, NoSerialException
import requests
import voluptuous as vol
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_HOST
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
STEP_USER_DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_HOST): str,
}
)
class GuntamaticConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a config flow for guntamatic."""
_discovered_ip: str
async def async_step_dhcp(
self, discovery_info: DhcpServiceInfo
) -> ConfigFlowResult:
"""Handle DHCP discovery."""
heater = Heater(discovery_info.ip)
try:
data = await self.hass.async_add_executor_job(heater.parse_data)
except requests.exceptions.RequestException:
return self.async_abort(reason="cannot_connect")
except NoSerialException:
return self.async_abort(reason="bad_data")
await self.async_set_unique_id(data["serial"][0])
self._abort_if_unique_id_configured(updates={CONF_HOST: discovery_info.ip})
self._discovered_ip = discovery_info.ip
return await self.async_step_discovery_confirm()
async def async_step_discovery_confirm(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Confirm discovery."""
if user_input is not None:
return self.async_create_entry(
title="Guntamatic Heater",
data={CONF_HOST: self._discovered_ip},
)
self._set_confirm_only()
return self.async_show_form(step_id="discovery_confirm")
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle the initial step."""
errors: dict[str, str] = {}
if user_input is not None:
try:
heater = Heater(user_input[CONF_HOST])
data = await self.hass.async_add_executor_job(heater.parse_data)
except requests.exceptions.RequestException:
errors["base"] = "cannot_connect"
except NoSerialException:
errors["base"] = "bad_data"
except Exception:
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
else:
# set serial as unique id for deduplication, ip isn't a good match
await self.async_set_unique_id(data["serial"][0])
self._abort_if_unique_id_configured()
return self.async_create_entry(
title="Guntamatic Heater", data=user_input
)
return self.async_show_form(
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
)