2020-09-24 07:36:12 +02:00
|
|
|
"""Support for covers."""
|
2020-09-17 05:41:32 +02:00
|
|
|
import time
|
|
|
|
|
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
|
|
|
|
|
|
|
|
2021-04-03 06:01:38 +02:00
|
|
|
class dooya(Device):
|
2020-09-17 05:41:32 +02:00
|
|
|
"""Controls a Dooya curtain motor."""
|
|
|
|
|
2024-04-10 01:32:41 +02:00
|
|
|
TYPE = "DT360E"
|
2020-09-17 05:41:32 +02:00
|
|
|
|
2024-04-10 01:32:41 +02:00
|
|
|
def _send(self, command: int, attribute: int = 0) -> int:
|
2020-09-17 05:41:32 +02:00
|
|
|
"""Send a packet to the device."""
|
|
|
|
packet = bytearray(16)
|
|
|
|
packet[0] = 0x09
|
2020-11-05 20:00:42 +01:00
|
|
|
packet[2] = 0xBB
|
2024-04-10 01:32:41 +02:00
|
|
|
packet[3] = command
|
|
|
|
packet[4] = attribute
|
2020-11-05 20:00:42 +01:00
|
|
|
packet[9] = 0xFA
|
2020-09-17 05:41:32 +02:00
|
|
|
packet[10] = 0x44
|
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:])
|
|
|
|
return payload[4]
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
def open(self) -> int:
|
|
|
|
"""Open the curtain."""
|
2024-04-10 01:32:41 +02:00
|
|
|
return self._send(0x01)
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
def close(self) -> int:
|
|
|
|
"""Close the curtain."""
|
2024-04-10 01:32:41 +02:00
|
|
|
return self._send(0x02)
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
def stop(self) -> int:
|
|
|
|
"""Stop the curtain."""
|
2024-04-10 01:32:41 +02:00
|
|
|
return self._send(0x03)
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
def get_percentage(self) -> int:
|
|
|
|
"""Return the position of the curtain."""
|
2020-11-05 20:00:42 +01:00
|
|
|
return self._send(0x06, 0x5D)
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
def set_percentage_and_wait(self, new_percentage: int) -> None:
|
|
|
|
"""Set the position of the curtain."""
|
|
|
|
current = self.get_percentage()
|
|
|
|
if current > new_percentage:
|
|
|
|
self.close()
|
|
|
|
while current is not None and current > new_percentage:
|
|
|
|
time.sleep(0.2)
|
|
|
|
current = self.get_percentage()
|
|
|
|
|
|
|
|
elif current < new_percentage:
|
|
|
|
self.open()
|
|
|
|
while current is not None and current < new_percentage:
|
|
|
|
time.sleep(0.2)
|
|
|
|
current = self.get_percentage()
|
|
|
|
self.stop()
|
2024-04-10 01:32:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
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)
|