about summary refs log tree commit diff
path: root/fun
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@google.com>2020-04-25T22·13+0100
committerVincent Ambo <tazjin@google.com>2020-04-25T22·14+0100
commitdff91042fd6f3244b6af28b70e09ce38676c8fb1 (patch)
tree6600ac57e9f35a422abf79f23f750a612232307e /fun
parente7aaa0bc2f3d1c1f7514076f990ece7adebcdace (diff)
feat(fun/idual): Implement some utilities for controlling lights r/686
This program, if build in its executable form, will try to turn the
lights on and put them into "morning mode".
Diffstat (limited to 'fun')
-rw-r--r--fun/idual/__init__.py (renamed from fun/idual/idual.py)37
-rw-r--r--fun/idual/setup.py4
2 files changed, 36 insertions, 5 deletions
diff --git a/fun/idual/idual.py b/fun/idual/__init__.py
index da0a73c309f2..4a1344e303a3 100644
--- a/fun/idual/idual.py
+++ b/fun/idual/__init__.py
@@ -1,7 +1,11 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
-colours = {
+import base64
+import broadlink
+import time
+
+commands = {
     # system commands
     'on'       : 'JgBIAAABK5AVERQ2FBEUERQSFBEUERQSFBEUNhQ2FDUUNhQ2FDYUNRU1FBIUERQRFBIUERQRFBIUERQ2FDYUNRQ2FDYUNhQ1FQANBQ==',
     'off'      : 'JgBIAAABLJAUERQ2FBEUEhQRFBEUEhQRFBEUNhQ2FDUVNRQ2FDYUNhQRFDYUERQSFBEUERQSFBEUNhQRFDYUNhQ2FDUUNhQ2FAANBQ==',
@@ -35,6 +39,33 @@ colours = {
     'desaturate' : 'JgBIAAABLI8VERQ2FBEUERQSFBEUERURFBEUNhQ1FTUUNhQ2FDYUNRQ2FDYUNhQ1FREUERQSFBEUERQSFBEUERQ2FDYUNhQ1FQANBQ==',
 }
 
+class LightController:
+    def __init__(self):
+        devices = broadlink.discover(timeout=2)
+        if devices == []:
+            raise Exception('no devices found')
+        devices[0].auth()
+        self.device = devices[0]
+
+    def send_cmd(self, name):
+        packet = base64.b64decode(commands[name])
+        self.device.send_data(packet)
+
+    def lights_on(self):
+        self.send_cmd('on')
+
+    def lights_off(self):
+        self.send_cmd('off')
+
 if __name__ == "__main__":
-    # execute only if run as a script
-    print(colours)
+    # Attempt to turn the lights on, in morning mode, 10 times.
+    #
+    # The command sending doesn't always work, hence this brute-force
+    # approach.
+    ctrl = LightController()
+
+    for i in range(9):
+        ctrl.send_cmd('morning')
+        time.sleep(0.2)
+        ctrl.lights_on()
+        time.sleep(0.8)
diff --git a/fun/idual/setup.py b/fun/idual/setup.py
index b218b07f7875..74156bcb0436 100644
--- a/fun/idual/setup.py
+++ b/fun/idual/setup.py
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
-from setuptools import setup, find_packages
+from setuptools import setup
 
 setup(
     name='idualctl',
@@ -10,7 +10,7 @@ setup(
     url='https://git.tazj.in/about/fun/idual',
     packages=['idual'],
     package_dir = {'idual': ''},
-    scripts = ['idual.py'],
+    scripts = ['__init__.py'],
     install_requires=['broadlink>=0.13.2'],
     include_package_data=True,
 )