From 330856fdf12a8301511ac04984b4c8976fa7da4f Mon Sep 17 00:00:00 2001 From: kyle Date: Wed, 31 Dec 2025 04:26:51 -0800 Subject: [PATCH] Add query-ha-entities.py --- query-ha-entities.py | 75 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 query-ha-entities.py diff --git a/query-ha-entities.py b/query-ha-entities.py new file mode 100644 index 0000000..a86897c --- /dev/null +++ b/query-ha-entities.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python3 +"""Query Home Assistant API for entity IDs""" +import urllib.request +import json +import sys + +TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiI4NmM2ZGNlMTY2MWU0M2U5YjQ2MDI3MjMxYjE0NDFlMyIsImlhdCI6MTc2NzE3ODUyNiwiZXhwIjoyMDgyNTM4NTI2fQ.E8eShOsejwDYglixpgM_d_JYBlB1OVNhN7cHPnPiLOs" +HA_URL = "http://homeassistant.local:8123" + +def get_entities(): + """Get all entities from Home Assistant""" + url = f"{HA_URL}/api/states" + req = urllib.request.Request(url) + req.add_header("Authorization", f"Bearer {TOKEN}") + req.add_header("Content-Type", "application/json") + + try: + with urllib.request.urlopen(req) as response: + data = json.loads(response.read().decode()) + return data + except Exception as e: + print(f"Error: {e}", file=sys.stderr) + return None + +def filter_weather_entities(entities): + """Filter for weather-related entities""" + weather_keywords = ['weather', 'temperature', 'humidity', 'pressure', 'wind', + 'forecast', 'rain', 'snow', 'cloud', 'uv', 'solar'] + + weather_entities = [] + for entity in entities: + entity_id = entity.get('entity_id', '').lower() + friendly_name = entity.get('attributes', {}).get('friendly_name', entity_id) + device_class = entity.get('attributes', {}).get('device_class', '').lower() + + # Check if it's a weather entity or sensor with weather-related attributes + if (any(kw in entity_id for kw in weather_keywords) or + any(kw in device_class for kw in weather_keywords) or + entity_id.startswith('weather.') or + (entity_id.startswith('sensor.') and device_class in ['temperature', 'humidity', 'pressure'])): + weather_entities.append({ + 'entity_id': entity.get('entity_id'), + 'friendly_name': friendly_name, + 'state': entity.get('state'), + 'device_class': device_class + }) + + return sorted(weather_entities, key=lambda x: x['entity_id']) + +if __name__ == '__main__': + print("Querying Home Assistant API...") + entities = get_entities() + + if entities: + print(f"\nFound {len(entities)} total entities") + weather = filter_weather_entities(entities) + print(f"\nFound {len(weather)} weather-related entities:\n") + + for e in weather: + print(f"{e['entity_id']}") + print(f" Name: {e['friendly_name']}") + print(f" State: {e['state']}") + if e['device_class']: + print(f" Device Class: {e['device_class']}") + print() + + # Also show all entity IDs for reference + print("\n" + "="*60) + print("ALL ENTITY IDs (for reference):") + print("="*60) + for entity in sorted(entities, key=lambda x: x.get('entity_id', '')): + print(entity.get('entity_id')) + else: + print("Failed to retrieve entities", file=sys.stderr) + sys.exit(1)