about summary refs log tree commit diff
path: root/fun
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2020-04-26T14·50+0100
committerVincent Ambo <tazjin@google.com>2020-04-26T14·50+0100
commit8681ac787e7b03e544cbfbcdf7b2d926c037c70c (patch)
tree65b6c357af3a269e35d111a8ff213028af3b5c36 /fun
parenta34f7ef11999636deffd47f8efdc6847d4c676d7 (diff)
feat(fun/idual): Introduce real CLI that can send all commands r/692
Adds an idualctl CLI that can be used to control the lights.
Diffstat (limited to 'fun')
-rw-r--r--fun/idual/idual/__init__.py (renamed from fun/idual/__init__.py)33
-rw-r--r--fun/idual/idualctl39
-rw-r--r--fun/idual/setup.py3
3 files changed, 50 insertions, 25 deletions
diff --git a/fun/idual/__init__.py b/fun/idual/idual/__init__.py
index d8a21dcc5b..f5a8d274f6 100644
--- a/fun/idual/__init__.py
+++ b/fun/idual/idual/__init__.py
@@ -1,9 +1,7 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-
 import base64
 import broadlink
 import time
+import sys
 
 commands = {
     # system commands
@@ -51,28 +49,17 @@ class LightController:
             device.auth()
 
     def send_cmd(self, name, iterations=5):
+        "Send a command, repeatedly for reliability"
         packet = cmd(name)
         for i in range(iterations):
             for device in self.devices:
                 device.send_data(packet)
 
-    def lights_on(self):
-        self.send_cmd('on')
-
-    def lights_off(self):
-        self.send_cmd('off')
-
-if __name__ == "__main__":
-    # Attempt to turn the lights on, in morning mode, 10 times.
-    #
-    # The command sending doesn't always work, hence this brute-force
-    # approach.
-    print('Initialising light controller')
-    ctrl = LightController()
-
-    print('Turning on the lights. Wakey, wakey!')
-    for i in range(9):
-        ctrl.send_cmd('morning')
-        time.sleep(0.2)
-        ctrl.lights_on()
-        time.sleep(0.8)
+    def wakey(self):
+        "Turn on the lights in the morning, try repeatedly for reasons."
+        print('Turning on the lights. Wakey, wakey!')
+        for i in range(5):
+            self.send_cmd('morning')
+            time.sleep(0.3)
+            self.send_cmd('on')
+            time.sleep(0.3)
diff --git a/fun/idual/idualctl b/fun/idual/idualctl
new file mode 100644
index 0000000000..10a85eba0a
--- /dev/null
+++ b/fun/idual/idualctl
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+
+import idual
+import sys
+
+def help():
+    print('Available commands:')
+    for cmd in idual.commands:
+        print('- ' + cmd)
+    sys.exit(0)
+
+def handle(ctrl, cmd):
+    if cmd == 'help':
+        help()
+    elif cmd == 'wakey':
+        ctrl.wakey()
+        sys.exit(0)
+    elif cmd == 'on':
+        print('Turning on the lights')
+        ctrl.send_cmd(cmd)
+    elif cmd == 'off':
+        print('Turning off the lights')
+        ctrl.send_cmd(cmd)
+    elif cmd in idual.commands:
+        print('Sending ' + cmd + '-command')
+        ctrl.send_cmd(cmd)
+    else:
+        print('unknown command \'' + cmd + '\'')
+        sys.exit(1)
+
+if __name__ == "__main__":
+    if len(sys.argv) == 1:
+        help()
+
+    print('Initialising light controller')
+    ctrl = idual.LightController()
+
+    for cmd in sys.argv[1:]:
+        handle(ctrl, cmd)
diff --git a/fun/idual/setup.py b/fun/idual/setup.py
index 74156bcb04..f10c3b86f2 100644
--- a/fun/idual/setup.py
+++ b/fun/idual/setup.py
@@ -9,8 +9,7 @@ setup(
     author_email='mail@tazj.in',
     url='https://git.tazj.in/about/fun/idual',
     packages=['idual'],
-    package_dir = {'idual': ''},
-    scripts = ['__init__.py'],
+    scripts = ['idualctl'],
     install_requires=['broadlink>=0.13.2'],
     include_package_data=True,
 )