Skip to content

Commit

Permalink
Add optional port (#762)
Browse files Browse the repository at this point in the history
* Add option to use different port

* Update the documentation

* Make the host more realistic
  • Loading branch information
klaasnicolaas authored Oct 3, 2024
1 parent d5d8655 commit 011be04
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ from p1monitor import P1Monitor

async def main():
"""Show example on getting P1 Monitor data."""
async with P1Monitor(host="example_host") as client:
async with P1Monitor(host="192.168.1.2", port=80) as client:
smartmeter = await client.smartmeter()
watermeter = await client.watermeter()
settings = await client.settings()
Expand All @@ -55,6 +55,17 @@ if __name__ == "__main__":
asyncio.run(main())
```

More examples can be found in the [examples folder](./examples/).

## Class: `P1Monitor`

This is the main class that you will use to interact with the P1 Monitor.

| Parameter | Required | Description |
| --------- | -------- | -------------------------------------------- |
| `host` | `True` | The IP address of the P1 Monitor. |
| `port` | `False` | The port of the P1 Monitor. Default is `80`. |

## Data

There is a lot of data that you can read via the API:
Expand Down
8 changes: 7 additions & 1 deletion src/p1monitor/p1monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class P1Monitor:
"""Main class for handling connections with the P1 Monitor API."""

host: str
port: int = 80
request_timeout: float = 10.0
session: ClientSession | None = None

Expand Down Expand Up @@ -55,7 +56,12 @@ async def _request(
P1MonitorError: Received an unexpected response from the P1 Monitor API.
"""
url = URL.build(scheme="http", host=self.host, path="/api/").join(URL(uri))
url = URL.build(
scheme="http",
host=self.host,
port=self.port,
path="/api/",
).join(URL(uri))

headers = {
"User-Agent": f"PythonP1Monitor/{VERSION}",
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ async def client() -> AsyncGenerator[P1Monitor, None]:
"""Return a P1Monitor client."""
async with (
ClientSession() as session,
P1Monitor(host="192.168.1.2", session=session) as p1monitor_client,
P1Monitor(host="192.168.1.2", port=80, session=session) as p1monitor_client,
):
yield p1monitor_client
18 changes: 18 additions & 0 deletions tests/test_p1monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ async def test_json_request(
await p1monitor_client.close()


async def test_different_port(aresponses: ResponsesMockServer) -> None:
"""Test different port is handled correctly."""
aresponses.add(
"192.168.1.2:84",
"/api/test",
"GET",
aresponses.Response(
status=200,
headers={"Content-Type": "application/json"},
text='{"status": "ok"}',
),
)
async with ClientSession() as session:
p1monitor = P1Monitor("192.168.1.2", port=84, session=session)
await p1monitor._request("test")
await p1monitor.close()


async def test_internal_session(aresponses: ResponsesMockServer) -> None:
"""Test JSON response is handled correctly."""
aresponses.add(
Expand Down

0 comments on commit 011be04

Please sign in to comment.