Skip to content

Commit

Permalink
Merge pull request #214 from daniel-deptula/main
Browse files Browse the repository at this point in the history
New sensors for sg01hp3 (total DC power and inverter status)
  • Loading branch information
kbialek authored Dec 4, 2024
2 parents e23277a + 7869976 commit 21546e0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/metric_group_deye_sg01hp3.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
|Metric|MQTT topic suffix|Unit|Modbus address (dec)|Modbus address (hex)|Modbus data type|Scale factor|
|---|---|:-:|:-:|:-:|:-:|:-:|
|Running status|`inverter/status`|N/A (0: standby, 1: selfcheck, 2: normal, 3: alarm, 4: fault)|500|1f4|U_WORD|1|
|PV1 Power|`dc/pv1/power`|W|672|2a0|U_WORD|10|
|PV2 Power|`dc/pv2/power`|W|673|2a1|U_WORD|10|
|PV3 Power|`dc/pv3/power`|W|674|2a2|U_WORD|10|
|PV4 Power|`dc/pv4/power`|W|675|2a3|U_WORD|10|
|DC Total Power|`dc/total_power`|W|672,673,674,675|2a0,2a1,2a2,2a3|U_WORD|10|
|PV1 Voltage|`dc/pv1/voltage`|V|676|2a4|U_WORD|0.1|
|PV1 Current|`dc/pv1/current`|A|677|2a5|U_WORD|0.1|
|PV2 Voltage|`dc/pv2/voltage`|V|678|2a6|U_WORD|0.1|
Expand Down
37 changes: 37 additions & 0 deletions src/deye_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,43 @@ def get_registers(self) -> list[int]:
return []


class EnumValueSensor(AbstractSensor):
"""
Solar inverter sensor with enum value stored as 32-bit integer in a single Modbus register
"""

def __init__(
self,
name: str,
reg_address: int,
signed=False,
mqtt_topic_suffix="",
groups=[],
enum_values={},
):
super().__init__(name, mqtt_topic_suffix, "", "", groups)
self.__reg_address = reg_address
self.__signed = signed
self.__enum_values = enum_values

def read_value(self, registers: dict[int, bytearray]):
if self.__reg_address in registers:
reg_value = registers[self.__reg_address]
return self.__enum_values.get(int.from_bytes(reg_value, "big", signed=self.__signed), "")
else:
return None

def get_registers(self) -> list[int]:
return [self.__reg_address]

@property
def data_type(self) -> str:
return "S_WORD" if self.__signed else "U_WORD"

def format_value(self, value):
return value


class SensorRegisterRange:
"""
Declares a Modbus register range that must be read to provide values for sensors within a metrics group
Expand Down
20 changes: 20 additions & 0 deletions src/deye_sensors_deye_sg01hp3.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@
from deye_sensor import (
SingleRegisterSensor,
DoubleRegisterSensor,
ComputedSumSensor,
SensorRegisterRange,
EnumValueSensor,
)

deye_sg01hp3_inverter_500 = EnumValueSensor(
"Running status",
500,
mqtt_topic_suffix="inverter/status",
groups=["deye_sg01hp3"],
enum_values={0: "standby", 1: "selfcheck", 2: "normal", 3: "alarm", 4: "fault"},
)

deye_sg01hp3_solar_672 = SingleRegisterSensor(
Expand Down Expand Up @@ -642,8 +652,16 @@
groups=["deye_sg01hp3_bms"],
)

total_pv_power_sensor = ComputedSumSensor(
"DC Total Power",
[deye_sg01hp3_solar_672, deye_sg01hp3_solar_673, deye_sg01hp3_solar_674, deye_sg01hp3_solar_675],
mqtt_topic_suffix="dc/total_power",
unit="W",
groups=["deye_sg01hp3"],
)

deye_sg01hp3_sensors = [
deye_sg01hp3_inverter_500,
deye_sg01hp3_solar_672,
deye_sg01hp3_solar_673,
deye_sg01hp3_solar_674,
Expand Down Expand Up @@ -723,9 +741,11 @@
deye_sg01hp3_inverter_635,
deye_sg01hp3_inverter_540,
deye_sg01hp3_inverter_541,
total_pv_power_sensor,
]

deye_sg01hp3_register_ranges = [
SensorRegisterRange(group="deye_sg01hp3", first_reg_address=500, last_reg_address=500),
SensorRegisterRange(group="deye_sg01hp3_ups", first_reg_address=514, last_reg_address=558),
SensorRegisterRange(group="deye_sg01hp3", first_reg_address=514, last_reg_address=558),
SensorRegisterRange(group="deye_sg01hp3_battery", first_reg_address=514, last_reg_address=558),
Expand Down

0 comments on commit 21546e0

Please sign in to comment.