diff --git a/goodwe/et.py b/goodwe/et.py index 3dae067..7007db2 100644 --- a/goodwe/et.py +++ b/goodwe/et.py @@ -115,7 +115,7 @@ class ET(Inverter): Voltage("bus_voltage", 35178, "Bus Voltage", None), Voltage("nbus_voltage", 35179, "NBus Voltage", None), Voltage("vbattery1", 35180, "Battery Voltage", Kind.BAT), - Current("ibattery1", 35181, "Battery Current", Kind.BAT), + CurrentS("ibattery1", 35181, "Battery Current", Kind.BAT), Power4("pbattery1", 35182, "Battery Power", Kind.BAT), Integer("battery_mode", 35184, "Battery Mode code", "", Kind.BAT), Enum2("battery_mode_label", 35184, BATTERY_MODES, "Battery Mode", Kind.BAT), diff --git a/goodwe/sensor.py b/goodwe/sensor.py index d1544de..cad1db1 100644 --- a/goodwe/sensor.py +++ b/goodwe/sensor.py @@ -83,7 +83,7 @@ def is_in_range(self, value: int) -> bool: class Voltage(Sensor): - """Sensor representing voltage [V] value encoded in 2 bytes""" + """Sensor representing voltage [V] value encoded in 2 (unsigned) bytes""" def __init__(self, id_: str, offset: int, name: str, kind: Optional[SensorKind]): super().__init__(id_, offset, name, 2, "V", kind) @@ -96,7 +96,7 @@ def encode_value(self, value: Any, register_value: bytes = None) -> bytes: class Current(Sensor): - """Sensor representing current [A] value encoded in 2 bytes""" + """Sensor representing current [A] value encoded in 2 (unsigned) bytes""" def __init__(self, id_: str, offset: int, name: str, kind: Optional[SensorKind]): super().__init__(id_, offset, name, 2, "A", kind) @@ -108,6 +108,19 @@ def encode_value(self, value: Any, register_value: bytes = None) -> bytes: return encode_current(value) +class CurrentS(Sensor): + """Sensor representing current [A] value encoded in 2 (signed) bytes""" + + def __init__(self, id_: str, offset: int, name: str, kind: Optional[SensorKind]): + super().__init__(id_, offset, name, 2, "A", kind) + + def read_value(self, data: ProtocolResponse): + return read_current_signed(data) + + def encode_value(self, value: Any, register_value: bytes = None) -> bytes: + return encode_current_signed(value) + + class Frequency(Sensor): """Sensor representing frequency [Hz] value encoded in 2 bytes""" @@ -736,7 +749,7 @@ def read_float4(buffer: ProtocolResponse, offset: int = None) -> float: def read_voltage(buffer: ProtocolResponse, offset: int = None) -> float: - """Retrieve voltage [V] value (2 bytes) from buffer""" + """Retrieve voltage [V] value (2 unsigned bytes) from buffer""" if offset is not None: buffer.seek(offset) value = int.from_bytes(buffer.read(2), byteorder="big", signed=False) @@ -744,12 +757,20 @@ def read_voltage(buffer: ProtocolResponse, offset: int = None) -> float: def encode_voltage(value: Any) -> bytes: - """Encode voltage value to raw (2 bytes) payload""" + """Encode voltage value to raw (2 unsigned bytes) payload""" return int.to_bytes(int(value * 10), length=2, byteorder="big", signed=False) def read_current(buffer: ProtocolResponse, offset: int = None) -> float: - """Retrieve current [A] value (2 bytes) from buffer""" + """Retrieve current [A] value (2 unsigned bytes) from buffer""" + if offset is not None: + buffer.seek(offset) + value = int.from_bytes(buffer.read(2), byteorder="big", signed=False) + return float(value) / 10 + + +def read_current_signed(buffer: ProtocolResponse, offset: int = None) -> float: + """Retrieve current [A] value (2 signed bytes) from buffer""" if offset is not None: buffer.seek(offset) value = int.from_bytes(buffer.read(2), byteorder="big", signed=True) @@ -757,7 +778,12 @@ def read_current(buffer: ProtocolResponse, offset: int = None) -> float: def encode_current(value: Any) -> bytes: - """Encode current value to raw (2 bytes) payload""" + """Encode current value to raw (2 unsigned bytes) payload""" + return int.to_bytes(int(value * 10), length=2, byteorder="big", signed=False) + + +def encode_current_signed(value: Any) -> bytes: + """Encode current value to raw (2 signed bytes) payload""" return int.to_bytes(int(value * 10), length=2, byteorder="big", signed=True) diff --git a/tests/test_sensor.py b/tests/test_sensor.py index 84feb21..11f659f 100644 --- a/tests/test_sensor.py +++ b/tests/test_sensor.py @@ -78,6 +78,12 @@ def test_voltage(self): self.assertEqual(803.6, testee.read(data)) self.assertEqual("1f64", testee.encode_value(803.6).hex()) + data = MockResponse("a000") + self.assertEqual(4096.0, testee.read(data)) + + data = MockResponse("ffff") + self.assertEqual(0, testee.read(data)) + def test_current(self): testee = Current("", 0, "", None) @@ -85,6 +91,17 @@ def test_current(self): self.assertEqual(4.9, testee.read(data)) self.assertEqual("0031", testee.encode_value(4.9).hex()) + data = MockResponse("ff9e") + self.assertEqual(6543.8, testee.read(data)) + self.assertEqual("ff9e", testee.encode_value(6543.8).hex()) + + def test_current_signed(self): + testee = CurrentS("", 0, "", None) + + data = MockResponse("0031") + self.assertEqual(4.9, testee.read(data)) + self.assertEqual("0031", testee.encode_value(4.9).hex()) + data = MockResponse("ff9e") self.assertEqual(-9.8, testee.read(data)) self.assertEqual("ff9e", testee.encode_value(-9.8).hex())