2020-09-24 07:36:12 +02:00
|
|
|
"""Support for switches."""
|
2020-09-17 05:41:32 +02:00
|
|
|
import json
|
|
|
|
import struct
|
|
|
|
|
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 mp1(Device):
|
2020-09-17 05:41:32 +02:00
|
|
|
"""Controls a Broadlink MP1."""
|
|
|
|
|
2021-01-28 23:16:25 +01:00
|
|
|
TYPE = "MP1"
|
2020-09-17 05:41:32 +02:00
|
|
|
|
2021-04-05 19:02:56 +02:00
|
|
|
def set_power_mask(self, sid_mask: int, pwr: bool) -> None:
|
2020-09-17 05:41:32 +02:00
|
|
|
"""Set the power state of the device."""
|
|
|
|
packet = bytearray(16)
|
2020-11-05 20:00:42 +01:00
|
|
|
packet[0x00] = 0x0D
|
|
|
|
packet[0x02] = 0xA5
|
|
|
|
packet[0x03] = 0xA5
|
|
|
|
packet[0x04] = 0x5A
|
|
|
|
packet[0x05] = 0x5A
|
2021-04-05 19:02:56 +02:00
|
|
|
packet[0x06] = 0xB2 + ((sid_mask << 1) if pwr else sid_mask)
|
2020-11-05 20:00:42 +01:00
|
|
|
packet[0x07] = 0xC0
|
2020-09-17 05:41:32 +02:00
|
|
|
packet[0x08] = 0x02
|
2020-11-05 20:00:42 +01:00
|
|
|
packet[0x0A] = 0x03
|
|
|
|
packet[0x0D] = sid_mask
|
2021-04-05 19:02:56 +02:00
|
|
|
packet[0x0E] = sid_mask if pwr else 0
|
2020-09-17 05:41:32 +02:00
|
|
|
|
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-17 05:41:32 +02:00
|
|
|
|
2021-04-05 19:02:56 +02:00
|
|
|
def set_power(self, sid: int, pwr: bool) -> None:
|
2020-09-17 05:41:32 +02:00
|
|
|
"""Set the power state of the device."""
|
|
|
|
sid_mask = 0x01 << (sid - 1)
|
2021-04-05 19:02:56 +02:00
|
|
|
self.set_power_mask(sid_mask, pwr)
|
2020-09-17 05:41:32 +02:00
|
|
|
|
2020-11-27 21:16:52 +01:00
|
|
|
def check_power_raw(self) -> int:
|
2020-09-17 05:41:32 +02:00
|
|
|
"""Return the power state of the device in raw format."""
|
|
|
|
packet = bytearray(16)
|
2020-11-05 20:00:42 +01:00
|
|
|
packet[0x00] = 0x0A
|
|
|
|
packet[0x02] = 0xA5
|
|
|
|
packet[0x03] = 0xA5
|
|
|
|
packet[0x04] = 0x5A
|
|
|
|
packet[0x05] = 0x5A
|
|
|
|
packet[0x06] = 0xAE
|
|
|
|
packet[0x07] = 0xC0
|
2020-09-17 05:41:32 +02:00
|
|
|
packet[0x08] = 0x01
|
|
|
|
|
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:])
|
2020-11-05 20:00:42 +01:00
|
|
|
return payload[0x0E]
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
def check_power(self) -> dict:
|
|
|
|
"""Return the power state of the device."""
|
2021-04-05 19:02:56 +02:00
|
|
|
data = self.check_power_raw()
|
|
|
|
return {
|
|
|
|
"s1": bool(data & 1),
|
|
|
|
"s2": bool(data & 2),
|
|
|
|
"s3": bool(data & 4),
|
|
|
|
"s4": bool(data & 8),
|
|
|
|
}
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
|
2021-04-03 06:01:38 +02:00
|
|
|
class bg1(Device):
|
2020-09-17 05:41:32 +02:00
|
|
|
"""Controls a BG Electrical smart outlet."""
|
|
|
|
|
2021-01-28 23:16:25 +01:00
|
|
|
TYPE = "BG1"
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
def get_state(self) -> dict:
|
|
|
|
"""Return the power state of the device.
|
|
|
|
|
|
|
|
Example: `{"pwr":1,"pwr1":1,"pwr2":0,"maxworktime":60,"maxworktime1":60,"maxworktime2":0,"idcbrightness":50}`
|
|
|
|
"""
|
2021-04-05 19:02:56 +02:00
|
|
|
packet = self._encode(1, {})
|
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-17 05:41:32 +02:00
|
|
|
return self._decode(response)
|
|
|
|
|
|
|
|
def set_state(
|
|
|
|
self,
|
|
|
|
pwr: bool = None,
|
|
|
|
pwr1: bool = None,
|
|
|
|
pwr2: bool = None,
|
|
|
|
maxworktime: int = None,
|
|
|
|
maxworktime1: int = None,
|
|
|
|
maxworktime2: int = None,
|
|
|
|
idcbrightness: int = None,
|
|
|
|
) -> dict:
|
|
|
|
"""Set the power state of the device."""
|
2021-04-05 19:02:56 +02:00
|
|
|
state = {}
|
2020-09-17 05:41:32 +02:00
|
|
|
if pwr is not None:
|
2021-04-05 19:02:56 +02:00
|
|
|
state["pwr"] = int(bool(pwr))
|
2020-09-17 05:41:32 +02:00
|
|
|
if pwr1 is not None:
|
2021-04-05 19:02:56 +02:00
|
|
|
state["pwr1"] = int(bool(pwr1))
|
2020-09-17 05:41:32 +02:00
|
|
|
if pwr2 is not None:
|
2021-04-05 19:02:56 +02:00
|
|
|
state["pwr2"] = int(bool(pwr2))
|
2020-09-17 05:41:32 +02:00
|
|
|
if maxworktime is not None:
|
2021-04-05 19:02:56 +02:00
|
|
|
state["maxworktime"] = maxworktime
|
2020-09-17 05:41:32 +02:00
|
|
|
if maxworktime1 is not None:
|
2021-04-05 19:02:56 +02:00
|
|
|
state["maxworktime1"] = maxworktime1
|
2020-09-17 05:41:32 +02:00
|
|
|
if maxworktime2 is not None:
|
2021-04-05 19:02:56 +02:00
|
|
|
state["maxworktime2"] = maxworktime2
|
2020-09-17 05:41:32 +02:00
|
|
|
if idcbrightness is not None:
|
2021-04-05 19:02:56 +02:00
|
|
|
state["idcbrightness"] = idcbrightness
|
|
|
|
|
|
|
|
packet = self._encode(2, state)
|
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-17 05:41:32 +02:00
|
|
|
return self._decode(response)
|
|
|
|
|
2021-04-05 19:02:56 +02:00
|
|
|
def _encode(self, flag: int, state: dict) -> bytes:
|
2020-09-17 05:41:32 +02:00
|
|
|
"""Encode a message."""
|
|
|
|
packet = bytearray(14)
|
2021-04-05 19:02:56 +02:00
|
|
|
data = json.dumps(state).encode()
|
|
|
|
length = 12 + len(data)
|
2020-11-05 20:00:42 +01:00
|
|
|
struct.pack_into(
|
2021-04-05 19:02:56 +02:00
|
|
|
"<HHHHBBI", packet, 0, length, 0xA5A5, 0x5A5A, 0x0000, flag, 0x0B, len(data)
|
2020-11-05 20:00:42 +01:00
|
|
|
)
|
2021-04-05 19:02:56 +02:00
|
|
|
packet.extend(data)
|
|
|
|
checksum = sum(packet[0x2:], 0xBEAF) & 0xFFFF
|
|
|
|
packet[0x06:0x08] = checksum.to_bytes(2, "little")
|
2020-09-17 05:41:32 +02:00
|
|
|
return packet
|
|
|
|
|
|
|
|
def _decode(self, response: bytes) -> dict:
|
|
|
|
"""Decode a message."""
|
2020-09-20 11:16:49 +02:00
|
|
|
payload = self.decrypt(response[0x38:])
|
2020-11-05 20:00:42 +01:00
|
|
|
js_len = struct.unpack_from("<I", payload, 0x0A)[0]
|
|
|
|
state = json.loads(payload[0x0E : 0x0E + js_len])
|
2020-09-17 05:41:32 +02:00
|
|
|
return state
|
|
|
|
|
|
|
|
|
2021-04-03 06:01:38 +02:00
|
|
|
class sp1(Device):
|
2020-09-17 05:41:32 +02:00
|
|
|
"""Controls a Broadlink SP1."""
|
|
|
|
|
2021-01-28 23:16:25 +01:00
|
|
|
TYPE = "SP1"
|
2020-09-17 05:41:32 +02:00
|
|
|
|
2021-04-05 19:02:56 +02:00
|
|
|
def set_power(self, pwr: bool) -> None:
|
2020-09-17 05:41:32 +02:00
|
|
|
"""Set the power state of the device."""
|
|
|
|
packet = bytearray(4)
|
2021-04-05 19:02:56 +02:00
|
|
|
packet[0] = bool(pwr)
|
2020-09-17 05:41:32 +02:00
|
|
|
response = self.send_packet(0x66, packet)
|
2021-03-31 19:27:05 +02:00
|
|
|
e.check_error(response[0x22:0x24])
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
|
2021-04-03 06:01:38 +02:00
|
|
|
class sp2(Device):
|
2020-09-17 05:41:32 +02:00
|
|
|
"""Controls a Broadlink SP2."""
|
|
|
|
|
2021-01-28 23:16:25 +01:00
|
|
|
TYPE = "SP2"
|
2020-09-17 05:41:32 +02:00
|
|
|
|
2021-04-05 19:02:56 +02:00
|
|
|
def set_power(self, pwr: bool) -> None:
|
2021-02-16 20:38:10 +01:00
|
|
|
"""Set the power state of the device."""
|
|
|
|
packet = bytearray(16)
|
|
|
|
packet[0] = 2
|
2021-04-05 19:02:56 +02:00
|
|
|
packet[4] = bool(pwr)
|
2021-02-16 20:38:10 +01:00
|
|
|
response = self.send_packet(0x6A, packet)
|
2021-03-31 19:27:05 +02:00
|
|
|
e.check_error(response[0x22:0x24])
|
2021-02-16 20:38:10 +01:00
|
|
|
|
|
|
|
def check_power(self) -> bool:
|
|
|
|
"""Return the power state of the device."""
|
|
|
|
packet = bytearray(16)
|
|
|
|
packet[0] = 1
|
|
|
|
response = self.send_packet(0x6A, packet)
|
2021-03-31 19:27:05 +02:00
|
|
|
e.check_error(response[0x22:0x24])
|
2021-02-16 20:38:10 +01:00
|
|
|
payload = self.decrypt(response[0x38:])
|
|
|
|
return bool(payload[0x4])
|
|
|
|
|
|
|
|
|
|
|
|
class sp2s(sp2):
|
|
|
|
"""Controls a Broadlink SP2S."""
|
|
|
|
|
|
|
|
TYPE = "SP2S"
|
|
|
|
|
|
|
|
def get_energy(self) -> float:
|
|
|
|
"""Return the power consumption in W."""
|
|
|
|
packet = bytearray(16)
|
|
|
|
packet[0] = 4
|
|
|
|
response = self.send_packet(0x6A, packet)
|
2021-03-31 19:27:05 +02:00
|
|
|
e.check_error(response[0x22:0x24])
|
2021-02-16 20:38:10 +01:00
|
|
|
payload = self.decrypt(response[0x38:])
|
|
|
|
return int.from_bytes(payload[0x4:0x7], "little") / 1000
|
|
|
|
|
|
|
|
|
2021-04-03 06:01:38 +02:00
|
|
|
class sp3(Device):
|
2021-02-16 20:38:10 +01:00
|
|
|
"""Controls a Broadlink SP3."""
|
|
|
|
|
|
|
|
TYPE = "SP3"
|
|
|
|
|
2021-04-05 19:02:56 +02:00
|
|
|
def set_power(self, pwr: bool) -> None:
|
2020-09-17 05:41:32 +02:00
|
|
|
"""Set the power state of the device."""
|
|
|
|
packet = bytearray(16)
|
|
|
|
packet[0] = 2
|
2021-04-05 19:02:56 +02:00
|
|
|
packet[4] = self.check_nightlight() << 1 | bool(pwr)
|
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-17 05:41:32 +02:00
|
|
|
|
2021-04-05 19:02:56 +02:00
|
|
|
def set_nightlight(self, ntlight: bool) -> None:
|
2020-09-17 05:41:32 +02:00
|
|
|
"""Set the night light state of the device."""
|
|
|
|
packet = bytearray(16)
|
|
|
|
packet[0] = 2
|
2021-04-05 19:02:56 +02:00
|
|
|
packet[4] = bool(ntlight) << 1 | self.check_power()
|
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-17 05:41:32 +02:00
|
|
|
|
|
|
|
def check_power(self) -> bool:
|
|
|
|
"""Return the power state of the device."""
|
|
|
|
packet = bytearray(16)
|
|
|
|
packet[0] = 1
|
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:])
|
2021-04-05 19:02:56 +02:00
|
|
|
return bool(payload[0x4] & 1)
|
2020-09-17 05:41:32 +02:00
|
|
|
|
|
|
|
def check_nightlight(self) -> bool:
|
|
|
|
"""Return the state of the night light."""
|
|
|
|
packet = bytearray(16)
|
|
|
|
packet[0] = 1
|
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:])
|
2021-04-05 19:02:56 +02:00
|
|
|
return bool(payload[0x4] & 2)
|
2020-09-17 05:41:32 +02:00
|
|
|
|
2021-02-16 20:38:10 +01:00
|
|
|
|
|
|
|
class sp3s(sp2):
|
|
|
|
"""Controls a Broadlink SP3S."""
|
|
|
|
|
|
|
|
TYPE = "SP3S"
|
|
|
|
|
2020-11-07 09:02:53 +01:00
|
|
|
def get_energy(self) -> float:
|
|
|
|
"""Return the power consumption in W."""
|
2020-09-17 05:41:32 +02:00
|
|
|
packet = bytearray([8, 0, 254, 1, 5, 1, 0, 0, 0, 45])
|
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:])
|
2020-11-07 09:02:53 +01:00
|
|
|
energy = payload[0x7:0x4:-1].hex()
|
|
|
|
return int(energy) / 100
|
2020-09-23 02:58:07 +02:00
|
|
|
|
|
|
|
|
2021-04-03 06:01:38 +02:00
|
|
|
class sp4(Device):
|
2020-11-05 20:00:42 +01:00
|
|
|
"""Controls a Broadlink SP4."""
|
2020-09-23 02:58:07 +02:00
|
|
|
|
2021-01-28 23:16:25 +01:00
|
|
|
TYPE = "SP4"
|
2020-09-23 02:58:07 +02:00
|
|
|
|
2021-04-05 19:02:56 +02:00
|
|
|
def set_power(self, pwr: bool) -> None:
|
2020-09-23 02:58:07 +02:00
|
|
|
"""Set the power state of the device."""
|
2021-04-05 19:02:56 +02:00
|
|
|
self.set_state(pwr=pwr)
|
2020-09-23 02:58:07 +02:00
|
|
|
|
2021-04-05 19:02:56 +02:00
|
|
|
def set_nightlight(self, ntlight: bool) -> None:
|
2020-09-23 02:58:07 +02:00
|
|
|
"""Set the night light state of the device."""
|
2021-04-05 19:02:56 +02:00
|
|
|
self.set_state(ntlight=ntlight)
|
2020-09-23 02:58:07 +02:00
|
|
|
|
|
|
|
def set_state(
|
|
|
|
self,
|
|
|
|
pwr: bool = None,
|
|
|
|
ntlight: bool = None,
|
|
|
|
indicator: bool = None,
|
|
|
|
ntlbrightness: int = None,
|
|
|
|
maxworktime: int = None,
|
2020-10-18 22:22:03 +02:00
|
|
|
childlock: bool = None,
|
2020-09-23 02:58:07 +02:00
|
|
|
) -> dict:
|
|
|
|
"""Set state of device."""
|
2021-04-05 19:02:56 +02:00
|
|
|
state = {}
|
2020-09-23 02:58:07 +02:00
|
|
|
if pwr is not None:
|
2021-04-05 19:02:56 +02:00
|
|
|
state["pwr"] = int(bool(pwr))
|
2020-09-23 02:58:07 +02:00
|
|
|
if ntlight is not None:
|
2021-04-05 19:02:56 +02:00
|
|
|
state["ntlight"] = int(bool(ntlight))
|
2020-09-23 02:58:07 +02:00
|
|
|
if indicator is not None:
|
2021-04-05 19:02:56 +02:00
|
|
|
state["indicator"] = int(bool(indicator))
|
2020-09-23 02:58:07 +02:00
|
|
|
if ntlbrightness is not None:
|
2021-04-05 19:02:56 +02:00
|
|
|
state["ntlbrightness"] = ntlbrightness
|
2020-09-23 02:58:07 +02:00
|
|
|
if maxworktime is not None:
|
2021-04-05 19:02:56 +02:00
|
|
|
state["maxworktime"] = maxworktime
|
2020-10-18 22:22:03 +02:00
|
|
|
if childlock is not None:
|
2021-04-05 19:02:56 +02:00
|
|
|
state["childlock"] = int(bool(childlock))
|
2020-09-23 02:58:07 +02:00
|
|
|
|
2021-04-05 19:02:56 +02:00
|
|
|
packet = self._encode(2, state)
|
2020-09-23 02:58:07 +02:00
|
|
|
response = self.send_packet(0x6A, packet)
|
|
|
|
return self._decode(response)
|
|
|
|
|
|
|
|
def check_power(self) -> bool:
|
|
|
|
"""Return the power state of the device."""
|
|
|
|
state = self.get_state()
|
2021-03-24 15:46:39 +01:00
|
|
|
return bool(state["pwr"])
|
2020-09-23 02:58:07 +02:00
|
|
|
|
|
|
|
def check_nightlight(self) -> bool:
|
2020-10-18 22:22:03 +02:00
|
|
|
"""Return the state of the night light."""
|
2020-09-23 02:58:07 +02:00
|
|
|
state = self.get_state()
|
2021-03-24 15:46:39 +01:00
|
|
|
return bool(state["ntlight"])
|
2020-09-23 02:58:07 +02:00
|
|
|
|
|
|
|
def get_state(self) -> dict:
|
|
|
|
"""Get full state of device."""
|
2020-10-18 22:22:03 +02:00
|
|
|
packet = self._encode(1, {})
|
2020-09-23 02:58:07 +02:00
|
|
|
response = self.send_packet(0x6A, packet)
|
|
|
|
return self._decode(response)
|
|
|
|
|
2020-10-18 22:22:03 +02:00
|
|
|
def _encode(self, flag: int, state: dict) -> bytes:
|
2020-09-23 02:58:07 +02:00
|
|
|
"""Encode a message."""
|
2020-10-18 22:22:03 +02:00
|
|
|
packet = bytearray(12)
|
2021-04-05 19:02:56 +02:00
|
|
|
data = json.dumps(state, separators=(",", ":")).encode()
|
2020-09-23 02:58:07 +02:00
|
|
|
struct.pack_into(
|
2021-04-05 19:02:56 +02:00
|
|
|
"<HHHBBI", packet, 0, 0xA5A5, 0x5A5A, 0x0000, flag, 0x0B, len(data)
|
2020-09-23 02:58:07 +02:00
|
|
|
)
|
2021-04-05 19:02:56 +02:00
|
|
|
packet.extend(data)
|
2020-10-18 22:22:03 +02:00
|
|
|
checksum = sum(packet, 0xBEAF) & 0xFFFF
|
2021-04-05 19:02:56 +02:00
|
|
|
packet[0x04:0x06] = checksum.to_bytes(2, "little")
|
2020-09-23 02:58:07 +02:00
|
|
|
return packet
|
|
|
|
|
|
|
|
def _decode(self, response: bytes) -> dict:
|
|
|
|
"""Decode a message."""
|
2021-03-31 19:27:05 +02:00
|
|
|
e.check_error(response[0x22:0x24])
|
2020-09-23 02:58:07 +02:00
|
|
|
payload = self.decrypt(response[0x38:])
|
|
|
|
js_len = struct.unpack_from("<I", payload, 0x08)[0]
|
|
|
|
state = json.loads(payload[0x0C : 0x0C + js_len])
|
|
|
|
return state
|
2020-10-18 22:22:03 +02:00
|
|
|
|
|
|
|
|
2020-11-04 03:15:34 +01:00
|
|
|
class sp4b(sp4):
|
|
|
|
"""Controls a Broadlink SP4 (type B)."""
|
2020-10-18 22:22:03 +02:00
|
|
|
|
2021-01-28 23:16:25 +01:00
|
|
|
TYPE = "SP4B"
|
2020-10-18 22:22:03 +02:00
|
|
|
|
2021-01-11 06:25:50 +01:00
|
|
|
def get_state(self) -> dict:
|
|
|
|
"""Get full state of device."""
|
|
|
|
state = super().get_state()
|
|
|
|
|
|
|
|
# Convert sensor data to float. Remove keys if sensors are not supported.
|
|
|
|
sensor_attrs = ["current", "volt", "power", "totalconsum", "overload"]
|
|
|
|
for attr in sensor_attrs:
|
|
|
|
value = state.pop(attr, -1)
|
|
|
|
if value != -1:
|
|
|
|
state[attr] = value / 1000
|
|
|
|
return state
|
|
|
|
|
2020-10-18 22:22:03 +02:00
|
|
|
def _encode(self, flag: int, state: dict) -> bytes:
|
|
|
|
"""Encode a message."""
|
|
|
|
packet = bytearray(14)
|
2021-04-05 19:02:56 +02:00
|
|
|
data = json.dumps(state, separators=(",", ":")).encode()
|
|
|
|
length = 12 + len(data)
|
2020-11-05 20:00:42 +01:00
|
|
|
struct.pack_into(
|
|
|
|
"<HHHHBBI",
|
|
|
|
packet,
|
|
|
|
0,
|
|
|
|
length,
|
|
|
|
0xA5A5,
|
|
|
|
0x5A5A,
|
|
|
|
0x0000,
|
|
|
|
flag,
|
|
|
|
0x0B,
|
2021-04-05 19:02:56 +02:00
|
|
|
len(data),
|
2020-11-05 20:00:42 +01:00
|
|
|
)
|
2021-04-05 19:02:56 +02:00
|
|
|
packet.extend(data)
|
|
|
|
checksum = sum(packet[0x02:], 0xBEAF) & 0xFFFF
|
|
|
|
packet[0x06:0x08] = checksum.to_bytes(2, "little")
|
2020-10-18 22:22:03 +02:00
|
|
|
return packet
|
|
|
|
|
|
|
|
def _decode(self, response: bytes) -> dict:
|
|
|
|
"""Decode a message."""
|
2021-03-31 19:27:05 +02:00
|
|
|
e.check_error(response[0x22:0x24])
|
2020-10-18 22:22:03 +02:00
|
|
|
payload = self.decrypt(response[0x38:])
|
|
|
|
js_len = struct.unpack_from("<I", payload, 0xA)[0]
|
|
|
|
state = json.loads(payload[0x0E : 0x0E + js_len])
|
|
|
|
return state
|