-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetup_home_assistant.py
More file actions
147 lines (126 loc) Β· 5.46 KB
/
Copy pathsetup_home_assistant.py
File metadata and controls
147 lines (126 loc) Β· 5.46 KB
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
147
#!/usr/bin/env python3
"""
Setup Home Assistant Integration for ADHDo
Gets your HA token and tests the connection
"""
import asyncio
import aiohttp
import os
import json
async def test_home_assistant():
"""Test Home Assistant connection and setup."""
print("π ADHDo Home Assistant Setup")
print("=" * 50)
# Check for existing config
ha_url = os.getenv("HOME_ASSISTANT_URL", "http://homeassistant.local:8123")
ha_token = os.getenv("HOME_ASSISTANT_TOKEN", "")
if not ha_token:
print("\nπ To connect to Home Assistant, you need a Long-Lived Access Token")
print("\nSteps to get your token:")
print("1. Open Home Assistant: http://homeassistant.local:8123")
print("2. Click on your profile (bottom left)")
print("3. Scroll down to 'Long-Lived Access Tokens'")
print("4. Click 'Create Token'")
print("5. Name it 'ADHDo Integration'")
print("6. Copy the token (you won't see it again!)")
print()
ha_token = input("Paste your Home Assistant token here: ").strip()
print(f"\nπ Testing connection to: {ha_url}")
headers = {
"Authorization": f"Bearer {ha_token}",
"Content-Type": "application/json"
}
async with aiohttp.ClientSession() as session:
try:
# Test API connection
async with session.get(
f"{ha_url}/api/",
headers=headers,
timeout=aiohttp.ClientTimeout(total=5)
) as response:
if response.status == 200:
data = await response.json()
print(f"β
Connected to Home Assistant!")
print(f" Version: {data.get('version', 'Unknown')}")
else:
print(f"β Connection failed: {response.status}")
return
except Exception as e:
print(f"β Could not connect: {e}")
print("\nTroubleshooting:")
print("- Is Home Assistant running?")
print("- Is the URL correct? (try http://192.168.1.XXX:8123)")
print("- Is the token correct?")
return
# Get available calendars
print("\nπ
Checking calendars...")
try:
async with session.get(
f"{ha_url}/api/calendars",
headers=headers
) as response:
if response.status == 200:
calendars = await response.json()
print(f" Found {len(calendars)} calendar(s):")
for cal in calendars:
print(f" - {cal.get('name', 'Unknown')}")
except:
print(" No calendars found")
# Get health sensors
print("\nπ Checking health sensors...")
try:
async with session.get(
f"{ha_url}/api/states",
headers=headers
) as response:
if response.status == 200:
states = await response.json()
health_sensors = []
for entity in states:
entity_id = entity.get('entity_id', '')
# Look for health-related sensors
health_keywords = ['step', 'heart', 'sleep', 'activity', 'weight', 'stress']
if any(kw in entity_id.lower() for kw in health_keywords):
health_sensors.append(entity_id)
if health_sensors:
print(f" Found {len(health_sensors)} health sensor(s):")
for sensor in health_sensors[:10]: # Show first 10
print(f" - {sensor}")
else:
print(" No health sensors found")
except:
print(" Could not check sensors")
# Save configuration
print("\nπΎ Saving configuration...")
# Update .env file
env_file = ".env"
env_lines = []
if os.path.exists(env_file):
with open(env_file, 'r') as f:
env_lines = f.readlines()
# Update or add HA settings
ha_url_set = False
ha_token_set = False
for i, line in enumerate(env_lines):
if line.startswith("HOME_ASSISTANT_URL="):
env_lines[i] = f"HOME_ASSISTANT_URL={ha_url}\n"
ha_url_set = True
elif line.startswith("HOME_ASSISTANT_TOKEN="):
env_lines[i] = f"HOME_ASSISTANT_TOKEN={ha_token}\n"
ha_token_set = True
if not ha_url_set:
env_lines.append(f"HOME_ASSISTANT_URL={ha_url}\n")
if not ha_token_set:
env_lines.append(f"HOME_ASSISTANT_TOKEN={ha_token}\n")
with open(env_file, 'w') as f:
f.writelines(env_lines)
print("β
Configuration saved to .env")
print("\nπ Home Assistant integration ready!")
print("\nWhat you can now do:")
print("- Ask 'what's next' to see calendar events")
print("- Say 'remind me to...' to create reminders")
print("- Get contextual awareness based on your schedule")
print("- View health data insights")
print("\nRestart the ADHDo server to use the new integration.")
if __name__ == "__main__":
asyncio.run(test_home_assistant())