Skip to content

Commit

Permalink
Add no data exception on watermeter (#208)
Browse files Browse the repository at this point in the history
* Add no data exception

* Add extra test to cover the code
  • Loading branch information
klaasnicolaas authored Aug 14, 2022
1 parent 8d8c325 commit 4cd2414
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
3 changes: 2 additions & 1 deletion p1monitor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""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

__all__ = [
"P1Monitor",
"P1MonitorError",
"P1MonitorConnectionError",
"P1MonitorNoDataError",
"SmartMeter",
"Settings",
"Phases",
Expand Down
4 changes: 4 additions & 0 deletions p1monitor/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ class P1MonitorError(Exception):

class P1MonitorConnectionError(P1MonitorError):
"""P1 Monitor connection exception."""


class P1MonitorNoDataError(P1MonitorError):
"""P1 Monitor no data exception."""
7 changes: 6 additions & 1 deletion p1monitor/p1monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/no_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
30 changes: 29 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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."""
Expand Down

0 comments on commit 4cd2414

Please sign in to comment.