Refactor UTE to class #4
@ -26,10 +26,11 @@ BASE_URL = "https://rocme.ute.com.uy/api/v1"
|
|||||||
|
|
||||||
|
|
||||||
class UTEClient:
|
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.email = email
|
||||||
self.phone_number = phone_number
|
self.phone_number = phone_number
|
||||||
self.device_id = device_id
|
self.device_id = device_id
|
||||||
|
self.average_cost_per_kwh = average_cost_per_kwh
|
||||||
self.authorization = self._login()
|
self.authorization = self._login()
|
||||||
|
|
||||||
if not self.device_id:
|
if not self.device_id:
|
||||||
@ -49,6 +50,13 @@ class UTEClient:
|
|||||||
|
|
||||||
self.device_id = devices[0]["accountServicePointId"]
|
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:
|
def _make_request(self, method: str, url: str, data: Optional[dict] = None) -> requests.Response:
|
||||||
"""
|
"""
|
||||||
Make a HTTP request
|
Make a HTTP request
|
||||||
@ -165,7 +173,6 @@ class UTEClient:
|
|||||||
|
|
||||||
def get_historic_consumption(
|
def get_historic_consumption(
|
||||||
self,
|
self,
|
||||||
average_cost_per_kwh: float,
|
|
||||||
date_start: Optional[str] = None,
|
date_start: Optional[str] = None,
|
||||||
date_end: Optional[str] = None,
|
date_end: Optional[str] = None,
|
||||||
) -> dict:
|
) -> dict:
|
||||||
@ -173,9 +180,8 @@ class UTEClient:
|
|||||||
Generate UTE historic consumption from device id and date range
|
Generate UTE historic consumption from device id and date range
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
cost_per_kwh (float): Cost per kwh
|
date_start (str): Start date to check in format YYYY-MM-DD
|
||||||
date_start (str): Start date to check
|
date_end (str): End date to check in format YYYY-MM-DD
|
||||||
date_end (str): End date to check
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
dict: UTE info
|
dict: UTE info
|
||||||
@ -205,19 +211,51 @@ class UTEClient:
|
|||||||
|
|
||||||
active_energy[date.strftime("%d/%m/%Y")] = {
|
active_energy[date.strftime("%d/%m/%Y")] = {
|
||||||
"kwh": value,
|
"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,
|
"day_in_week": day_in_week,
|
||||||
}
|
}
|
||||||
active_energy["total"]["sum_in_kwh"] += value
|
active_energy["total"]["sum_in_kwh"] += value
|
||||||
|
|
||||||
active_energy["total"]["aproximated_cost_in_uyu"] = round(
|
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"]["daily_average_cost"] = round(
|
||||||
active_energy["total"]["aproximated_cost_in_uyu"] / (len(active_energy) - 1), 3
|
active_energy["total"]["aproximated_cost_in_uyu"] / (len(active_energy) - 1), 3
|
||||||
)
|
)
|
||||||
return active_energy
|
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:
|
def get_current_usage_info(self) -> dict:
|
||||||
"""
|
"""
|
||||||
Get current usage info from device id
|
Get current usage info from device id
|
||||||
@ -251,26 +289,7 @@ class UTEClient:
|
|||||||
|
|
||||||
readings = response["data"]["readings"]
|
readings = response["data"]["readings"]
|
||||||
|
|
||||||
for reading in readings:
|
power_in_watts = self._convert_triphasic_powers_to_power_in_watts(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)
|
|
||||||
|
|
||||||
return_dict = {**response}
|
return_dict = {**response}
|
||||||
return_dict["data"]["power_in_watts"] = power_in_watts
|
return_dict["data"]["power_in_watts"] = power_in_watts
|
||||||
|
Reference in New Issue
Block a user