2020-09-24 07:36:12 +02:00
|
|
|
"""Support for sensors."""
|
2021-01-10 02:18:56 +01:00
|
|
|
import struct
|
|
|
|
|
2021-03-31 19:27:05 +02:00
|
|
|
from . import exceptions as e
|
2020-09-17 05:41:32 +02:00
|
|
|
from .device import device
|
|
|
|
|
|
|
|
|
|
|
|
class a1(device):
|
|
|
|
"""Controls a Broadlink A1."""
|
|
|
|
|
2021-01-28 23:16:25 +01:00
|
|
|
TYPE = "A1"
|
|
|
|
|
2020-09-17 05:41:32 +02:00
|
|
|
_SENSORS_AND_LEVELS = (
|
2020-11-05 20:00:42 +01:00
|
|
|
("light", ("dark", "dim", "normal", "bright")),
|
|
|
|
("air_quality", ("excellent", "good", "normal", "bad")),
|
|
|
|
("noise", ("quiet", "normal", "noisy")),
|
2020-09-17 05:41:32 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
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:
|
2020-11-05 20:00:42 +01:00
|
|
|
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])
|
2020-11-05 20:00:42 +01:00
|
|
|
response = self.send_packet(0x6A, packet)
|
2021-03-31 19:27:05 +02:00
|
|
|
e.check_error(response[0x22:0x24])
|
2020-09-20 11:16:49 +02:00
|
|
|
payload = self.decrypt(response[0x38:])
|
2021-01-10 02:18:56 +01:00
|
|
|
data = payload[0x4:]
|
|
|
|
|
|
|
|
temperature = struct.unpack("<bb", data[:0x2])
|
|
|
|
temperature = temperature[0x0] + temperature[0x1] / 10.0
|
|
|
|
humidity = data[0x2] + data[0x3] / 10.0
|
|
|
|
|
2020-09-17 05:41:32 +02:00
|
|
|
return {
|
2021-01-10 02:18:56 +01:00
|
|
|
"temperature": temperature,
|
|
|
|
"humidity": humidity,
|
2020-11-05 20:00:42 +01:00
|
|
|
"light": data[0x4],
|
|
|
|
"air_quality": data[0x6],
|
|
|
|
"noise": data[0x8],
|
2020-09-17 05:41:32 +02:00
|
|
|
}
|