Skip to content

Commit 5afc64d

Browse files
Add Temperature quantity
Signed-off-by: Christian Parpart <[email protected]>
1 parent 1b7053e commit 5afc64d

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

src/frequenz/sdk/timeseries/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@
3838
from ._base_types import UNIX_EPOCH, Sample, Sample3Phase
3939
from ._moving_window import MovingWindow
4040
from ._periodic_feature_extractor import PeriodicFeatureExtractor
41-
from ._quantities import Current, Energy, Percentage, Power, Quantity, Voltage
41+
from ._quantities import (
42+
Current,
43+
Energy,
44+
Percentage,
45+
Power,
46+
Quantity,
47+
Temperature,
48+
Voltage,
49+
)
4250
from ._resampling import ResamplerConfig
4351

4452
__all__ = [
@@ -55,6 +63,7 @@
5563
"Current",
5664
"Energy",
5765
"Power",
66+
"Temperature",
5867
"Voltage",
5968
"Percentage",
6069
]

src/frequenz/sdk/timeseries/_quantities.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@
1010
from typing import Any, NoReturn, Self, TypeVar, overload
1111

1212
QuantityT = TypeVar(
13-
"QuantityT", "Quantity", "Power", "Current", "Voltage", "Energy", "Percentage"
13+
"QuantityT",
14+
"Current",
15+
"Energy",
16+
"Percentage",
17+
"Power",
18+
"Quantity",
19+
"Temperature",
20+
"Voltage",
1421
)
1522

1623

@@ -301,6 +308,38 @@ def __call__(cls, *_args: Any, **_kwargs: Any) -> NoReturn:
301308
)
302309

303310

311+
class Temperature(
312+
Quantity,
313+
metaclass=_NoDefaultConstructible,
314+
exponent_unit_map={
315+
0: "°C",
316+
},
317+
):
318+
"""A temperature quantity (in degrees Celsius)."""
319+
320+
@classmethod
321+
def from_celsius(cls, value: float) -> Self:
322+
"""Initialize a new temperature quantity.
323+
324+
Args:
325+
value: The temperature in degrees Celsius.
326+
327+
Returns:
328+
A new temperature quantity.
329+
"""
330+
power = cls.__new__(cls)
331+
power._base_value = value
332+
return power
333+
334+
def as_celsius(self) -> float:
335+
"""Return the temperature in degrees Celsius.
336+
337+
Returns:
338+
The temperature in degrees Celsius.
339+
"""
340+
return self._base_value
341+
342+
304343
class Power(
305344
Quantity,
306345
metaclass=_NoDefaultConstructible,

0 commit comments

Comments
 (0)