Skip to content

Commit

Permalink
Allow strings to be passed to set_parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
mletenay committed May 24, 2024
1 parent 1c06fbf commit 46e3ff7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion goodwe/es.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion goodwe/et.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions goodwe/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
Expand All @@ -51,43 +53,51 @@ 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)

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)

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)

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))
Expand All @@ -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))
Expand All @@ -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)
Expand Down

0 comments on commit 46e3ff7

Please sign in to comment.