1 """Config flow for iskra integration."""
3 from __future__
import annotations
8 from pyiskra.adapters
import Modbus, RestAPI
9 from pyiskra.exceptions
import (
10 DeviceConnectionError,
15 from pyiskra.helper
import BasicInfo
16 import voluptuous
as vol
37 from .const
import DOMAIN
39 _LOGGER = logging.getLogger(__name__)
42 STEP_USER_DATA_SCHEMA = vol.Schema(
44 vol.Required(CONF_HOST): str,
47 options=[
"rest_api",
"modbus_tcp"],
48 mode=SelectSelectorMode.LIST,
49 translation_key=
"protocol",
55 STEP_AUTHENTICATION_DATA_SCHEMA = vol.Schema(
57 vol.Required(CONF_USERNAME): str,
58 vol.Required(CONF_PASSWORD): str,
63 STEP_MODBUS_TCP_DATA_SCHEMA = vol.Schema(
65 vol.Required(CONF_PORT, default=10001): vol.All(
66 vol.Coerce(int), vol.Range(min=0, max=65535)
76 """Check if the RestAPI requires authentication."""
78 rest_api = RestAPI(ip_address=host, authentication=user_input)
80 basic_info = await rest_api.get_basic_info()
81 except NotAuthorised
as e:
82 raise NotAuthorised
from e
83 except (DeviceConnectionError, DeviceTimeoutError, InvalidResponseCode)
as e:
84 raise CannotConnect
from e
85 except Exception
as e:
86 _LOGGER.error(
"Unexpected exception: %s", e)
87 raise UnknownException
from e
93 """Test the Modbus connection."""
97 port=user_input[CONF_PORT],
98 modbus_address=user_input[CONF_ADDRESS],
102 basic_info = await modbus_api.get_basic_info()
103 except NotAuthorised
as e:
104 raise NotAuthorised
from e
105 except (DeviceConnectionError, DeviceTimeoutError, InvalidResponseCode)
as e:
106 raise CannotConnect
from e
107 except Exception
as e:
108 _LOGGER.error(
"Unexpected exception: %s", e)
109 raise UnknownException
from e
115 """Handle a config flow for iskra."""
122 self, user_input: dict[str, Any] |
None =
None
123 ) -> ConfigFlowResult:
124 """Handle a flow initiated by the user."""
125 errors: dict[str, str] = {}
126 if user_input
is not None:
127 self.
hosthost = user_input[CONF_HOST]
129 if self.
protocolprotocol ==
"rest_api":
133 except CannotConnect:
134 errors[
"base"] =
"cannot_connect"
135 except NotAuthorised:
138 except UnknownException:
139 errors[
"base"] =
"unknown"
147 device_info=device_info,
148 user_input=user_input,
151 if self.
protocolprotocol ==
"modbus_tcp":
157 data_schema=STEP_USER_DATA_SCHEMA,
162 self, user_input: dict[str, Any] |
None =
None
163 ) -> ConfigFlowResult:
164 """Handle the authentication step."""
165 errors: dict[str, str] = {}
166 if user_input
is not None:
170 except CannotConnect:
171 errors[
"base"] =
"cannot_connect"
173 except NotAuthorised:
174 errors[
"base"] =
"invalid_auth"
175 except UnknownException:
176 errors[
"base"] =
"unknown"
183 device_info=device_info,
184 user_input=user_input,
189 step_id=
"authentication",
190 data_schema=STEP_AUTHENTICATION_DATA_SCHEMA,
195 self, user_input: dict[str, Any] |
None =
None
196 ) -> ConfigFlowResult:
197 """Handle the Modbus TCP step."""
198 errors: dict[str, str] = {}
201 if user_input
is not None:
203 user_input[CONF_ADDRESS] =
int(user_input[CONF_ADDRESS])
209 except CannotConnect:
210 errors[
"base"] =
"cannot_connect"
211 except UnknownException:
212 errors[
"base"] =
"unknown"
219 device_info=device_info,
220 user_input=user_input,
225 step_id=
"modbus_tcp",
226 data_schema=STEP_MODBUS_TCP_DATA_SCHEMA,
234 device_info: BasicInfo,
235 user_input: dict[str, Any],
236 ) -> ConfigFlowResult:
237 """Create the config entry."""
243 title=device_info.model,
244 data={CONF_HOST: host, CONF_PROTOCOL: protocol, **user_input},
249 """Error to indicate we cannot connect."""
253 """Error to indicate an unknown exception occurred."""
ConfigFlowResult async_step_authentication(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult _create_entry(self, str host, str protocol, BasicInfo device_info, dict[str, Any] user_input)
ConfigFlowResult async_step_modbus_tcp(self, dict[str, Any]|None user_input=None)
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
ConfigEntry|None async_set_unique_id(self, str|None unique_id=None, *bool raise_on_progress=True)
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
BasicInfo test_modbus_connection(str host, dict[str, Any] user_input)
BasicInfo test_rest_api_connection(str host, dict[str, Any] user_input)