Add check-ha-backups.py
This commit is contained in:
97
check-ha-backups.py
Normal file
97
check-ha-backups.py
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Check Home Assistant backups"""
|
||||||
|
import urllib.request
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
|
||||||
|
TOKEN = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiI4NmM2ZGNlMTY2MWU0M2U5YjQ2MDI3MjMxYjE0NDFlMyIsImlhdCI6MTc2NzE3ODUyNiwiZXhwIjoyMDgyNTM4NTI2fQ.E8eShOsejwDYglixpgM_d_JYBlB1OVNhN7cHPnPiLOs"
|
||||||
|
HA_URL = "http://homeassistant.local:8123"
|
||||||
|
|
||||||
|
def get_backup_info():
|
||||||
|
"""Get backup information from Home Assistant"""
|
||||||
|
|
||||||
|
# Get backup sensor entities
|
||||||
|
url = f"{HA_URL}/api/states"
|
||||||
|
req = urllib.request.Request(url)
|
||||||
|
req.add_header("Authorization", f"Bearer {TOKEN}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
with urllib.request.urlopen(req) as response:
|
||||||
|
states = json.loads(response.read().decode())
|
||||||
|
|
||||||
|
# Find backup-related entities
|
||||||
|
backup_entities = [e for e in states if 'backup' in e['entity_id'].lower()]
|
||||||
|
|
||||||
|
print("="*60)
|
||||||
|
print("HOME ASSISTANT BACKUP INFORMATION")
|
||||||
|
print("="*60)
|
||||||
|
print()
|
||||||
|
|
||||||
|
if backup_entities:
|
||||||
|
for entity in sorted(backup_entities, key=lambda x: x['entity_id']):
|
||||||
|
eid = entity['entity_id']
|
||||||
|
state = entity.get('state', 'N/A')
|
||||||
|
attrs = entity.get('attributes', {})
|
||||||
|
name = attrs.get('friendly_name', eid)
|
||||||
|
|
||||||
|
print(f"{name}")
|
||||||
|
print(f" Entity ID: {eid}")
|
||||||
|
print(f" State: {state}")
|
||||||
|
|
||||||
|
# Show additional attributes if available
|
||||||
|
if attrs:
|
||||||
|
for key, value in attrs.items():
|
||||||
|
if key not in ['friendly_name', 'entity_id'] and value:
|
||||||
|
print(f" {key}: {value}")
|
||||||
|
print()
|
||||||
|
else:
|
||||||
|
print("No backup entities found.")
|
||||||
|
print()
|
||||||
|
print("Backup entities might be named differently.")
|
||||||
|
print("Checking all sensor entities for backup-related ones...")
|
||||||
|
print()
|
||||||
|
|
||||||
|
# Check for any sensor that might be backup-related
|
||||||
|
sensors = [e for e in states if e['entity_id'].startswith('sensor.')]
|
||||||
|
backup_keywords = ['backup', 'snapshot', 'restore']
|
||||||
|
|
||||||
|
for sensor in sensors:
|
||||||
|
eid = sensor['entity_id'].lower()
|
||||||
|
if any(keyword in eid for keyword in backup_keywords):
|
||||||
|
print(f" {sensor['entity_id']}: {sensor.get('state', 'N/A')}")
|
||||||
|
|
||||||
|
# Try backup API endpoint
|
||||||
|
print()
|
||||||
|
print("="*60)
|
||||||
|
print("Checking backup API endpoint...")
|
||||||
|
print("="*60)
|
||||||
|
|
||||||
|
backup_url = f"{HA_URL}/api/backup"
|
||||||
|
req_backup = urllib.request.Request(backup_url)
|
||||||
|
req_backup.add_header("Authorization", f"Bearer {TOKEN}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
with urllib.request.urlopen(req_backup) as response:
|
||||||
|
backups = json.loads(response.read().decode())
|
||||||
|
print("\nBackups found via API:")
|
||||||
|
if isinstance(backups, list):
|
||||||
|
for backup in backups:
|
||||||
|
print(f" - {backup}")
|
||||||
|
else:
|
||||||
|
print(json.dumps(backups, indent=2))
|
||||||
|
except urllib.error.HTTPError as e:
|
||||||
|
if e.code == 404:
|
||||||
|
print("Backup API endpoint not available (404)")
|
||||||
|
else:
|
||||||
|
print(f"Error accessing backup API: {e.code}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Could not access backup API: {e}")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error: {e}", file=sys.stderr)
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
get_backup_info()
|
||||||
Reference in New Issue
Block a user