Skip to content

Commit

Permalink
Add new data entities to dataclass model (#16)
Browse files Browse the repository at this point in the history
* Update data in models

* Add exception for wrong data source

* Add extra tests for pytest
  • Loading branch information
klaasnicolaas committed Aug 31, 2021
1 parent 0e3473e commit 3742080
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 20 deletions.
3 changes: 2 additions & 1 deletion omnikinverter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Asynchronous Python client for the Omnik Inverter."""

from .models import Inverter
from .models import Inverter, OmnikInverterWrongSourceError
from .omnikinverter import (
OmnikInverter,
OmnikInverterConnectionError,
Expand All @@ -12,4 +12,5 @@
"OmnikInverter",
"OmnikInverterError",
"OmnikInverterConnectionError",
"OmnikInverterWrongSourceError",
]
4 changes: 4 additions & 0 deletions omnikinverter/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ class OmnikInverterError(Exception):

class OmnikInverterConnectionError(OmnikInverterError):
"""Omnik Inverter connection exception."""


class OmnikInverterWrongSourceError(OmnikInverterError):
"""Omnik Inverter wrong data source url exception."""
35 changes: 24 additions & 11 deletions omnikinverter/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
from dataclasses import dataclass
from typing import Any

from .exceptions import OmnikInverterWrongSourceError


@dataclass
class Inverter:
"""Object representing an Inverter response from Omnik Inverter."""

serial_number: str | None
model: str | None
firmware: str | None
firmware_main: str | None
firmware_slave: str | None
solar_rated_power: int | None
solar_current_power: int | None
solar_energy_today: float | None
solar_energy_total: float | None
Expand All @@ -31,10 +35,12 @@ def from_json(data: dict[str, Any]) -> Inverter:

data = json.loads(data)
return Inverter(
serial_number=data["g_sn"],
serial_number=data["i_sn"],
model=data["i_modle"],
firmware=data["i_ver_m"],
solar_current_power=data["i_pow_n"],
firmware_main=data["i_ver_m"],
firmware_slave=data["i_ver_s"],
solar_rated_power=int(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"]),
)
Expand All @@ -56,17 +62,24 @@ def get_values(position):
else:
matches = re.search(r'(?<=myDeviceArray\[0\]=").*?(?=";)', data)

data_list = matches.group(0).split(",")
if position in [5, 6, 7]:
if position == 5:
return int(data_list[position])
return float(data_list[position]) / 100
return data_list[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])
return float(data_list[position]) / 100
return data_list[position]
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(2),
firmware_main=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),
Expand Down
4 changes: 3 additions & 1 deletion test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ async def main():
print()
print(f"Serial Number: {inverter.serial_number}")
print(f"Model: {inverter.model}")
print(f"Firmware: {inverter.firmware}")
print(f"Firmware Main: {inverter.firmware_main}")
print(f"Firmware Slave: {inverter.firmware_slave}")
print(f"Rated Power: {inverter.solar_rated_power}")
print(f"Current Power: {inverter.solar_current_power}")
print(f"Energy Production Today: {inverter.solar_energy_today}")
print(f"Energy Production Total: {inverter.solar_energy_total}")
Expand Down
3 changes: 1 addition & 2 deletions tests/fixtures/status.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"g_sn":"12345678910",
"g_ver":"VER:ME-111001-V1.0.6(2015-10-16)",
"i_sn":"12345678910",
"i_ver_m":"V1.25Build23261",
"i_ver_s":"V1.40Build52927",
"i_modle":"omnik2000tl2",
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/wrong_status.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 49 additions & 5 deletions tests/test_input.py → tests/test_omnik.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
"""Test for retrieving information from the Omnik Inverter device."""
import asyncio

import aiohttp
import pytest

from omnikinverter import Inverter, OmnikInverter
from omnikinverter import (
Inverter,
OmnikInverter,
OmnikInverterConnectionError,
OmnikInverterWrongSourceError,
)

from . import load_fixtures

Expand All @@ -24,9 +31,10 @@ async def test_js_input(aresponses):
omnik = OmnikInverter(host="example.com", use_json=False, session=session)
inverter: Inverter = await omnik.inverter()
assert inverter
assert inverter.firmware == "V5.3-00157"
assert inverter.model == "omnik2000tl2"
assert inverter.serial_number == "12345678910"
assert inverter.firmware_main == "NL2-V9.8-5931"
assert inverter.firmware_slave == "V5.3-00157"
assert inverter.model == "omnik2000tl2"
assert inverter.solar_current_power == 1010
assert inverter.solar_energy_today == 4.88
assert inverter.solar_energy_total == 1053.19
Expand All @@ -49,9 +57,45 @@ async def test_json_input(aresponses):
omnik = OmnikInverter(host="example.com", use_json=True, session=session)
inverter: Inverter = await omnik.inverter()
assert inverter
assert inverter.firmware == "V1.25Build23261"
assert inverter.model == "omnik2000tl2"
assert inverter.serial_number == "12345678910"
assert inverter.firmware_slave == "V1.40Build52927"
assert inverter.firmware_main == "V1.25Build23261"
assert inverter.model == "omnik2000tl2"
assert inverter.solar_current_power == 1225
assert inverter.solar_energy_today == 10.90
assert inverter.solar_energy_total == 8674.0


@pytest.mark.asyncio
async def test_wrong_source(aresponses):
"""Test on wrong data source error raise."""
aresponses.add(
"example.com",
"/js/status.js",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/x-javascript"},
text=load_fixtures("wrong_status.js"),
),
)
async with aiohttp.ClientSession() as session:
omnik = OmnikInverter(host="example.com", use_json=False, session=session)
with pytest.raises(OmnikInverterWrongSourceError):
assert await omnik.inverter()


@pytest.mark.asyncio
async def test_timeout(aresponses):
"""Test request timeout from Omnik Inverter."""
# Faking a timeout by sleeping
async def response_handler(_):
await asyncio.sleep(0.2)
return aresponses.Response(body="Goodmorning!", text=load_fixtures("status.js"))

aresponses.add("example.com", "/js/status.js", "GET", response_handler)

async with aiohttp.ClientSession() as session:
omnik = OmnikInverter(host="example.com", use_json=False, session=session)
with pytest.raises(OmnikInverterConnectionError):
assert await omnik.inverter()

0 comments on commit 3742080

Please sign in to comment.