145 lines
4.5 KiB
Python
Executable File
145 lines
4.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import json
|
|
import getpass
|
|
import readline
|
|
from cmd import Cmd
|
|
from PyP100 import PyP100
|
|
|
|
|
|
class LampPrompt(Cmd):
|
|
def __init__(self, *args):
|
|
self.lamp=None
|
|
self.valid_args_get = {"brightness": self.get_brightness,
|
|
"hue": self.get_hue,
|
|
"state": self.get_state,
|
|
"saturation": self.get_saturation,
|
|
"temperature": self.get_temp}
|
|
self.valid_args_set = {"brightness": self.set_brightness,
|
|
"state": self.set_state}
|
|
super().__init__(*args)
|
|
self.intro = "Hey ! Let's control this lamp"
|
|
self.prompt = "> "
|
|
|
|
def set_lamp(self, lamp):
|
|
self.lamp = lamp
|
|
|
|
def set_state(self, arg):
|
|
valid_args={"on": self.lamp.turnOn,
|
|
"off": self.lamp.turnOff}
|
|
try:
|
|
valid_args[arg]()
|
|
except KeyError:
|
|
print("State must be either 'on' or 'off'")
|
|
|
|
def set_brightness(self, arg):
|
|
"""Usage: brightness value
|
|
Set the lamp's brightness.
|
|
Acceptable values for "value" are integer between 1 and 100.
|
|
"""
|
|
try:
|
|
arg = int(arg)
|
|
except ValueError:
|
|
print(f"Invalid value for brightness: '{arg}'")
|
|
return False
|
|
try:
|
|
assert arg <= 100
|
|
assert arg > 0
|
|
except AssertionError:
|
|
print(f"'{arg}' is not in the [1,100] range.")
|
|
return False
|
|
self.lamp.setBrightness(arg)
|
|
|
|
def do_set(self, arg):
|
|
args = arg.split()
|
|
try:
|
|
return self.valid_args_set[args[0]](args[1])
|
|
except KeyError as excpt:
|
|
print(f"Invalid argument for set: {excpt}")
|
|
print(f"It must be one of {list(valid_args.keys())}")
|
|
except IndexError:
|
|
print(f"Wrong number of arguments. Expected 2, got {len(args)}")
|
|
|
|
def complete_set(self, text, line, begidx, endidx):
|
|
splitted_line = line.split()
|
|
if len(splitted_line) < 2 or (len(splitted_line) == 2 and not line.endswith(" ")):
|
|
arg_list = self.valid_args_set.keys()
|
|
elif splitted_line[1] == "state" and (len(splitted_line) < 3 or (len(splitted_line) == 3 and not line.endswith(" "))):
|
|
arg_list = ["on", "off"]
|
|
else:
|
|
arg_list = []
|
|
return [arg for arg in arg_list if arg.startswith(text)]
|
|
|
|
def do_get(self, arg):
|
|
"""Usage: get information
|
|
Print information about the lamp.
|
|
Valid informations are:
|
|
- brightness
|
|
- state
|
|
- color
|
|
- temperature
|
|
"""
|
|
#args = arg.split()
|
|
try:
|
|
print(self.valid_args_get[arg]())
|
|
except KeyError as excpt:
|
|
print(f"Invalid argument for get: '{arg}'")
|
|
print(f"It must be one of {list(valid_args.keys())}")
|
|
|
|
def complete_get(self, text, line, begidx, endidx):
|
|
splitted_line = line.split()
|
|
return [arg for arg in self.valid_args_get.keys() if arg.startswith(text) and (len(splitted_line) < 2 or (len(splitted_line) == 2 and not line.endswith(" ")))]
|
|
|
|
def do_exit(self, arg):
|
|
""" Exit the program
|
|
"""
|
|
return True
|
|
|
|
def emptyline(self):
|
|
return False
|
|
|
|
def default(self, line):
|
|
if line == "EOF":
|
|
return True
|
|
return super().default(line)
|
|
|
|
def get_brightness(self):
|
|
infos = json.loads(self.lamp.getDeviceInfo())
|
|
return infos["result"]["brightness"]
|
|
|
|
def get_hue(self):
|
|
infos = json.loads(self.lamp.getDeviceInfo())
|
|
return infos["result"]["hue"]
|
|
|
|
def get_temp(self):
|
|
infos = json.loads(self.lamp.getDeviceInfo())
|
|
return infos["result"]["color_temp"]
|
|
|
|
def get_saturation(self):
|
|
infos = json.loads(self.lamp.getDeviceInfo())
|
|
return infos["result"]["saturation"]
|
|
|
|
def get_state(self):
|
|
infos = json.loads(self.lamp.getDeviceInfo())
|
|
return "on" if infos["result"]["device_on"] else "off"
|
|
|
|
def init_lamp(lamp_address, username, _password):
|
|
lamp = PyP100.P100(lamp_address, username, _password)
|
|
lamp.handshake()
|
|
lamp.login()
|
|
return lamp
|
|
|
|
def main():
|
|
lamp_address = "192.168.3.15"
|
|
username = "saxo.poubelle@saxodwarf.fr"
|
|
_password = getpass.getpass()
|
|
lamp = init_lamp(lamp_address, username, _password)
|
|
print(f"Connected to {lamp.getDeviceName()}")
|
|
prompt = LampPrompt()
|
|
prompt.set_lamp(lamp)
|
|
prompt.cmdloop()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|