From 4766d68289c1bfeab26378d0620646bfca662223 Mon Sep 17 00:00:00 2001 From: Felipe Martins Diel <41558831+felipediel@users.noreply.github.com> Date: Tue, 9 Apr 2024 20:32:41 -0300 Subject: [PATCH] Add support for Dooya DT360E (v2) (#785) --- broadlink/__init__.py | 5 +++- broadlink/cover.py | 62 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/broadlink/__init__.py b/broadlink/__init__.py index 3f63930d..3bfff8e9 100644 --- a/broadlink/__init__.py +++ b/broadlink/__init__.py @@ -7,7 +7,7 @@ from .const import DEFAULT_BCAST_ADDR, DEFAULT_PORT, DEFAULT_TIMEOUT from .alarm import S1C from .climate import hysen -from .cover import dooya +from .cover import dooya, dooya2 from .device import Device, ping, scan from .hub import s3 from .light import lb1, lb2 @@ -180,6 +180,9 @@ dooya: { 0x4E4D: ("DT360E-45/20", "Dooya"), }, + dooya2: { + 0x4F6E: ("DT360E-45/20", "Dooya"), + }, bg1: { 0x51E3: ("BG800/BG900", "BG Electrical"), }, diff --git a/broadlink/cover.py b/broadlink/cover.py index c0f08abb..1889c5c3 100644 --- a/broadlink/cover.py +++ b/broadlink/cover.py @@ -8,15 +8,15 @@ class dooya(Device): """Controls a Dooya curtain motor.""" - TYPE = "Dooya DT360E" + TYPE = "DT360E" - def _send(self, magic1: int, magic2: int) -> int: + def _send(self, command: int, attribute: int = 0) -> int: """Send a packet to the device.""" packet = bytearray(16) packet[0] = 0x09 packet[2] = 0xBB - packet[3] = magic1 - packet[4] = magic2 + packet[3] = command + packet[4] = attribute packet[9] = 0xFA packet[10] = 0x44 response = self.send_packet(0x6A, packet) @@ -26,15 +26,15 @@ def _send(self, magic1: int, magic2: int) -> int: def open(self) -> int: """Open the curtain.""" - return self._send(0x01, 0x00) + return self._send(0x01) def close(self) -> int: """Close the curtain.""" - return self._send(0x02, 0x00) + return self._send(0x02) def stop(self) -> int: """Stop the curtain.""" - return self._send(0x03, 0x00) + return self._send(0x03) def get_percentage(self) -> int: """Return the position of the curtain.""" @@ -55,3 +55,51 @@ def set_percentage_and_wait(self, new_percentage: int) -> None: time.sleep(0.2) current = self.get_percentage() self.stop() + + +class dooya2(Device): + """Controls a Dooya curtain motor (version 2).""" + + TYPE = "DT360E-2" + + def _send(self, command: int, attribute: int = 0) -> int: + """Send a packet to the device.""" + checksum = 0xC0C4 + command + attribute & 0xFFFF + packet = bytearray(32) + packet[0] = 0x16 + packet[2] = 0xA5 + packet[3] = 0xA5 + packet[4] = 0x5A + packet[5] = 0x5A + packet[6] = checksum & 0xFF + packet[7] = checksum >> 8 + packet[8] = 0x02 + packet[9] = 0x0B + packet[10] = 0x0A + packet[15] = command + packet[16] = attribute + + response = self.send_packet(0x6A, packet) + e.check_error(response[0x22:0x24]) + payload = self.decrypt(response[0x38:]) + return payload[0x11] + + def open(self) -> None: + """Open the curtain.""" + self._send(0x01) + + def close(self) -> None: + """Close the curtain.""" + self._send(0x02) + + def stop(self) -> None: + """Stop the curtain.""" + self._send(0x03) + + def get_percentage(self) -> int: + """Return the position of the curtain.""" + return self._send(0x06) + + def set_percentage(self, new_percentage: int) -> None: + """Set the position of the curtain.""" + self._send(0x09, new_percentage)