feat(fun/idual): Introduce real CLI that can send all commands

Adds an idualctl CLI that can be used to control the lights.
This commit is contained in:
Vincent Ambo 2020-04-26 15:50:21 +01:00
parent a34f7ef119
commit 8681ac787e
3 changed files with 50 additions and 25 deletions

View file

@ -1,9 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import base64 import base64
import broadlink import broadlink
import time import time
import sys
commands = { commands = {
# system commands # system commands
@ -51,28 +49,17 @@ class LightController:
device.auth() device.auth()
def send_cmd(self, name, iterations=5): def send_cmd(self, name, iterations=5):
"Send a command, repeatedly for reliability"
packet = cmd(name) packet = cmd(name)
for i in range(iterations): for i in range(iterations):
for device in self.devices: for device in self.devices:
device.send_data(packet) device.send_data(packet)
def lights_on(self): def wakey(self):
self.send_cmd('on') "Turn on the lights in the morning, try repeatedly for reasons."
print('Turning on the lights. Wakey, wakey!')
def lights_off(self): for i in range(5):
self.send_cmd('off') self.send_cmd('morning')
time.sleep(0.3)
if __name__ == "__main__": self.send_cmd('on')
# Attempt to turn the lights on, in morning mode, 10 times. time.sleep(0.3)
#
# 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)

39
fun/idual/idualctl Normal file
View file

@ -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)

View file

@ -9,8 +9,7 @@ setup(
author_email='mail@tazj.in', author_email='mail@tazj.in',
url='https://git.tazj.in/about/fun/idual', url='https://git.tazj.in/about/fun/idual',
packages=['idual'], packages=['idual'],
package_dir = {'idual': ''}, scripts = ['idualctl'],
scripts = ['__init__.py'],
install_requires=['broadlink>=0.13.2'], install_requires=['broadlink>=0.13.2'],
include_package_data=True, include_package_data=True,
) )