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)
|
2024-04-10 21:35:25 +02:00
|
|
|
packet[0x00] = 0x09
|
|
|
|
packet[0x02] = 0xBB
|
|
|
|
packet[0x03] = command
|
|
|
|
packet[0x04] = attribute
|
|
|
|
packet[0x09] = 0xFA
|
|
|
|
packet[0x0A] = 0x44
|
|
|
|
|
|
|
|
resp = self.send_packet(0x6A, packet)
|
|
|
|
e.check_error(resp[0x22:0x24])
|
|
|
|
payload = self.decrypt(resp[0x38:])
|
2020-09-20 11:16:49 +02:00
|
|
|
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"
|
|
|
|
|
2024-04-12 07:10:06 +02:00
|
|
|
def _send(self, operation: int, data: bytes = b""):
|
2024-04-10 21:35:25 +02:00
|
|
|
"""Send a command to the device."""
|
2024-04-12 07:10:06 +02:00
|
|
|
packet = bytearray(12)
|
2024-04-10 21:35:25 +02:00
|
|
|
packet[0x02] = 0xA5
|
|
|
|
packet[0x03] = 0xA5
|
|
|
|
packet[0x04] = 0x5A
|
|
|
|
packet[0x05] = 0x5A
|
|
|
|
packet[0x08] = operation
|
|
|
|
packet[0x09] = 0x0B
|
|
|
|
|
2024-04-12 07:10:06 +02:00
|
|
|
if data:
|
|
|
|
data_len = len(data)
|
|
|
|
packet[0x0A] = data_len & 0xFF
|
|
|
|
packet[0x0B] = data_len >> 8
|
|
|
|
packet += bytes(2)
|
|
|
|
packet.extend(data)
|
2024-04-10 21:35:25 +02:00
|
|
|
|
|
|
|
checksum = sum(packet, 0xBEAF) & 0xFFFF
|
2024-04-12 07:10:06 +02:00
|
|
|
packet[0x06] = checksum & 0xFF
|
|
|
|
packet[0x07] = checksum >> 8
|
2024-04-10 01:32:41 +02:00
|
|
|
|
2024-04-10 21:35:25 +02:00
|
|
|
packet_len = len(packet) - 2
|
2024-04-12 07:10:06 +02:00
|
|
|
packet[0x00] = packet_len & 0xFF
|
|
|
|
packet[0x01] = packet_len >> 8
|
2024-04-10 21:35:25 +02:00
|
|
|
|
2024-04-12 07:10:06 +02:00
|
|
|
resp = self.send_packet(0x6A, packet)
|
2024-04-10 21:35:25 +02:00
|
|
|
e.check_error(resp[0x22:0x24])
|
|
|
|
payload = self.decrypt(resp[0x38:])
|
|
|
|
return payload
|
2024-04-10 01:32:41 +02:00
|
|
|
|
|
|
|
def open(self) -> None:
|
|
|
|
"""Open the curtain."""
|
2024-04-10 21:35:25 +02:00
|
|
|
self._send(2, [0x00, 0x01, 0x00])
|
2024-04-10 01:32:41 +02:00
|
|
|
|
|
|
|
def close(self) -> None:
|
|
|
|
"""Close the curtain."""
|
2024-04-10 21:35:25 +02:00
|
|
|
self._send(2, [0x00, 0x02, 0x00])
|
2024-04-10 01:32:41 +02:00
|
|
|
|
|
|
|
def stop(self) -> None:
|
|
|
|
"""Stop the curtain."""
|
2024-04-10 21:35:25 +02:00
|
|
|
self._send(2, [0x00, 0x03, 0x00])
|
2024-04-10 01:32:41 +02:00
|
|
|
|
|
|
|
def get_percentage(self) -> int:
|
|
|
|
"""Return the position of the curtain."""
|
2024-04-10 21:35:25 +02:00
|
|
|
resp = self._send(1, [0x00, 0x06, 0x00])
|
|
|
|
return resp[0x11]
|
2024-04-10 01:32:41 +02:00
|
|
|
|
|
|
|
def set_percentage(self, new_percentage: int) -> None:
|
|
|
|
"""Set the position of the curtain."""
|
2024-04-10 21:35:25 +02:00
|
|
|
self._send(2, [0x00, 0x09, new_percentage])
|
|
|
|
|
|
|
|
|
|
|
|
class wser(Device):
|
|
|
|
"""Controls a Wistar curtain motor"""
|
|
|
|
|
|
|
|
TYPE = "WSER"
|
|
|
|
|
2024-04-12 07:10:06 +02:00
|
|
|
def _send(self, operation: int, data: bytes = b""):
|
2024-04-10 21:35:25 +02:00
|
|
|
"""Send a command to the device."""
|
2024-04-12 07:10:06 +02:00
|
|
|
packet = bytearray(12)
|
2024-04-10 21:35:25 +02:00
|
|
|
packet[0x02] = 0xA5
|
|
|
|
packet[0x03] = 0xA5
|
|
|
|
packet[0x04] = 0x5A
|
|
|
|
packet[0x05] = 0x5A
|
|
|
|
packet[0x08] = operation
|
|
|
|
packet[0x09] = 0x0B
|
|
|
|
|
2024-04-12 07:10:06 +02:00
|
|
|
if data:
|
|
|
|
data_len = len(data)
|
|
|
|
packet[0x0A] = data_len & 0xFF
|
|
|
|
packet[0x0B] = data_len >> 8
|
|
|
|
packet += bytes(2)
|
|
|
|
packet.extend(data)
|
2024-04-10 21:35:25 +02:00
|
|
|
|
|
|
|
checksum = sum(packet, 0xBEAF) & 0xFFFF
|
2024-04-12 07:10:06 +02:00
|
|
|
packet[0x06] = checksum & 0xFF
|
|
|
|
packet[0x07] = checksum >> 8
|
2024-04-10 21:35:25 +02:00
|
|
|
|
|
|
|
packet_len = len(packet) - 2
|
2024-04-12 07:10:06 +02:00
|
|
|
packet[0x00] = packet_len & 0xFF
|
|
|
|
packet[0x01] = packet_len >> 8
|
2024-04-10 21:35:25 +02:00
|
|
|
|
2024-04-12 07:10:06 +02:00
|
|
|
resp = self.send_packet(0x6A, packet)
|
2024-04-10 21:35:25 +02:00
|
|
|
e.check_error(resp[0x22:0x24])
|
|
|
|
payload = self.decrypt(resp[0x38:])
|
|
|
|
return payload
|
|
|
|
|
|
|
|
def get_position(self) -> int:
|
|
|
|
"""Return the position of the curtain."""
|
|
|
|
resp = self._send(1, [])
|
|
|
|
position = resp[0x0E]
|
|
|
|
return position
|
|
|
|
|
|
|
|
def open(self) -> int:
|
|
|
|
"""Open the curtain."""
|
2024-04-12 07:10:06 +02:00
|
|
|
resp = self._send(2, [0x4A, 0x31, 0xA0])
|
2024-04-10 21:35:25 +02:00
|
|
|
position = resp[0x0E]
|
|
|
|
return position
|
|
|
|
|
|
|
|
def close(self) -> int:
|
|
|
|
"""Close the curtain."""
|
2024-04-12 07:10:06 +02:00
|
|
|
resp = self._send(2, [0x61, 0x32, 0xA0])
|
2024-04-10 21:35:25 +02:00
|
|
|
position = resp[0x0E]
|
|
|
|
return position
|
|
|
|
|
|
|
|
def stop(self) -> int:
|
|
|
|
"""Stop the curtain."""
|
2024-04-12 07:10:06 +02:00
|
|
|
resp = self._send(2, [0x4C, 0x73, 0xA0])
|
2024-04-10 21:35:25 +02:00
|
|
|
position = resp[0x0E]
|
|
|
|
return position
|
|
|
|
|
|
|
|
def set_position(self, position: int) -> int:
|
|
|
|
"""Set the position of the curtain."""
|
2024-04-12 07:10:06 +02:00
|
|
|
resp = self._send(2, [position, 0x70, 0xA0])
|
2024-04-10 21:35:25 +02:00
|
|
|
position = resp[0x0E]
|
|
|
|
return position
|