Skip to content

Commit

Permalink
Fix issue with watermeter requests (#233)
Browse files Browse the repository at this point in the history
* Add exception for status 404

* Small tweak on tests

* Small update on the documentation

* Raise exception in request class
  • Loading branch information
klaasnicolaas authored Nov 4, 2022
1 parent 48d2ecb commit ffd43f3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ async def main():
"""Show example on getting P1 Monitor data."""
async with P1Monitor(host="example_host") as client:
smartmeter = await client.smartmeter()
watermeter = await client.watermeter()
print(smartmeter)
print(watermeter)


if __name__ == "__main__":
Expand All @@ -69,6 +71,8 @@ There is a lot of data that you can read via the API:
- Power Produced phases L1/2/3

### WaterMeter
> **_NOTE:_** WaterMeter is only available when you run version 0.9.14 or higher.
- Day Consumption (liters)
- Total Consumption (m3)
- Day Pulse count
Expand Down
5 changes: 5 additions & 0 deletions p1monitor/p1monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,16 @@ async def _request(
headers=headers,
)
response.raise_for_status()

except asyncio.TimeoutError as exception:
raise P1MonitorConnectionError(
"Timeout occurred while connecting to P1 Monitor device"
) from exception
except (aiohttp.ClientError, socket.gaierror) as exception:
if "watermeter" in uri and response.status == 404:
raise P1MonitorConnectionError(
"No water meter is connected to P1 Monitor device"
) from exception
raise P1MonitorConnectionError(
"Error occurred while communicating with P1 Monitor device"
) from exception
Expand Down
23 changes: 22 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from p1monitor import (
P1Monitor,
P1MonitorConnectionError,
P1MonitorNoDataError,
Phases,
Settings,
Expand Down Expand Up @@ -91,7 +92,7 @@ async def test_watermeter(aresponses: ResponsesMockServer) -> None:


@pytest.mark.asyncio
async def test_no_watermeter_data(aresponses: ResponsesMockServer) -> None:
async def test_no_watermeter_data_new(aresponses: ResponsesMockServer) -> None:
"""Test no WaterMeter data from P1 Monitor device."""
aresponses.add(
"example.com",
Expand All @@ -111,6 +112,26 @@ async def test_no_watermeter_data(aresponses: ResponsesMockServer) -> None:
assert not watermeter


@pytest.mark.asyncio
async def test_no_watermeter_data_old(aresponses: ResponsesMockServer) -> None:
"""Test no WaterMeter data from P1 Monitor device."""
aresponses.add(
"example.com",
"/api/v2/watermeter/day",
"GET",
aresponses.Response(
status=404,
headers={"Content-Type": "application/json; charset=utf-8"},
),
)

async with aiohttp.ClientSession() as session:
client = P1Monitor(host="example.com", session=session)
with pytest.raises(P1MonitorConnectionError):
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
4 changes: 2 additions & 2 deletions tests/test_p1monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


@pytest.mark.asyncio
async def test__json_request(aresponses: ResponsesMockServer) -> None:
async def test_json_request(aresponses: ResponsesMockServer) -> None:
"""Test JSON response is handled correctly."""
aresponses.add(
"example.com",
Expand Down Expand Up @@ -118,7 +118,7 @@ async def test_http_error401(aresponses: ResponsesMockServer, status: int) -> No


@pytest.mark.asyncio
async def test_http_error400(aresponses: ResponsesMockServer) -> None:
async def test_http_error404(aresponses: ResponsesMockServer) -> None:
"""Test HTTP 404 response handling."""
aresponses.add(
"example.com",
Expand Down

0 comments on commit ffd43f3

Please sign in to comment.