File: util.py

package info (click to toggle)
python-toonapi 0.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 224 kB
  • sloc: python: 708; xml: 325; makefile: 97
file content (57 lines) | stat: -rw-r--r-- 1,711 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""Collection of small utility functions for ToonAPI."""
from datetime import datetime
from typing import Any, Optional


def convert_temperature(temperature: int) -> Optional[float]:
    """Convert a temperature value from the ToonAPI to a float value."""
    if temperature is None:
        return None
    return temperature / 100.0


def convert_boolean(value: Any) -> Optional[bool]:
    """Convert a value from the ToonAPI to a boolean."""
    if value is None:
        return None
    return bool(value)


def convert_datetime(timestamp: int) -> datetime:
    """Convert a java microseconds timestamp from the ToonAPI to a datetime."""
    return datetime.utcfromtimestamp(timestamp // 1000.0).replace(
        microsecond=timestamp % 1000 * 1000
    )


def convert_kwh(value: int) -> Optional[float]:
    """Convert a Wh value from the ToonAPI to a kWH value."""
    if value is None:
        return None
    return round(float(value) / 1000.0, 2)


def convert_cm3(value: int) -> Optional[float]:
    """Convert a value from the ToonAPI to a CM3 value."""
    if value is None:
        return None
    return round(float(value) / 1000.0, 2)


def convert_negative_none(value: int) -> Optional[int]:
    """Convert an negative int value from the ToonAPI to a NoneType."""
    return None if value < 0 else value


def convert_m3(value: int) -> Optional[float]:
    """Convert a value from the ToonAPI to a M3 value."""
    if value is None:
        return None
    return round(float(value) / 1000.0, 2)


def convert_lmin(value: int) -> Optional[float]:
    """Convert a value from the ToonAPI to a L/MINUTE value."""
    if value is None:
        return None
    return round(float(value) / 60.0, 1)