Skip to content

Commit

Permalink
Add new exception for JSON inverter models (#31)
Browse files Browse the repository at this point in the history
* Add new exception for wrong values from JSON

* Add extra check for JSON inverters

* Add test for the new exception
  • Loading branch information
klaasnicolaas committed Sep 19, 2021
1 parent cac98a5 commit 66492cf
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 9 deletions.
9 changes: 6 additions & 3 deletions omnikinverter/__init__.py
@@ -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
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
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
@@ -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
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()

0 comments on commit 66492cf

Please sign in to comment.