From 3209d7e68cb6608b662fae5b9814b10eb793a77e Mon Sep 17 00:00:00 2001 From: Roger Gonzalez Date: Thu, 17 Aug 2023 15:16:44 -0300 Subject: [PATCH] Made further improvements to code readability --- src/ute_wrapper/ute.py | 73 ++++++++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 27 deletions(-) diff --git a/src/ute_wrapper/ute.py b/src/ute_wrapper/ute.py index 5897d2d..d41ec8a 100755 --- a/src/ute_wrapper/ute.py +++ b/src/ute_wrapper/ute.py @@ -26,10 +26,11 @@ BASE_URL = "https://rocme.ute.com.uy/api/v1" class UTEClient: - def __init__(self, email: str, phone_number: str, device_id: str = None): + def __init__(self, email: str, phone_number: str, device_id: str = None, average_cost_per_kwh: float = None): self.email = email self.phone_number = phone_number self.device_id = device_id + self.average_cost_per_kwh = average_cost_per_kwh self.authorization = self._login() if not self.device_id: @@ -49,6 +50,13 @@ class UTEClient: self.device_id = devices[0]["accountServicePointId"] + if not self.average_cost_per_kwh: + try: + tariff_type = self.get_account()["meterInfo"]["tariffType"].lower() + self.average_cost_per_kwh = self.get_average_price(tariff_type) + except Exception: + raise Exception("Your tariff type is not standard. Try making it explicit on the client initialization") + def _make_request(self, method: str, url: str, data: Optional[dict] = None) -> requests.Response: """ Make a HTTP request @@ -165,7 +173,6 @@ class UTEClient: def get_historic_consumption( self, - average_cost_per_kwh: float, date_start: Optional[str] = None, date_end: Optional[str] = None, ) -> dict: @@ -173,9 +180,8 @@ class UTEClient: Generate UTE historic consumption from device id and date range Args: - cost_per_kwh (float): Cost per kwh - date_start (str): Start date to check - date_end (str): End date to check + date_start (str): Start date to check in format YYYY-MM-DD + date_end (str): End date to check in format YYYY-MM-DD Returns: dict: UTE info @@ -205,19 +211,51 @@ class UTEClient: active_energy[date.strftime("%d/%m/%Y")] = { "kwh": value, - "aproximated_cost_in_uyu": round(value * average_cost_per_kwh, 3), + "aproximated_cost_in_uyu": round(value * self.average_cost_per_kwh, 3), "day_in_week": day_in_week, } active_energy["total"]["sum_in_kwh"] += value active_energy["total"]["aproximated_cost_in_uyu"] = round( - active_energy["total"]["sum_in_kwh"] * average_cost_per_kwh, 3 + active_energy["total"]["sum_in_kwh"] * self.average_cost_per_kwh, 3 ) active_energy["total"]["daily_average_cost"] = round( active_energy["total"]["aproximated_cost_in_uyu"] / (len(active_energy) - 1), 3 ) return active_energy + def _convert_triphasic_powers_to_power_in_watts(self, readings: List[dict]) -> float: + """ + Convert triphasic powers to power in watts + + Args: + readings (List[dict]): List of readings + + Returns: + float: Power in watts + """ + + for reading in readings: + reading_type = reading["tipoLecturaMGMI"] + if reading_type == "I1": + i1 = float(reading["valor"]) + elif reading_type == "I2": + i2 = float(reading["valor"]) + elif reading_type == "I3": + i3 = float(reading["valor"]) + elif reading_type == "V1": + v1 = float(reading["valor"]) + elif reading_type == "V2": + v2 = float(reading["valor"]) + elif reading_type == "V3": + v3 = float(reading["valor"]) + + power_1_in_watts = v1 * i1 + power_2_in_watts = v2 * i2 + power_3_in_watts = v3 * i3 + + return round(power_1_in_watts + power_2_in_watts + power_3_in_watts, 3) + def get_current_usage_info(self) -> dict: """ Get current usage info from device id @@ -251,26 +289,7 @@ class UTEClient: readings = response["data"]["readings"] - for reading in readings: - reading_type = reading["tipoLecturaMGMI"] - if reading_type == "I1": - i1 = float(reading["valor"]) - elif reading_type == "I2": - i2 = float(reading["valor"]) - elif reading_type == "I3": - i3 = float(reading["valor"]) - elif reading_type == "V1": - v1 = float(reading["valor"]) - elif reading_type == "V2": - v2 = float(reading["valor"]) - elif reading_type == "V3": - v3 = float(reading["valor"]) - - power_1_in_watts = v1 * i1 - power_2_in_watts = v2 * i2 - power_3_in_watts = v3 * i3 - - power_in_watts = round(power_1_in_watts + power_2_in_watts + power_3_in_watts, 3) + power_in_watts = self._convert_triphasic_powers_to_power_in_watts(readings) return_dict = {**response} return_dict["data"]["power_in_watts"] = power_in_watts