smart_brain_test/smart_brain_test.py

95 行
3.0 KiB
Python

import config
import mqtt
import readline
from simulation.rooms import house
import sys
from tests.all import test_smarthome
# TODO: Extend tests in simulation
# - Switching button functions (gfw_dirk, ffe.sleep)
# - Brightness button functions (gfw.dirk, ffe.sleep)
# - Synch functions of amplifier with spotify, mpd
# - Remote actions after amplifier on
# - Heating functionality (extended: mode switch off by other function, timer)
# - Circulation pump (Extend Timer)
# - Stairways (Extend Motion sensor and Timer)
if __name__ == "__main__":
mc = mqtt.mqtt_client(host=config.MQTT_SERVER, port=config.MQTT_PORT, username=config.MQTT_USER,
password=config.MQTT_PASSWORD, name=config.APP_NAME + '_simulation')
#
COMMANDS = ['quit', 'help']
#
h = house(mc)
for name in h.getmembers():
d = h.getobjbyname(name)
for c in d.capabilities():
COMMANDS.append(name + '.' + c)
#
ts = test_smarthome(h, mc)
for name in ts.getmembers():
d = ts.getobjbyname(name)
for c in d.capabilities():
COMMANDS.append('test.' + name + '.' + c)
def reduced_list(text):
"""
Create reduced completation list
"""
reduced_list = {}
for cmd in COMMANDS:
next_dot = cmd[len(text):].find('.')
if next_dot >= 0:
reduced_list[cmd[:len(text) + next_dot + 1]] = None
else:
reduced_list[cmd] = None
return reduced_list.keys()
def completer(text, state):
"""
Our custom completer function
"""
options = [x for x in reduced_list(text) if x.startswith(text)]
return options[state]
def complete(text, state):
for cmd in COMMANDS:
if cmd.startswith(text):
if not state:
hit = ""
index = 0
sub_list = cmd.split('.')
while len(text) >= len(hit):
hit += sub_list[index] + '.'
return hit # cmd
else:
state -= 1
if len(sys.argv) == 1:
readline.parse_and_bind("tab: complete")
readline.set_completer(completer)
print("\nEnter command: ")
while True:
userfeedback = input('')
command = userfeedback.split(' ')[0]
if userfeedback == 'quit':
break
elif userfeedback == 'help':
print("Help is not yet implemented!")
elif userfeedback.startswith("test"):
ts.command(userfeedback)
elif command in COMMANDS[2:]:
h.command(userfeedback)
elif userfeedback != "":
print("Unknown command!")
else:
print()
else:
cmd = sys.argv[1]
if cmd.startswith('test'):
ts.command(cmd)
else:
h.command(cmd)
ts.close()