-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-architect data calculators (#319)
* Re-architect calculators * Complete
- Loading branch information
Showing
28 changed files
with
1,870 additions
and
1,547 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
"""Define humidity calculators.""" | ||
from __future__ import annotations | ||
|
||
from ecowitt2mqtt.const import ( | ||
DATA_POINT_HUMIDITY, | ||
DATA_POINT_TEMP, | ||
PERCENTAGE, | ||
UNIT_SYSTEM_IMPERIAL, | ||
WATER_VAPOR_GRAMS_PER_CUBIC_METER, | ||
WATER_VAPOR_POUNDS_PER_CUBIC_FOOT, | ||
) | ||
from ecowitt2mqtt.helpers.calculator import ( | ||
CalculatedDataPoint, | ||
Calculator, | ||
SimpleCalculator, | ||
) | ||
from ecowitt2mqtt.helpers.typing import PreCalculatedValueType | ||
from ecowitt2mqtt.util.meteo import ( | ||
get_absolute_humidity, | ||
get_temperature_meteocalc_object, | ||
) | ||
|
||
|
||
class AbsoluteHumidityCalculator(Calculator): | ||
"""Define an absolute humidity calculator.""" | ||
|
||
@property | ||
def output_unit(self) -> str | None: | ||
"""Get the output unit of measurement for this calculation.""" | ||
if self._config.output_unit_system == UNIT_SYSTEM_IMPERIAL: | ||
return WATER_VAPOR_POUNDS_PER_CUBIC_FOOT | ||
return WATER_VAPOR_GRAMS_PER_CUBIC_METER | ||
|
||
@Calculator.requires_keys(DATA_POINT_TEMP, DATA_POINT_HUMIDITY) | ||
def calculate_from_payload( | ||
self, payload: dict[str, PreCalculatedValueType] | ||
) -> CalculatedDataPoint: | ||
"""Perform the calculation.""" | ||
assert isinstance(payload[DATA_POINT_TEMP], float) | ||
assert isinstance(payload[DATA_POINT_HUMIDITY], float) | ||
|
||
temp_obj = get_temperature_meteocalc_object( | ||
payload[DATA_POINT_TEMP], self._config.input_unit_system | ||
) | ||
value = get_absolute_humidity(temp_obj, payload[DATA_POINT_HUMIDITY]) | ||
|
||
if self._config.output_unit_system == UNIT_SYSTEM_IMPERIAL: | ||
value /= 16018.46592051 | ||
|
||
return self.get_calculated_data_point(value) | ||
|
||
|
||
class RelativeHumidityCalculator(SimpleCalculator): | ||
"""Define a boolean leak calculator.""" | ||
|
||
@property | ||
def output_unit(self) -> str | None: | ||
"""Get the output unit of measurement for this calculation.""" | ||
return PERCENTAGE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
"""Define illuminance calculators.""" | ||
from __future__ import annotations | ||
|
||
import math | ||
|
||
from ecowitt2mqtt.const import ( | ||
DATA_POINT_SOLARRADIATION, | ||
ILLUMINANCE_WATTS_PER_SQUARE_METER, | ||
LIGHT_LUX, | ||
PERCENTAGE, | ||
) | ||
from ecowitt2mqtt.helpers.calculator import ( | ||
CalculatedDataPoint, | ||
Calculator, | ||
SimpleCalculator, | ||
) | ||
from ecowitt2mqtt.helpers.typing import PreCalculatedValueType | ||
|
||
|
||
class IlluminanceLuxCalculator(Calculator): | ||
"""Define a illuminance calculator (lux).""" | ||
|
||
@property | ||
def output_unit(self) -> str | None: | ||
"""Get the output unit of measurement for this calculation.""" | ||
return LIGHT_LUX | ||
|
||
@Calculator.requires_keys(DATA_POINT_SOLARRADIATION) | ||
def calculate_from_payload( | ||
self, payload: dict[str, PreCalculatedValueType] | ||
) -> CalculatedDataPoint: | ||
"""Perform the calculation.""" | ||
assert isinstance(payload[DATA_POINT_SOLARRADIATION], float) | ||
|
||
return self.get_calculated_data_point( | ||
round(float(payload[DATA_POINT_SOLARRADIATION]) / 0.0079, 1) | ||
) | ||
|
||
|
||
class IlluminancePerceivedCalculator(Calculator): | ||
"""Define a illuminance calculator (perceived).""" | ||
|
||
@property | ||
def output_unit(self) -> str | None: | ||
"""Get the output unit of measurement for this calculation.""" | ||
return PERCENTAGE | ||
|
||
@Calculator.requires_keys(DATA_POINT_SOLARRADIATION) | ||
def calculate_from_payload( | ||
self, payload: dict[str, PreCalculatedValueType] | ||
) -> CalculatedDataPoint: | ||
"""Perform the calculation.""" | ||
assert isinstance(payload[DATA_POINT_SOLARRADIATION], float) | ||
|
||
lux_value = round(float(payload[DATA_POINT_SOLARRADIATION]) / 0.0079, 1) | ||
|
||
try: | ||
final_value = round(math.log10(lux_value) / 5, 2) * 100 | ||
except ValueError: | ||
# If we've approached negative infinity, we'll get a math domain error; in | ||
# that case, return 0.0: | ||
final_value = 0.0 | ||
|
||
return self.get_calculated_data_point(final_value) | ||
|
||
|
||
class IlluminanceWM2Calculator(SimpleCalculator): | ||
"""Define a illuminance calculator (W/m²).""" | ||
|
||
@property | ||
def output_unit(self) -> str | None: | ||
"""Get the output unit of measurement for this calculation.""" | ||
return ILLUMINANCE_WATTS_PER_SQUARE_METER |
Oops, something went wrong.