mvandersteen / SungrowInverter

Allows to pull data from a Sungrow residential inverter via Modbus TCP
Other
28 stars 16 forks source link

import error #2

Closed hallonstedt closed 2 years ago

hallonstedt commented 2 years ago

I would like to get data out of my inverter but I seem unable to import the SungrowInverter library. pip3 successfully installed both SungrowModbusTcpClient-0.1.6 and SungrowInverter-0.1.7 but when I try to import the module I get a TypeError that I am unable to figure out the reason for.

`PI:~# python3 Python 3.7.3 (default, Jul 25 2020, 13:03:44) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information.

from sungrowinverter import SungrowInverter Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.7/dist-packages/sungrowinverter/init.py", line 4, in from sungrowinverter.SungrowInverter import SungrowInverter File "/usr/local/lib/python3.7/dist-packages/sungrowinverter/SungrowInverter.py", line 16, in from sungrowinverter.configs.inverter import ( File "/usr/local/lib/python3.7/dist-packages/sungrowinverter/configs/inverter.py", line 21, in ModBusRegister(5001, "nominal_output_power", "U16", 0.1, KILO_WATT), TypeError: 'type' object is not subscriptable `

hallonstedt commented 2 years ago

After some additional trolling on the Internet, I realized that the problem is that the code is using type hinting, which was introduced in python 3.9. The setup.cfg file incorrectly lists python >= 3.5 as requirements.

The workaround, if you would like to avoid upgrading python, is to use the typing library to avoid problems with generic typing. See example diff for configs/inverter.py;

PI:~# diff /usr/src/SungrowInverter/sungrowinverter/configs/inverter.py /usr/local/lib/python3.7/dist-packages/sungrowinverter/configs/inverter.py
12a13,14
> from typing import Tuple, List
18c20
< INVERTER_READ_REGISTERS: tuple[ModBusRegister, ...] = (
---
> INVERTER_READ_REGISTERS: Tuple[ModBusRegister, ...] = (
24c26
< INVERTER_HOLDING_REGISTERS: tuple[ModBusRegister, ...] = (
---
> INVERTER_HOLDING_REGISTERS: Tuple[ModBusRegister, ...] = (
30c32
< INVERTER_MODELS: list[SungrowInverterModel] = (
---
> INVERTER_MODELS: List[SungrowInverterModel] = (

Repeat procedure for hybrid.py and string.py and it will work like a charm!