diff --git a/p1monitor/__init__.py b/p1monitor/__init__.py index 5331017..a8e5185 100644 --- a/p1monitor/__init__.py +++ b/p1monitor/__init__.py @@ -1,6 +1,6 @@ """Asynchronous Python client for the P1 Monitor API.""" -from .exceptions import P1MonitorConnectionError, P1MonitorError +from .exceptions import P1MonitorConnectionError, P1MonitorError, P1MonitorNoDataError from .models import Phases, Settings, SmartMeter, WaterMeter from .p1monitor import P1Monitor @@ -8,6 +8,7 @@ "P1Monitor", "P1MonitorError", "P1MonitorConnectionError", + "P1MonitorNoDataError", "SmartMeter", "Settings", "Phases", diff --git a/p1monitor/exceptions.py b/p1monitor/exceptions.py index 960b6f9..b110676 100644 --- a/p1monitor/exceptions.py +++ b/p1monitor/exceptions.py @@ -7,3 +7,7 @@ class P1MonitorError(Exception): class P1MonitorConnectionError(P1MonitorError): """P1 Monitor connection exception.""" + + +class P1MonitorNoDataError(P1MonitorError): + """P1 Monitor no data exception.""" diff --git a/p1monitor/p1monitor.py b/p1monitor/p1monitor.py index 07bcdfa..8b0fb1a 100644 --- a/p1monitor/p1monitor.py +++ b/p1monitor/p1monitor.py @@ -14,7 +14,7 @@ from aiohttp import hdrs from yarl import URL -from .exceptions import P1MonitorConnectionError, P1MonitorError +from .exceptions import P1MonitorConnectionError, P1MonitorError, P1MonitorNoDataError from .models import Phases, Settings, SmartMeter, WaterMeter @@ -125,10 +125,15 @@ async def watermeter(self) -> WaterMeter: Returns: A WaterMeter data object from the P1 Monitor API. + + Raises: + P1MonitorNoDataError: No data was received from the P1 Monitor API. """ data = await self._request( "v2/watermeter/day", params={"json": "object", "limit": 1} ) + if data == []: + raise P1MonitorNoDataError("No data received from P1 Monitor") return WaterMeter.from_dict(data) async def close(self) -> None: diff --git a/tests/fixtures/no_data.json b/tests/fixtures/no_data.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/tests/fixtures/no_data.json @@ -0,0 +1 @@ +[] diff --git a/tests/test_models.py b/tests/test_models.py index 45926c4..abc8f9b 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -3,7 +3,14 @@ import pytest from aresponses import ResponsesMockServer -from p1monitor import P1Monitor, Phases, Settings, SmartMeter, WaterMeter +from p1monitor import ( + P1Monitor, + P1MonitorNoDataError, + Phases, + Settings, + SmartMeter, + WaterMeter, +) from . import load_fixtures @@ -83,6 +90,27 @@ async def test_watermeter(aresponses: ResponsesMockServer) -> None: assert watermeter.pulse_count == 128 +@pytest.mark.asyncio +async def test_no_watermeter_data(aresponses: ResponsesMockServer) -> None: + """Test no WaterMeter data from P1 Monitor device.""" + aresponses.add( + "example.com", + "/api/v2/watermeter/day", + "GET", + aresponses.Response( + text=load_fixtures("no_data.json"), + status=200, + headers={"Content-Type": "application/json; charset=utf-8"}, + ), + ) + + async with aiohttp.ClientSession() as session: + client = P1Monitor(host="example.com", session=session) + with pytest.raises(P1MonitorNoDataError): + watermeter: WaterMeter = await client.watermeter() + assert not watermeter + + @pytest.mark.asyncio async def test_settings(aresponses: ResponsesMockServer) -> None: """Test request from a P1 Monitor device - Settings object."""