Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
felipediel committed Apr 12, 2024
1 parent ee905b9 commit b46f89b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
10 changes: 6 additions & 4 deletions broadlink/cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
42 changes: 18 additions & 24 deletions broadlink/sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"""Support for sensors."""
import struct

from . import exceptions as e
from .device import Device

Expand Down Expand Up @@ -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("<bb", data[:0x2])
temperature = temperature[0x0] + temperature[0x1] / 10.0
humidity = data[0x2] + data[0x3] / 10.0
resp = self.send_packet(0x6A, packet)
e.check_error(resp[0x22:0x24])
data = self.decrypt(resp[0x38:])

return {
"temperature": temperature,
"humidity": humidity,
"light": data[0x4],
"air_quality": data[0x6],
"noise": data[0x8],
"temperature": data[0x04] + data[0x05] / 10.0,
"humidity": data[0x06] + data[0x07] / 10.0,
"light": data[0x08],
"air_quality": data[0x0A],
"noise": data[0x0C],
}


Expand All @@ -54,7 +47,7 @@ class a2(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
Expand All @@ -66,7 +59,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
Expand All @@ -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],
}

0 comments on commit b46f89b

Please sign in to comment.