Advertisement
pqpxoxa

HA-Check1

Nov 13th, 2021
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. #### /config/appdaemon/apps/scripts.yaml
  2. #### AND
  3. #### /config/appdaemon/apps/config-check/scripts.yaml
  4. #### AND
  5. #### /config/components/scripts/check.yaml
  6.  
  7. check_config:
  8. sequence: []
  9. alias: Check Configuration
  10.  
  11. #### /config/appdaemon/apps/apps.yaml
  12.  
  13. check_config:
  14. module: checkconfig
  15. class: CheckConfig
  16. restart: false
  17.  
  18. #### /config/appdaemon/apps/config-check/checkconfig.py
  19.  
  20. import appdaemon.plugins.hass.hassapi as hass
  21. import requests
  22. import json
  23.  
  24.  
  25. # Check Home Assistant Configuration
  26.  
  27. class CheckConfig(hass.Hass):
  28.  
  29. def initialize(self):
  30. # is auto-restart set?
  31. if "restart" in self.args and self.args["restart"] == False:
  32. self.restart = False
  33. else:
  34. self.restart = True
  35.  
  36. # is folder watcher set?
  37. if "folder_watcher" in self.args and self.args["folder_watcher"] == True:
  38. self.folder_watcher = True
  39. self.throttle_timer = None
  40. else:
  41. self.folder_watcher = False
  42.  
  43. # create a sensor to track check result
  44. self.set_state("sensor.config_result", state="-", attributes = {"friendly_name": "Config Result", "detail": None})
  45.  
  46. # get HASS URL
  47. self.apiurl = "{}/api/config/core/check_config".format(self.config["plugins"]["HASS"]["ha_url"])
  48.  
  49. # token or key to authenticate
  50. if "token" in self.config["plugins"]["HASS"]:
  51. self.auth = "token"
  52. if self.folder_watcher == False:
  53. self.listen_state(self.check_config, "script.check_config", attribute="last_triggered")
  54. else:
  55. self.listen_event(self.config_throttle, "folder_watcher", file="configuration.yaml")
  56. elif "ha_key" in self.config["plugins"]["HASS"]:
  57. self.auth = "key"
  58. if self.folder_watcher == False:
  59. self.listen_state(self.check_config, "script.check_config", attribute="last_triggered")
  60. else:
  61. self.listen_event(self.config_throttle, "folder_watcher", file="configuration.yaml")
  62. else:
  63. self.log("AppDaemon config must use a token or key to authenticate with HASS")
  64. 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"})
  65.  
  66. def check_config(self, entity, attribute, old, new, kwargs):
  67. # reset sensor while check is in progress
  68. self.set_state("sensor.config_result", state="checking", attributes = {"detail": None})
  69. # set headers for auth
  70. if self.auth == "token":
  71. self.headers = {'Authorization': "Bearer {}".format(self.config["plugins"]["HASS"]["token"])}
  72. else: #key
  73. self.headers = {'x-ha-access': self.config["plugins"]["HASS"]["ha_key"]}
  74. # make the request
  75. r = requests.post(self.apiurl, headers=self.headers)
  76. # evaluate result
  77. if json.loads(r.text)['result'] == "valid":
  78. self.set_state("sensor.config_result", state="valid", attributes = {"detail": None})
  79. # restart if auto-restart is on
  80. if self.restart == True:
  81. self.call_service("homeassistant/restart")
  82. else:
  83. self.set_state("sensor.config_result", state="invalid", attributes = {"detail": json.loads(r.text)['errors']})
  84.  
  85. def config_throttle(self, event_name, data, kwargs):
  86. #throttle function to ensure that we don't call check multiple times
  87. self.cancel_timer(self.throttle_timer)
  88. self.throttle_timer = self.run_in(self.auto_check_config, 3)
  89.  
  90. def auto_check_config(self, kwargs):
  91. # reset sensor while check is in progress
  92. self.set_state("sensor.config_result", state="checking", attributes = {"detail": None})
  93. # set headers for auth
  94. if self.auth == "token":
  95. self.headers = {'Authorization': "Bearer {}".format(self.config["plugins"]["HASS"]["token"])}
  96. else: #key
  97. self.headers = {'x-ha-access': self.config["plugins"]["HASS"]["ha_key"]}
  98. # make the request
  99. r = requests.post(self.apiurl, headers=self.headers)
  100. # evaluate result
  101. if json.loads(r.text)['result'] == "valid":
  102. self.set_state("sensor.config_result", state="valid", attributes = {"detail": None})
  103. # restart if auto-restart is on
  104. if self.restart == True:
  105. self.call_service("homeassistant/restart")
  106. else:
  107. self.set_state("sensor.config_result", state="invalid", attributes = {"detail": json.loads(r.text)['errors']})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement