diff options
author | Nightreaver <Nightreaver.b@googlemail.com> | 2018-03-18T22·03+0900 |
---|---|---|
committer | Matthew Garrett <mjg59-github@srcf.ucam.org> | 2018-03-18T22·03-0700 |
commit | 33a2e4ae54e996d37c814494f5da629fb5428f96 (patch) | |
tree | dfcb2fdac18cc31761e7d4d8afb059efc59ec64c /broadlink | |
parent | 39cc64efcea723cdc5e97220bd345535f558bb8f (diff) |
implemented method to toggle nightlight on some SP3 devices (#159)
* implemented method to toggle nightlight on some SP3 devices * implement nightlight feature to cli * check_power/check_nightligh fixes for py2.7
Diffstat (limited to 'broadlink')
-rw-r--r-- | broadlink/__init__.py | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/broadlink/__init__.py b/broadlink/__init__.py index 2af62aabd0cf..910184a025dd 100644 --- a/broadlink/__init__.py +++ b/broadlink/__init__.py @@ -391,7 +391,20 @@ class sp2(device): """Sets the power state of the smart plug.""" packet = bytearray(16) packet[0] = 2 - packet[4] = 1 if state else 0 + if self.check_nightlight(): + packet[4] = 3 if state else 2 + else: + packet[4] = 1 if state else 0 + self.send_packet(0x6a, packet) + + def set_nightlight(self, state): + """Sets the night light state of the smart plug""" + packet = bytearray(16) + packet[0] = 2 + if self.check_power(): + packet[4] = 3 if state else 1 + else: + packet[4] = 2 if state else 0 self.send_packet(0x6a, packet) def check_power(self): @@ -402,10 +415,24 @@ class sp2(device): err = response[0x22] | (response[0x23] << 8) if err == 0: payload = self.decrypt(bytes(response[0x38:])) - if type(payload[0x4]) == int: - state = bool(payload[0x4]) + if ord(payload[0x4]) == 1 or ord(payload[0x4]) == 3: + state = True + else: + state = False + return state + + def check_nightlight(self): + """Returns the power state of the smart plug.""" + packet = bytearray(16) + packet[0] = 1 + response = self.send_packet(0x6a, packet) + err = response[0x22] | (response[0x23] << 8) + if err == 0: + payload = self.decrypt(bytes(response[0x38:])) + if ord(payload[0x4]) == 2 or ord(payload[0x4]) == 3: + state = True else: - state = bool(ord(payload[0x4])) + state = False return state def get_energy(self): |