Skip to content

Commit

Permalink
Renname model to SmartBridge (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
klaasnicolaas authored Jan 23, 2022
1 parent 0776bbf commit 34ff74c
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 32 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ async def main():
host="example.com",
) as client:
device = await client.device()
smartmeter = await client.smartmeter()
smartbridge = await client.smartbridge()
print(device)
print(smartmeter)
print(smartbridge)

if __name__ == "__main__":
loop = asyncio.get_event_loop()
Expand All @@ -69,7 +69,7 @@ You can read the following data with this package, the `power flow` entity can a
- Hardware version
- Manufacturer

### SmartMeter
### SmartBridge

- Power Flow (W)
- Energy Consumption (kWh)
Expand Down
4 changes: 2 additions & 2 deletions net2grid/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"""Asynchronous Python client for a NED2GRID device."""

from .exceptions import Net2GridConnectionError, Net2GridError
from .models import Device, SmartMeter
from .models import Device, SmartBridge
from .net2grid import Net2Grid

__all__ = [
"Device",
"Net2Grid",
"Net2GridError",
"Net2GridConnectionError",
"SmartMeter",
"SmartBridge",
]
12 changes: 6 additions & 6 deletions net2grid/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@


@dataclass
class SmartMeter:
"""Object representing an SmartMeter response from a NET2GRID device."""
class SmartBridge:
"""Object representing an SmartBridge response from a NET2GRID device."""

power_flow: int
energy_consumption_total: float
energy_production_total: float

@staticmethod
def from_dict(data: dict[str, Any]) -> SmartMeter:
"""Return SmartMeter object from the NET2GRID API response.
def from_dict(data: dict[str, Any]) -> SmartBridge:
"""Return SmartBridge object from the NET2GRID API response.
Args:
data: The data from the NET2GRID API.
Returns:
A SmartMeter object.
A SmartBridge object.
"""
data = data["elec"]

Expand All @@ -37,7 +37,7 @@ def convert(value):
value = value / 1000
return round(value, 1)

return SmartMeter(
return SmartBridge(
power_flow=data["power"]["now"].get("value"),
energy_consumption_total=convert(data["import"]["now"].get("value")),
energy_production_total=convert(data["export"]["now"].get("value")),
Expand Down
8 changes: 4 additions & 4 deletions net2grid/net2grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from yarl import URL

from .exceptions import Net2GridConnectionError
from .models import Device, SmartMeter
from .models import Device, SmartBridge


@dataclass
Expand Down Expand Up @@ -95,15 +95,15 @@ async def device(self) -> Device:
data = json.loads(data)
return Device.from_dict(data)

async def smartmeter(self) -> SmartMeter:
async def smartbridge(self) -> SmartBridge:
"""Get the latest values from a NET2GRID device.
Returns:
A SmartMeter data object from the API.
A SmartBridge data object from the API.
"""
data = await self.request("meter/now")
data = json.loads(data)
return SmartMeter.from_dict(data)
return SmartBridge.from_dict(data)

async def close(self) -> None:
"""Close open client session."""
Expand Down
12 changes: 6 additions & 6 deletions test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@

import asyncio

from net2grid import Device, Net2Grid, SmartMeter
from net2grid import Device, Net2Grid, SmartBridge


async def main():
"""Test."""
async with Net2Grid(
host="example.com",
) as net2grid:
smartmeter: SmartMeter = await net2grid.smartmeter()
smartbridge: SmartBridge = await net2grid.smartbridge()
device: Device = await net2grid.device()
print(smartmeter)
print(smartbridge)
print()
print(f"Power flow: {smartmeter.power_flow}")
print(f"Energy consumption: {smartmeter.energy_consumption_total}")
print(f"Energy production: {smartmeter.energy_production_total}")
print(f"Power flow: {smartbridge.power_flow}")
print(f"Energy consumption: {smartbridge.energy_consumption_total}")
print(f"Energy production: {smartbridge.energy_production_total}")
print()
print(device)
print(f"ID: {device.n2g_id}")
Expand Down
File renamed without changes.
18 changes: 9 additions & 9 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import aiohttp
import pytest

from net2grid import Device, Net2Grid, SmartMeter
from net2grid import Device, Net2Grid, SmartBridge

from . import load_fixtures

Expand Down Expand Up @@ -33,22 +33,22 @@ async def test_device(aresponses):


@pytest.mark.asyncio
async def test_smartmeter(aresponses):
"""Test request from a NET2GRID device - SmartMeter object."""
async def test_smartbridge(aresponses):
"""Test request from a NET2GRID device - SmartBridge object."""
aresponses.add(
"example.com",
"/meter/now",
"GET",
aresponses.Response(
text=load_fixtures("smartmeter.json"),
text=load_fixtures("smartbridge.json"),
status=200,
),
)

async with aiohttp.ClientSession() as session:
client = Net2Grid(host="example.com", session=session)
smartmeter: SmartMeter = await client.smartmeter()
assert smartmeter
assert smartmeter.power_flow == 338
assert smartmeter.energy_consumption_total == 17762.1
assert smartmeter.energy_production_total == 21214.6
smartbridge: SmartBridge = await client.smartbridge()
assert smartbridge
assert smartbridge.power_flow == 338
assert smartbridge.energy_consumption_total == 17762.1
assert smartbridge.energy_production_total == 21214.6
4 changes: 2 additions & 2 deletions tests/test_net2grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ async def test_timeout(aresponses):
async def response_handler(_):
await asyncio.sleep(0.2)
return aresponses.Response(
body="Goodmorning!", text=load_fixtures("smartmeter.json")
body="Goodmorning!", text=load_fixtures("smartbridge.json")
)

aresponses.add("example.com", "/meter/now", "GET", response_handler)

async with aiohttp.ClientSession() as session:
client = Net2Grid(host="example.com", session=session, request_timeout=0.1)
with pytest.raises(Net2GridConnectionError):
assert await client.smartmeter()
assert await client.smartbridge()


@pytest.mark.asyncio
Expand Down

0 comments on commit 34ff74c

Please sign in to comment.