Skip to content

Commit 4d15bfe

Browse files
committed
Add environmental abnormality state and deprecate grid code register in Modbus v. 2.7
1 parent d550078 commit 4d15bfe

3 files changed

Lines changed: 92 additions & 81 deletions

File tree

custom_components/sigen/modbusregisterdefinitions.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class RunningState(IntEnum):
4343
RUNNING = 1
4444
FAULT = 2
4545
SHUTDOWN = 3
46+
ENVIRONMENTAL_ABNORMALITY = 7
4647

4748
# Alarm code mappings based on Sigenergy Modbus Protocol appendices
4849
ALARM_CODES = {
@@ -1937,17 +1938,16 @@ class ModbusRegisterDefinition:
19371938
applicable_to=["hybrid_inverter", "pv_inverter"],
19381939
),
19391940
# This register is deprecated in Modbus v. 2.7 and is now marked as reserved.
1940-
# It still functions in the current implementation but may be removed in future versions.
1941-
"inverter_grid_code": ModbusRegisterDefinition(
1942-
address=40501,
1943-
count=1,
1944-
register_type=RegisterType.HOLDING,
1945-
data_type=DataType.U16,
1946-
gain=1,
1947-
description="Grid code setting",
1948-
applicable_to=["hybrid_inverter", "pv_inverter"],
1949-
update_frequency=UpdateFrequencyType.LOW,
1950-
),
1941+
# "inverter_grid_code": ModbusRegisterDefinition(
1942+
# address=40501,
1943+
# count=1,
1944+
# register_type=RegisterType.HOLDING,
1945+
# data_type=DataType.U16,
1946+
# gain=1,
1947+
# description="Grid code setting",
1948+
# applicable_to=["hybrid_inverter", "pv_inverter"],
1949+
# update_frequency=UpdateFrequencyType.LOW,
1950+
# ),
19511951
"inverter_remote_ems_dispatch_enable": ModbusRegisterDefinition(
19521952
address=41500,
19531953
count=1,

custom_components/sigen/select.py

Lines changed: 79 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from homeassistant.components.select import SelectEntity, SelectEntityDescription
1010
from 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
1212
from homeassistant.core import HomeAssistant
1313
from homeassistant.helpers.entity import DeviceInfo
1414
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@@ -21,65 +21,65 @@
2121
)
2222
from .modbusregisterdefinitions import (RemoteEMSControlMode)
2323
from .coordinator import SigenergyDataUpdateCoordinator # Import coordinator
24-
from .modbus import SigenergyModbusError
24+
# from .modbus import SigenergyModbusError
2525
from .common import generate_sigen_entity # Added generate_device_id
2626
from .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

140140
INVERTER_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

161160
AC_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()

custom_components/sigen/sensor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,13 +555,15 @@ def native_value(self) -> Any:
555555
RunningState.RUNNING: "Running",
556556
RunningState.FAULT: "Fault",
557557
RunningState.SHUTDOWN: "Shutdown",
558+
RunningState.ENVIRONMENTAL_ABNORMALITY: "Environmental Abnormality",
558559
}.get(value, f"Unknown: {value}")
559560
if self.entity_description.key == "inverter_running_state":
560561
return {
561562
RunningState.STANDBY: "Standby",
562563
RunningState.RUNNING: "Running",
563564
RunningState.FAULT: "Fault",
564565
RunningState.SHUTDOWN: "Shutdown",
566+
RunningState.ENVIRONMENTAL_ABNORMALITY: "Environmental Abnormality",
565567
}.get(value, f"Unknown: {value}")
566568
if self.entity_description.key == "ac_charger_system_state":
567569
return {

0 commit comments

Comments
 (0)