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

Treat when the API returns null as lastupdate date #10

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,5 @@ venv.bak/
dmypy.json

# Pyre type checker
.pyre/
.pyre/
reports/
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"label": "Code Coverage",
"detail": "Generate code coverage report.",
"type": "shell",
"command": "/home/lucas/.local/bin/pytest ./tests/ --cov=sunweg --cov-report term-missing --durations-min=1 --durations=0",
"command": "pytest ./tests/ --cov=sunweg --cov-report term-missing --durations-min=1 --durations=0",
"group": {
"kind": "test",
"isDefault": true
Expand Down
5 changes: 4 additions & 1 deletion sunweg/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""API Helper."""

import json
from datetime import datetime
from typing import Any
Expand Down Expand Up @@ -190,7 +191,9 @@ def plant(self, id: int, retry=True) -> Plant | None:
total_carbon_saving=result["reduz_carbono_total_number"],
last_update=datetime.strptime(
result["ultimaAtualizacao"], "%Y-%m-%d %H:%M:%S"
),
)
if result["ultimaAtualizacao"] is not None
else None,
)

plant.inverters.extend(
Expand Down
9 changes: 5 additions & 4 deletions sunweg/plant.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Sunweg API plant."""

from datetime import datetime

from .device import Inverter
Expand All @@ -19,7 +20,7 @@ def __init__(
today_energy_metric: str,
total_energy: float,
total_carbon_saving: float,
last_update: datetime,
last_update: datetime | None,
) -> None:
"""
Initialize Plant.
Expand All @@ -45,7 +46,7 @@ def __init__(
:param total_carbon_saving: total of CO2 saved
:type total_carbon_saving: float
:param last_update: when the data was updated
:type last_update: datetime
:type last_update: datetime | None
"""
self._id = id
self._name = name
Expand Down Expand Up @@ -161,12 +162,12 @@ def total_carbon_saving(self) -> float:
return self._total_carbon_saving

@property
def last_update(self) -> datetime:
def last_update(self) -> datetime | None:
"""
Get when the plant data was updated.

:return: when the plant data was updated
:rtype: datetime
:rtype: datetime | None
"""
return self._last_update

Expand Down
13 changes: 8 additions & 5 deletions sunweg/util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Sunweg API util."""

from datetime import date
from enum import Enum

Expand All @@ -10,12 +11,14 @@ class Status(Enum):
WARN = 2
ERROR = 1


class ProductionStats:
"""Energy production statistics"""
def __init__(self, date:date, production:float, prognostic:float) -> None:

def __init__(self, date: date, production: float, prognostic: float) -> None:
"""
Initialize energy production statistics.

:param date: statistics date
:type date: date
:param production: statistics production in kWh
Expand All @@ -35,12 +38,12 @@ def date(self) -> date:
def production(self) -> float:
"""Get energy production in kWh."""
return self._production

@property
def prognostic(self) -> float:
"""Get expected energy production in kWh."""
return self._prognostic

def __str__(self) -> str:
"""Cast Phase to str."""
return str(self.__class__) + ": " + str(self.__dict__)
return str(self.__class__) + ": " + str(self.__dict__)
27 changes: 27 additions & 0 deletions tests/responses/plant_success_alt_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"success": true,
"usinas": {
"id": 16925,
"nome": "Plant Name",
"inversores": [
{
"id": 21255,
"nome": "Inverter Name",
"descricao": "Inverter Description",
"esn": "1234ABC",
"situacao": 1,
"tensaoca": 242,
"temperatura": 80
}
]
},
"ultimaAtualizacao": null,
"AcumuladoPotencia": "25,23 kW",
"energiaGeradaHoje": "1,23 kWh",
"energiaacumuladanumber": "23.20",
"taxaPerformance": 1.48,
"KWHporkWp": "1,2",
"PerformanceRate": 0.1,
"reduz_carbono_total_number": 0.012296,
"economia": 12.786912
}
39 changes: 39 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test sunweg.api."""

from datetime import date, datetime
from os import path
import os
Expand Down Expand Up @@ -191,6 +192,44 @@ def test_plant_success(self) -> None:
assert inverter.total_energy_metric == ""
assert not inverter.is_complete

def test_plant_success_alt(self) -> None:
"""Test plant success."""
with patch(
"requests.Session.get",
return_value=self.responses["plant_success_alt_response.json"],
), patch("sunweg.api.APIHelper.inverter", return_value=INVERTER_MOCK):
api = APIHelper("[email protected]", "password")
plant = api.plant(16925)
assert plant is not None
assert plant.id == 16925
assert plant.name == "Plant Name"
assert plant.total_power == 25.23
assert plant.last_update is None
assert plant.kwh_per_kwp == 1.2
assert plant.performance_rate == 1.48
assert plant.saving == 12.786912
assert plant.today_energy == 1.23
assert plant.today_energy_metric == "kWh"
assert plant.total_carbon_saving == 0.012296
assert plant.total_energy == 23.2
assert plant.__str__().startswith("<class 'sunweg.plant.Plant'>")
assert len(plant.inverters) == 1
for inverter in plant.inverters:
assert inverter.id == 21255
assert inverter.name == "Inverter Name"
assert inverter.frequency == 0
assert inverter.power == 0.0
assert inverter.power_metric == ""
assert inverter.power_factor == 0.0
assert inverter.sn == "1234ABC"
assert inverter.status == Status.ERROR
assert inverter.temperature == 80
assert inverter.today_energy == 0.0
assert inverter.today_energy_metric == ""
assert inverter.total_energy == 0.0
assert inverter.total_energy_metric == ""
assert not inverter.is_complete

def test_plant_401(self) -> None:
"""Test plant with expired token."""
with patch(
Expand Down
Loading