diff --git a/Pipfile b/Pipfile new file mode 100644 index 0000000..cd4a732 --- /dev/null +++ b/Pipfile @@ -0,0 +1,12 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] + +[packages] +pyp100 = "*" + +[requires] +python_version = "3.7" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 0000000..b5fbfef --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,109 @@ +{ + "_meta": { + "hash": { + "sha256": "f9f00582cdf63138dcbc5d939bf1987085f8a52ae8a7e4ec643320a7792e9edf" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.7" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "certifi": { + "hashes": [ + "sha256:1a4995114262bffbc2413b159f2a1a480c969de6e6eb13ee966d470af86af59c", + "sha256:719a74fb9e33b9bd44cc7f3a8d94bc35e4049deebe19ba7d8e108280cfd59830" + ], + "version": "==2020.12.5" + }, + "chardet": { + "hashes": [ + "sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae", + "sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691" + ], + "version": "==3.0.4" + }, + "idna": { + "hashes": [ + "sha256:b307872f855b18632ce0c21c5e45be78c0ea7ae4c15c828c20788b26921eb3f6", + "sha256:b97d804b1e9b523befed77c48dacec60e6dcb0b5391d57af6a65a312a90648c0" + ], + "version": "==2.10" + }, + "pkcs7": { + "hashes": [ + "sha256:b683994a7d6404cf82840639605b7208884aa2367f84cb0ae731e32d4a1c5eff" + ], + "version": "==0.1.2" + }, + "pycryptodome": { + "hashes": [ + "sha256:02e51e1d5828d58f154896ddfd003e2e7584869c275e5acbe290443575370fba", + "sha256:03d5cca8618620f45fd40f827423f82b86b3a202c8d44108601b0f5f56b04299", + "sha256:0e24171cf01021bc5dc17d6a9d4f33a048f09d62cc3f62541e95ef104588bda4", + "sha256:132a56abba24e2e06a479d8e5db7a48271a73a215f605017bbd476d31f8e71c1", + "sha256:1e655746f539421d923fd48df8f6f40b3443d80b75532501c0085b64afed9df5", + "sha256:2b998dc45ef5f4e5cf5248a6edfcd8d8e9fb5e35df8e4259b13a1b10eda7b16b", + "sha256:360955eece2cd0fa694a708d10303c6abd7b39614fa2547b6bd245da76198beb", + "sha256:39ef9fb52d6ec7728fce1f1693cb99d60ce302aeebd59bcedea70ca3203fda60", + "sha256:4350a42028240c344ee855f032c7d4ad6ff4f813bfbe7121547b7dc579ecc876", + "sha256:50348edd283afdccddc0938cdc674484533912ba8a99a27c7bfebb75030aa856", + "sha256:54bdedd28476dea8a3cd86cb67c0df1f0e3d71cae8022354b0f879c41a3d27b2", + "sha256:55eb61aca2c883db770999f50d091ff7c14016f2769ad7bca3d9b75d1d7c1b68", + "sha256:6276478ada411aca97c0d5104916354b3d740d368407912722bd4d11aa9ee4c2", + "sha256:663f8de2b3df2e744d6e1610506e0ea4e213bde906795953c1e82279c169f0a7", + "sha256:67dcad1b8b201308586a8ca2ffe89df1e4f731d5a4cdd0610cc4ea790351c739", + "sha256:709b9f144d23e290b9863121d1ace14a72e01f66ea9c903fbdc690520dfdfcf0", + "sha256:8063a712fba642f78d3c506b0896846601b6de7f5c3d534e388ad0cc07f5a149", + "sha256:80d57177a0b7c14d4594c62bbb47fe2f6309ad3b0a34348a291d570925c97a82", + "sha256:87006cf0d81505408f1ae4f55cf8a5d95a8e029a4793360720ae17c6500f7ecc", + "sha256:9f62d21bc693f3d7d444f17ed2ad7a913b4c37c15cd807895d013c39c0517dfd", + "sha256:a207231a52426de3ff20f5608f0687261a3329d97a036c51f7d4c606a6f30c23", + "sha256:abc2e126c9490e58a36a0f83516479e781d83adfb134576a5cbe5c6af2a3e93c", + "sha256:b56638d58a3a4be13229c6a815cd448f9e3ce40c00880a5398471b42ee86f50e", + "sha256:bcd5b8416e73e4b0d48afba3704d8c826414764dafaed7a1a93c442188d90ccc", + "sha256:bec2bcdf7c9ce7f04d718e51887f3b05dc5c1cfaf5d2c2e9065ecddd1b2f6c9a", + "sha256:c8bf40cf6e281a4378e25846924327e728a887e8bf0ee83b2604a0f4b61692e8", + "sha256:cecbf67e81d6144a50dc615629772859463b2e4f815d0c082fa421db362f040e", + "sha256:d8074c8448cfd0705dfa71ca333277fce9786d0b9cac75d120545de6253f996a", + "sha256:dd302b6ae3965afeb5ef1b0d92486f986c0e65183cd7835973f0b593800590e6", + "sha256:de6e1cd75677423ff64712c337521e62e3a7a4fc84caabbd93207752e831a85a", + "sha256:ef39c98d9b8c0736d91937d193653e47c3b19ddf4fc3bccdc5e09aaa4b0c5d21", + "sha256:f2e045224074d5664dc9cbabbf4f4d4d46f1ee90f24780e3a9a668fd096ff17f", + "sha256:f521178e5a991ffd04182ed08f552daca1affcb826aeda0e1945cd989a9d4345", + "sha256:f78a68c2c820e4731e510a2df3eef0322f24fde1781ced970bf497b6c7d92982", + "sha256:fbe65d5cfe04ff2f7684160d50f5118bdefb01e3af4718eeb618bfed40f19d94" + ], + "version": "==3.9.8" + }, + "pyp100": { + "hashes": [ + "sha256:90b8cda63dd810fc37a05555f1af6902f8de7ba8a563a7614a190e9ac4aa6bf2" + ], + "index": "pypi", + "version": "==0.0.11" + }, + "requests": { + "hashes": [ + "sha256:b3559a131db72c33ee969480840fff4bb6dd111de7dd27c8ee1f820f4f00231b", + "sha256:fe75cc94a9443b9246fc7049224f75604b113c36acb93f87b80ed42c44cbb898" + ], + "version": "==2.24.0" + }, + "urllib3": { + "hashes": [ + "sha256:8d7eaa5a82a1cac232164990f04874c594c9453ec55eef02eab885aa02fc17a2", + "sha256:f5321fbe4bf3fefa0efd0bfe7fb14e90909eb62a48ccda331726b4319897dd5e" + ], + "version": "==1.25.11" + } + }, + "develop": {} +} diff --git a/lamp_control.py b/lamp_control.py new file mode 100755 index 0000000..ef89c3a --- /dev/null +++ b/lamp_control.py @@ -0,0 +1,144 @@ +#!/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()