Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add listing for SmartBatteries and related tests #176

Merged
merged 4 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion python_frank_energie/frank_energie.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
from aiohttp.client import ClientError, ClientSession

from .exceptions import AuthException, AuthRequiredException
from .models import Authentication, Invoices, MarketPrices, Me, MonthSummary
from .models import (
Authentication,
Invoices,
MarketPrices,
Me,
MonthSummary,
SmartBatteries,
)


class FrankEnergie:
Expand Down Expand Up @@ -484,6 +491,49 @@ async def user_prices(self, start_date: date, site_reference: str) -> MarketPric

return MarketPrices.from_userprices_dict(await self._query(query_data))

async def smart_batteries(self) -> SmartBatteries:
"""Get the users smart batteries.

Returns a list of all smart batteries.

Full query:
query {
smartBatteries {
brand
capacity
createdAt
externalReference
id
maxChargePower
maxDischargePower
provider
updatedAt
}
}
"""
if self._auth is None:
raise AuthRequiredException

query = {
"query": """
query {
smartBatteries {
brand
capacity
createdAt
externalReference
id
maxChargePower
maxDischargePower
provider
updatedAt
}
""",
"operationName": "SmartBatteries",
}

return SmartBatteries.from_dict(await self._query(query))

@property
def is_authenticated(self) -> bool:
"""Return if client is authenticated.
Expand Down
56 changes: 56 additions & 0 deletions python_frank_energie/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,3 +419,59 @@ def from_userprices_dict(data: dict[str, str]) -> MarketPrices:
electricity=PriceData(customerMarketPrices.get("electricityPrices")),
gas=PriceData(customerMarketPrices.get("gasPrices")),
)


@dataclass
class SmartBatteries:
"""Collection of the users SmartBatteries"""
DCSBL marked this conversation as resolved.
Show resolved Hide resolved

smart_batteries: list[SmartBattery]

@staticmethod
def from_dict(data: dict[str, str]) -> SmartBatteries:
"""Parse the response from the smartBatteries query."""
_LOGGER.debug("SmartBatteries %s", data)

if errors := data.get("errors"):
raise RequestException(errors[0]["message"])

payload = data.get("data")
if not payload:
raise RequestException("Unexpected response")

return SmartBatteries(
smart_batteries=[
SmartBatteries.SmartBattery.from_dict(smart_battery)
for smart_battery in payload.get("smartBatteries")
],
)

@dataclass
class SmartBattery:
DCSBL marked this conversation as resolved.
Show resolved Hide resolved

brand: str
capacity: float
external_reference: str
id: str
max_charge_power: float
max_discharge_power: float
provider: str
created_at: datetime
updated_at: datetime

@staticmethod
def from_dict(payload: dict[str, str]) -> SmartBatteries.SmartBattery:
"""Parse the response from the me query."""
DCSBL marked this conversation as resolved.
Show resolved Hide resolved
_LOGGER.debug("DeliverySites %s", payload)

return SmartBatteries.SmartBattery(
brand=payload.get("brand"),
capacity=payload.get("capacity"),
external_reference=payload.get("externalReference"),
id=payload.get("id"),
max_charge_power=payload.get("maxChargePower"),
max_discharge_power=payload.get("maxDischargePower"),
provider=payload.get("provider"),
created_at=payload.get("createdAt"),
updated_at=payload.get("updatedAt"),
)
4 changes: 4 additions & 0 deletions tests/__snapshots__/test_smart_batteries.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# serializer version: 1
# name: test_smart_batteries
SmartBatteries(smart_batteries=[SmartBatteries.SmartBattery(brand='SESSY', capacity=5.2, external_reference='SESSYREF', id='unique_identifier', max_charge_power=2.2, max_discharge_power=1.7, provider='SESSY', created_at='2024-04-30T13:33:42.776Z', updated_at='2024-05-29T08:55:58.270Z')])
# ---
17 changes: 17 additions & 0 deletions tests/fixtures/smart_batteries.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"data": {
"smartBatteries": [
{
"brand": "SESSY",
"capacity": 5.2,
"createdAt": "2024-04-30T13:33:42.776Z",
"externalReference": "SESSYREF",
"id": "unique_identifier",
"maxChargePower": 2.2,
"maxDischargePower": 1.7,
"provider": "SESSY",
"updatedAt": "2024-05-29T08:55:58.270Z"
}
]
}
}
32 changes: 32 additions & 0 deletions tests/test_smart_batteries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import aiohttp
DCSBL marked this conversation as resolved.
Show resolved Hide resolved
import pytest
from syrupy.assertion import SnapshotAssertion

from python_frank_energie import FrankEnergie

from . import load_fixtures

SIMPLE_DATA_URL = "frank-graphql-prod.graphcdn.app"


@pytest.mark.asyncio
async def test_smart_batteries(aresponses, snapshot: SnapshotAssertion):
"""Test request with authentication."""
aresponses.add(
SIMPLE_DATA_URL,
"/",
"POST",
aresponses.Response(
text=load_fixtures("smart_batteries.json"),
status=200,
headers={"Content-Type": "application/json"},
),
)

async with aiohttp.ClientSession() as session:
api = FrankEnergie(session, auth_token="a", refresh_token="b") # noqa: S106
smart_batteries = await api.smart_batteries()
await api.close()

assert smart_batteries is not None
assert smart_batteries == snapshot
Loading