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

simple power command

parents
[submodule "eins"]
path = eins
url = git@ssh.git.revreso.de:eins/python-eins.git
Subproject commit 56e1b2b22e28a4630499644270c292047b62e7cf
[speakers]
Description=Desktop speakers
ExecStart=echo speaker turned on
ExecStop=echo speaker turned off
#!/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()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment