From 1cdbc8bab2dd84d149911e05d9e55689de837bac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Mind=C3=AAllo=20de=20Andrade?= Date: Wed, 29 May 2024 17:59:36 -0300 Subject: [PATCH] fix(plant): saving returned as string --- sunweg/api.py | 24 ++++++++++++++++--- .../list_plant_success_1_response.json | 2 +- .../list_plant_success_2_response.json | 2 +- .../list_plant_success_none_response.json | 2 +- .../responses/plant_success_alt_response.json | 2 +- tests/responses/plant_success_response.json | 2 +- tests/test_api.py | 4 ++-- 7 files changed, 28 insertions(+), 10 deletions(-) diff --git a/sunweg/api.py b/sunweg/api.py index aa4a699..0331188 100644 --- a/sunweg/api.py +++ b/sunweg/api.py @@ -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. @@ -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], @@ -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 @@ -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"]), diff --git a/tests/responses/list_plant_success_1_response.json b/tests/responses/list_plant_success_1_response.json index 6392bc5..f741a56 100644 --- a/tests/responses/list_plant_success_1_response.json +++ b/tests/responses/list_plant_success_1_response.json @@ -1 +1 @@ -{"success":true,"conectadas":[{"id":16925}]} \ No newline at end of file +{"success":true,"conectadas":[{"id":16925}],"nao_comissionadas":[],"falhas":[],"alertas":[],"atendimento":[]} \ No newline at end of file diff --git a/tests/responses/list_plant_success_2_response.json b/tests/responses/list_plant_success_2_response.json index 4952ca9..66bb8ef 100644 --- a/tests/responses/list_plant_success_2_response.json +++ b/tests/responses/list_plant_success_2_response.json @@ -1 +1 @@ -{"success":true,"conectadas":[{"id":16925},{"id":16926}]} \ No newline at end of file +{"success":true,"conectadas":[{"id":16925},{"id":16926}],"nao_comissionadas":[],"falhas":[],"alertas":[],"atendimento":[]} \ No newline at end of file diff --git a/tests/responses/list_plant_success_none_response.json b/tests/responses/list_plant_success_none_response.json index 944f268..d7d2a08 100644 --- a/tests/responses/list_plant_success_none_response.json +++ b/tests/responses/list_plant_success_none_response.json @@ -1 +1 @@ -{"success":true,"conectadas":[]} \ No newline at end of file +{"success":true,"conectadas":[],"nao_comissionadas":[],"falhas":[],"alertas":[],"atendimento":[]} \ No newline at end of file diff --git a/tests/responses/plant_success_alt_response.json b/tests/responses/plant_success_alt_response.json index 79c4c5a..fc93121 100644 --- a/tests/responses/plant_success_alt_response.json +++ b/tests/responses/plant_success_alt_response.json @@ -23,5 +23,5 @@ "KWHporkWp": "1,2", "PerformanceRate": 0.1, "reduz_carbono_total_number": 0.012296, - "economia": 12.786912 + "economia": "12,78" } \ No newline at end of file diff --git a/tests/responses/plant_success_response.json b/tests/responses/plant_success_response.json index 6266324..9a70baa 100644 --- a/tests/responses/plant_success_response.json +++ b/tests/responses/plant_success_response.json @@ -23,5 +23,5 @@ "KWHporkWp": "1,2", "PerformanceRate": 0.1, "reduz_carbono_total_number": 0.012296, - "economia": 12.786912 + "economia": "12,78" } \ No newline at end of file diff --git a/tests/test_api.py b/tests/test_api.py index b5f5df8..d5aca0c 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -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 @@ -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