Skip to content

Commit

Permalink
coverage and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
altendky committed Jan 14, 2021
1 parent 51859ae commit 5c24bcf
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
43 changes: 43 additions & 0 deletions src/ssst/_tests/sunspec/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,46 @@ async def test_write_point(

server_point = sunspec_server.server[1].points["DA"]
assert server_point.cvalue == new_id


async def test_write_point_with_scale_factor(
sunspec_server: ssst._tests.conftest.SunSpecServerFixtureResult,
sunspec_client: ssst.sunspec.client.Client,
) -> None:
point = sunspec_client[103].points["W"]
scale_factor_point = point.model.points[point.sf]

scale_factor = -2
scaled_watts = 273

server_point = sunspec_server.server[103].points["W"]
server_scale_factor_point = server_point.model.points[server_point.sf]

server_scale_factor_point.cvalue = scale_factor
server_point.cvalue = 0

scale_factor_point.cvalue = 0
point.cvalue = scaled_watts

await sunspec_client.write_point(point=point)

assert point.cvalue == server_point.cvalue == scaled_watts
assert scale_factor_point.cvalue == server_scale_factor_point.cvalue == scale_factor

# assert scale_factor_point.cvalue == server_scale_factor_point.cvalue
# assert server_scale_factor_point.cvalue == scale_factor
# assert server_point.cvalue == scaled_watts


async def test_read_modbus_exception_raises(
sunspec_client: ssst.sunspec.client.Client,
):
with pytest.raises(ssst.ModbusError):
await sunspec_client.read_registers(address=0, count=1)


async def test_write_modbus_exception_raises(
sunspec_client: ssst.sunspec.client.Client,
):
with pytest.raises(ssst.ModbusError):
await sunspec_client.write_registers(address=0, values=b":]")
12 changes: 11 additions & 1 deletion src/ssst/sunspec/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,15 @@ async def read_point(
count=point.len,
)
point.set_mb(data=read_bytes)

if point.pdef["type"] == "sunssf":
for other_point in point.model.points.values():
if other_point.sf == point.pdef["name"]:
other_cvalue = other_point.cvalue
other_point.sf_value = point.cvalue
if other_cvalue is not None:
other_point.cvalue = other_cvalue

return point.cvalue # type: ignore[no-any-return]

def point_address(
Expand Down Expand Up @@ -250,5 +259,6 @@ async def write_point(

bytes_to_write = point.get_mb()
await self.write_registers(
address=self.point_address(point=point), values=bytes_to_write
address=self.point_address(point=point),
values=bytes_to_write,
)
2 changes: 1 addition & 1 deletion src/ssst/sunspec/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def setValues(self, fx: int, address: int, values: bytes) -> None:
request = PreparedRequest.build(
base_address=self.sunspec_device.base_addr,
requested_address=address,
count=len(values),
count=len(values) // 2,
all_registers=self.sunspec_device.get_mb(),
)
data = bytearray(request.data)
Expand Down

0 comments on commit 5c24bcf

Please sign in to comment.