summaryrefslogtreecommitdiff
path: root/src/home/mqtt/message/sensors.py
blob: ee522f0999260b52310d16974f3abb49419f91de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import struct

from typing import Tuple


class Temperature:
    format = 'IhH'

    def pack(self, time: int, temp: float, rh: float) -> bytes:
        return struct.pack(
            self.format,
            time,
            int(temp*100),
            int(rh*100)
        )

    def unpack(self, buf: bytes) -> Tuple[int, float, float]:
        data = struct.unpack(self.format, buf)
        return data[0], data[1]/100, data[2]/100