Skip to content

Commit

Permalink
Folder watcher support
Browse files Browse the repository at this point in the history
  • Loading branch information
apop880 committed Jul 21, 2019
1 parent 23c2051 commit bf72ea1
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions apps/check-config/checkconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import requests
import json


# Check Home Assistant Configuration

class CheckConfig(hass.Hass):
Expand All @@ -13,6 +14,13 @@ def initialize(self):
else:
self.restart = True

# is folder watcher set?
if "folder_watcher" in self.args and self.args["folder_watcher"] == True:
self.folder_watcher = True
self.throttle_timer = None
else:
self.folder_watcher = False

# create a sensor to track check result
self.set_state("sensor.config_result", state="-", attributes = {"friendly_name": "Config Result", "detail": None})

Expand All @@ -22,15 +30,48 @@ def initialize(self):
# token or key to authenticate
if "token" in self.config["plugins"]["HASS"]:
self.auth = "token"
self.listen_state(self.check_config, "script.check_config")
if self.folder_watcher == False:
self.listen_state(self.check_config, "script.check_config")
else:
self.listen_event(self.config_throttle, "folder_watcher", file="configuration.yaml")
elif "ha_key" in self.config["plugins"]["HASS"]:
self.auth = "key"
self.listen_state(self.check_config, "script.check_config")
if self.folder_watcher == False:
self.listen_state(self.check_config, "script.check_config")
else:
self.listen_event(self.config_throttle, "folder_watcher", file="configuration.yaml")
else:
self.log("AppDaemon config must use a token or key to authenticate with HASS")
self.set_state("sensor.config_result", state="ERROR", attributes = {"friendly_name": "Config Result", "detail": "AppDaemon config must use a token or key to authenticate with HASS"})

def check_config(self, entity, attribute, old, new, kwargs):
# reset sensor while check is in progress
self.set_state("sensor.config_result", state="checking", attributes = {"detail": None})
# set headers for auth
if self.auth == "token":
self.headers = {'Authorization': "Bearer {}".format(self.config["plugins"]["HASS"]["token"])}
else: #key
self.headers = {'x-ha-access': self.config["plugins"]["HASS"]["ha_key"]}
# make the request
r = requests.post(self.apiurl, headers=self.headers)
# evaluate result
if json.loads(r.text)['result'] == "valid":
self.set_state("sensor.config_result", state="valid", attributes = {"detail": None})
# restart if auto-restart is on
if self.restart == True:
self.call_service("homeassistant/restart")
else:
self.set_state("sensor.config_result", state="invalid", attributes = {"detail": json.loads(r.text)['errors']})

def config_throttle(self, event_name, data, kwargs):
#throttle function to ensure that we don't call check multiple times
self.log("File update event triggered")
self.cancel_timer(self.throttle_timer)
self.throttle_timer = self.run_in(self.auto_check_config, 3)

def auto_check_config(self, kwargs):
# reset sensor while check is in progress
self.set_state("sensor.config_result", state="checking", attributes = {"detail": None})
# set headers for auth
if self.auth == "token":
self.headers = {'Authorization': "Bearer {}".format(self.config["plugins"]["HASS"]["token"])}
Expand Down

0 comments on commit bf72ea1

Please sign in to comment.