88
99from homeassistant .components .select import SelectEntity , SelectEntityDescription
1010from homeassistant .config_entries import ConfigEntry #pylint: disable=no-name-in-module, syntax-error
11- from homeassistant .const import CONF_NAME , EntityCategory
11+ from homeassistant .const import CONF_NAME # , EntityCategory
1212from homeassistant .core import HomeAssistant
1313from homeassistant .helpers .entity import DeviceInfo
1414from homeassistant .helpers .entity_platform import AddEntitiesCallback
2121)
2222from .modbusregisterdefinitions import (RemoteEMSControlMode )
2323from .coordinator import SigenergyDataUpdateCoordinator # Import coordinator
24- from .modbus import SigenergyModbusError
24+ # from .modbus import SigenergyModbusError
2525from .common import generate_sigen_entity # Added generate_device_id
2626from .sigen_entity import SigenergyEntity # Import the new base class
2727
2828_LOGGER = logging .getLogger (__name__ )
2929
30+ # This register is deprecated in Modbus v. 2.7 and is now marked as reserved.
3031# Map of grid codes to country names
31- GRID_CODE_MAP = {
32- 1 : "Germany" ,
33- 2 : "UK" ,
34- 3 : "Italy" ,
35- 4 : "Spain" ,
36- 5 : "Portugal" ,
37- 6 : "France" ,
38- 7 : "Poland" ,
39- 8 : "Hungary" ,
40- 9 : "Belgium" ,
41- 10 : "Norway" ,
42- 11 : "Sweden" ,
43- 12 : "Finland" ,
44- 13 : "Denmark" ,
45- 19 : "Australia" ,
46- 26 : "Austria" ,
47- 36 : "Ireland" ,
48- # Add more mappings as they are discovered
49- }
32+ # GRID_CODE_MAP = {
33+ # 1: "Germany",
34+ # 2: "UK",
35+ # 3: "Italy",
36+ # 4: "Spain",
37+ # 5: "Portugal",
38+ # 6: "France",
39+ # 7: "Poland",
40+ # 8: "Hungary",
41+ # 9: "Belgium",
42+ # 10: "Norway",
43+ # 11: "Sweden",
44+ # 12: "Finland",
45+ # 13: "Denmark",
46+ # 19: "Australia",
47+ # 26: "Austria",
48+ # 36: "Ireland",
49+ # # Add more mappings as they are discovered
50+ # }
5051
5152# Reverse mapping for looking up codes by country name
52- COUNTRY_TO_CODE_MAP = {country : code for code , country in GRID_CODE_MAP .items ()}
53+ # COUNTRY_TO_CODE_MAP = {country: code for code, country in GRID_CODE_MAP.items()}
5354# Debug log the grid code map
5455
55- def _get_grid_code_display (data , device_name ): # Changed inverter_id to device_name
56- """Get the display value for grid code with debug logging."""
57- # This register is deprecated in Modbus v. 2.7 and is now marked as reserved.
58- # It still functions in the current implementation but may be removed in future versions.
59-
60- # Get the raw grid code value using device_name
61- grid_code = data ["inverters" ].get (device_name , {}).get ("inverter_grid_code" )
62-
63- # Handle None case
64- if grid_code is None :
65- return "Unknown"
66-
67- # Try to convert to int and look up in map
68- try :
69- grid_code_int = int (grid_code )
70- # _LOGGER.debug("Converted grid code to int: %s", grid_code_int)
71-
72- # Look up in map
73- result = GRID_CODE_MAP .get (grid_code_int )
74- # _LOGGER.debug("Grid code map lookup result: %s", result)
75-
76- if result is not None :
77- return result
78- else :
79- return f"Unknown ({ grid_code } )"
80- except (ValueError , TypeError ) as e :
81- _LOGGER .debug ("Error converting grid code for %s: %s" , device_name , e )
82- return f"Unknown ({ grid_code } )"
56+ # This register is deprecated in Modbus v. 2.7 and is now marked as reserved.
57+ # def _get_grid_code_display(data, device_name): # Changed inverter_id to device_name
58+ # """Get the display value for grid code with debug logging."""
59+
60+ # # Get the raw grid code value using device_name
61+ # grid_code = data["inverters"].get(device_name, {}).get("inverter_grid_code")
62+
63+ # # Handle None case
64+ # if grid_code is None:
65+ # return "Unknown"
66+
67+ # # Try to convert to int and look up in map
68+ # try:
69+ # grid_code_int = int(grid_code)
70+ # # _LOGGER.debug("Converted grid code to int: %s", grid_code_int)
71+
72+ # # Look up in map
73+ # result = GRID_CODE_MAP.get(grid_code_int)
74+ # # _LOGGER.debug("Grid code map lookup result: %s", result)
75+
76+ # if result is not None:
77+ # return result
78+ # else:
79+ # return f"Unknown ({grid_code})"
80+ # except (ValueError, TypeError) as e:
81+ # _LOGGER.debug("Error converting grid code for %s: %s", device_name, e)
82+ # return f"Unknown ({grid_code})"
8383
8484
8585
@@ -139,23 +139,22 @@ class SigenergySelectEntityDescription(SelectEntityDescription):
139139
140140INVERTER_SELECTS = [
141141 # This register is deprecated in Modbus v. 2.7 and is now marked as reserved.
142- # It still functions in the current implementation but may be removed in future versions.
143- SigenergySelectEntityDescription (
144- key = "inverter_grid_code" ,
145- name = "Grid Code" ,
146- icon = "mdi:transmission-tower" ,
147- options = list (GRID_CODE_MAP .values ()),
148- entity_category = EntityCategory .CONFIG ,
149- # Use identifier (device_name for inverters)
150- current_option_fn = lambda data , identifier : _get_grid_code_display (data , identifier ),
151- # Use identifier (device_name for inverters)
152- select_option_fn = lambda coordinator , identifier , option : coordinator .async_write_parameter (
153- "inverter" , identifier , "inverter_grid_code" ,
154- COUNTRY_TO_CODE_MAP .get (option , 0 ) # Default to 0 if country not found
155- ),
156- entity_registry_enabled_default = False ,
142+ # SigenergySelectEntityDescription(
143+ # key="inverter_grid_code",
144+ # name="Grid Code",
145+ # icon="mdi:transmission-tower",
146+ # options=list(GRID_CODE_MAP.values()),
147+ # entity_category=EntityCategory.CONFIG,
148+ # # Use identifier (device_name for inverters)
149+ # current_option_fn=lambda data, identifier: _get_grid_code_display(data, identifier),
150+ # # Use identifier (device_name for inverters)
151+ # select_option_fn=lambda coordinator, identifier, option: coordinator.async_write_parameter(
152+ # "inverter", identifier, "inverter_grid_code",
153+ # COUNTRY_TO_CODE_MAP.get(option, 0) # Default to 0 if country not found
154+ # ),
155+ # entity_registry_enabled_default=False,
157156
158- ),
157+ # ),
159158]
160159
161160AC_CHARGER_SELECTS = []
@@ -170,7 +169,7 @@ async def async_setup_entry(
170169 coordinator : SigenergyDataUpdateCoordinator = (
171170 hass .data [DOMAIN ][config_entry .entry_id ]["coordinator" ])
172171 plant_name = config_entry .data [CONF_NAME ]
173- _LOGGER .debug (f "Starting to add { SigenergySelect } " )
172+ _LOGGER .debug ("Starting to add %s" , SigenergySelect )
174173 # Add plant Selects
175174 entities : list [SigenergySelect ] = generate_sigen_entity (plant_name , None , None , coordinator ,
176175 SigenergySelect ,
@@ -234,19 +233,29 @@ def current_option(self) -> str:
234233 """Return the selected entity option."""
235234 if self .coordinator .data is None :
236235 return self .options [0 ] if self .options else ""
237-
236+
238237 # Use device_name as the primary identifier passed to the lambda/function
239238 identifier = self ._device_name
240239 try :
241240 option = self .entity_description .current_option_fn (self .coordinator .data , identifier )
242241 return option if option is not None else ""
243242 except Exception as e :
244- _LOGGER .error (f"Error getting current_option for { self .entity_id } (identifier: { identifier } ): { e } " )
243+ _LOGGER .error ("Error getting current_option for %s (identifier: %s): %s" ,
244+ self .entity_id , identifier , e )
245245 return ""
246246
247247 async def async_select_option (self , option : str ) -> None :
248248 """Change the selected option."""
249249 # Use device_name as the primary identifier passed to the lambda/function
250250 identifier = self ._device_name
251251 # Exceptions are handled and logged in coordinator.async_write_parameter
252- await self .entity_description .select_option_fn (self .coordinator , identifier , option )
252+ await self .entity_description .select_option_fn (self .coordinator , identifier , option )
253+
254+ def select_option (self , option : str ) -> None :
255+ """Change the selected option."""
256+ # This is the synchronous version of the method.
257+ # We are using an async function so we need to wrap this in a task
258+ # and wait for it to complete.
259+ asyncio .run_coroutine_threadsafe (
260+ self .async_select_option (option ), self .hass .loop
261+ ).result ()
0 commit comments