From b46f89b1072f4ce091bf8cca2fc21bf1759420ef Mon Sep 17 00:00:00 2001 From: Felipe Martins Diel Date: Fri, 12 Apr 2024 02:02:23 -0300 Subject: [PATCH] Fix bugs --- broadlink/cover.py | 10 ++++++---- broadlink/sensor.py | 42 ++++++++++++++++++------------------------ 2 files changed, 24 insertions(+), 28 deletions(-) diff --git a/broadlink/cover.py b/broadlink/cover.py index ae25c8c7..1d8b41ef 100644 --- a/broadlink/cover.py +++ b/broadlink/cover.py @@ -65,7 +65,7 @@ class dooya2(Device): def _send(self, operation: int, data: bytes = b""): """Send a command to the device.""" - packet = bytearray(14) + packet = bytearray(12) packet[0x02] = 0xA5 packet[0x03] = 0xA5 packet[0x04] = 0x5A @@ -77,7 +77,8 @@ def _send(self, operation: int, data: bytes = b""): data_len = len(data) packet[0x0A] = data_len & 0xFF packet[0x0B] = data_len >> 8 - packet += bytes(data) + packet += bytes(2) + packet.extend(data) checksum = sum(packet, 0xBEAF) & 0xFFFF packet[0x06] = checksum & 0xFF @@ -121,7 +122,7 @@ class wser(Device): def _send(self, operation: int, data: bytes = b""): """Send a command to the device.""" - packet = bytearray(14) + packet = bytearray(12) packet[0x02] = 0xA5 packet[0x03] = 0xA5 packet[0x04] = 0x5A @@ -133,7 +134,8 @@ def _send(self, operation: int, data: bytes = b""): data_len = len(data) packet[0x0A] = data_len & 0xFF packet[0x0B] = data_len >> 8 - packet += bytes(data) + packet += bytes(2) + packet.extend(data) checksum = sum(packet, 0xBEAF) & 0xFFFF packet[0x06] = checksum & 0xFF diff --git a/broadlink/sensor.py b/broadlink/sensor.py index c8a888d0..21bae0b9 100644 --- a/broadlink/sensor.py +++ b/broadlink/sensor.py @@ -1,6 +1,4 @@ """Support for sensors.""" -import struct - from . import exceptions as e from .device import Device @@ -29,21 +27,16 @@ def check_sensors(self) -> dict: def check_sensors_raw(self) -> dict: """Return the state of the sensors in raw format.""" packet = bytearray([0x1]) - response = self.send_packet(0x6A, packet) - e.check_error(response[0x22:0x24]) - payload = self.decrypt(response[0x38:]) - data = payload[0x4:] - - temperature = struct.unpack("> 8 - packet += bytes(data) + packet += bytes(2) + packet.extend(data) checksum = sum(packet, 0xBEAF) & 0xFFFF packet[0x06] = checksum & 0xFF @@ -83,12 +77,12 @@ def _send(self, operation: int, data: bytes = b""): def check_sensors_raw(self) -> dict: """Return the state of the sensors in raw format.""" - resp = self._send(1) - data = resp[0x6:] + data = self._send(1) + return { - "temperature": data[0x0D] * 255 + data[0x0E], - "humidity": data[0x0F] * 255 + data[0x10], - "pm100": data[0x07] * 255 + data[0x08], - "pm25": data[0x09] * 255 + data[0x0A], - "pm10": data[0x0B] * 255 + data[0x0C], + "temperature": data[0x13] * 256 + data[0x14], + "humidity": data[0x15] * 256 + data[0x16], + "pm10": data[0x0D] * 256 + data[0x0E], + "pm2_5": data[0x0F] * 256 + data[0x10], + "pm1": data[0x11] * 256 + data[0x12], }