Skip to content

Commit

Permalink
Enable OptionFlow
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsK1 committed Mar 31, 2024
1 parent 82f5d22 commit 0fe650f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 62 deletions.
51 changes: 13 additions & 38 deletions custom_components/solvis_control/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,6 @@ def __init__(self) -> None:
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:
Expand All @@ -66,11 +57,13 @@ async def async_step_user(
await self.async_set_unique_id(self.data[CONF_HOST], raise_on_progress=False)
self._abort_if_unique_id_configured()

return self.async_create_entry(
title=self.data[CONF_NAME], data=self.data)
return self.async_create_entry(title=self.data[CONF_NAME], data=self.data)

@staticmethod
@callback
def async_get_options_flow(config_entry: config_entries.ConfigEntry,) -> config_entries.OptionsFlow:
def async_get_options_flow(
config_entry: config_entries.ConfigEntry,
) -> config_entries.OptionsFlow:
"""Create the options flow."""
return SolvisOptionsFlow(config_entry)

Expand All @@ -90,23 +83,24 @@ def async_get_options_flow(config_entry: config_entries.ConfigEntry,) -> config_
# _LOGGER.debug(f"Received ModbusException({exc}) from library")
# else:
# await self.client.close()

# 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):
class SolvisOptionsFlow(config_entries.OptionsFlow):
# 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.config: ConfigType = config
self.data = dict(config.data)
self.client = None

async def async_step_init(
Expand All @@ -115,29 +109,10 @@ async def async_step_init(
"""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)
step_id="init",
data_schema=get_host_schema_options(self.data),
)
self.data = user_input
return self.async_create_entry(
title=self.data[CONF_NAME], data=self.data
)
# 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()
# errors["base"] = "cannot_connect"

# return self.async_show_form(
# step_id="init", data_schema=self.data_schema, errors=errors
# )
title=self.config_entry.get(CONF_NAME), data=self.data
)
8 changes: 7 additions & 1 deletion custom_components/solvis_control/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
"host": "host",
"port": "port"
}
}
},
"init": {
"data": {
"host": "host",
"port": "port"
}
}
},
"abort": {
"already_configured": "Device is already configured"
Expand Down
16 changes: 11 additions & 5 deletions custom_components/solvis_control/translations/de.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
{
"config": {
"flow_title": "Solvis Setup",
"flow_title": "Solvis Einrichtung",
"step": {
"user": {
"data": {
"name": "name",
"host": "host",
"port": "port"
"name": "Gerätename",
"host": "Host",
"port": "Port"
}
}
},
"init": {
"data": {
"host": "Host",
"port": "Port"
}
}
},
"abort": {
"already_configured": "Device is already configured"
Expand Down
42 changes: 24 additions & 18 deletions custom_components/solvis_control/translations/en.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
{
"config": {
"flow_title": "Solvis Setup",
"step": {
"user": {
"data": {
"name": "name",
"host": "host",
"port": "port"
}
}
},
"abort": {
"already_configured": "Device is already configured"
"config": {
"flow_title": "Solvis Setup",
"step": {
"user": {
"data": {
"name": "Name",
"host": "Host",
"port": "Port"
}
},
"error": {
"cannot_connect": "Failed to connect",
"invalid_auth": "Invalid authentication",
"unknown": "Unexpected error"
}
"init": {
"data": {
"host": "Host",
"port": "Port"
}
}
},
"abort": {
"already_configured": "Device is already configured"
},
"error": {
"cannot_connect": "Failed to connect",
"invalid_auth": "Invalid authentication",
"unknown": "Unexpected error"
}
}
}

0 comments on commit 0fe650f

Please sign in to comment.