Skip to content

Commit

Permalink
Merge pull request #567 from WillCodeForCats/config-flow-reconfigure
Browse files Browse the repository at this point in the history
Add support for config flow reconfigure
  • Loading branch information
WillCodeForCats committed Jun 15, 2024
2 parents 32096ad + d0e3e3b commit 3ecb5e3
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ After rebooting Home Assistant, this integration can be configured through the i
[WillCodeForCats/solaredge-modbus-multi/wiki](https://github.com/WillCodeForCats/solaredge-modbus-multi/wiki)

### Required Versions
* Home Assistant 2024.3.2 or newer
* Home Assistant 2024.4.0 or newer
* Python 3.11 or newer
* pymodbus 3.6.6 or newer

Expand Down
90 changes: 86 additions & 4 deletions custom_components/solaredge_modbus_multi/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from __future__ import annotations

from typing import Any

import homeassistant.helpers.config_validation as cv
import voluptuous as vol
from homeassistant import config_entries
Expand Down Expand Up @@ -35,7 +37,9 @@ def async_get_options_flow(config_entry: ConfigEntry) -> OptionsFlow:
"""Create the options flow for SolarEdge Modbus Multi."""
return SolaredgeModbusMultiOptionsFlowHandler(config_entry)

async def async_step_user(self, user_input=None) -> FlowResult:
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle the initial config flow step."""
errors = {}

Expand Down Expand Up @@ -99,6 +103,78 @@ async def async_step_user(self, user_input=None) -> FlowResult:
errors=errors,
)

async def async_step_reconfigure(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle the reconfigure flow step."""
errors = {}
config_entry = self.hass.config_entries.async_get_entry(
self.context["entry_id"]
)
assert config_entry
unique_id = config_entry.unique_id

if user_input is not None:
user_input[CONF_HOST] = user_input[CONF_HOST].lower()

if not host_valid(user_input[CONF_HOST]):
errors[CONF_HOST] = "invalid_host"
elif user_input[CONF_PORT] < 1:
errors[CONF_PORT] = "invalid_tcp_port"
elif user_input[CONF_PORT] > 65535:
errors[CONF_PORT] = "invalid_tcp_port"
elif user_input[ConfName.DEVICE_ID] > 247:
errors[ConfName.DEVICE_ID] = "max_device_id"
elif user_input[ConfName.DEVICE_ID] < 1:
errors[ConfName.DEVICE_ID] = "min_device_id"
elif user_input[ConfName.NUMBER_INVERTERS] > 32:
errors[ConfName.NUMBER_INVERTERS] = "max_inverters"
elif user_input[ConfName.NUMBER_INVERTERS] < 1:
errors[ConfName.NUMBER_INVERTERS] = "min_inverters"
elif (
user_input[ConfName.NUMBER_INVERTERS] + user_input[ConfName.DEVICE_ID]
> 247
):
errors[ConfName.NUMBER_INVERTERS] = "too_many_inverters"
else:
return self.async_update_reload_and_abort(
config_entry,
unique_id=unique_id,
data={**config_entry.data, **user_input},
reason="reconfigure_successful",
)
else:
user_input = {
CONF_HOST: config_entry.data.get(CONF_HOST),
CONF_PORT: config_entry.data.get(CONF_PORT, ConfDefaultInt.PORT),
ConfName.NUMBER_INVERTERS: config_entry.data.get(
ConfName.NUMBER_INVERTERS, ConfDefaultInt.NUMBER_INVERTERS
),
ConfName.DEVICE_ID: config_entry.data.get(
ConfName.DEVICE_ID, ConfDefaultInt.DEVICE_ID
),
}

return self.async_show_form(
step_id="reconfigure",
data_schema=vol.Schema(
{
vol.Required(CONF_HOST, default=user_input[CONF_HOST]): cv.string,
vol.Required(CONF_PORT, default=user_input[CONF_PORT]): vol.Coerce(
int
),
vol.Required(
f"{ConfName.NUMBER_INVERTERS}",
default=user_input[ConfName.NUMBER_INVERTERS],
): vol.Coerce(int),
vol.Required(
f"{ConfName.DEVICE_ID}", default=user_input[ConfName.DEVICE_ID]
): vol.Coerce(int),
},
),
errors=errors,
)


class SolaredgeModbusMultiOptionsFlowHandler(OptionsFlow):
"""Handle an options flow for SolarEdge Modbus Multi."""
Expand All @@ -107,7 +183,9 @@ def __init__(self, config_entry: ConfigEntry):
"""Initialize options flow."""
self.config_entry = config_entry

async def async_step_init(self, user_input=None) -> FlowResult:
async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle the initial options flow step."""
errors = {}

Expand Down Expand Up @@ -194,7 +272,9 @@ async def async_step_init(self, user_input=None) -> FlowResult:
errors=errors,
)

async def async_step_battery_options(self, user_input=None) -> FlowResult:
async def async_step_battery_options(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Battery Options"""
errors = {}

Expand Down Expand Up @@ -249,7 +329,9 @@ async def async_step_battery_options(self, user_input=None) -> FlowResult:
errors=errors,
)

async def async_step_adv_pwr_ctl(self, user_input=None) -> FlowResult:
async def async_step_adv_pwr_ctl(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Power Control Options"""
errors = {}

Expand Down
12 changes: 11 additions & 1 deletion custom_components/solaredge_modbus_multi/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
"device_id": "Inverter Modbus Address (Device ID)",
"number_of_inverters": "Number of Inverters"
}
},
"reconfigure": {
"title": "SolarEdge Modbus Configuration",
"data": {
"host": "Inverter IP Address",
"port": "Modbus/TCP Port",
"device_id": "Inverter Modbus Address (Device ID)",
"number_of_inverters": "Number of Inverters"
}
}
},
"error": {
Expand All @@ -23,7 +32,8 @@
"invalid_tcp_port": "Valid port range is 1 to 65535."
},
"abort": {
"already_configured": "Device is already configured!"
"already_configured": "Device is already configured",
"reconfigure_successful": "Re-configuration was successful"
}
},
"options": {
Expand Down
12 changes: 11 additions & 1 deletion custom_components/solaredge_modbus_multi/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
"device_id": "Wechselrichter-Modbus-Adresse (Geräte-ID)",
"number_of_inverters": "Anzahl Wechselrichter"
}
},
"reconfigure": {
"title": "SolarEdge Modbus-Konfiguration",
"data": {
"host": "Wechselrichter-IP-Adresse",
"port": "Modbus/TCP-Port",
"device_id": "Wechselrichter-Modbus-Adresse (Geräte-ID)",
"number_of_inverters": "Anzahl Wechselrichter"
}
}
},
"error": {
Expand All @@ -23,7 +32,8 @@
"invalid_tcp_port": "Der gültige Portbereich ist 1 bis 65535."
},
"abort": {
"already_configured": "Der Wechselrichter ist bereits konfiguriert."
"already_configured": "Gerät ist bereits konfiguriert",
"reconfigure_successful": "Die Neukonfiguration war erfolgreich"
}
},
"options": {
Expand Down
12 changes: 11 additions & 1 deletion custom_components/solaredge_modbus_multi/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
"device_id": "Inverter Modbus Address (Device ID)",
"number_of_inverters": "Number of Inverters"
}
},
"reconfigure": {
"title": "SolarEdge Modbus Configuration",
"data": {
"host": "Inverter IP Address",
"port": "Modbus/TCP Port",
"device_id": "Inverter Modbus Address (Device ID)",
"number_of_inverters": "Number of Inverters"
}
}
},
"error": {
Expand All @@ -23,7 +32,8 @@
"invalid_tcp_port": "Valid port range is 1 to 65535."
},
"abort": {
"already_configured": "Device is already configured!"
"already_configured": "Device is already configured",
"reconfigure_successful": "Re-configuration was successful"
}
},
"options": {
Expand Down
12 changes: 11 additions & 1 deletion custom_components/solaredge_modbus_multi/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
"device_id": "L'adresse Modbus de l'onduleur (Device ID)",
"number_of_inverters": "Nombre d'onduleurs"
}
},
"reconfigure": {
"title": "Configuration SolarEdge Modbus",
"data": {
"host": "Adresse IP de l'onduleur",
"port": "Port Modbus/TCP",
"device_id": "L'adresse Modbus de l'onduleur (Device ID)",
"number_of_inverters": "Nombre d'onduleurs"
}
}
},
"error": {
Expand All @@ -23,7 +32,8 @@
"invalid_tcp_port": "La plage de ports valide est comprise entre 1 et 65535."
},
"abort": {
"already_configured": "L'appareil est déjà configuré!"
"already_configured": "L'appareil est déjà configuré",
"reconfigure_successful": "La reconfiguration a réussi"
}
},
"options": {
Expand Down
12 changes: 11 additions & 1 deletion custom_components/solaredge_modbus_multi/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
"device_id": "Indirizzo Modbus dell'inverter (ID dispositivo)",
"number_of_inverters": "Numero di inverter"
}
},
"reconfigure": {
"title": "Configurazione Modbus SolarEdge",
"data": {
"host": "Indirizzo IP dell'inverter",
"port": "Porta Modbus/TCP",
"device_id": "Indirizzo Modbus dell'inverter (ID dispositivo)",
"number_of_inverters": "Numero di inverter"
}
}
},
"error": {
Expand All @@ -23,7 +32,8 @@
"invalid_tcp_port": "L'intervallo di porte valido è compreso tra 1 e 65535."
},
"abort": {
"already_configured": "Il dispositivo è già configurato!"
"already_configured": "Il dispositivo è già configurato",
"reconfigure_successful": "La riconfigurazione ha avuto successo"
}
},
"options": {
Expand Down
12 changes: 11 additions & 1 deletion custom_components/solaredge_modbus_multi/translations/nb.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
"device_id": "Inverter Modbus-adresse (enhets-ID)",
"number_of_inverters": "Antall omformere koblet sammen"
}
},
"reconfigure": {
"title": "SolarEdge Modbus-konfigurasjon",
"data": {
"host": "IP-adres van omvormer",
"port": "Modbus/TCP-poort",
"device_id": "Inverter Modbus-adresse (enhets-ID)",
"number_of_inverters": "Antall omformere koblet sammen"
}
}
},
"error": {
Expand All @@ -23,7 +32,8 @@
"invalid_tcp_port": "Gyldig portområde er 1 til 65535."
},
"abort": {
"already_configured": "Enheten er allerede konfigurert"
"already_configured": "Enheten er allerede konfigurert",
"reconfigure_successful": "Omkonfigureringen var vellykket"
}
},
"options": {
Expand Down
12 changes: 11 additions & 1 deletion custom_components/solaredge_modbus_multi/translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
"device_id": "Omvormer Modbus-adres (apparaat-ID)",
"number_of_inverters": "Aantal aangesloten omvormers"
}
},
"reconfigure": {
"title": "SolarEdge Modbus-configuratie",
"data": {
"host": "omvormer IP-adres",
"port": "Modbus/TCP Port",
"device_id": "Omvormer Modbus-adres (apparaat-ID)",
"number_of_inverters": "Aantal aangesloten omvormers"
}
}
},
"error": {
Expand All @@ -23,7 +32,8 @@
"invalid_tcp_port": "Geldig poortbereik is 1 tot 65535."
},
"abort": {
"already_configured": "Apparaat is al geconfigureerd"
"already_configured": "Apparaat is al geconfigureerd",
"reconfigure_successful": "De herconfiguratie was succesvol"
}
},
"options": {
Expand Down
12 changes: 11 additions & 1 deletion custom_components/solaredge_modbus_multi/translations/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
"device_id": "Adres Modbus Inwertera (Device ID)",
"number_of_inverters": "Ilość inwerterów"
}
},
"reconfigure": {
"title": "Konfiguracja SolarEdge Modbus",
"data": {
"host": "Adres IP inwertera",
"port": "Modbus/TCP Port",
"device_id": "Adres Modbus Inwertera (Device ID)",
"number_of_inverters": "Ilość inwerterów"
}
}
},
"error": {
Expand All @@ -23,7 +32,8 @@
"invalid_tcp_port": "Dozwolony zakres portów to od 1 do 65535."
},
"abort": {
"already_configured": "Urządzenie jest już skonfigurowane!"
"already_configured": "Urządzenie jest już skonfigurowane",
"reconfigure_successful": "Ponowna konfiguracja przebiegła pomyślnie"
}
},
"options": {
Expand Down
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "SolarEdge Modbus Multi",
"content_in_root": false,
"homeassistant": "2024.3.2",
"homeassistant": "2024.4.0",
"render_readme": false
}
2 changes: 1 addition & 1 deletion info.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ Read more on the wiki: [WillCodeForCats/solaredge-modbus-multi/wiki](https://git
* Supports status and error reporting sensors.
* User friendly configuration through Config Flow.

Requires Home Assistant 2024.3.2 and newer with pymodbus 3.6.6 and newer.
Requires Home Assistant 2024.4.0 and newer with pymodbus 3.6.6 and newer.

0 comments on commit 3ecb5e3

Please sign in to comment.