Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new exception for JSON inverter models #31

Merged
merged 3 commits into from
Sep 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions omnikinverter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""Asynchronous Python client for the Omnik Inverter."""

from .models import Device, Inverter, OmnikInverterWrongSourceError
from .omnikinverter import (
OmnikInverter,
from .exceptions import (
OmnikInverterConnectionError,
OmnikInverterError,
OmnikInverterWrongSourceError,
OmnikInverterWrongValuesError,
)
from .models import Device, Inverter
from .omnikinverter import OmnikInverter

__all__ = [
"Device",
Expand All @@ -14,4 +16,5 @@
"OmnikInverterError",
"OmnikInverterConnectionError",
"OmnikInverterWrongSourceError",
"OmnikInverterWrongValuesError",
]
9 changes: 9 additions & 0 deletions omnikinverter/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ class OmnikInverterConnectionError(OmnikInverterError):

class OmnikInverterWrongSourceError(OmnikInverterError):
"""Omnik Inverter wrong data source url exception."""


class OmnikInverterWrongValuesError(OmnikInverterError):
"""Omnik Inverter gives wrong values.

This error appears when your inverter
shows the same value for both day and
total production.
"""
25 changes: 23 additions & 2 deletions omnikinverter/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from dataclasses import dataclass
from typing import Any

from .exceptions import OmnikInverterWrongSourceError
from .exceptions import OmnikInverterWrongSourceError, OmnikInverterWrongValuesError


@dataclass
Expand All @@ -31,14 +31,35 @@ def from_json(data: dict[str, Any]) -> Inverter:

Returns:
An Inverter object.

Raises:
OmnikInverterWrongValuesError: Inverter pass on
incorrect data (day and total are equal).
"""
data = json.loads(data)

def get_value(search):
if data[search] != "":
return data[search]
return None

data = json.loads(data)
def validation(data_list):
"""Check if the values are not equal to each other.

Args:
data_list: List of values to check.

Returns:
Boolean value.
"""
res = all(ele == data_list[0] for ele in data_list)
return res

if validation([data["i_eday"], data["i_eall"]]):
raise OmnikInverterWrongValuesError(
"Inverter pass on incorrect data (day and total are equal)"
)

return Inverter(
serial_number=get_value("i_sn"),
model=get_value("i_modle"),
Expand Down
12 changes: 12 additions & 0 deletions tests/fixtures/wrong_status.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"g_ver":"VER:ME-111001-V1.0.6(2015-10-16)",
"ip":"192.168.0.10",
"i_sn":"",
"i_ver_m":"V1.25Build23261",
"i_ver_s":"V1.40Build52927",
"i_modle":"omnik2000tl2",
"i_pow":"",
"i_pow_n":1225,
"i_eday":"0.0",
"i_eall":"0.0"
}
29 changes: 25 additions & 4 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

from omnikinverter import Device, Inverter, OmnikInverter
from omnikinverter.exceptions import OmnikInverterWrongValuesError

from . import load_fixtures

Expand Down Expand Up @@ -124,8 +125,8 @@ async def test_inverter_json(aresponses):
)

async with aiohttp.ClientSession() as session:
omnik = OmnikInverter(host="example.com", use_json=True, session=session)
inverter: Inverter = await omnik.inverter()
client = OmnikInverter(host="example.com", use_json=True, session=session)
inverter: Inverter = await client.inverter()
assert inverter
assert inverter.serial_number is None
assert inverter.firmware == "V1.25Build23261"
Expand All @@ -152,9 +153,29 @@ async def test_device_json(aresponses):
)

async with aiohttp.ClientSession() as session:
omnik = OmnikInverter(host="example.com", use_json=True, session=session)
device: Device = await omnik.device()
client = OmnikInverter(host="example.com", use_json=True, session=session)
device: Device = await client.device()
assert device
assert device.signal_quality is None
assert device.firmware == "ME-111001-V1.0.6(2015-10-16)"
assert device.ip_address == "192.168.0.10"


@pytest.mark.asyncio
async def test_wrong_values(aresponses):
"""Test on wrong inverter values."""
aresponses.add(
"example.com",
"/status.json",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text=load_fixtures("wrong_status.json"),
),
)

async with aiohttp.ClientSession() as session:
client = OmnikInverter(host="example.com", use_json=True, session=session)
with pytest.raises(OmnikInverterWrongValuesError):
assert await client.inverter()