-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallstates.py
67 lines (43 loc) · 2.09 KB
/
allstates.py
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
from sqlitelib import sqlitelib
import json
import os
# dev/debug:
from pprint import pprint
# purpose: quick way to get current states for all lights, and time since last update.
# to be run on command line - sudo python3 allstates.py
# todo: take lights as a list, insert dynamically into query.
# This is a little clunky, because the db needs to be accessed by root - sqlite gives error otherwise, even in read only mode.
# also not clear if this locks DB for HA? though query is super quick so... maybe not an issue.
# eh. I think I saw some odd behavior when testing this. not sure.
db_file = "/home/homeassistant/.homeassistant/home-assistant_v2.db"
# ----------------------------------------
def mireds_to_kelvin(mireds):
return round(1000000 / mireds)
if os.geteuid() != 0:
exit("You need to have root privileges to run this script.")
# use uri mode so we can do read only
db_uri = "file:" + db_file + "?mode=ro"
db = sqlitelib(db_file)
latest_states_query = """select datetime(s.last_updated,'localtime') as last_updated,
s.entity_id, s.state, s.attributes,
strftime('%s','now') - strftime('%s', last_upd) as seconds_since_last_update
from states s
join (select entity_id, max(last_updated) as last_upd
from states
where entity_id in ('light.geo_desk','light.desk_lamp','light.lightstrip_2',
'light.mon_left','light.mon_right','light.up_left','light.up_right')
group by entity_id) l on s.entity_id=l.entity_id and s.last_updated = l.last_upd
where s.entity_id in ('light.geo_desk','light.desk_lamp','light.lightstrip_2',
'light.mon_left','light.mon_right','light.up_left','light.up_right')
and s.last_updated = last_upd;"""
data = db.execute(latest_states_query)
for n, i in enumerate(data):
attrs = json.loads(i['attributes'])
if 'color_temp' in attrs:
color_temp_kelvin = mireds_to_kelvin(attrs['color_temp'])
data[n]['color_temp_kelvin'] = color_temp_kelvin
if 'brightness' in attrs:
data[n]['brightness'] = attrs['brightness']
# trim attrs from original dict, we don't need them anymore
del data[n]['attributes']
pprint(data, indent=4)