Skip to content

Commit

Permalink
Extend config UI to allow host/protocol change
Browse files Browse the repository at this point in the history
  • Loading branch information
mletenay committed May 12, 2024
1 parent 2e3a0a1 commit 07bb738
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 22 deletions.
8 changes: 4 additions & 4 deletions custom_components/goodwe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up the Goodwe components from a config entry."""
hass.data.setdefault(DOMAIN, {})
host = entry.data[CONF_HOST]
port = 502 if entry.data.get(CONF_PROTOCOL) == "TCP" else 8899
model_family = entry.data.get(CONF_MODEL_FAMILY)
host = entry.options.get(CONF_HOST, entry.data[CONF_HOST])
protocol = entry.options.get(CONF_PROTOCOL, entry.data[CONF_PROTOCOL])
model_family = entry.options.get(CONF_MODEL_FAMILY, entry.data[CONF_MODEL_FAMILY])
network_retries = entry.options.get(CONF_NETWORK_RETRIES, DEFAULT_NETWORK_RETRIES)
network_timeout = entry.options.get(CONF_NETWORK_TIMEOUT, DEFAULT_NETWORK_TIMEOUT)

# Connect to Goodwe inverter
try:
inverter = await connect(
host=host,
port=port,
port=502 if protocol == "TCP" else 8899,
family=model_family,
comm_addr=0,
timeout=network_timeout,
Expand Down
40 changes: 23 additions & 17 deletions custom_components/goodwe/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@
vol.Required(CONF_MODEL_FAMILY, default="none"): str,
}
)
OPTIONS_SCHEMA = vol.Schema(
{
vol.Required(CONF_HOST): str,
vol.Required(CONF_PROTOCOL): vol.In(PROTOCOL_CHOICES),
vol.Required(CONF_MODEL_FAMILY): str,
vol.Optional(CONF_SCAN_INTERVAL): int,
vol.Optional(CONF_NETWORK_RETRIES): cv.positive_int,
vol.Optional(CONF_NETWORK_TIMEOUT): cv.positive_int,
}
)

_LOGGER = logging.getLogger(__name__)

Expand All @@ -48,31 +58,27 @@ async def async_step_init(self, user_input: dict | None = None) -> FlowResult:
if user_input is not None:
return self.async_create_entry(title="", data=user_input)

settings_schema = vol.Schema(
{
vol.Optional(
CONF_SCAN_INTERVAL,
default=self.config_entry.options.get(
return self.async_show_form(
step_id="init",
data_schema=self.add_suggested_values_to_schema(
OPTIONS_SCHEMA,
{
CONF_HOST: self.config_entry.data[CONF_HOST],
CONF_PROTOCOL: self.config_entry.data[CONF_PROTOCOL],
CONF_MODEL_FAMILY: self.config_entry.data[CONF_MODEL_FAMILY],
CONF_SCAN_INTERVAL: self.config_entry.options.get(
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
),
): int,
vol.Optional(
CONF_NETWORK_RETRIES,
default=self.config_entry.options.get(
CONF_NETWORK_RETRIES: self.config_entry.options.get(
CONF_NETWORK_RETRIES, DEFAULT_NETWORK_RETRIES
),
): cv.positive_int,
vol.Optional(
CONF_NETWORK_TIMEOUT,
default=self.config_entry.options.get(
CONF_NETWORK_TIMEOUT: self.config_entry.options.get(
CONF_NETWORK_TIMEOUT, DEFAULT_NETWORK_TIMEOUT
),
): cv.positive_int,
}
},
),
)

return self.async_show_form(step_id="init", data_schema=settings_schema)


class GoodweFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a Goodwe config flow."""
Expand Down
2 changes: 1 addition & 1 deletion custom_components/goodwe/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"issue_tracker": "https://github.com/mletenay/home-assistant-goodwe-inverter/issues",
"loggers": ["goodwe"],
"requirements": ["goodwe==0.4.1"],
"version": "1.0.0"
"version": "0.9.9.20"
}
4 changes: 4 additions & 0 deletions custom_components/goodwe/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"description": "Connect to inverter",
"data": {
"host": "[%key:common::config_flow::data::ip%]",
"protocol": "Protocol",
"model_family": "Inverter Family (optional)"
}
}
Expand Down Expand Up @@ -64,6 +65,9 @@
"title": "GoodWe optional settings",
"description": "Specify optional (network) settings",
"data": {
"host": "[%key:common::config_flow::data::ip%]",
"protocol": "Protocol",
"model_family": "Inverter Family (optional)",
"scan_interval": "Scan interval (s)",
"network_retries": "Network retry attempts",
"network_timeout": "Network request timeout (s)"
Expand Down
3 changes: 3 additions & 0 deletions custom_components/goodwe/translations/cs.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@
"step": {
"init": {
"data": {
"host": "IP adresa",
"protocol": "Protokol",
"model_family": "Typ střídače",
"scan_interval": "Interval skenování (s)",
"network_retries": "Počet opakování síťového požadavku",
"network_timeout": "Časový limit síťového požadavku (s)"
Expand Down
3 changes: 3 additions & 0 deletions custom_components/goodwe/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@
"step": {
"init": {
"data": {
"host": "Hostname / IP Address",
"protocol": "Protocol",
"model_family": "Inverter Family",
"scan_interval": "Scan interval (s)",
"network_retries": "Network retry attempts",
"network_timeout": "Network request timeout (s)"
Expand Down

0 comments on commit 07bb738

Please sign in to comment.