Skip to content

Commit 7ac243b

Browse files
author
Matthew Garrett
authored
Merge pull request mjg59#16 from PeWu/smartplug
Updated SmartPlug commands - set_power() and check_power()
2 parents c68fcea + 1e04ec2 commit 7ac243b

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ Obtain sensor data from an A1:
4444
data = devices[0].check_sensors()
4545
```
4646

47-
Set power state on an SP2/SP3 (0 for off, 1 for on):
47+
Set power state on a SmartPlug SP2/SP3:
48+
```
49+
devices[0].set_power(True)
50+
```
51+
52+
Check power state on a SmartPlug:
53+
```
54+
state = devices[0].check_power()
4855
```
49-
devices[0].set_power(1)
50-
```

broadlink/__init__.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,22 @@ def __init__ (self, host, mac):
241241
device.__init__(self, host, mac)
242242

243243
def set_power(self, state):
244-
packet = bytearray(8)
244+
"""Sets the power state of the smart plug."""
245+
packet = bytearray(16)
245246
packet[0] = 2
246-
packet[4] = state
247+
packet[4] = 1 if state else 0
247248
self.send_packet(0x6a, packet)
248249

250+
def check_power(self):
251+
"""Returns the power state of the smart plug."""
252+
packet = bytearray(16)
253+
packet[0] = 1
254+
response = self.send_packet(0x6a, packet)
255+
err = ord(response[0x22]) | (ord(response[0x23]) << 8)
256+
if err == 0:
257+
aes = AES.new(str(self.key), AES.MODE_CBC, str(self.iv))
258+
payload = aes.decrypt(str(response[0x38:]))
259+
return bool(ord(payload[0x4]))
249260

250261
class a1(device):
251262
def __init__ (self, host, mac):

0 commit comments

Comments
 (0)