Skip to content

Commit

Permalink
Add reconfigure flow
Browse files Browse the repository at this point in the history
Fixes #689
  • Loading branch information
wlcrs committed Jun 15, 2024
1 parent 469cc2e commit 42b0164
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
37 changes: 23 additions & 14 deletions config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,18 +206,25 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
# Only used in reconfigure flows:
_reconfigure_entry: config_entries.ConfigEntry | None = None

# Only used for async_step_network_login
_inverter_info: dict[str, Any] | None = None

VERSION = 1

async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Step when user initializes a integration."""
return self.async_step_setup_connection_type()
return await self.async_step_setup_connection_type()

def _update_config_data_from_entry_data(self, entry_data: dict[str, Any]):
self._host = entry_data.get(CONF_HOST)
self._port = entry_data.get(CONF_PORT)
self._slave_ids = entry_data.get(CONF_SLAVE_IDS)

slave_ids = entry_data.get(CONF_SLAVE_IDS)
if not isinstance(slave_ids, list):
slave_ids = [slave_ids]
self._slave_ids = ",".join(map(str, slave_ids))

self._username = entry_data.get(CONF_USERNAME)
self._password = entry_data.get(CONF_PASSWORD)
Expand All @@ -232,8 +239,8 @@ async def async_step_reconfigure(self, user_input: dict[str, Any] | None = None)
self.context["entry_id"]
)
self._update_config_data_from_entry_data(self._reconfigure_entry.data)

return self.async_step_setup_connection_type()
await self.hass.config_entries.async_unload(self.context["entry_id"])
return await self.async_step_setup_connection_type()

async def async_step_reauth(
self, config: dict[str, Any] | None = None
Expand Down Expand Up @@ -330,7 +337,7 @@ async def async_step_setup_serial(
vol.Required(CONF_PORT, default=self._port): vol.In(list_of_ports),
vol.Required(
CONF_SLAVE_IDS,
default=",".join(self._slave_ids)
default=",".join(map(str, self._slave_ids))
if self._slave_ids
else str(DEFAULT_SERIAL_SLAVE_ID),
): str,
Expand Down Expand Up @@ -374,7 +381,7 @@ async def async_step_setup_serial_manual_path(
vol.Required(CONF_PORT, default=self._port): str,
vol.Required(
CONF_SLAVE_IDS,
default=",".join(self._slave_ids)
default=",".join(map(str, self._slave_ids))
if self._slave_ids
else str(DEFAULT_SERIAL_SLAVE_ID),
): str,
Expand Down Expand Up @@ -404,6 +411,7 @@ async def async_step_setup_network(
host=self._host,
port=self._port,
slave_ids=self._slave_ids,
elevated_permissions=self._elevated_permissions,
)

except ConnectionException:
Expand All @@ -429,14 +437,15 @@ async def async_step_setup_network(
self.context["title_placeholders"] = {
"name": info["model_name"]
}
self._inverter_info = info
return await self.async_step_network_login()

# In case of a reconfigure, the user can have unchecked the elevated permissions checkbox
self._username = None
self._password = None

# Otherwise, we can directly create the device entry!
return await self._create_or_update_entry()
return await self._create_or_update_entry(info)

return self.async_show_form(
step_id="setup_network",
Expand All @@ -448,7 +457,7 @@ async def async_step_setup_network(
): cv.port,
vol.Required(
CONF_SLAVE_IDS,
default=",".join(self._slave_ids)
default=",".join(map(str, self._slave_ids))
if self._slave_ids
else str(DEFAULT_SLAVE_ID),
): str,
Expand All @@ -468,6 +477,7 @@ async def async_step_network_login(
assert self._host is not None
assert self._port is not None
assert self._slave_ids is not None
assert self._inverter_info is not None

errors = {}

Expand All @@ -484,7 +494,7 @@ async def async_step_network_login(
password=self._password,
)
if login_success:
return await self._create_or_update_entry()
return await self._create_or_update_entry(self._inverter_info)

errors["base"] = "invalid_auth"
except ConnectionException:
Expand Down Expand Up @@ -515,10 +525,9 @@ async def async_step_network_login(
errors=errors,
)

async def _create_or_update_entry(self, inverter_info):
async def _create_or_update_entry(self, inverter_info: dict[str, Any] | None):
"""Create the entry, or update the existing one if present."""

self.context["title_placeholders"] = {"name": inverter_info["model_name"]}
data = {
CONF_HOST: self._host,
CONF_PORT: self._port,
Expand All @@ -533,6 +542,8 @@ async def _create_or_update_entry(self, inverter_info):
await self.hass.config_entries.async_reload(self._reauth_entry.entry_id)
return self.async_abort(reason="reauth_successful")

assert inverter_info
self.context["title_placeholders"] = {"name": inverter_info["model_name"]}
if self._reconfigure_entry:
self.hass.config_entries.async_update_entry(
self._reconfigure_entry, data=data
Expand All @@ -545,9 +556,7 @@ async def _create_or_update_entry(self, inverter_info):
await self.async_set_unique_id(inverter_info["serial_number"])
self._abort_if_unique_id_configured(updates=data)

return self.async_create_entry(
title=self._inverter_info["model_name"], data=data
)
return self.async_create_entry(title=inverter_info["model_name"], data=data)


class SlaveIdsParseException(Exception):
Expand Down
4 changes: 3 additions & 1 deletion strings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"config": {
"abort": {
"already_configured": "Device is already configured"
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
"reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]",
"reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]"
},
"error": {
"cannot_connect": "Failed to connect",
Expand Down
4 changes: 3 additions & 1 deletion translations/en.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"config": {
"abort": {
"already_configured": "Device is already configured"
"already_configured": "Device is already configured",
"reconfigure_successful": "Re-configuration was successful",
"reauth_successful": "Re-authentication was successful"
},
"error": {
"cannot_connect": "Failed to connect",
Expand Down

0 comments on commit 42b0164

Please sign in to comment.