olivergregorius / sun2000_modbus

Library for reading Huawei Sun2000 inverter metrics via Modbus TCP
MIT License
23 stars 9 forks source link

Python 3.9 retrocompatibility / Format using locale #27

Closed hchiper closed 3 months ago

hchiper commented 10 months ago

First, thank a lot for sun2000_modbus. I am using it with Python 3.9, so in sun2000_modbus/datatypes.py I had to replace the match/case/case... by if/elif/elif.../else in decode(). I also found very convenient to modify sun2000_modbus/inverter.py in order to benefit from the locale formatting in read_formatted(). To achieve this goal, I modified it in the following way:

def read_formatted(self, register, use_locale=False):  # added the argument use_locale
    # (...)
    if register.value.unit is not None:
        if use_locale:
            return f'{value:n} {register.value.unit}'  # added the :n to format according to the locale's thousand separator, decimal sign
        else:
            return f'{value} {register.value.unit}'
    # (...)

Maybe you could consider worth to include these modifications in your sources.

olivergregorius commented 10 months ago

Hi @hchiper, thanks for your contribution. What do you think about creating a Pull Request containing your changes? After a review I could simply merge it and create a new release.