2020-09-24 07:36:12 +02:00
|
|
|
"""Support for covers."""
|
2020-09-17 05:41:32 +02:00
|
|
|
import time
|
|
|
|
|
|
|
|
from .device import device
|
|
|
|
from .exceptions import check_error
|
|
|
|
|
|
|
|
|
|
|
|
class dooya(device):
|
|
|
|
"""Controls a Dooya curtain motor."""
|
|
|
|
|
2021-01-28 23:16:25 +01:00
|
|
|
TYPE = "Dooya DT360E"
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
def _send(self, magic1: int, magic2: int) -> int:
|
|
|
|
"""Send a packet to the device."""
|
|
|
|
packet = bytearray(16)
|
|
|
|
packet[0] = 0x09
|
2020-11-05 20:00:42 +01:00
|
|
|
packet[2] = 0xBB
|
2020-09-17 05:41:32 +02:00
|
|
|
packet[3] = magic1
|
|
|
|
packet[4] = magic2
|
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)
|
2020-09-17 05:41:32 +02:00
|
|
|
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."""
|
|
|
|
return self._send(0x01, 0x00)
|
|
|
|
|
|
|
|
def close(self) -> int:
|
|
|
|
"""Close the curtain."""
|
|
|
|
return self._send(0x02, 0x00)
|
|
|
|
|
|
|
|
def stop(self) -> int:
|
|
|
|
"""Stop the curtain."""
|
|
|
|
return self._send(0x03, 0x00)
|
|
|
|
|
|
|
|
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()
|