Skip to content

Instantly share code, notes, and snippets.

@mdeweerd
Last active October 24, 2022 21:14
Show Gist options
  • Save mdeweerd/b752de149f1af764cecb4724cbfba505 to your computer and use it in GitHub Desktop.
Save mdeweerd/b752de149f1af764cecb4724cbfba505 to your computer and use it in GitHub Desktop.
Quick implementation to call scripts from AppDaemon
---
alias: DemoScript1
description: ''
trigger:
- platform: time_pattern
minutes: '45'
hours: '1'
condition: []
action:
# Trigger script 1
- event: call_script1
event_data: {}
mode: single
import sys
import subprocess as s
import adbase as ad
import hassapi as hass
class CallScript(hass.Hass):
def initialize(self):
event_name = "call_script"
if "event" in self.args:
event_name = self.args["event"]
self.listen_event(self.call_script, event_name)
@ad.app_lock
def call_script(self, *args, **kwargs): # pylint: disable=unused-argument
if "script" not in self.args:
raise Exception("Missing script parameter for CallScript")
script_args = [self.args["script"]]
if "args" in self.args:
s_args = self.args["args"]
script_args.extend(s_args)
out = sys.stdout
err = sys.stderr
closeout = False
closeerr = False
if "outfile" in self.args:
out = open(self.args["outfile"], "w")
closeout = True
if "errfile" in self.args:
err = open(self.args["errfile"], "w")
closeerr = True
s.call(script_args, stdout=out, stderr=err)
# Close files as needed
if closeout:
out.close()
if closeerr:
err.close()
---
script1:
module: call_script
class: CallScript
event: call_script1
# stdout file (optional)
outfile: /config/appdaemon/apps/test1.log
# stderr file (optional)
errfile: /config/appdaemon/apps/test1err.log
# executable name
script: /bin/ls
# Arguments to executable
args:
- -lRt
- /config
script2:
module: call_script
class: CallScript
# Set the name that will trgger this script
event: call_script2
# stdout file (optional)
outfile: /config/appdaemon/apps/test2.log
# stderr file (optional)
errfile: /config/appdaemon/apps/test2err.log
# executable name
script: /bin/ls
# Arguments to executable
args:
- -lRt
- /data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment