1
0
mirror of https://github.com/mjg59/python-broadlink.git synced 2024-11-21 22:51:41 +01:00

Split the rm and rm4 classes into smaller classes (#529)

This commit is contained in:
Felipe Martins Diel 2021-02-16 18:14:11 -03:00 committed by GitHub
parent 1b73cfce3a
commit 39ee67bb98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 101 additions and 67 deletions

View File

@ -9,7 +9,7 @@ from .cover import dooya
from .device import device, ping, scan from .device import device, ping, scan
from .exceptions import exception from .exceptions import exception
from .light import lb1 from .light import lb1
from .remote import rm, rm4 from .remote import rm, rm4, rm4mini, rm4pro, rmmini, rmminib, rmpro
from .sensor import a1 from .sensor import a1
from .switch import bg1, mp1, sp1, sp2, sp2s, sp3, sp3s, sp4, sp4b from .switch import bg1, mp1, sp1, sp2, sp2s, sp3, sp3s, sp4, sp4b
@ -54,45 +54,45 @@ SUPPORTED_TYPES = {
0x618B: (sp4b, "SP4L-EU", "Broadlink"), 0x618B: (sp4b, "SP4L-EU", "Broadlink"),
0x6489: (sp4b, "SP4L-AU", "Broadlink"), 0x6489: (sp4b, "SP4L-AU", "Broadlink"),
0x648B: (sp4b, "SP4M-US", "Broadlink"), 0x648B: (sp4b, "SP4M-US", "Broadlink"),
0x2712: (rm, "RM pro/pro+", "Broadlink"), 0x2737: (rmmini, "RM mini 3", "Broadlink"),
0x272A: (rm, "RM pro", "Broadlink"), 0x278F: (rmmini, "RM mini", "Broadlink"),
0x2737: (rm, "RM mini 3", "Broadlink"), 0x27C2: (rmmini, "RM mini 3", "Broadlink"),
0x273D: (rm, "RM pro", "Broadlink"), 0x27C7: (rmmini, "RM mini 3", "Broadlink"),
0x277C: (rm, "RM home", "Broadlink"), 0x27CC: (rmmini, "RM mini 3", "Broadlink"),
0x2783: (rm, "RM home", "Broadlink"), 0x27CD: (rmmini, "RM mini 3", "Broadlink"),
0x2787: (rm, "RM pro", "Broadlink"), 0x27D0: (rmmini, "RM mini 3", "Broadlink"),
0x278B: (rm, "RM plus", "Broadlink"), 0x27D1: (rmmini, "RM mini 3", "Broadlink"),
0x278F: (rm, "RM mini", "Broadlink"), 0x27D3: (rmmini, "RM mini 3", "Broadlink"),
0x2797: (rm, "RM pro+", "Broadlink"), 0x27DE: (rmmini, "RM mini 3", "Broadlink"),
0x279D: (rm, "RM pro+", "Broadlink"), 0x2712: (rmpro, "RM pro/pro+", "Broadlink"),
0x27A1: (rm, "RM plus", "Broadlink"), 0x272A: (rmpro, "RM pro", "Broadlink"),
0x27A6: (rm, "RM plus", "Broadlink"), 0x273D: (rmpro, "RM pro", "Broadlink"),
0x27A9: (rm, "RM pro+", "Broadlink"), 0x277C: (rmpro, "RM home", "Broadlink"),
0x27C2: (rm, "RM mini 3", "Broadlink"), 0x2783: (rmpro, "RM home", "Broadlink"),
0x27C3: (rm, "RM pro+", "Broadlink"), 0x2787: (rmpro, "RM pro", "Broadlink"),
0x27C7: (rm, "RM mini 3", "Broadlink"), 0x278B: (rmpro, "RM plus", "Broadlink"),
0x27CC: (rm, "RM mini 3", "Broadlink"), 0x2797: (rmpro, "RM pro+", "Broadlink"),
0x27CD: (rm, "RM mini 3", "Broadlink"), 0x279D: (rmpro, "RM pro+", "Broadlink"),
0x27D0: (rm, "RM mini 3", "Broadlink"), 0x27A1: (rmpro, "RM plus", "Broadlink"),
0x27D1: (rm, "RM mini 3", "Broadlink"), 0x27A6: (rmpro, "RM plus", "Broadlink"),
0x27D3: (rm, "RM mini 3", "Broadlink"), 0x27A9: (rmpro, "RM pro+", "Broadlink"),
0x27DE: (rm, "RM mini 3", "Broadlink"), 0x27C3: (rmpro, "RM pro+", "Broadlink"),
0x51DA: (rm4, "RM4 mini", "Broadlink"), 0x5F36: (rmminib, "RM mini 3", "Broadlink"),
0x5F36: (rm4, "RM mini 3", "Broadlink"), 0x6508: (rmminib, "RM mini 3", "Broadlink"),
0x6026: (rm4, "RM4 pro", "Broadlink"), 0x51DA: (rm4mini, "RM4 mini", "Broadlink"),
0x6070: (rm4, "RM4C mini", "Broadlink"), 0x6070: (rm4mini, "RM4C mini", "Broadlink"),
0x610E: (rm4, "RM4 mini", "Broadlink"), 0x610E: (rm4mini, "RM4 mini", "Broadlink"),
0x610F: (rm4, "RM4C mini", "Broadlink"), 0x610F: (rm4mini, "RM4C mini", "Broadlink"),
0x61A2: (rm4, "RM4 pro", "Broadlink"), 0x62BC: (rm4mini, "RM4 mini", "Broadlink"),
0x62BC: (rm4, "RM4 mini", "Broadlink"), 0x62BE: (rm4mini, "RM4C mini", "Broadlink"),
0x62BE: (rm4, "RM4C mini", "Broadlink"), 0x6364: (rm4mini, "RM4S", "Broadlink"),
0x6364: (rm4, "RM4S", "Broadlink"), 0x648D: (rm4mini, "RM4 mini", "Broadlink"),
0x648D: (rm4, "RM4 mini", "Broadlink"), 0x6539: (rm4mini, "RM4C mini", "Broadlink"),
0x649B: (rm4, "RM4 pro", "Broadlink"), 0x653A: (rm4mini, "RM4 mini", "Broadlink"),
0x6508: (rm4, "RM mini 3", "Broadlink"), 0x6026: (rm4pro, "RM4 pro", "Broadlink"),
0x6539: (rm4, "RM4C mini", "Broadlink"), 0x61A2: (rm4pro, "RM4 pro", "Broadlink"),
0x653A: (rm4, "RM4 mini", "Broadlink"), 0x649B: (rm4pro, "RM4 pro", "Broadlink"),
0x653C: (rm4, "RM4 pro", "Broadlink"), 0x653C: (rm4pro, "RM4 pro", "Broadlink"),
0x2714: (a1, "e-Sensor", "Broadlink"), 0x2714: (a1, "e-Sensor", "Broadlink"),
0x4EB5: (mp1, "MP1-1K4S", "Broadlink"), 0x4EB5: (mp1, "MP1-1K4S", "Broadlink"),
0x4EF7: (mp1, "MP1-1K4S", "Broadlink (OEM)"), 0x4EF7: (mp1, "MP1-1K4S", "Broadlink (OEM)"),

View File

@ -5,10 +5,10 @@ from .device import device
from .exceptions import check_error from .exceptions import check_error
class rm(device): class rmmini(device):
"""Controls a Broadlink RM.""" """Controls a Broadlink RM mini 3."""
TYPE = "RM2" TYPE = "RMMINI"
def _send(self, command: int, data: bytes = b'') -> bytes: def _send(self, command: int, data: bytes = b'') -> bytes:
"""Send a packet to the device.""" """Send a packet to the device."""
@ -18,10 +18,6 @@ class rm(device):
payload = self.decrypt(resp[0x38:]) payload = self.decrypt(resp[0x38:])
return payload[0x4:] return payload[0x4:]
def check_data(self) -> bytes:
"""Return the last captured code."""
return self._send(0x4)
def send_data(self, data: bytes) -> None: def send_data(self, data: bytes) -> None:
"""Send a code to the device.""" """Send a code to the device."""
self._send(0x2, data) self._send(0x2, data)
@ -30,14 +26,20 @@ class rm(device):
"""Enter infrared learning mode.""" """Enter infrared learning mode."""
self._send(0x3) self._send(0x3)
def check_data(self) -> bytes:
"""Return the last captured code."""
return self._send(0x4)
class rmpro(rmmini):
"""Controls a Broadlink RM pro."""
TYPE = "RMPRO"
def sweep_frequency(self) -> None: def sweep_frequency(self) -> None:
"""Sweep frequency.""" """Sweep frequency."""
self._send(0x19) self._send(0x19)
def cancel_sweep_frequency(self) -> None:
"""Cancel sweep frequency."""
self._send(0x1E)
def check_frequency(self) -> bool: def check_frequency(self) -> bool:
"""Return True if the frequency was identified successfully.""" """Return True if the frequency was identified successfully."""
resp = self._send(0x1A) resp = self._send(0x1A)
@ -47,22 +49,25 @@ class rm(device):
"""Enter radiofrequency learning mode.""" """Enter radiofrequency learning mode."""
self._send(0x1B) self._send(0x1B)
def check_temperature(self) -> float: def cancel_sweep_frequency(self) -> None:
"""Return the temperature.""" """Cancel sweep frequency."""
return self.check_sensors()["temperature"] self._send(0x1E)
def check_sensors(self) -> dict: def check_sensors(self) -> dict:
"""Return the state of the sensors.""" """Return the state of the sensors."""
resp = self._send(0x1) resp = self._send(0x1)
temperature = struct.unpack("<bb", resp[:0x2]) temp = struct.unpack("<bb", resp[:0x2])
temperature = temperature[0x0] + temperature[0x1] / 10.0 return {"temperature": temp[0x0] + temp[0x1] / 10.0}
return {"temperature": temperature}
def check_temperature(self) -> float:
"""Return the temperature."""
return self.check_sensors()["temperature"]
class rm4(rm): class rmminib(rmmini):
"""Controls a Broadlink RM4.""" """Controls a Broadlink RM mini 3 (new firmware)."""
TYPE = "RM4" TYPE = "RMMINIB"
def _send(self, command: int, data: bytes = b'') -> bytes: def _send(self, command: int, data: bytes = b'') -> bytes:
"""Send a packet to the device.""" """Send a packet to the device."""
@ -73,14 +78,43 @@ class rm4(rm):
p_len = struct.unpack("<H", payload[:0x2])[0] p_len = struct.unpack("<H", payload[:0x2])[0]
return payload[0x6:p_len+2] return payload[0x6:p_len+2]
def check_humidity(self) -> float:
"""Return the humidity.""" class rm4mini(rmminib):
return self.check_sensors()["humidity"] """Controls a Broadlink RM4 mini."""
TYPE = "RM4MINI"
def check_sensors(self) -> dict: def check_sensors(self) -> dict:
"""Return the state of the sensors.""" """Return the state of the sensors."""
resp = self._send(0x24) resp = self._send(0x24)
temperature = struct.unpack("<bb", resp[:0x2]) temp = struct.unpack("<bb", resp[:0x2])
temperature = temperature[0x0] + temperature[0x1] / 100.0 return {
humidity = resp[0x2] + resp[0x3] / 100.0 "temperature": temp[0x0] + temp[0x1] / 100.0,
return {"temperature": temperature, "humidity": humidity} "humidity": resp[0x2] + resp[0x3] / 100.0
}
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"