1
0
mirror of https://github.com/mjg59/python-broadlink.git synced 2024-11-22 07:00:12 +01:00
python-broadlink/broadlink/sensor.py
Felipe Martins Diel 1a8ee21a34 Make better use of namespaces (#564)
Use namespaces for typing and exceptions.
2021-04-26 17:57:02 -03:00

48 lines
1.4 KiB
Python

"""Support for sensors."""
import struct
from . import exceptions as e
from .device import device
class a1(device):
"""Controls a Broadlink A1."""
TYPE = "A1"
_SENSORS_AND_LEVELS = (
("light", ("dark", "dim", "normal", "bright")),
("air_quality", ("excellent", "good", "normal", "bad")),
("noise", ("quiet", "normal", "noisy")),
)
def check_sensors(self) -> dict:
"""Return the state of the sensors."""
data = self.check_sensors_raw()
for sensor, levels in self._SENSORS_AND_LEVELS:
try:
data[sensor] = levels[data[sensor]]
except IndexError:
data[sensor] = "unknown"
return data
def check_sensors_raw(self) -> dict:
"""Return the state of the sensors in raw format."""
packet = bytearray([0x1])
response = self.send_packet(0x6A, packet)
e.check_error(response[0x22:0x24])
payload = self.decrypt(response[0x38:])
data = payload[0x4:]
temperature = struct.unpack("<bb", data[:0x2])
temperature = temperature[0x0] + temperature[0x1] / 10.0
humidity = data[0x2] + data[0x3] / 10.0
return {
"temperature": temperature,
"humidity": humidity,
"light": data[0x4],
"air_quality": data[0x6],
"noise": data[0x8],
}