Skip to content

Commit

Permalink
Create SP3S class and fix get_energy()
Browse files Browse the repository at this point in the history
  • Loading branch information
felipediel committed Dec 16, 2020
1 parent 29345a1 commit 6970d7f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
6 changes: 3 additions & 3 deletions broadlink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from .light import lb1
from .remote import rm, rm4
from .sensor import a1
from .switch import bg1, mp1, sp1, sp2, sp4, sp4b
from .switch import bg1, mp1, sp1, sp2, sp3s, sp4, sp4b


SUPPORTED_TYPES = {
Expand All @@ -38,8 +38,8 @@
0x791A: (sp2, "SP2-compatible", "Honeywell"),
0x7D00: (sp2, "SP3-EU", "Broadlink (OEM)"),
0x7D0D: (sp2, "SP mini 3", "Broadlink (OEM)"),
0x9479: (sp2, "SP3S-US", "Broadlink"),
0x947A: (sp2, "SP3S-EU", "Broadlink"),
0x9479: (sp3s, "SP3S-US", "Broadlink"),
0x947A: (sp3s, "SP3S-EU", "Broadlink"),
0x756C: (sp4, "SP4M", "Broadlink"),
0x7579: (sp4, "SP4L-EU", "Broadlink"),
0x7583: (sp4, "SP mini 3", "Broadlink"),
Expand Down
31 changes: 27 additions & 4 deletions broadlink/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import struct

from .device import device
from .exceptions import check_error
from .exceptions import check_error, exception


class mp1(device):
Expand Down Expand Up @@ -212,13 +212,36 @@ def check_nightlight(self) -> bool:
payload = self.decrypt(response[0x38:])
return bool(payload[0x4] == 2 or payload[0x4] == 3 or payload[0x4] == 0xFF)

def get_energy(self) -> int:
"""Return the energy state of the device."""
def get_energy(self) -> float:
"""Return the power consumption in W."""
packet = bytearray(16)
packet[0] = 4
response = self.send_packet(0x6A, packet)
check_error(response[0x22:0x24])
payload = self.decrypt(response[0x38:])
return int.from_bytes(payload[0x4:0x7], "little") / 1000


class sp3s(sp2):
"""Controls a Broadlink SP3S."""

def __init__(self, *args, **kwargs) -> None:
"""Initialize the controller."""
device.__init__(self, *args, **kwargs)
self.type = "SP3S"

def get_energy(self) -> float:
"""Return the power consumption in W."""
packet = bytearray([8, 0, 254, 1, 5, 1, 0, 0, 0, 45])
response = self.send_packet(0x6A, packet)
check_error(response[0x22:0x24])
payload = self.decrypt(response[0x38:])
return int((payload[0x07] + payload[0x06] / 100) * 100) + payload[0x05] / 100
energy = payload[0x7:0x4:-1].hex()

try:
return int(energy) / 100
except ValueError: # Firmware issue
raise exception(-4026, "The device returned malformed data", payload[0x4:])


class sp4(device):
Expand Down

0 comments on commit 6970d7f

Please sign in to comment.