Added calculating monophasic and biphasic powers

This commit is contained in:
Roger Gonzalez 2023-12-12 19:49:09 -03:00
parent be7c46055e
commit dade47ce16
Signed by: rogs
GPG Key ID: C7ECE9C6C36EC2E6
2 changed files with 25 additions and 21 deletions

View File

@ -45,7 +45,7 @@ build-backend = "hatchling.build"
[project]
name = "ute_wrapper"
version = "1.1.0"
version = "1.2.0"
authors = [
{ name="Roger Gonzalez", email="roger@rogs.me" },
]

View File

@ -235,9 +235,10 @@ class UTEClient:
)
return active_energy
def _convert_triphasic_powers_to_power_in_watts(self, readings: List[dict]) -> float:
def _convert_powers_to_power_in_watts(self, readings: List[dict]) -> float:
"""
Convert triphasic powers to power in watts
Convert powers to power in watts and determine the system type (monophasic, biphasic, or triphasic)
automatically.
Args:
readings (List[dict]): List of readings
@ -245,29 +246,32 @@ class UTEClient:
Returns:
float: Power in watts
"""
reading_sums = {"I1": 0, "I2": 0, "I3": 0, "V1": 0, "V2": 0, "V3": 0}
num_voltages = num_currents = 0
total_power_in_watts = 0
square_root_of_three = 1.732
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"])
if reading_type in reading_sums:
reading_sums[reading_type] += float(reading["valor"])
if "V" in reading_type:
num_voltages += 1
elif "I" in reading_type:
num_currents += 1
power_factor = self.power_factor or 1
if num_voltages > 0 and num_currents > 0:
averaged_voltage = sum(reading_sums[v] for v in ["V1", "V2", "V3"]) / num_voltages
averaged_current = sum(reading_sums[i] for i in ["I1", "I2", "I3"]) / num_currents
power_1_in_watts = v1 * i1 * power_factor
power_2_in_watts = v2 * i2 * power_factor
power_3_in_watts = v3 * i3 * power_factor
if num_voltages == 3 and num_currents == 3:
total_power_in_watts = averaged_voltage * averaged_current * self.power_factor * square_root_of_three
elif num_voltages == 2 and num_currents == 2:
total_power_in_watts = averaged_voltage * averaged_current * self.power_factor * square_root_of_three
else:
total_power_in_watts = averaged_voltage * averaged_current * self.power_factor
return round(power_1_in_watts + power_2_in_watts + power_3_in_watts, 3)
return round(total_power_in_watts, 3)
def get_current_usage_info(self) -> dict:
"""
@ -302,7 +306,7 @@ class UTEClient:
readings = response["data"]["readings"]
power_in_watts = self._convert_triphasic_powers_to_power_in_watts(readings)
power_in_watts = self._convert_powers_to_power_in_watts(readings)
return_dict = {**response}
return_dict["data"]["power_in_watts"] = power_in_watts