-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_missing.py
executable file
·146 lines (115 loc) · 3.81 KB
/
find_missing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
""" Find missing entity_id's from automations in Home Assistant"""
from html import entities
import os
import sys
import yaml
try:
from yaml import CLoader as Loader
except ImportError:
from yaml import Loader
from requests import get
import requests
try:
from simplejson.errors import JSONDecodeError
except ImportError:
from json.decoder import JSONDecodeError
def findkeys(node, key_value):
"""Find a key in a dict or list"""
if isinstance(node, list):
for i in node:
for item in findkeys(i, key_value):
yield item
elif isinstance(node, dict):
if key_value in node:
if isinstance(node[key_value], list):
for i in node[key_value]:
yield i
else:
yield node[key_value]
for j in node.values():
for item in findkeys(j, key_value):
yield item
def load_entities():
json = load_from_url("/states")
entities_list = []
for e in json:
entities_list.append(e["entity_id"])
return entities_list
def load_services():
json = load_from_url("/services")
services_list = []
for domain in json:
for service in domain["services"]:
services_list.append(domain["domain"] + "." + service)
return services_list
def load_from_url(append_url):
full_url = url + append_url
headers = {
"Authorization": "Bearer " + token,
"content-type": "application/json",
}
try:
response = get(full_url, headers=headers)
except requests.exceptions.RequestException: # This is the correct syntax
raise SystemExit()
try:
json = response.json()
except JSONDecodeError:
print("Error with JSON decoding. Check your URL for you server.")
sys.exit()
return json
def find_missing(filename):
"""Load the file data"""
yaml_file = open(filename)
try:
data = yaml.load(yaml_file, Loader=Loader)
except yaml.YAMLError:
print("Failed to load: " + filename)
return
automation_entities = set(findkeys(data, "entity_id"))
find_missing_entities(automation_entities, filename)
automation_services = set(findkeys(data, "service"))
find_missing_services(automation_services, filename)
def find_missing_entities(automation_entities, filename):
"""Find the missing entities"""
global entites
set_entities = set(entities)
missing_entities = automation_entities.difference(set_entities)
missing_entities = {e for e in missing_entities if "{" not in e}
global error_count
if len(missing_entities) > 0:
print(filename + " - Missing entities")
print(missing_entities)
error_count = error_count + 1
else:
if verbose is True:
print(filename + " - No missing entities")
def find_missing_services(automation_services, filename):
global services
set_services = set(services)
missing_services = automation_services.difference(set_services)
missing_services = {e for e in missing_services if "{" not in e}
global error_count
if len(missing_services) > 0:
print(filename + " - Missing Services")
print(missing_services)
error_count = error_count + 1
else:
if verbose is True:
print(filename + " - Not missing services")
url = os.environ.get("HASS_SERVER")
if url is None:
print("HASS_SERVER environmental variable needs to be set")
url = url + "/api"
verbose = False
token = os.environ.get("HASS_TOKEN")
if token is None:
print("HASS_TOKEN environmental variable needs to be set")
error_count = 0
entities = load_entities()
services = load_services()
with os.scandir(".") as files:
for file in files:
if file.name.endswith(".yaml"):
find_missing(file.name)
sys.exit(error_count)