Skip to content

Commit

Permalink
Created a new sensor class for enum-type values that translate to str…
Browse files Browse the repository at this point in the history
…ings (and made the sg01hp3 inverter status to use that class)
  • Loading branch information
daniel-deptula committed Nov 29, 2024
1 parent fbab534 commit 63761c2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
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
5 changes: 3 additions & 2 deletions src/deye_sensors_deye_sg01hp3.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
DoubleRegisterSensor,
ComputedSumSensor,
SensorRegisterRange,
EnumValueSensor,
)

deye_sg01hp3_inverter_500 = SingleRegisterSensor(
"Running status", 500, 1, mqtt_topic_suffix="inverter/status", unit="", signed=False, groups=["deye_sg01hp3"]
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

0 comments on commit 63761c2

Please sign in to comment.