Verified Commit ac780602 authored by Gigadoc 2's avatar Gigadoc 2
Browse files

simple power command

parents
Loading
Loading
Loading
Loading

.gitmodules

0 → 100644
+3 −0
Original line number Diff line number Diff line
[submodule "eins"]
	path = eins
	url = git@ssh.git.revreso.de:eins/python-eins.git
Original line number Diff line number Diff line
Subproject commit 56e1b2b22e28a4630499644270c292047b62e7cf

power_command.ini

0 → 100644
+4 −0
Original line number Diff line number Diff line
[speakers]
Description=Desktop speakers
ExecStart=echo speaker turned on
ExecStop=echo speaker turned off

power_command.py

0 → 100755
+49 −0
Original line number Diff line number Diff line
#!/usr/bin/env python3

import configparser
import sys
import subprocess

from eins import Eins

actors = configparser.ConfigParser()
actors.read("power_command.ini")
#XXX: Complain on not present, make name configurable

# Check if all the devices have the required settings
error = False
for actor in actors.sections():
    for key in ["ExecStart", "ExecStop"]:
        if key not in actors[actor]:
            #XXX: Logging
            print("Device {} is missing the required setting {}.".format(actor, key))
            error = True
if error:
    sys.exit(-1)

eins = Eins()

@eins.simple_cmd()
def power(target, state):
    if target not in actors.sections():
        print("Don't know the device {}.".format(target)) #XXX: Logging
        return

    if state == "on":
        subprocess.run(actors[target]['ExecStart'], shell=True, check=True)
        #TODO: catch errors, logging
    elif state == "off":
        subprocess.run(actors[target]['ExecStop'], shell=True, check=True)
        #TODO: above
    else:
        print("Unknown power state {}, did you mean \"on\" or \"off\"?".format(target))
        #XXX: Logging

#TODO: Replace with standardized discovery
@eins.simple_cmd()
def commands():
    print("Known devices:")
    for key in actors.sections():
        print(key)

eins.run()