Skip to content

Commit

Permalink
Redesign sensor definitions and loading.
Browse files Browse the repository at this point in the history
Declare real modbus registers in sensors instead of offset of some first sensor.
Delegate seeking and reading to ProtocolResponse, so modbus values can be potentially read individually and/or in smaller consecutive chunks.
  • Loading branch information
mletenay committed Dec 16, 2023
1 parent 2decd98 commit d592b2a
Show file tree
Hide file tree
Showing 7 changed files with 498 additions and 435 deletions.
143 changes: 71 additions & 72 deletions goodwe/dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,94 +15,94 @@ class DT(Inverter):
"""Class representing inverter of DT/MS/D-NS/XS or GE's GEP(PSB/PSC) families"""

__all_sensors: Tuple[Sensor, ...] = (
Timestamp("timestamp", 0, "Timestamp"),
Voltage("vpv1", 6, "PV1 Voltage", Kind.PV),
Current("ipv1", 8, "PV1 Current", Kind.PV),
Timestamp("timestamp", 30100, "Timestamp"),
Voltage("vpv1", 30103, "PV1 Voltage", Kind.PV),
Current("ipv1", 30104, "PV1 Current", Kind.PV),
Calculated("ppv1",
lambda data: round(read_voltage(data, 6) * read_current(data, 8)),
lambda data: round(read_voltage(data, 30103) * read_current(data, 30104)),
"PV1 Power", "W", Kind.PV),
Voltage("vpv2", 10, "PV2 Voltage", Kind.PV),
Current("ipv2", 12, "PV2 Current", Kind.PV),
Voltage("vpv2", 30105, "PV2 Voltage", Kind.PV),
Current("ipv2", 30106, "PV2 Current", Kind.PV),
Calculated("ppv2",
lambda data: round(read_voltage(data, 10) * read_current(data, 12)),
lambda data: round(read_voltage(data, 30105) * read_current(data, 30106)),
"PV2 Power", "W", Kind.PV),
Voltage("vpv3", 14, "PV3 Voltage", Kind.PV),
Current("ipv3", 16, "PV3 Current", Kind.PV),
Voltage("vpv3", 30107, "PV3 Voltage", Kind.PV),
Current("ipv3", 30108, "PV3 Current", Kind.PV),
Calculated("ppv3",
lambda data: round(read_voltage(data, 14) * read_current(data, 16)),
lambda data: round(read_voltage(data, 30107) * read_current(data, 30108)),
"PV3 Power", "W", Kind.PV),
# Voltage("vpv4", 14, "PV4 Voltage", Kind.PV),
# Current("ipv4", 16, "PV4 Current", Kind.PV),
# Voltage("vpv5", 14, "PV5 Voltage", Kind.PV),
# Current("ipv5", 16, "PV5 Current", Kind.PV),
# Voltage("vpv6", 14, "PV6 Voltage", Kind.PV),
# Current("ipv6", 16, "PV7 Current", Kind.PV),
Voltage("vline1", 30, "On-grid L1-L2 Voltage", Kind.AC),
Voltage("vline2", 32, "On-grid L2-L3 Voltage", Kind.AC),
Voltage("vline3", 34, "On-grid L3-L1 Voltage", Kind.AC),
Voltage("vgrid1", 36, "On-grid L1 Voltage", Kind.AC),
Voltage("vgrid2", 38, "On-grid L2 Voltage", Kind.AC),
Voltage("vgrid3", 40, "On-grid L3 Voltage", Kind.AC),
Current("igrid1", 42, "On-grid L1 Current", Kind.AC),
Current("igrid2", 44, "On-grid L2 Current", Kind.AC),
Current("igrid3", 46, "On-grid L3 Current", Kind.AC),
Frequency("fgrid1", 48, "On-grid L1 Frequency", Kind.AC),
Frequency("fgrid2", 50, "On-grid L2 Frequency", Kind.AC),
Frequency("fgrid3", 52, "On-grid L3 Frequency", Kind.AC),
Voltage("vline1", 30115, "On-grid L1-L2 Voltage", Kind.AC),
Voltage("vline2", 30116, "On-grid L2-L3 Voltage", Kind.AC),
Voltage("vline3", 30117, "On-grid L3-L1 Voltage", Kind.AC),
Voltage("vgrid1", 30118, "On-grid L1 Voltage", Kind.AC),
Voltage("vgrid2", 30119, "On-grid L2 Voltage", Kind.AC),
Voltage("vgrid3", 30120, "On-grid L3 Voltage", Kind.AC),
Current("igrid1", 30121, "On-grid L1 Current", Kind.AC),
Current("igrid2", 30122, "On-grid L2 Current", Kind.AC),
Current("igrid3", 30123, "On-grid L3 Current", Kind.AC),
Frequency("fgrid1", 30124, "On-grid L1 Frequency", Kind.AC),
Frequency("fgrid2", 30125, "On-grid L2 Frequency", Kind.AC),
Frequency("fgrid3", 30126, "On-grid L3 Frequency", Kind.AC),
Calculated("pgrid1",
lambda data: round(read_voltage(data, 36) * read_current(data, 42)),
lambda data: round(read_voltage(data, 30118) * read_current(data, 30121)),
"On-grid L1 Power", "W", Kind.AC),
Calculated("pgrid2",
lambda data: round(read_voltage(data, 38) * read_current(data, 44)),
lambda data: round(read_voltage(data, 30119) * read_current(data, 30122)),
"On-grid L2 Power", "W", Kind.AC),
Calculated("pgrid3",
lambda data: round(read_voltage(data, 40) * read_current(data, 46)),
lambda data: round(read_voltage(data, 30120) * read_current(data, 30123)),
"On-grid L3 Power", "W", Kind.AC),
Integer("xx54", 54, "Unknown sensor@54"),
Power("ppv", 56, "PV Power", Kind.PV),
Integer("work_mode", 58, "Work Mode code"),
Enum2("work_mode_label", 58, WORK_MODES, "Work Mode"),
Long("error_codes", 60, "Error Codes"),
Integer("warning_code", 64, "Warning code"),
Integer("xx66", 66, "Unknown sensor@66"),
Integer("xx68", 68, "Unknown sensor@68"),
Integer("xx70", 70, "Unknown sensor@70"),
Integer("xx72", 72, "Unknown sensor@72"),
Integer("xx74", 74, "Unknown sensor@74"),
Integer("xx76", 76, "Unknown sensor@76"),
Integer("xx78", 78, "Unknown sensor@78"),
Integer("xx80", 80, "Unknown sensor@80"),
Temp("temperature", 82, "Inverter Temperature", Kind.AC),
Integer("xx84", 84, "Unknown sensor@84"),
Integer("xx86", 86, "Unknown sensor@86"),
Energy("e_day", 88, "Today's PV Generation", Kind.PV),
Energy4("e_total", 90, "Total PV Generation", Kind.PV),
Long("h_total", 94, "Hours Total", "h", Kind.PV),
Integer("safety_country", 98, "Safety Country code", "", Kind.AC),
Enum2("safety_country_label", 98, SAFETY_COUNTRIES, "Safety Country", Kind.AC),
Integer("xx100", 100, "Unknown sensor@100"),
Integer("xx102", 102, "Unknown sensor@102"),
Integer("xx104", 104, "Unknown sensor@104"),
Integer("xx106", 106, "Unknown sensor@106"),
Integer("xx108", 108, "Unknown sensor@108"),
Integer("xx110", 110, "Unknown sensor@110"),
Integer("xx112", 112, "Unknown sensor@112"),
Integer("xx114", 114, "Unknown sensor@114"),
Integer("xx116", 116, "Unknown sensor@116"),
Integer("xx118", 118, "Unknown sensor@118"),
Integer("xx120", 120, "Unknown sensor@120"),
Integer("xx122", 122, "Unknown sensor@122"),
Integer("funbit", 124, "FunBit", "", Kind.PV),
Voltage("vbus", 126, "Bus Voltage", Kind.PV),
Voltage("vnbus", 128, "NBus Voltage", Kind.PV),
Integer("xx130", 130, "Unknown sensor@130"),
Integer("xx132", 132, "Unknown sensor@132"),
Integer("xx134", 134, "Unknown sensor@134"),
Integer("xx136", 136, "Unknown sensor@136"),
Integer("xx138", 138, "Unknown sensor@138"),
Integer("xx140", 140, "Unknown sensor@140"),
Integer("xx142", 142, "Unknown sensor@142"),
Integer("xx144", 144, "Unknown sensor@144"),
Integer("xx54", 30127, "Unknown sensor@54"),
Power("ppv", 30128, "PV Power", Kind.PV),
Integer("work_mode", 30129, "Work Mode code"),
Enum2("work_mode_label", 30129, WORK_MODES, "Work Mode"),
Long("error_codes", 30130, "Error Codes"),
Integer("warning_code", 30132, "Warning code"),
Integer("xx66", 30133, "Unknown sensor@66"),
Integer("xx68", 30134, "Unknown sensor@68"),
Integer("xx70", 30135, "Unknown sensor@70"),
Integer("xx72", 30136, "Unknown sensor@72"),
Integer("xx74", 30137, "Unknown sensor@74"),
Integer("xx76", 30138, "Unknown sensor@76"),
Integer("xx78", 30139, "Unknown sensor@78"),
Integer("xx80", 30140, "Unknown sensor@80"),
Temp("temperature", 30141, "Inverter Temperature", Kind.AC),
Integer("xx84", 30142, "Unknown sensor@84"),
Integer("xx86", 30143, "Unknown sensor@86"),
Energy("e_day", 30144, "Today's PV Generation", Kind.PV),
Energy4("e_total", 30145, "Total PV Generation", Kind.PV),
Long("h_total", 30147, "Hours Total", "h", Kind.PV),
Integer("safety_country", 30149, "Safety Country code", "", Kind.AC),
Enum2("safety_country_label", 30149, SAFETY_COUNTRIES, "Safety Country", Kind.AC),
Integer("xx100", 30150, "Unknown sensor@100"),
Integer("xx102", 30151, "Unknown sensor@102"),
Integer("xx104", 30152, "Unknown sensor@104"),
Integer("xx106", 30153, "Unknown sensor@106"),
Integer("xx108", 30154, "Unknown sensor@108"),
Integer("xx110", 30155, "Unknown sensor@110"),
Integer("xx112", 30156, "Unknown sensor@112"),
Integer("xx114", 30157, "Unknown sensor@114"),
Integer("xx116", 30158, "Unknown sensor@116"),
Integer("xx118", 30159, "Unknown sensor@118"),
Integer("xx120", 30160, "Unknown sensor@120"),
Integer("xx122", 30161, "Unknown sensor@122"),
Integer("funbit", 30162, "FunBit", "", Kind.PV),
Voltage("vbus", 30163, "Bus Voltage", Kind.PV),
Voltage("vnbus", 30164, "NBus Voltage", Kind.PV),
Integer("xx130", 30165, "Unknown sensor@130"),
Integer("xx132", 30166, "Unknown sensor@132"),
Integer("xx134", 30167, "Unknown sensor@134"),
Integer("xx136", 30168, "Unknown sensor@136"),
Integer("xx138", 30169, "Unknown sensor@138"),
Integer("xx140", 30170, "Unknown sensor@140"),
Integer("xx142", 30171, "Unknown sensor@142"),
Integer("xx144", 30172, "Unknown sensor@144"),
)

# Modbus registers of inverter settings, offsets are modbus register addresses
Expand Down Expand Up @@ -183,8 +183,7 @@ async def read_setting(self, setting_id: str) -> Any:
raise ValueError(f'Unknown setting "{setting_id}"')
count = (setting.size_ + (setting.size_ % 2)) // 2
response = await self._read_from_socket(ModbusReadCommand(self.comm_addr, setting.offset, count))
with io.BytesIO(response.response_data()) as buffer:
return setting.read_value(buffer)
return setting.read_value(response)

async def write_setting(self, setting_id: str, value: Any):
setting = self._settings.get(setting_id)
Expand Down
6 changes: 2 additions & 4 deletions goodwe/es.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,10 @@ async def read_setting(self, setting_id: str) -> Any:
count = (setting.size_ + (setting.size_ % 2)) // 2
if self._is_modbus_setting(setting):
response = await self._read_from_socket(ModbusReadCommand(self.comm_addr, setting.offset, count))
with io.BytesIO(response.response_data()) as buffer:
return setting.read_value(buffer)
return setting.read_value(response)
else:
response = await self._read_from_socket(Aa55ReadCommand(setting.offset, count))
with io.BytesIO(response.response_data()) as buffer:
return setting.read_value(buffer)
return setting.read_value(response)
else:
all_settings = await self.read_settings_data()
return all_settings.get(setting_id)
Expand Down
Loading

0 comments on commit d592b2a

Please sign in to comment.