adjust tests to new naming

This commit is contained in:
Paul Warren 2024-10-14 20:19:38 +11:00
parent 733997f247
commit 65b84d1503
6 changed files with 46 additions and 63 deletions

View file

@ -38,4 +38,3 @@ print("Post CRate: %d%%" % charge)
mode = froniusreg.StorCtl_Mode.getValue(fronius1)
print("Post Mode: %d" % mode)

View file

@ -6,6 +6,7 @@ import os
def lint(session):
session.install("ruff")
session.run("ruff", "check", "--exclude", "examples")
session.run("ruff", "check", "--extend-select", "N", "src/pyfroniusreg/froniusreg.py")
@nox.session

View file

@ -61,21 +61,15 @@ string16 = DataType(8, decode_string16, BinaryPayloadBuilder.add_string)
string32 = DataType(16, decode_string32, BinaryPayloadBuilder.add_string)
int16 = DataType(1, BinaryPayloadDecoder.decode_16bit_int, encode_16bit_int)
uint16 = DataType(1, BinaryPayloadDecoder.decode_16bit_uint, encode_16bit_int)
int32 = DataType(
2, BinaryPayloadDecoder.decode_32bit_int, BinaryPayloadBuilder.add_32bit_int
)
uint32 = DataType(
2, BinaryPayloadDecoder.decode_32bit_uint, BinaryPayloadBuilder.add_32bit_uint
)
int32 = DataType(2, BinaryPayloadDecoder.decode_32bit_int, BinaryPayloadBuilder.add_32bit_int)
uint32 = DataType(2, BinaryPayloadDecoder.decode_32bit_uint, BinaryPayloadBuilder.add_32bit_uint)
float32 = DataType(
2, BinaryPayloadDecoder.decode_32bit_float, BinaryPayloadBuilder.add_32bit_float
)
uint64 = DataType(
4, BinaryPayloadDecoder.decode_64bit_uint, BinaryPayloadBuilder.add_64bit_uint
)
uint64 = DataType(4, BinaryPayloadDecoder.decode_64bit_uint, BinaryPayloadBuilder.add_64bit_uint)
class registerReadError(Exception):
class RegisterReadError(Exception):
pass
@ -100,46 +94,46 @@ class FroniusReg:
self.unit = unit
self.description = description
def getValue(self, modbusClient):
return self.__getRegisterValue(modbusClient)
def get(self, modbus_client):
return self.__get_register_value(modbus_client)
def setValue(self, modbusClient, value):
return self.__setRegisterValue(modbusClient, value)
def set(self, modbus_client, value):
return self._set_register_value(modbus_client, value)
def __getRegisterValue(self, modbusClient):
modbusValue = modbusClient.read_holding_registers(
def __get_register_value(self, modbus_client):
modbus_value = modbus_client.read_holding_registers(
self.address - 1, self.datatype.width, slave=self.unit
)
if modbusValue.isError():
raise registerReadError(
if modbus_value.isError():
raise RegisterReadError(
"Unable to read from Fronius Register: %d, %s\n%s"
% (self.address, self.description, modbusValue)
% (self.address, self.description, modbus_value)
)
if modbusValue is None:
raise registerReadError("It's NONE!")
return self.datatype.decode_from_register(modbusValue.registers)
if modbus_value is None:
raise RegisterReadError("It's NONE!")
return self.datatype.decode_from_register(modbus_value.registers)
def __setRegisterValue(self, modbusClient, value):
modbusValue = modbusClient.write_registers(
def _set_register_value(self, modbus_client, value):
modbus_value = modbus_client.write_registers(
self.address - 1,
self.datatype.encode_to_buffer(value),
slave=self.unit,
skip_encode=True,
)
return modbusValue
return modbus_value
class ScaledFroniusReg:
def __init__(self, valueReg, scaleReg):
self.valueReg = valueReg
self.scaleReg = scaleReg
def __init__(self, value_register, scale_register):
self.value_register = value_register
self.scale_register = scale_register
def getValue(self, modbusClient):
return self.valueReg.getValue(modbusClient) * 10 ** self.scaleReg.getValue(
modbusClient
def get(self, modbus_client):
return self.value_register.get(modbus_client) * 10 ** self.scale_register.get(
modbus_client
)
def setValue(self, modbusClient, value):
return self.valueReg.setValue(
modbusClient, value / 10 ** self.scaleReg.getValue(modbusClient)
def set(self, modbus_client, value):
return self.value_register.set(
modbus_client, value / 10 ** self.scale_register.get(modbus_client)
)

View file

@ -10,9 +10,7 @@ storageStateOfCharge = froniusreg.FroniusReg(
storageStateOfChargeSF = froniusreg.FroniusReg(
40376, froniusreg.int16, 1, "Storage State of Charge Scaling Factor"
)
scaledStateOfCharge = froniusreg.ScaledFroniusReg(
storageStateOfCharge, storageStateOfChargeSF
)
scaledStateOfCharge = froniusreg.ScaledFroniusReg(storageStateOfCharge, storageStateOfChargeSF)
ID = froniusreg.FroniusReg(
40003,
@ -20,9 +18,7 @@ ID = froniusreg.FroniusReg(
1,
"Well-known value. Uniquely identifies this as a sunspec model 'common' (1)",
)
L = froniusreg.FroniusReg(
40004, froniusreg.uint16, 1, "Sunspec model commen register count"
)
L = froniusreg.FroniusReg(40004, froniusreg.uint16, 1, "Sunspec model commen register count")
Mn = froniusreg.FroniusReg(40005, froniusreg.string16, 1, "Manufacturer")
Md = froniusreg.FroniusReg(40021, froniusreg.string16, 1, "Device Model")
Vr = froniusreg.FroniusReg(40045, froniusreg.string8, 1, "SW version")
@ -50,12 +46,8 @@ DCW_SF = froniusreg.FroniusReg(40268, froniusreg.int16, 1, "DC Power Scaling fac
OutWRte = froniusreg.FroniusReg(40366, froniusreg.int16, 1, "DischargeRate")
InWRte = froniusreg.FroniusReg(40367, froniusreg.int16, 1, "ChargeRate")
WRteSF = froniusreg.FroniusReg(
40379, froniusreg.int16, 1, "ScalingFactor for storage Watts"
)
StorCtl_Mode = froniusreg.FroniusReg(
40359, froniusreg.uint16, 1, "Hold/Charge/Discharge limit"
)
WRteSF = froniusreg.FroniusReg(40379, froniusreg.int16, 1, "ScalingFactor for storage Watts")
StorCtl_Mode = froniusreg.FroniusReg(40359, froniusreg.uint16, 1, "Hold/Charge/Discharge limit")
MinRsvPct = froniusreg.FroniusReg(40361, froniusreg.uint16, 1, "Reserve Percentage")
InOutWRte_RvrtTms = froniusreg.FroniusReg(
40369, froniusreg.uint16, 1, "Revert timer for charge settings"
@ -63,12 +55,8 @@ InOutWRte_RvrtTms = froniusreg.FroniusReg(
ChaGriSet = froniusreg.FroniusReg(
40371, froniusreg.uint16, 1, "enum16, 0 = PV only, 1 = Grid enabled"
)
WChaDisChaGra_SF = froniusreg.FroniusReg(
40373, froniusreg.int16, 1, "Charge/Discharge Power SF"
)
MinRsvPct_SF = froniusreg.FroniusReg(
40375, froniusreg.int16, 1, "Reserve Percentage Scaling"
)
WChaDisChaGra_SF = froniusreg.FroniusReg(40373, froniusreg.int16, 1, "Charge/Discharge Power SF")
MinRsvPct_SF = froniusreg.FroniusReg(40375, froniusreg.int16, 1, "Reserve Percentage Scaling")
scaledOutWRte = froniusreg.ScaledFroniusReg(OutWRte, WRteSF)
scaledInWRte = froniusreg.ScaledFroniusReg(InWRte, WRteSF)

View file

@ -3,6 +3,7 @@ import unittest
from pyfroniusreg import froniusreg
class TestDataTypes(unittest.TestCase):
def test_int16(self):
int16_buffer = froniusreg.int16.encode_to_buffer(1024)

View file

@ -11,39 +11,39 @@ fronius1.connect()
class TestRead(unittest.TestCase):
def test_read_scaled(self):
soc = gen24_registers.scaledStateOfCharge.getValue(fronius1)
soc = gen24_registers.scaledStateOfCharge.get(fronius1)
assert isinstance(soc, float)
def test_read_direct(self):
dr = gen24_registers.OutWRte.getValue(fronius1)
dr = gen24_registers.OutWRte.get(fronius1)
assert isinstance(dr, int)
def test_read_string16(self):
Mn = gen24_registers.Mn.getValue(fronius1)
Mn = gen24_registers.Mn.get(fronius1)
assert Mn[0:7] == "Fronius"
def test_read_string8(self):
# this isn't a good test, this value changes regularly
Vr = gen24_registers.Vr.getValue(fronius1)
Vr = gen24_registers.Vr.get(fronius1)
assert Vr == "1.33.7-1"
def test_read_model(self):
Md = gen24_registers.Md.getValue(fronius1)
Md = gen24_registers.Md.get(fronius1)
assert Md == "Primo GEN24 5.0\x00"
# def test_read_sn(self):
# This doesn't seem to return anything useful
# SN = gen24_registers.SN.getValue(fronius1)
# SN = gen24_registers.SN.get(fronius1)
# assert SN == "12345567"
def test_write_direct(self):
current = gen24_registers.OutWRte.getValue(fronius1)
retval = gen24_registers.OutWRte.setValue(fronius1, current)
current = gen24_registers.OutWRte.get(fronius1)
retval = gen24_registers.OutWRte.set(fronius1, current)
assert retval is not None
def test_write_scaled(self):
current = gen24_registers.scaledInWRte.getValue(fronius1)
retval = gen24_registers.scaledInWRte.setValue(fronius1, current)
current = gen24_registers.scaledInWRte.get(fronius1)
retval = gen24_registers.scaledInWRte.set(fronius1, current)
assert retval is not None