18 lines
889 B
Python
18 lines
889 B
Python
import urllib.request
|
|
import json
|
|
|
|
HA_URL = "http://14.4.4.20:8123"
|
|
HA_TOKEN = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJkZWNlMWI5ZjJhZDQ0MTRmODU4NGYzN2I1YjgwZDQxMiIsImlhdCI6MTc5MzQ1MjQ2MywiZXhwIjoyMTA4ODEyNDYzfQ.JznpOqsCSnHz6aTVa8K9pQCz1816DC8SrmS1xn1Sm2k"
|
|
|
|
req = urllib.request.Request(f"{HA_URL}/api/states", headers={"Authorization": f"Bearer {HA_TOKEN}", "Content-Type": "application/json"})
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=5) as r:
|
|
states = json.loads(r.read().decode())
|
|
ups_states = [s for s in states if "ups" in s["entity_id"] or "server_closet" in s["entity_id"] or "nut" in s["entity_id"]]
|
|
print(f"Found {len(ups_states)} UPS entities in Home Assistant:")
|
|
for s in ups_states:
|
|
print(f" {s['entity_id']}: {s['state']} {s.get('attributes', {}).get('unit_of_measurement', '')}")
|
|
except Exception as e:
|
|
print("HA err:", e)
|
|
|