/
config_flow.py
132 lines (113 loc) · 4.47 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
import logging
from pymodbus import ModbusException
import pymodbus.client as ModbusClient
from pymodbus.exceptions import ConnectionException
import voluptuous as vol
from voluptuous.schema_builder import Schema
from homeassistant import config_entries
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.typing import ConfigType
from .const import CONF_HOST, CONF_NAME, CONF_PORT, DOMAIN
_LOGGER = logging.getLogger(__name__)
def get_host_schema_config(data: ConfigType) -> Schema:
return vol.Schema(
{
vol.Required(CONF_NAME, default="Solvis Heizung"): str,
vol.Required(CONF_HOST, default=data.get(CONF_HOST)): str,
vol.Required(CONF_PORT, default=502): int,
}
)
def get_host_schema_options(data: ConfigType) -> Schema:
return vol.Schema(
{
vol.Required(CONF_HOST, default=data.get(CONF_HOST)): str,
vol.Required(CONF_PORT, default=data.get(CONF_PORT)): int,
}
)
class SolvisConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
VERSION = 1
MINOR_VERSION = 1
def __init__(self) -> None:
"""Init the ConfigFlow."""
self.data: ConfigType = {}
self.client = None
# @staticmethod
# @callback
# def async_get_options_flow(
# entry: config_entries.ConfigEntry,
# ) -> config_entries.OptionsFlow:
# """Get the options flow for this handler."""
# _LOGGER.debug(f'config_flow.py:ConfigFlow.async_get_options_flow: {entry}')
# return SolvisOptionsFlow(entry)
async def async_step_user(
self, user_input: dict[str, str, int] | None = None
) -> FlowResult:
"""Handle the initial step."""
if user_input is None:
return self.async_show_form(
step_id="user", data_schema=get_host_schema_config(self.data)
)
self.data = user_input
errors = {}
try:
self.client = ModbusClient.AsyncModbusTcpClient(
user_input[CONF_HOST], user_input[CONF_PORT]
)
await self.client.connect()
except ConnectionException:
errors["base"] = "Es konnte keine Verbinung aufgebaut werden"
else:
try:
await self.client.read_coils(32770, 3, slave=1)
except ModbusException as exc:
_LOGGER.debug(f"Received ModbusException({exc}) from library")
else:
await self.client.close()
return self.async_create_entry(
title=self.data[CONF_NAME], data=self.data
)
errors["base"] = "cannot_connect"
return self.async_show_form(
step_id="user", data_schema=get_host_schema_config(self.data), errors=errors
)
# class SolvisOptionsFlow(config_entries.OptionsFlow, domain=DOMAIN):
# # The schema version of the entries that it creates
# # Home Assistant will call your migrate method if the version changes
# VERSION = 1
# MINOR_VERSION = 1
# def __init__(self, config) -> None:
# """Init the ConfigFlow."""
# self.data: ConfigType = config
# self.client = None
# async def async_step_init(
# self, user_input: dict[str, int] | None = None
# ) -> FlowResult:
# """Handle the initial step."""
# if user_input is None:
# return self.async_show_form(
# step_id="user", data_schema=get_host_schema_options(self.data)
# )
# self.data = user_input
# errors = {}
# try:
# self.client = ModbusClient.AsyncModbusTcpClient(
# user_input[CONF_HOST], user_input[CONF_PORT]
# )
# await self.client.connect()
# except ConnectionException:
# errors["base"] = "Es konnte keine Verbinung aufgebaut werden"
# else:
# try:
# rr = await self.client.read_coils(32770, 3, slave=1)
# except ModbusException as exc:
# print(f"Received ModbusException({exc}) from library")
# finally:
# await self.client.close()
# return self.async_create_entry(
# title=self.data[CONF_NAME], data=self.data
# )
# errors["base"] = "cannot_connect"
# return self.async_show_form(
# step_id="init", data_schema=self.data_schema, errors=errors
# )