From 99a0b773342543309a5e44f8ae7fb322fda853bd Mon Sep 17 00:00:00 2001 From: Klaas Schoute Date: Fri, 17 Sep 2021 23:43:09 +0200 Subject: [PATCH 1/3] Improve the dataclass models --- omnikinverter/models.py | 70 ++++++++++++++++++---------------- omnikinverter/omnikinverter.py | 2 +- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/omnikinverter/models.py b/omnikinverter/models.py index e1c07d59..0f0fbb10 100644 --- a/omnikinverter/models.py +++ b/omnikinverter/models.py @@ -33,16 +33,21 @@ def from_json(data: dict[str, Any]) -> Inverter: An Inverter object. """ + def get_value(search): + if data[search] != "": + return data[search] + return None + data = json.loads(data) return Inverter( - serial_number=data["i_sn"], - model=data["i_modle"], - firmware=data["i_ver_m"], - firmware_slave=data["i_ver_s"], - solar_rated_power=data["i_pow"], - solar_current_power=int(data["i_pow_n"]), - solar_energy_today=float(data["i_eday"]), - solar_energy_total=float(data["i_eall"]), + serial_number=get_value("i_sn"), + model=get_value("i_modle"), + firmware=get_value("i_ver_m"), + firmware_slave=get_value("i_ver_s"), + solar_rated_power=get_value("i_pow"), + solar_current_power=int(get_value("i_pow_n")), + solar_energy_today=float(get_value("i_eday")), + solar_energy_total=float(get_value("i_eall")), ) @staticmethod @@ -56,7 +61,7 @@ def from_js(data: dict[str, Any]) -> Inverter: An Inverter object. """ - def get_values(position): + def get_value(position): if data.find("webData") != -1: matches = re.search(r'(?<=webData=").*?(?=";)', data) else: @@ -64,31 +69,32 @@ def get_values(position): try: data_list = matches.group(0).split(",") - if position in [4, 5, 6, 7]: - if position in [4, 5]: - return int(data_list[position]) - - if position == 6: - energy_value = float(data_list[position]) / 100 - if position == 7: - energy_value = float(data_list[position]) / 10 - return energy_value - - return data_list[position].replace(" ", "") + if data_list[position] != "": + if position in [4, 5, 6, 7]: + if position in [4, 5]: + return int(data_list[position]) + + if position == 6: + energy_value = float(data_list[position]) / 100 + if position == 7: + energy_value = float(data_list[position]) / 10 + return energy_value + return data_list[position].replace(" ", "") + return None except AttributeError as exception: raise OmnikInverterWrongSourceError( "Your inverter has no data source from a javascript file." ) from exception return Inverter( - serial_number=get_values(0), - model=get_values(3), - firmware=get_values(1), - firmware_slave=get_values(2), - solar_rated_power=get_values(4), - solar_current_power=get_values(5), - solar_energy_today=get_values(6), - solar_energy_total=get_values(7), + serial_number=get_value(0), + model=get_value(3), + firmware=get_value(1), + firmware_slave=get_value(2), + solar_rated_power=get_value(4), + solar_current_power=get_value(5), + solar_energy_today=get_value(6), + solar_energy_total=get_value(7), ) @@ -129,7 +135,7 @@ def from_js(data: dict[str, Any]) -> Device: An Device object. """ - def get_values(value_type): + def get_value(value_type): if value_type == "ip" and data.find("wanIp") != -1: match = re.search(r'(?<=wanIp=").*?(?=";)', data.replace(" ", "")) value = match.group(0) @@ -143,7 +149,7 @@ def get_values(value_type): return value return Device( - signal_quality=get_values("signal"), - firmware=get_values("version"), - ip_address=get_values("ip"), + signal_quality=get_value("signal"), + firmware=get_value("version"), + ip_address=get_value("ip"), ) diff --git a/omnikinverter/omnikinverter.py b/omnikinverter/omnikinverter.py index ae730fb2..5615629d 100644 --- a/omnikinverter/omnikinverter.py +++ b/omnikinverter/omnikinverter.py @@ -29,7 +29,7 @@ def __init__( Args: host: Hostname or IP address of the Omnik Inverter. - use_json: Boolean to confirm you use a JSON input. + use_json: Boolean to confirm you use a JSON source. request_timeout: An integer with the request timeout in seconds. session: Optional, shared, aiohttp client session. """ From 635dfe2d1487aa259a8237d1a866788894b07a91 Mon Sep 17 00:00:00 2001 From: Klaas Schoute Date: Fri, 17 Sep 2021 23:50:08 +0200 Subject: [PATCH 2/3] Update test models --- tests/fixtures/status.json | 4 ++-- tests/fixtures/status_devicearray.js | 2 +- tests/fixtures/status_webdata.js | 2 +- tests/test_models.py | 5 ++++- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/fixtures/status.json b/tests/fixtures/status.json index 18e325bb..f4289d0d 100644 --- a/tests/fixtures/status.json +++ b/tests/fixtures/status.json @@ -1,11 +1,11 @@ { "g_ver":"VER:ME-111001-V1.0.6(2015-10-16)", "ip":"192.168.0.10", - "i_sn":"12345678910", + "i_sn":"", "i_ver_m":"V1.25Build23261", "i_ver_s":"V1.40Build52927", "i_modle":"omnik2000tl2", - "i_pow":"2000", + "i_pow":"", "i_pow_n":1225, "i_eday":"10.90", "i_eall":"8674.0" diff --git a/tests/fixtures/status_devicearray.js b/tests/fixtures/status_devicearray.js index 5edbfde2..9981106a 100644 --- a/tests/fixtures/status_devicearray.js +++ b/tests/fixtures/status_devicearray.js @@ -1 +1 @@ -var version= "H4.01.51MW.2.01W1.0.64(2018-01-251-D)";var m2mRssi= "39%";var wanIp= "192.168.0.10";var myDeviceArray=new Array();myDeviceArray[0]="12345678910,V4.08Build215,V4.12Build246,Omnik1500tl ,1500,850,232,52002,,1,"; +var version= "H4.01.51MW.2.01W1.0.64(2018-01-251-D)";var m2mRssi= "39%";var wanIp= "192.168.0.10";var myDeviceArray=new Array();myDeviceArray[0]="12345678910,V4.08Build215,V4.12Build246,Omnik1500tl ,,850,232,52002,,1,"; diff --git a/tests/fixtures/status_webdata.js b/tests/fixtures/status_webdata.js index ce104475..3e4f931f 100644 --- a/tests/fixtures/status_webdata.js +++ b/tests/fixtures/status_webdata.js @@ -1 +1 @@ -var version="H4.01.38Y1.0.09W1.0.08";var m2mRssi="96%";var wanIp="192.168.0.10";var webData="12345678910,NL2-V9.8-5931,V5.3-00157,omnik2000tl2,2000,1010,488,105319,,4,"; +var version="H4.01.38Y1.0.09W1.0.08";var m2mRssi="96%";var wanIp="192.168.0.10";var webData="12345678910,NL2-V9.8-5931,V5.3-00157,omnik2000tl2,,1010,488,105319,,4,"; diff --git a/tests/test_models.py b/tests/test_models.py index 92982557..637afdf4 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -29,6 +29,7 @@ async def test_inverter_js_webdata(aresponses): assert inverter.firmware == "NL2-V9.8-5931" assert inverter.firmware_slave == "V5.3-00157" assert inverter.model == "omnik2000tl2" + assert inverter.solar_rated_power is None assert inverter.solar_current_power == 1010 assert inverter.solar_energy_today == 4.88 assert inverter.solar_energy_total == 10531.9 @@ -79,6 +80,7 @@ async def test_inverter_js_devicearray(aresponses): assert inverter.firmware == "V4.08Build215" assert inverter.firmware_slave == "V4.12Build246" assert inverter.model == "Omnik1500tl" + assert inverter.solar_rated_power is None assert inverter.solar_current_power == 850 assert inverter.solar_energy_today == 2.32 assert inverter.solar_energy_total == 5200.2 @@ -125,10 +127,11 @@ async def test_inverter_json(aresponses): omnik = OmnikInverter(host="example.com", use_json=True, session=session) inverter: Inverter = await omnik.inverter() assert inverter - assert inverter.serial_number == "12345678910" + assert inverter.serial_number is None assert inverter.firmware == "V1.25Build23261" assert inverter.firmware_slave == "V1.40Build52927" assert inverter.model == "omnik2000tl2" + assert inverter.solar_rated_power is None assert inverter.solar_current_power == 1225 assert inverter.solar_energy_today == 10.90 assert inverter.solar_energy_total == 8674.0 From 58ae5410d219aa22b26a36a4d8508af061f66b53 Mon Sep 17 00:00:00 2001 From: Klaas Schoute Date: Fri, 17 Sep 2021 23:58:57 +0200 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=93=9A=20Update=20the=20documentation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index c033778f..e5993ee2 100644 --- a/README.md +++ b/README.md @@ -94,18 +94,18 @@ You need at least: - Python 3.8+ - [Poetry][poetry-install] -Create a virtualenv and enter it: +To install all packages, including all development requirements: ```bash -python3 -m venv venv -source venv/bin/activate +poetry install +pre-commit install ``` -To install all packages, including all development requirements: +Poetry creates by default an virtual environment where it installs all necessary pip packages, to enter or exit the venv run the following commands: ```bash -poetry install -pre-commit install +poetry shell +exit ``` As this repository uses the [pre-commit][pre-commit] framework, all changes