forked from sunnythaper/python_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene_generator.py
53 lines (42 loc) · 1.5 KB
/
scene_generator.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
# Pass the following:
#
# {
# "domain": "light",
# "attributes": ["brightness", "color_temp", "xy_color", "rgb_color"],
# "save_file": true
# }
# SETUP VARIABLES FROM HASS CALL
domain = data.get('domain')
attributes = data.get('attributes')
save_file = data.get('save_file')
# PRINT NICELY FORMATTED HEADER
text = "\n\n"
text = text + "#############################################\n"
text = text + "## " + domain + "\n"
text = text + "#############################################\n\n"
# FIND ALL ENTITIES BY DOMAIN
entities = hass.states.entity_ids(domain)
# GET ENTITY STATE & ATTRIBUTES
for i in entities:
# GET ENTITY DATA FROM HASS
entity = ("%r" % i).strip("'")
status = hass.states.get(entity)
# ENTITY STATE
text = text + entity + ":\n"
text = text + " state: " + status.state + "\n"
# ENTITY ATTRIBUTES WHEN STATE IS ON
if status.state == 'on':
for i in attributes:
# GET ATTRIBUTE BY JSON ARRAY PASSED BY HASS CALL
attributeState = ("%r" % i).strip("'")
# DISPLAY ATTRIBUTE IF NOT EMPTY
if status.attributes.get(attributeState):
text = text + " " + attributeState + ": " + str(status.attributes.get(attributeState)) + "\n"
# GIVE THE PO DECLARATION SOME SPACE
text = text + "\n"
if save_file:
# SAVE SCENE CONFIGURATION TO FILE
hass.services.call("notify", "scene_generator", {"message": "{}".format(text)})
else:
# OUTPUT FORMATTED YAML TO LOG FILE
logger.warning(text)