diff --git a/broadlink/__init__.py b/broadlink/__init__.py index 2af62aa..910184a 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 = bool(ord(payload[0x4])) + 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 = False return state def get_energy(self): diff --git a/cli/broadlink_cli b/cli/broadlink_cli index 8d055f7..4c0c5d7 100755 --- a/cli/broadlink_cli +++ b/cli/broadlink_cli @@ -69,8 +69,11 @@ parser.add_argument("--host", help="host address") parser.add_argument("--mac", help="mac address (hex reverse), as used by python-broadlink library") parser.add_argument("--temperature",action="store_true", help="request temperature from device") parser.add_argument("--check", action="store_true", help="check current power state") +parser.add_argument("--checknl", action="store_true", help="check current nightlight state") parser.add_argument("--turnon", action="store_true", help="turn on device") parser.add_argument("--turnoff", action="store_true", help="turn off device") +parser.add_argument("--turnnlon", action="store_true", help="turn on nightlight on the device") +parser.add_argument("--turnnloff", action="store_true", help="turn off nightlight on the device") parser.add_argument("--switch", action="store_true", help="switch state from on to off and off to on") parser.add_argument("--send", action="store_true", help="send command") parser.add_argument("--sensors", action="store_true", help="check all sensors") @@ -139,6 +142,11 @@ if args.check: print '* ON *' else: print '* OFF *' +if args.checknl: + if dev.check_nightlight(): + print '* ON *' + else: + print '* OFF *' if args.turnon: dev.set_power(True) if dev.check_power(): @@ -151,6 +159,18 @@ if args.turnoff: print '!! Still ON !!' else: print '== Turned * OFF * ==' +if args.turnnlon: + dev.set_nightlight(True) + if dev.check_nightlight(): + print '== Turned * ON * ==' + else: + print '!! Still OFF !!' +if args.turnnloff: + dev.set_nightlight(False) + if dev.check_nightlight(): + print '!! Still ON !!' + else: + print '== Turned * OFF * ==' if args.switch: if dev.check_power(): dev.set_power(False)