diff options
author | Matthew Garrett <mjg59@coreos.com> | 2016-11-20T00·47-0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-11-20T00·47-0800 |
commit | 7ac243b838376da8735820fa5debe085a8c6f34d (patch) | |
tree | 5d290f782a4aa97dd083f91701eb234733ac14c4 /broadlink/__init__.py | |
parent | c68fcea5e6bd561a21a90d40318b3d8b955d4ff8 (diff) | |
parent | 1e04ec2be57fa61a8d205df8f8cbfa18c8fdd40a (diff) |
Merge pull request #16 from PeWu/smartplug
Updated SmartPlug commands - set_power() and check_power()
Diffstat (limited to 'broadlink/__init__.py')
-rwxr-xr-x | broadlink/__init__.py | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/broadlink/__init__.py b/broadlink/__init__.py index 5d5dd4c3fbf2..942b017ffcf6 100755 --- a/broadlink/__init__.py +++ b/broadlink/__init__.py @@ -241,11 +241,22 @@ class sp2(device): device.__init__(self, host, mac) def set_power(self, state): - packet = bytearray(8) + """Sets the power state of the smart plug.""" + packet = bytearray(16) packet[0] = 2 - packet[4] = state + packet[4] = 1 if state else 0 self.send_packet(0x6a, packet) + def check_power(self): + """Returns the power state of the smart plug.""" + packet = bytearray(16) + packet[0] = 1 + response = self.send_packet(0x6a, packet) + err = ord(response[0x22]) | (ord(response[0x23]) << 8) + if err == 0: + aes = AES.new(str(self.key), AES.MODE_CBC, str(self.iv)) + payload = aes.decrypt(str(response[0x38:])) + return bool(ord(payload[0x4])) class a1(device): def __init__ (self, host, mac): |