2020-09-24 07:36:12 +02:00
|
|
|
"""Support for universal remotes."""
|
2021-01-09 23:21:54 +01:00
|
|
|
import struct
|
2024-04-17 08:20:13 +02:00
|
|
|
from typing import List, Optional, Tuple
|
2021-01-09 23:21:54 +01:00
|
|
|
|
2021-03-31 19:27:05 +02:00
|
|
|
from . import exceptions as e
|
2021-04-03 06:01:38 +02:00
|
|
|
from .device import Device
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
|
2024-04-17 08:20:13 +02:00
|
|
|
def pulses_to_data(pulses: List[int], tick: float = 32.84) -> bytes:
|
2024-04-11 03:51:41 +02:00
|
|
|
"""Convert a microsecond duration sequence into a Broadlink IR packet."""
|
|
|
|
result = bytearray(4)
|
|
|
|
result[0x00] = 0x26
|
|
|
|
|
|
|
|
for pulse in pulses:
|
|
|
|
div, mod = divmod(int(pulse // tick), 256)
|
|
|
|
if div:
|
|
|
|
result.append(0)
|
|
|
|
result.append(div)
|
|
|
|
result.append(mod)
|
|
|
|
|
|
|
|
data_len = len(result) - 4
|
|
|
|
result[0x02] = data_len & 0xFF
|
|
|
|
result[0x03] = data_len >> 8
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2024-04-17 08:20:13 +02:00
|
|
|
def data_to_pulses(data: bytes, tick: float = 32.84) -> List[int]:
|
2024-04-11 03:51:41 +02:00
|
|
|
"""Parse a Broadlink packet into a microsecond duration sequence."""
|
|
|
|
result = []
|
|
|
|
index = 4
|
|
|
|
end = min(256 * data[0x03] + data[0x02] + 4, len(data))
|
|
|
|
|
|
|
|
while index < end:
|
|
|
|
chunk = data[index]
|
|
|
|
index += 1
|
|
|
|
|
|
|
|
if chunk == 0:
|
|
|
|
try:
|
|
|
|
chunk = 256 * data[index] + data[index + 1]
|
2024-04-17 07:49:13 +02:00
|
|
|
except IndexError as err:
|
|
|
|
raise ValueError("Malformed data.") from err
|
2024-04-11 03:51:41 +02:00
|
|
|
index += 2
|
|
|
|
|
|
|
|
result.append(int(chunk * tick))
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2021-04-03 06:01:38 +02:00
|
|
|
class rmmini(Device):
|
2021-02-16 22:14:11 +01:00
|
|
|
"""Controls a Broadlink RM mini 3."""
|
2020-09-17 05:41:32 +02:00
|
|
|
|
2021-02-16 22:14:11 +01:00
|
|
|
TYPE = "RMMINI"
|
2021-01-09 23:21:54 +01:00
|
|
|
|
2021-04-05 19:02:56 +02:00
|
|
|
def _send(self, command: int, data: bytes = b"") -> bytes:
|
2021-01-09 23:21:54 +01:00
|
|
|
"""Send a packet to the device."""
|
|
|
|
packet = struct.pack("<I", command) + data
|
|
|
|
resp = self.send_packet(0x6A, packet)
|
2021-03-31 19:27:05 +02:00
|
|
|
e.check_error(resp[0x22:0x24])
|
2021-01-09 23:21:54 +01:00
|
|
|
payload = self.decrypt(resp[0x38:])
|
|
|
|
return payload[0x4:]
|
2020-09-17 05:41:32 +02:00
|
|
|
|
2021-03-11 05:00:10 +01:00
|
|
|
def update(self) -> None:
|
|
|
|
"""Update device name and lock status."""
|
|
|
|
resp = self._send(0x1)
|
|
|
|
self.name = resp[0x48:].split(b"\x00")[0].decode()
|
|
|
|
self.is_locked = bool(resp[0x87])
|
|
|
|
|
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
|
|
|
|
2021-02-16 22:14:11 +01:00
|
|
|
def check_data(self) -> bytes:
|
|
|
|
"""Return the last captured code."""
|
|
|
|
return self._send(0x4)
|
|
|
|
|
|
|
|
|
|
|
|
class rmpro(rmmini):
|
|
|
|
"""Controls a Broadlink RM pro."""
|
|
|
|
|
|
|
|
TYPE = "RMPRO"
|
|
|
|
|
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
|
|
|
|
2024-04-17 08:20:13 +02:00
|
|
|
def check_frequency(self) -> Tuple[bool, float]:
|
2020-09-17 05:41:32 +02:00
|
|
|
"""Return True if the frequency was identified successfully."""
|
2021-01-09 23:21:54 +01:00
|
|
|
resp = self._send(0x1A)
|
2024-04-09 20:40:00 +02:00
|
|
|
is_found = bool(resp[0])
|
|
|
|
frequency = struct.unpack("<I", resp[1:5])[0] / 1000.0
|
|
|
|
return is_found, frequency
|
2020-09-17 05:41:32 +02:00
|
|
|
|
2024-04-17 08:20:13 +02:00
|
|
|
def find_rf_packet(self, frequency: Optional[float] = None) -> None:
|
2020-09-17 05:41:32 +02:00
|
|
|
"""Enter radiofrequency learning mode."""
|
2024-04-09 20:40:00 +02:00
|
|
|
payload = bytearray()
|
|
|
|
if frequency:
|
|
|
|
payload += struct.pack("<I", int(frequency * 1000))
|
|
|
|
self._send(0x1B, payload)
|
2021-01-09 23:21:54 +01:00
|
|
|
|
2021-02-16 22:14:11 +01:00
|
|
|
def cancel_sweep_frequency(self) -> None:
|
|
|
|
"""Cancel sweep frequency."""
|
|
|
|
self._send(0x1E)
|
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-02-16 22:14:11 +01:00
|
|
|
temp = struct.unpack("<bb", resp[:0x2])
|
|
|
|
return {"temperature": temp[0x0] + temp[0x1] / 10.0}
|
|
|
|
|
|
|
|
def check_temperature(self) -> float:
|
|
|
|
"""Return the temperature."""
|
|
|
|
return self.check_sensors()["temperature"]
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
|
2021-02-16 22:14:11 +01:00
|
|
|
class rmminib(rmmini):
|
|
|
|
"""Controls a Broadlink RM mini 3 (new firmware)."""
|
2020-09-17 05:41:32 +02:00
|
|
|
|
2021-02-16 22:14:11 +01:00
|
|
|
TYPE = "RMMINIB"
|
2020-09-17 05:41:32 +02:00
|
|
|
|
2021-04-05 19:02:56 +02:00
|
|
|
def _send(self, command: int, data: bytes = b"") -> bytes:
|
2021-01-09 23:21:54 +01:00
|
|
|
"""Send a packet to the device."""
|
|
|
|
packet = struct.pack("<HI", len(data) + 4, command) + data
|
|
|
|
resp = self.send_packet(0x6A, packet)
|
2021-03-31 19:27:05 +02:00
|
|
|
e.check_error(resp[0x22:0x24])
|
2021-01-09 23:21:54 +01:00
|
|
|
payload = self.decrypt(resp[0x38:])
|
|
|
|
p_len = struct.unpack("<H", payload[:0x2])[0]
|
2024-04-17 07:49:13 +02:00
|
|
|
return payload[0x6:p_len+2]
|
2021-01-09 23:21:54 +01:00
|
|
|
|
2021-02-16 22:14:11 +01:00
|
|
|
|
|
|
|
class rm4mini(rmminib):
|
|
|
|
"""Controls a Broadlink RM4 mini."""
|
2021-04-05 19:02:56 +02:00
|
|
|
|
2021-02-16 22:14:11 +01:00
|
|
|
TYPE = "RM4MINI"
|
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-02-16 22:14:11 +01:00
|
|
|
temp = struct.unpack("<bb", resp[:0x2])
|
|
|
|
return {
|
|
|
|
"temperature": temp[0x0] + temp[0x1] / 100.0,
|
2021-04-05 19:02:56 +02:00
|
|
|
"humidity": resp[0x2] + resp[0x3] / 100.0,
|
2021-02-16 22:14:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
def check_temperature(self) -> float:
|
|
|
|
"""Return the temperature."""
|
|
|
|
return self.check_sensors()["temperature"]
|
|
|
|
|
|
|
|
def check_humidity(self) -> float:
|
|
|
|
"""Return the humidity."""
|
|
|
|
return self.check_sensors()["humidity"]
|
|
|
|
|
|
|
|
|
|
|
|
class rm4pro(rm4mini, rmpro):
|
|
|
|
"""Controls a Broadlink RM4 pro."""
|
|
|
|
|
|
|
|
TYPE = "RM4PRO"
|
|
|
|
|
|
|
|
|
|
|
|
class rm(rmpro):
|
|
|
|
"""For backwards compatibility."""
|
|
|
|
|
|
|
|
TYPE = "RM2"
|
|
|
|
|
|
|
|
|
|
|
|
class rm4(rm4pro):
|
|
|
|
"""For backwards compatibility."""
|
|
|
|
|
|
|
|
TYPE = "RM4"
|