Skip to content

Commit

Permalink
fix(plant): saving returned as string
Browse files Browse the repository at this point in the history
  • Loading branch information
rokam committed May 29, 2024
1 parent 384da07 commit 1cdbc8b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 10 deletions.
24 changes: 21 additions & 3 deletions sunweg/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def convert_situation_status(situation: int) -> Status:


def separate_value_metric(
value_with_metric: str | None, default_metric: str = ""
value_with_metric: str | None, default_metric: str = "", metric_before: bool = False
) -> tuple[float, str]:
"""
Separate the value from the metric.
Expand All @@ -57,12 +57,21 @@ def separate_value_metric(
:type value_with_metric: str | None
:param default_metric: metric that should be returned if `value_with_metric` is None
:type default_metric: str
:param metric_before: true when metric appears before the value
:type metric_before: bool
:return: tuple with value and metric
:rtype: tuple[float, str]
"""
if value_with_metric is None or len(value_with_metric) == 0:
return (0.0, default_metric)
split = value_with_metric.split(" ")
if metric_before:
return (
float(split[0].replace(",", "."))
if len(split) < 2
else float(split[1].replace(",", ".")),
default_metric if len(split) < 2 else split[0],
)
return (
float(split[0].replace(",", ".")),
default_metric if len(split) < 2 else split[1],
Expand Down Expand Up @@ -148,7 +157,15 @@ def listPlants(self, retry=True) -> list[Plant]:
try:
result = self._get(SUNWEG_PLANT_LIST_PATH)
ret_list = []
for plant in result["conectadas"]:
plantlist = (
result["nao_comissionadas"]
+ result["conectadas"]
+ result["falhas"]
+ result["alertas"]
+ result["atendimento"]
)

for plant in plantlist:
if (plant := self.plant(plant["id"])) is not None:
ret_list.append(plant)
return ret_list
Expand Down Expand Up @@ -176,13 +193,14 @@ def plant(self, id: int, retry=True) -> Plant | None:
result["energiadia"], "kWh"
)
total_power = separate_value_metric(result["AcumuladoPotencia"])[0]
saving = separate_value_metric(result["economia"], metric_before=True)[0]
plant = Plant(
id=id,
name=result["usinas"]["nome"],
total_power=total_power,
kwh_per_kwp=float(0),
performance_rate=float(0),
saving=result["economia"],
saving=saving,
today_energy=today_energy,
today_energy_metric=today_energy_metric,
total_energy=float(result["energiaacumuladanumber"]),
Expand Down
2 changes: 1 addition & 1 deletion tests/responses/list_plant_success_1_response.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"success":true,"conectadas":[{"id":16925}]}
{"success":true,"conectadas":[{"id":16925}],"nao_comissionadas":[],"falhas":[],"alertas":[],"atendimento":[]}
2 changes: 1 addition & 1 deletion tests/responses/list_plant_success_2_response.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"success":true,"conectadas":[{"id":16925},{"id":16926}]}
{"success":true,"conectadas":[{"id":16925},{"id":16926}],"nao_comissionadas":[],"falhas":[],"alertas":[],"atendimento":[]}
2 changes: 1 addition & 1 deletion tests/responses/list_plant_success_none_response.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"success":true,"conectadas":[]}
{"success":true,"conectadas":[],"nao_comissionadas":[],"falhas":[],"alertas":[],"atendimento":[]}
2 changes: 1 addition & 1 deletion tests/responses/plant_success_alt_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
"KWHporkWp": "1,2",
"PerformanceRate": 0.1,
"reduz_carbono_total_number": 0.012296,
"economia": 12.786912
"economia": "12,78"
}
2 changes: 1 addition & 1 deletion tests/responses/plant_success_response.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
"KWHporkWp": "1,2",
"PerformanceRate": 0.1,
"reduz_carbono_total_number": 0.012296,
"economia": 12.786912
"economia": "12,78"
}
4 changes: 2 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def test_plant_success(self) -> None:
assert plant.last_update == datetime(2023, 2, 25, 8, 4, 22)
assert plant.kwh_per_kwp == 0.0
assert plant.performance_rate == 0.0
assert plant.saving == 12.786912
assert plant.saving == 12.78
assert plant.today_energy == 1.23
assert plant.today_energy_metric == "kWh"
assert plant.total_carbon_saving == 0.012296
Expand Down Expand Up @@ -207,7 +207,7 @@ def test_plant_success_alt(self) -> None:
assert plant.last_update is None
assert plant.kwh_per_kwp == 0.0
assert plant.performance_rate == 0.0
assert plant.saving == 12.786912
assert plant.saving == 12.78
assert plant.today_energy == 1.23
assert plant.today_energy_metric == "kWh"
assert plant.total_carbon_saving == 0.012296
Expand Down

0 comments on commit 1cdbc8b

Please sign in to comment.