11"""The Goodwe inverter component."""
22
3- from goodwe import InverterError , connect
4- from goodwe .const import GOODWE_TCP_PORT , GOODWE_UDP_PORT
3+ from goodwe import Inverter , InverterError , connect
4+ from goodwe .const import GOODWE_UDP_PORT
55
6- from homeassistant .const import CONF_HOST
6+ from homeassistant .const import CONF_HOST , CONF_PORT
77from homeassistant .core import HomeAssistant
88from homeassistant .exceptions import ConfigEntryNotReady
99from homeassistant .helpers .device_registry import DeviceInfo
1010
11+ from .config_flow import GoodweFlowHandler
1112from .const import CONF_MODEL_FAMILY , DOMAIN , PLATFORMS
1213from .coordinator import GoodweConfigEntry , GoodweRuntimeData , GoodweUpdateCoordinator
1314
1415
1516async def async_setup_entry (hass : HomeAssistant , entry : GoodweConfigEntry ) -> bool :
1617 """Set up the Goodwe components from a config entry."""
1718 host = entry .data [CONF_HOST ]
19+ port = entry .data .get (CONF_PORT , GOODWE_UDP_PORT )
1820 model_family = entry .data [CONF_MODEL_FAMILY ]
1921
2022 # Connect to Goodwe inverter
2123 try :
2224 inverter = await connect (
2325 host = host ,
24- port = GOODWE_UDP_PORT ,
26+ port = port ,
2527 family = model_family ,
2628 retries = 10 ,
2729 )
28- except InverterError as err_udp :
29- # First try with UDP failed, trying with the TCP port
30+ except InverterError as err :
3031 try :
31- inverter = await connect (
32- host = host ,
33- port = GOODWE_TCP_PORT ,
34- family = model_family ,
35- retries = 10 ,
36- )
32+ inverter = await async_check_port (hass , entry , host )
3733 except InverterError :
38- # Both ports are unavailable
39- raise ConfigEntryNotReady from err_udp
34+ raise ConfigEntryNotReady from err
4035
4136 device_info = DeviceInfo (
4237 configuration_url = "https://www.semsportal.com" ,
@@ -66,6 +61,23 @@ async def async_setup_entry(hass: HomeAssistant, entry: GoodweConfigEntry) -> bo
6661 return True
6762
6863
64+ async def async_check_port (
65+ hass : HomeAssistant , entry : GoodweConfigEntry , host : str
66+ ) -> Inverter :
67+ """Check the communication port of the inverter, it may have changed after a firmware update."""
68+ inverter , port = await GoodweFlowHandler .async_detect_inverter_port (host = host )
69+ family = type (inverter ).__name__
70+ hass .config_entries .async_update_entry (
71+ entry ,
72+ data = {
73+ CONF_HOST : host ,
74+ CONF_PORT : port ,
75+ CONF_MODEL_FAMILY : family ,
76+ },
77+ )
78+ return inverter
79+
80+
6981async def async_unload_entry (
7082 hass : HomeAssistant , config_entry : GoodweConfigEntry
7183) -> bool :
@@ -76,3 +88,31 @@ async def async_unload_entry(
7688async def update_listener (hass : HomeAssistant , config_entry : GoodweConfigEntry ) -> None :
7789 """Handle options update."""
7890 await hass .config_entries .async_reload (config_entry .entry_id )
91+
92+
93+ async def async_migrate_entry (
94+ hass : HomeAssistant , config_entry : GoodweConfigEntry
95+ ) -> bool :
96+ """Migrate old config entries."""
97+
98+ if config_entry .version > 2 :
99+ # This means the user has downgraded from a future version
100+ return False
101+
102+ if config_entry .version == 1 :
103+ # Update from version 1 to version 2 adding the PROTOCOL to the config entry
104+ host = config_entry .data [CONF_HOST ]
105+ try :
106+ inverter , port = await GoodweFlowHandler .async_detect_inverter_port (
107+ host = host
108+ )
109+ except InverterError as err :
110+ raise ConfigEntryNotReady from err
111+ new_data = {
112+ CONF_HOST : host ,
113+ CONF_PORT : port ,
114+ CONF_MODEL_FAMILY : type (inverter ).__name__ ,
115+ }
116+ hass .config_entries .async_update_entry (config_entry , data = new_data , version = 2 )
117+
118+ return True
0 commit comments