diff --git a/omnikinverter/models.py b/omnikinverter/models.py index 6298434c..4ec13123 100644 --- a/omnikinverter/models.py +++ b/omnikinverter/models.py @@ -74,7 +74,7 @@ def get_values(position): energy_value = float(data_list[position]) / 10 return energy_value - return data_list[position] + return data_list[position].replace(" ", "") except AttributeError as exception: raise OmnikInverterWrongSourceError( "Your inverter has no data source from a javascript file." diff --git a/tests/fixtures/status_devicearray.js b/tests/fixtures/status_devicearray.js new file mode 100644 index 00000000..ccce88eb --- /dev/null +++ b/tests/fixtures/status_devicearray.js @@ -0,0 +1,3 @@ +var myDeviceArray=new Array(); +myDeviceArray[0]="12345678910,V4.08Build215,V4.12Build246,Omnik1500tl ,1500,850,232,52002,,1,"; +; diff --git a/tests/fixtures/status.js b/tests/fixtures/status_webdata.js similarity index 100% rename from tests/fixtures/status.js rename to tests/fixtures/status_webdata.js diff --git a/tests/fixtures/wrong_status.js b/tests/fixtures/status_wrong.js similarity index 100% rename from tests/fixtures/wrong_status.js rename to tests/fixtures/status_wrong.js diff --git a/tests/test_models.py b/tests/test_models.py new file mode 100644 index 00000000..015161d5 --- /dev/null +++ b/tests/test_models.py @@ -0,0 +1,88 @@ +"""Test the models.""" +import aiohttp +import pytest + +from omnikinverter import Inverter, OmnikInverter + +from . import load_fixtures + + +@pytest.mark.asyncio +async def test_js_webdata(aresponses): + """Test request from a JS Webdata source.""" + aresponses.add( + "example.com", + "/js/status.js", + "GET", + aresponses.Response( + status=200, + headers={"Content-Type": "application/x-javascript"}, + text=load_fixtures("status_webdata.js"), + ), + ) + + async with aiohttp.ClientSession() as session: + omnik = OmnikInverter(host="example.com", session=session) + inverter: Inverter = await omnik.inverter() + assert inverter + 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 == 10531.9 + + +@pytest.mark.asyncio +async def test_js_devicearray(aresponses): + """Test request from a JS DeviceArray source.""" + aresponses.add( + "example.com", + "/js/status.js", + "GET", + aresponses.Response( + status=200, + headers={"Content-Type": "application/x-javascript"}, + text=load_fixtures("status_devicearray.js"), + ), + ) + + async with aiohttp.ClientSession() as session: + omnik = OmnikInverter(host="example.com", session=session) + inverter: Inverter = await omnik.inverter() + assert inverter + assert inverter.serial_number == "12345678910" + assert inverter.firmware_main == "V4.08Build215" + assert inverter.firmware_slave == "V4.12Build246" + assert inverter.model == "Omnik1500tl" + assert inverter.solar_current_power == 850 + assert inverter.solar_energy_today == 2.32 + assert inverter.solar_energy_total == 5200.2 + + +@pytest.mark.asyncio +async def test_json(aresponses): + """Test request from a JSON source.""" + aresponses.add( + "example.com", + "/status.json", + "GET", + aresponses.Response( + status=200, + headers={"Content-Type": "application/json"}, + text=load_fixtures("status.json"), + ), + ) + + async with aiohttp.ClientSession() as session: + omnik = OmnikInverter(host="example.com", use_json=True, session=session) + inverter: Inverter = await omnik.inverter() + assert inverter + 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 diff --git a/tests/test_omnik.py b/tests/test_omnik.py index 95867916..41525cfb 100644 --- a/tests/test_omnik.py +++ b/tests/test_omnik.py @@ -1,71 +1,19 @@ -"""Test for retrieving information from the Omnik Inverter device.""" +"""Basic tests for the Omnik Inverter device.""" import asyncio import aiohttp import pytest from omnikinverter import ( - Inverter, OmnikInverter, OmnikInverterConnectionError, OmnikInverterWrongSourceError, ) +from omnikinverter.exceptions import OmnikInverterError from . import load_fixtures -@pytest.mark.asyncio -async def test_js_input(aresponses): - """Test request from a JS input.""" - aresponses.add( - "example.com", - "/js/status.js", - "GET", - aresponses.Response( - status=200, - headers={"Content-Type": "application/x-javascript"}, - text=load_fixtures("status.js"), - ), - ) - async with aiohttp.ClientSession() as session: - omnik = OmnikInverter(host="example.com", use_json=False, session=session) - inverter: Inverter = await omnik.inverter() - assert inverter - 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 == 10531.9 - - -@pytest.mark.asyncio -async def test_json_input(aresponses): - """Test request from a JSON input.""" - aresponses.add( - "example.com", - "/status.json", - "GET", - aresponses.Response( - status=200, - headers={"Content-Type": "application/json"}, - text=load_fixtures("status.json"), - ), - ) - async with aiohttp.ClientSession() as session: - omnik = OmnikInverter(host="example.com", use_json=True, session=session) - inverter: Inverter = await omnik.inverter() - assert inverter - 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.""" @@ -76,11 +24,12 @@ async def test_wrong_source(aresponses): aresponses.Response( status=200, headers={"Content-Type": "application/x-javascript"}, - text=load_fixtures("wrong_status.js"), + text=load_fixtures("status_wrong.js"), ), ) + async with aiohttp.ClientSession() as session: - omnik = OmnikInverter(host="example.com", use_json=False, session=session) + omnik = OmnikInverter(host="example.com", session=session) with pytest.raises(OmnikInverterWrongSourceError): assert await omnik.inverter() @@ -91,11 +40,45 @@ async def test_timeout(aresponses): # Faking a timeout by sleeping async def response_handler(_): await asyncio.sleep(0.2) - return aresponses.Response(body="Goodmorning!", text=load_fixtures("status.js")) + return aresponses.Response( + body="Goodmorning!", text=load_fixtures("status_webdata.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) + omnik = OmnikInverter(host="example.com", session=session) with pytest.raises(OmnikInverterConnectionError): assert await omnik.inverter() + + +@pytest.mark.asyncio +async def test_http_error404(aresponses): + """Test HTTP 404 response handling.""" + aresponses.add( + "example.com", + "/omnik/test", + "GET", + aresponses.Response(text="Give me energy!", status=404), + ) + + async with aiohttp.ClientSession() as session: + omnik = OmnikInverter(host="example.com", session=session) + with pytest.raises(OmnikInverterError): + assert await omnik.request("test") + + +@pytest.mark.asyncio +async def test_unexpected_response(aresponses): + """Test unexpected response handling.""" + aresponses.add( + "example.com", + "/omnik/test", + "GET", + aresponses.Response(text="Give me energy!", status=200), + ) + + async with aiohttp.ClientSession() as session: + omnik = OmnikInverter(host="example.com", session=session) + with pytest.raises(OmnikInverterError): + assert await omnik.request("test")