Skip to content

Commit

Permalink
Changes imports/dependencies (#180)
Browse files Browse the repository at this point in the history
* Update aiohttp imports

* Update dependencies

* Change request function and default load json
  • Loading branch information
klaasnicolaas authored Jun 25, 2022
1 parent 543de9e commit d27cad8
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 145 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ async def main():


if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
asyncio.run(main())
```

## Data
Expand Down
31 changes: 16 additions & 15 deletions p1monitor/p1monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
from __future__ import annotations

import asyncio
import json
import socket
from collections.abc import Mapping
from dataclasses import dataclass
from importlib import metadata
from typing import Any

from aiohttp.client import ClientError, ClientResponseError, ClientSession
from aiohttp.hdrs import METH_GET
from async_timeout import timeout
import aiohttp
import async_timeout
from aiohttp import hdrs
from yarl import URL

from .exceptions import P1MonitorConnectionError, P1MonitorError
Expand All @@ -22,15 +24,15 @@ class P1Monitor:

host: str
request_timeout: float = 10.0
session: ClientSession | None = None
session: aiohttp.ClientSession | None = None

_close_session: bool = False

async def request(
async def _request(
self,
uri: str,
*,
method: str = METH_GET,
method: str = hdrs.METH_GET,
params: Mapping[str, Any] | None = None,
) -> Any:
"""Handle a request to a P1 Monitor device.
Expand Down Expand Up @@ -58,11 +60,11 @@ async def request(
}

if self.session is None:
self.session = ClientSession()
self.session = aiohttp.ClientSession()
self._close_session = True

try:
async with timeout(self.request_timeout):
async with async_timeout.timeout(self.request_timeout):
response = await self.session.request(
method,
url,
Expand All @@ -74,7 +76,7 @@ async def request(
raise P1MonitorConnectionError(
"Timeout occurred while connecting to P1 Monitor device"
) from exception
except (ClientError, ClientResponseError) as exception:
except (aiohttp.ClientError, socket.gaierror) as exception:
raise P1MonitorConnectionError(
"Error occurred while communicating with P1 Monitor device"
) from exception
Expand All @@ -87,16 +89,15 @@ async def request(
{"Content-Type": content_type, "response": text},
)

response_data: dict[str, Any] = await response.json(content_type=None)
return response_data
return json.loads(await response.text())

async def smartmeter(self) -> SmartMeter:
"""Get the latest values from you smart meter.
Returns:
A SmartMeter data object from the P1 Monitor API.
"""
data = await self.request(
data = await self._request(
"v1/smartmeter", params={"json": "object", "limit": 1}
)
return SmartMeter.from_dict(data)
Expand All @@ -107,7 +108,7 @@ async def settings(self) -> Settings:
Returns:
A Settings data object from the P1 Monitor API.
"""
data = await self.request("v1/configuration", params={"json": "object"})
data = await self._request("v1/configuration", params={"json": "object"})
return Settings.from_dict(data)

async def phases(self) -> Phases:
Expand All @@ -116,7 +117,7 @@ async def phases(self) -> Phases:
Returns:
A Phases data object from the P1 Monitor API.
"""
data = await self.request("v1/status", params={"json": "object"})
data = await self._request("v1/status", params={"json": "object"})
return Phases.from_dict(data)

async def watermeter(self) -> WaterMeter:
Expand All @@ -125,7 +126,7 @@ async def watermeter(self) -> WaterMeter:
Returns:
A WaterMeter data object from the P1 Monitor API.
"""
data = await self.request(
data = await self._request(
"v2/watermeter/day", params={"json": "object", "limit": 1}
)
return WaterMeter.from_dict(data)
Expand Down
Loading

0 comments on commit d27cad8

Please sign in to comment.