1
0
mirror of https://github.com/mjg59/python-broadlink.git synced 2024-09-21 12:30:10 +02:00
python-broadlink/broadlink/sensor.py

44 lines
1.4 KiB
Python
Raw Normal View History

"""Support for sensors."""
2020-09-17 05:41:32 +02:00
from .device import device
from .exceptions import check_error
class a1(device):
"""Controls a Broadlink A1."""
_SENSORS_AND_LEVELS = (
("light", ("dark", "dim", "normal", "bright")),
("air_quality", ("excellent", "good", "normal", "bad")),
("noise", ("quiet", "normal", "noisy")),
2020-09-17 05:41:32 +02:00
)
def __init__(self, *args, **kwargs) -> None:
"""Initialize the controller."""
device.__init__(self, *args, **kwargs)
self.type = "A1"
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"
2020-09-17 05:41:32 +02:00
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)
2020-09-17 05:41:32 +02:00
check_error(response[0x22:0x24])
2020-09-20 11:16:49 +02:00
payload = self.decrypt(response[0x38:])
2020-09-17 05:41:32 +02:00
data = bytearray(payload[0x4:])
return {
"temperature": data[0x0] + data[0x1] / 10.0,
"humidity": data[0x2] + data[0x3] / 10.0,
"light": data[0x4],
"air_quality": data[0x6],
"noise": data[0x8],
2020-09-17 05:41:32 +02:00
}