2020-09-24 07:36:12 +02:00
|
|
|
"""Support for universal remotes."""
|
2021-01-09 23:21:54 +01:00
|
|
|
import struct
|
|
|
|
|
2020-09-17 05:41:32 +02:00
|
|
|
from .device import device
|
|
|
|
from .exceptions import check_error
|
|
|
|
|
|
|
|
|
|
|
|
class rm(device):
|
|
|
|
"""Controls a Broadlink RM."""
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs) -> None:
|
|
|
|
"""Initialize the controller."""
|
|
|
|
device.__init__(self, *args, **kwargs)
|
|
|
|
self.type = "RM2"
|
2021-01-09 23:21:54 +01:00
|
|
|
|
|
|
|
def _send(self, command: int, data: bytes = b'') -> bytes:
|
|
|
|
"""Send a packet to the device."""
|
|
|
|
packet = struct.pack("<I", command) + data
|
|
|
|
resp = self.send_packet(0x6A, packet)
|
|
|
|
check_error(resp[0x22:0x24])
|
|
|
|
payload = self.decrypt(resp[0x38:])
|
|
|
|
return payload[0x4:]
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
def check_data(self) -> bytes:
|
|
|
|
"""Return the last captured code."""
|
2021-01-09 23:21:54 +01:00
|
|
|
return self._send(0x4)
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
def send_data(self, data: bytes) -> None:
|
|
|
|
"""Send a code to the device."""
|
2021-01-09 23:21:54 +01:00
|
|
|
self._send(0x2, data)
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
def enter_learning(self) -> None:
|
|
|
|
"""Enter infrared learning mode."""
|
2021-01-09 23:21:54 +01:00
|
|
|
self._send(0x3)
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
def sweep_frequency(self) -> None:
|
|
|
|
"""Sweep frequency."""
|
2021-01-09 23:21:54 +01:00
|
|
|
self._send(0x19)
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
def cancel_sweep_frequency(self) -> None:
|
|
|
|
"""Cancel sweep frequency."""
|
2021-01-09 23:21:54 +01:00
|
|
|
self._send(0x1E)
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
def check_frequency(self) -> bool:
|
|
|
|
"""Return True if the frequency was identified successfully."""
|
2021-01-09 23:21:54 +01:00
|
|
|
resp = self._send(0x1A)
|
|
|
|
return resp[0] == 1
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
def find_rf_packet(self) -> bool:
|
|
|
|
"""Enter radiofrequency learning mode."""
|
2021-01-09 23:21:54 +01:00
|
|
|
resp = self._send(0x1B)
|
|
|
|
return resp[0] == 1
|
|
|
|
|
|
|
|
def check_temperature(self) -> float:
|
2020-09-17 05:41:32 +02:00
|
|
|
"""Return the temperature."""
|
2021-01-09 23:21:54 +01:00
|
|
|
return self.check_sensors()["temperature"]
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
def check_sensors(self) -> dict:
|
|
|
|
"""Return the state of the sensors."""
|
2021-01-09 23:21:54 +01:00
|
|
|
resp = self._send(0x1)
|
2021-01-10 02:18:56 +01:00
|
|
|
temperature = struct.unpack("<bb", resp[:0x2])
|
|
|
|
temperature = temperature[0x0] + temperature[0x1] / 10.0
|
|
|
|
return {"temperature": temperature}
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
class rm4(rm):
|
|
|
|
"""Controls a Broadlink RM4."""
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs) -> None:
|
|
|
|
"""Initialize the controller."""
|
|
|
|
device.__init__(self, *args, **kwargs)
|
|
|
|
self.type = "RM4"
|
|
|
|
|
2021-01-09 23:21:54 +01:00
|
|
|
def _send(self, command: int, data: bytes = b'') -> bytes:
|
|
|
|
"""Send a packet to the device."""
|
|
|
|
packet = struct.pack("<HI", len(data) + 4, command) + data
|
|
|
|
resp = self.send_packet(0x6A, packet)
|
|
|
|
check_error(resp[0x22:0x24])
|
|
|
|
payload = self.decrypt(resp[0x38:])
|
|
|
|
p_len = struct.unpack("<H", payload[:0x2])[0]
|
|
|
|
return payload[0x6:p_len+2]
|
2020-09-17 05:41:32 +02:00
|
|
|
|
2021-01-09 23:21:54 +01:00
|
|
|
def check_humidity(self) -> float:
|
2020-09-17 05:41:32 +02:00
|
|
|
"""Return the humidity."""
|
2021-01-09 23:21:54 +01:00
|
|
|
return self.check_sensors()["humidity"]
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
def check_sensors(self) -> dict:
|
|
|
|
"""Return the state of the sensors."""
|
2021-01-09 23:21:54 +01:00
|
|
|
resp = self._send(0x24)
|
2021-01-10 02:18:56 +01:00
|
|
|
temperature = struct.unpack("<bb", resp[:0x2])
|
|
|
|
temperature = temperature[0x0] + temperature[0x1] / 100.0
|
|
|
|
humidity = resp[0x2] + resp[0x3] / 100.0
|
|
|
|
return {"temperature": temperature, "humidity": humidity}
|