From 46e3ff7f710a735cf355c8423e2879cfc496f03f Mon Sep 17 00:00:00 2001 From: mle Date: Fri, 24 May 2024 23:49:55 +0200 Subject: [PATCH] Allow strings to be passed to set_parameter --- goodwe/es.py | 2 +- goodwe/et.py | 2 +- goodwe/sensor.py | 8 ++++---- tests/test_sensor.py | 14 ++++++++++++++ 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/goodwe/es.py b/goodwe/es.py index e4a27a1..096c4d5 100644 --- a/goodwe/es.py +++ b/goodwe/es.py @@ -296,7 +296,7 @@ async def get_operation_mode(self) -> OperationMode | None: try: mode = OperationMode(mode_id) except ValueError: - logger.debug("Unknown work_mode value %d", mode_id) + logger.debug("Unknown work_mode value %s", mode_id) return None if OperationMode.ECO != mode: return mode diff --git a/goodwe/et.py b/goodwe/et.py index 6fec20d..a2e007c 100644 --- a/goodwe/et.py +++ b/goodwe/et.py @@ -687,7 +687,7 @@ async def get_operation_mode(self) -> OperationMode | None: try: mode = OperationMode(mode_id) except ValueError: - logger.debug("Unknown work_mode value %d", mode_id) + logger.debug("Unknown work_mode value %s", mode_id) return None if OperationMode.ECO != mode: return mode diff --git a/goodwe/sensor.py b/goodwe/sensor.py index 4831ec0..92cb55c 100644 --- a/goodwe/sensor.py +++ b/goodwe/sensor.py @@ -364,7 +364,7 @@ def read_value(self, data: ProtocolResponse): return read_decimal2(data, self.scale) def encode_value(self, value: Any, register_value: bytes = None) -> bytes: - return int.to_bytes(int(value * self.scale), length=2, byteorder="big", signed=True) + return int.to_bytes(int(float(value) * self.scale), length=2, byteorder="big", signed=True) class Float(Sensor): @@ -868,7 +868,7 @@ def read_voltage(buffer: ProtocolResponse, offset: int = None) -> float: def encode_voltage(value: Any) -> bytes: """Encode voltage value to raw (2 unsigned bytes) payload""" - return int.to_bytes(int(value * 10), length=2, byteorder="big", signed=False) + return int.to_bytes(int(float(value) * 10), length=2, byteorder="big", signed=False) def read_current(buffer: ProtocolResponse, offset: int = None) -> float: @@ -889,12 +889,12 @@ def read_current_signed(buffer: ProtocolResponse, offset: int = None) -> float: def encode_current(value: Any) -> bytes: """Encode current value to raw (2 unsigned bytes) payload""" - return int.to_bytes(int(value * 10), length=2, byteorder="big", signed=False) + return int.to_bytes(int(float(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) + return int.to_bytes(int(float(value) * 10), length=2, byteorder="big", signed=True) def read_freq(buffer: ProtocolResponse, offset: int = None) -> float: diff --git a/tests/test_sensor.py b/tests/test_sensor.py index c34d077..657e07a 100644 --- a/tests/test_sensor.py +++ b/tests/test_sensor.py @@ -30,6 +30,7 @@ def test_byteH(self): self.assertEqual(32, testee.read(data)) self.assertEqual("2039", testee.encode_value(32, bytes.fromhex("3039")).hex()) + self.assertEqual("2039", testee.encode_value("32", bytes.fromhex("3039")).hex()) self.assertEqual("ff39", testee.encode_value(-1, bytes.fromhex("3039")).hex()) self.assertEqual("7f39", testee.encode_value(127, bytes.fromhex("3039")).hex()) self.assertEqual("20ff", testee.encode_value(32, bytes.fromhex("ffff")).hex()) @@ -41,6 +42,7 @@ def test_byteL(self): self.assertEqual(127, testee.read(data)) self.assertEqual("3020", testee.encode_value(32, bytes.fromhex("3039")).hex()) + self.assertEqual("3020", testee.encode_value("32", bytes.fromhex("3039")).hex()) self.assertEqual("30ff", testee.encode_value(-1, bytes.fromhex("3039")).hex()) self.assertEqual("307f", testee.encode_value(127, bytes.fromhex("3039")).hex()) self.assertEqual("ff20", testee.encode_value(32, bytes.fromhex("ffff")).hex()) @@ -51,10 +53,12 @@ def test_integer(self): data = MockResponse("0031") self.assertEqual(49, testee.read(data)) self.assertEqual("0031", testee.encode_value(49).hex()) + self.assertEqual("0031", testee.encode_value("49").hex()) data = MockResponse("ff9e") self.assertEqual(65438, testee.read(data)) self.assertEqual("ff9e", testee.encode_value(65438).hex()) + self.assertEqual("ff9e", testee.encode_value("65438").hex()) def test_integer_signed(self): testee = IntegerS("", 0, "", "", None) @@ -62,10 +66,12 @@ def test_integer_signed(self): data = MockResponse("0031") self.assertEqual(49, testee.read(data)) self.assertEqual("0031", testee.encode_value(49).hex()) + self.assertEqual("0031", testee.encode_value("49").hex()) data = MockResponse("ff9e") self.assertEqual(-98, testee.read(data)) self.assertEqual("ff9e", testee.encode_value(-98).hex()) + self.assertEqual("ff9e", testee.encode_value("-98").hex()) def test_decimal(self): testee = Decimal("", 0, 10, "", "", None) @@ -73,10 +79,12 @@ def test_decimal(self): data = MockResponse("0031") self.assertEqual(4.9, testee.read(data)) self.assertEqual("0031", testee.encode_value(4.9).hex()) + 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()) + self.assertEqual("ff9e", testee.encode_value("-9.8").hex()) def test_voltage(self): testee = Voltage("", 0, "", None) @@ -84,10 +92,12 @@ def test_voltage(self): data = MockResponse("0cfe") self.assertEqual(332.6, testee.read(data)) self.assertEqual("0cfe", testee.encode_value(332.6).hex()) + self.assertEqual("0cfe", testee.encode_value("332.6").hex()) data = MockResponse("1f64") self.assertEqual(803.6, testee.read(data)) self.assertEqual("1f64", testee.encode_value(803.6).hex()) + self.assertEqual("1f64", testee.encode_value("803.6").hex()) data = MockResponse("a000") self.assertEqual(4096.0, testee.read(data)) @@ -101,10 +111,12 @@ def test_current(self): data = MockResponse("0031") self.assertEqual(4.9, testee.read(data)) self.assertEqual("0031", testee.encode_value(4.9).hex()) + 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()) + self.assertEqual("ff9e", testee.encode_value("6543.8").hex()) data = MockResponse("ffff") self.assertEqual(0, testee.read(data)) @@ -115,10 +127,12 @@ def test_current_signed(self): data = MockResponse("0031") self.assertEqual(4.9, testee.read(data)) self.assertEqual("0031", testee.encode_value(4.9).hex()) + 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()) + self.assertEqual("ff9e", testee.encode_value("-9.8").hex()) def test_power4(self): testee = Power4("", 0, "", None)