dewet22 / givenergy-modbus

A python library to access GivEnergy inverters via Modbus TCP on a local network, with no dependency on the GivEnergy Cloud.
Other
21 stars 14 forks source link

Querying inverter fails with ValueError: hour must be in 0..23 #8

Closed martinbark closed 2 years ago

martinbark commented 2 years ago

Description

I was trying to query my Giv-AC 3.0 but the code fails

What I Did

This is my test code

from givenergy_modbus.client import GivEnergyClient
from givenergy_modbus.model.plant import Plant

client = GivEnergyClient(host="192.168.99.99")

p = Plant(number_batteries=1)
client.refresh_plant(p, full_refresh=True)

print(p.inverter.json())

and it fails with the following error

Traceback (most recent call last):
  File "/home/martin/test/./test.py", line 8, in <module>
    print(p.inverter.json())
  File "/home/martin/test/.env/lib/python3.10/site-packages/givenergy_modbus/model/plant.py", line 32, in inverter
    return Inverter.from_orm(self.inverter_rc)
  File "pydantic/main.py", line 562, in pydantic.main.BaseModel.from_orm
  File "pydantic/main.py", line 1001, in pydantic.main.validate_model
  File "/home/martin/test/.env/lib/python3.10/site-packages/givenergy_modbus/model/register_getter.py", line 61, in get
    return self.get(f'{key}_start'), self.get(f'{key}_end')
  File "/home/martin/test/.env/lib/python3.10/site-packages/givenergy_modbus/model/register_getter.py", line 69, in get
    return getattr(self._obj, key, default)
  File "/home/martin/test/.env/lib/python3.10/site-packages/givenergy_modbus/model/register_cache.py", line 27, in __getattr__
    return register.convert(val)
  File "/home/martin/test/.env/lib/python3.10/site-packages/givenergy_modbus/model/register.py", line 155, in convert
    return self.type.convert(val, self.scaling.value)
  File "/home/martin/test/.env/lib/python3.10/site-packages/givenergy_modbus/model/register.py", line 49, in convert
    return time(hour=int(f'{value:04}'[:2]), minute=int(f'{value:04}'[2:]) % 60)
ValueError: hour must be in 0..23

After some debug it seems discharge_slot_1 end time is 2400. As a work around i added the following to givenergy_modbus/model/register.py when handling time

if value >= 2400:
  value = 0

This fixed the issues and i can now query the inverter. All the other values look correct.

dewet22 commented 2 years ago

Thanks for the report, and apologies for the delayed fix. Will be going out with the next release in a few days.