File: air_data.py

package info (click to toggle)
python-awair 0.2.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,016 kB
  • sloc: python: 1,041; makefile: 12
file content (40 lines) | stat: -rw-r--r-- 1,135 bytes parent folder | download | duplicates (2)
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
"""Wrapper class for awair airdata responses."""

from datetime import datetime
from typing import Any, Dict

from python_awair import const
from python_awair.indices import Indices
from python_awair.sensors import Sensors


class AirData:
    """Wrapper class for awair airdata responses."""

    timestamp: datetime
    score: float
    sensors: Sensors
    indices: Indices

    def __init__(self, attributes: Dict[str, Any]) -> None:
        """Initialize from API data."""
        self.timestamp = datetime.strptime(attributes["timestamp"], const.DATE_FORMAT)
        self.score = attributes["score"]

        self.sensors = Sensors(
            {
                sensor["comp"]: sensor["value"]
                for sensor in attributes.get("sensors", [])
            }
        )

        self.indices = Indices(
            {index["comp"]: index["value"] for index in attributes.get("indices", [])}
        )

    def __repr__(self) -> str:
        """Return a friendly representation."""
        return (
            f"<AirData@{self.timestamp}: "
            + f"score={round(self.score, 2)} sensors={self.sensors}>"
        )