Skip to content

Instantly share code, notes, and snippets.

@ov1d1u
Last active October 26, 2023 12:51
Show Gist options
  • Save ov1d1u/5cf0b83a450593f0e0939c0a92e0bdb3 to your computer and use it in GitHub Desktop.
Save ov1d1u/5cf0b83a450593f0e0939c0a92e0bdb3 to your computer and use it in GitHub Desktop.
Script to validate entities in configuration files
import sys
import re
import requests
# USAGE: python3 validate_entities.py < flows.json
# Configuration
# Replace with your Home Assistant instance's address
BASE_URL = "http://homeassistant.local:8123"
# Replace with your API token
TOKEN = ""
# Add here entity ids you'd like to except
INVALID_ENTITY_IDS = []
HEADERS = {
"Authorization": f"Bearer {TOKEN}",
"Content-Type": "application/json",
}
def get_entities(base_url, headers):
# Endpoint for fetching states (which includes entities)
url = f"{base_url}/api/states"
response = requests.get(url, headers=headers)
if response.status_code != 200:
print("Failed to retrieve entities. Ensure your TOKEN and BASE_URL are correct.")
return
states = response.json()
# Extract entity ids from states
entities = [state['entity_id'] for state in states]
return entities
def get_services(base_url, headers):
url = f"{base_url}/api/services"
response = requests.get(url, headers=headers)
if response.status_code != 200:
print("Failed to retrieve services. Ensure your TOKEN and BASE_URL are correct.")
return []
services = []
services_data = response.json()
for service_data in services_data:
domain = service_data["domain"]
for service in service_data["services"]:
services.append(f"{domain}.{service}")
# Extracting and returning list of services in "domain.service" format
return services
def get_event_types(base_url, headers):
url = f"{base_url}/api/events"
response = requests.get(url, headers=headers)
if response.status_code != 200:
print("Failed to retrieve event types. Ensure your TOKEN and BASE_URL are correct.")
return []
events_data = response.json()
# Extracting and returning list of event types
return [event['event'] for event in events_data]
def extract_domains_from_entities(entities):
return set(entity.split('.')[0] for entity in entities)
def extract_patterns(s):
# The regex pattern looks for sequences of characters that are separated by a dot.
# \w+ captures one or more word characters (equivalent to [a-zA-Z0-9_])
pattern = r'\b\w+\.\w+\b'
return re.findall(pattern, s)
if __name__ == "__main__":
flows_entities = []
services = get_services(BASE_URL, HEADERS)
entities = get_entities(BASE_URL, HEADERS)
events = get_event_types(BASE_URL, HEADERS)
valid_domains = extract_domains_from_entities(entities)
# Read flows_data from stdin
flows_data = sys.stdin.read()
flows_entities = [eid for eid in extract_patterns(flows_data) if eid.split('.')[0] in valid_domains]
flows_entities = list(set(flows_entities))
flows_entities = [eid for eid in flows_entities if eid not in INVALID_ENTITY_IDS]
flows_entities = [eid for eid in flows_entities if eid not in services]
flows_entities = [eid for eid in flows_entities if eid not in events]
for flow_entity in flows_entities:
if flow_entity not in entities:
print(f"Entity {flow_entity} not found in Home Assistant.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment