From 9d2ffa637248126335bea4544d20b88bff36b51a Mon Sep 17 00:00:00 2001 From: jlanchares <87146197+jlanchares@users.noreply.github.com> Date: Mon, 7 Jul 2025 19:37:20 +0200 Subject: [PATCH] Goodwe TCP support (port 502) (#147900) --- homeassistant/components/goodwe/__init__.py | 16 +++++++-- .../components/goodwe/config_flow.py | 36 ++++++++++++------- homeassistant/components/goodwe/manifest.json | 2 +- homeassistant/components/goodwe/select.py | 29 +++++++++------ requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 6 files changed, 58 insertions(+), 29 deletions(-) diff --git a/homeassistant/components/goodwe/__init__.py b/homeassistant/components/goodwe/__init__.py index b6637bc8b50..e191e1b775f 100644 --- a/homeassistant/components/goodwe/__init__.py +++ b/homeassistant/components/goodwe/__init__.py @@ -1,6 +1,7 @@ """The Goodwe inverter component.""" from goodwe import InverterError, connect +from goodwe.const import GOODWE_TCP_PORT, GOODWE_UDP_PORT from homeassistant.const import CONF_HOST from homeassistant.core import HomeAssistant @@ -20,11 +21,22 @@ async def async_setup_entry(hass: HomeAssistant, entry: GoodweConfigEntry) -> bo try: inverter = await connect( host=host, + port=GOODWE_UDP_PORT, family=model_family, retries=10, ) - except InverterError as err: - raise ConfigEntryNotReady from err + except InverterError as err_udp: + # First try with UDP failed, trying with the TCP port + try: + inverter = await connect( + host=host, + port=GOODWE_TCP_PORT, + family=model_family, + retries=10, + ) + except InverterError: + # Both ports are unavailable + raise ConfigEntryNotReady from err_udp device_info = DeviceInfo( configuration_url="https://www.semsportal.com", diff --git a/homeassistant/components/goodwe/config_flow.py b/homeassistant/components/goodwe/config_flow.py index 354877e782f..72d27e02b2e 100644 --- a/homeassistant/components/goodwe/config_flow.py +++ b/homeassistant/components/goodwe/config_flow.py @@ -6,6 +6,7 @@ import logging from typing import Any from goodwe import InverterError, connect +from goodwe.const import GOODWE_TCP_PORT, GOODWE_UDP_PORT import voluptuous as vol from homeassistant.config_entries import ConfigFlow, ConfigFlowResult @@ -27,6 +28,18 @@ class GoodweFlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 + async def _handle_successful_connection(self, inverter, host): + await self.async_set_unique_id(inverter.serial_number) + self._abort_if_unique_id_configured() + + return self.async_create_entry( + title=DEFAULT_NAME, + data={ + CONF_HOST: host, + CONF_MODEL_FAMILY: type(inverter).__name__, + }, + ) + async def async_step_user( self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: @@ -34,22 +47,19 @@ class GoodweFlowHandler(ConfigFlow, domain=DOMAIN): errors = {} if user_input is not None: host = user_input[CONF_HOST] - try: - inverter = await connect(host=host, retries=10) + inverter = await connect(host=host, port=GOODWE_UDP_PORT, retries=10) except InverterError: - errors[CONF_HOST] = "connection_error" + try: + inverter = await connect( + host=host, port=GOODWE_TCP_PORT, retries=10 + ) + except InverterError: + errors[CONF_HOST] = "connection_error" + else: + return await self._handle_successful_connection(inverter, host) else: - await self.async_set_unique_id(inverter.serial_number) - self._abort_if_unique_id_configured() - - return self.async_create_entry( - title=DEFAULT_NAME, - data={ - CONF_HOST: host, - CONF_MODEL_FAMILY: type(inverter).__name__, - }, - ) + return await self._handle_successful_connection(inverter, host) return self.async_show_form( step_id="user", data_schema=CONFIG_SCHEMA, errors=errors diff --git a/homeassistant/components/goodwe/manifest.json b/homeassistant/components/goodwe/manifest.json index 41e0ed91f6a..2f04ee3982f 100644 --- a/homeassistant/components/goodwe/manifest.json +++ b/homeassistant/components/goodwe/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/goodwe", "iot_class": "local_polling", "loggers": ["goodwe"], - "requirements": ["goodwe==0.3.6"] + "requirements": ["goodwe==0.4.8"] } diff --git a/homeassistant/components/goodwe/select.py b/homeassistant/components/goodwe/select.py index c26e8135b3f..7d58b099ddc 100644 --- a/homeassistant/components/goodwe/select.py +++ b/homeassistant/components/goodwe/select.py @@ -54,17 +54,24 @@ async def async_setup_entry( # Inverter model does not support this setting _LOGGER.debug("Could not read inverter operation mode") else: - async_add_entities( - [ - InverterOperationModeEntity( - device_info, - OPERATION_MODE, - inverter, - [v for k, v in _MODE_TO_OPTION.items() if k in supported_modes], - _MODE_TO_OPTION[active_mode], - ) - ] - ) + active_mode_option = _MODE_TO_OPTION.get(active_mode) + if active_mode_option is not None: + async_add_entities( + [ + InverterOperationModeEntity( + device_info, + OPERATION_MODE, + inverter, + [v for k, v in _MODE_TO_OPTION.items() if k in supported_modes], + active_mode_option, + ) + ] + ) + else: + _LOGGER.warning( + "Active mode %s not found in Goodwe Inverter Operation Mode Entity. Skipping entity creation", + active_mode, + ) class InverterOperationModeEntity(SelectEntity): diff --git a/requirements_all.txt b/requirements_all.txt index bfd989f849e..974bdbd8d81 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1035,7 +1035,7 @@ go2rtc-client==0.2.1 goalzero==0.2.2 # homeassistant.components.goodwe -goodwe==0.3.6 +goodwe==0.4.8 # homeassistant.components.google_mail # homeassistant.components.google_tasks diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 78882ff5bd9..fcb92537c90 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -902,7 +902,7 @@ go2rtc-client==0.2.1 goalzero==0.2.2 # homeassistant.components.goodwe -goodwe==0.3.6 +goodwe==0.4.8 # homeassistant.components.google_mail # homeassistant.components.google_tasks