-
Notifications
You must be signed in to change notification settings - Fork 1
/
write_profile.py
executable file
·245 lines (228 loc) · 8.07 KB
/
write_profile.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/usr/bin/python
import yaml,collections,re
from harmony_hub_funcs import harmony_hub_client
pfx = "write_profile:"
config_file_name = 'config.yaml'
config_file = open(config_file_name, 'r')
config_data = yaml.load(config_file)
config_file.close
# TODO: Check that server node & name are defined.
#if 'server' in config and config['host'] is not None:
# # use what the user has defined.
# this_host = config['host']
NODEDEF_TMPL_ACTIVITY = """
<nodeDef id="%s" nodeType="139" nls="%s">
<sts>
<st id="ST" editor="HUBST" />
<st id="GV3" editor="%s" />
</sts>
<cmds>
<sends>
<cmd id="DON" />
<cmd id="DOF" />
</sends>
<accepts>
<cmd id="SET_ACTIVITY">
<p id="" editor="%s" init="%s"/>
</cmd>
<cmd id="CHANGE_CHANNEL">
<p id="" editor="CHANNEL"/>
</cmd>
<cmd id="QUERY" />
<cmd id="DOF" />
<cmd id="DFOF" />
</accepts>
</cmds>
</nodeDef>
"""
NODEDEF_TMPL_DEVICE = """
<nodeDef id="%s" nodeType="139" nls="%s">
<sts />
<cmds>
<sends />
<accepts>
<cmd id="SET_BUTTON">
<p id="" editor="%s"/>
</cmd>
<cmd id="DON" />
<cmd id="DOF" />
</accepts>
</cmds>
</nodeDef>
"""
EDITOR_TMPL_S = """
<editor id="%s">
<range uom="25" subset="%s" nls="%s"/>
</editor>
"""
EDITOR_TMPL_MM = """
<editor id="%s">
<range uom="25" min="%d" max="%d" nls="%s"/>
</editor>
"""
# The NLS entries for the node definition
NLS_NODE_TMPL = """
ND-%s-NAME = %s
ND-%s-ICON = Input
"""
# The NLS entry for each indexed item
NLS_TMPL = "%s-%d = %s\n"
#
# Remove our old data from the nls file if present
#
nls_file = "profile/nls/en_US.txt"
nls = open(nls_file, "r")
found = False
nls_lines = []
split_line = "# Below is generated from the harmony hubs by write_profile.py"
p = re.compile(split_line)
for line in nls:
if not found:
if p.match(line):
found = True
else:
nls_lines.append(line)
nls.close()
nodedef = open("profile/nodedef/custom.xml", "w")
editor = open("profile/editor/custom.xml", "w")
nls = open(nls_file, "w")
for line in nls_lines:
nls.write(line)
nls.write(split_line+"\n")
editor.write("<editors>\n")
nodedef.write("<nodeDefs>\n")
#
# This is all the activities available for all hubs.
#
activites = collections.OrderedDict()
ai = 0
#
# This is all the button functions available for all devices.
#
buttons = collections.OrderedDict()
bi = 0
#
# Loop over each Hub in the config data.
#
config_data['info'] = dict()
config_data['info']['activities'] = list()
config_data['info']['functions'] = list()
warn_string_1 = ""
for key in config_data:
# Ignore server.
if key != 'server' and key != 'info':
#
# Process this hub.
#
host = config_data[key]['host']
name = config_data[key]['name']
info = "Hub: %s '%s'" % (key,name)
nodedef.write("\n <!-- === %s -->\n" % (info))
nodedef.write(NODEDEF_TMPL_ACTIVITY % (key, 'HARMONYHUB', 'Act' + key, 'Act' + key, 'GV3'))
nls.write("\n# %s" % (info))
nls.write(NLS_NODE_TMPL % (key, name, key))
#
# Connect to the hub and get the configuration
print(pfx + " Initializing Client")
client = harmony_hub_client(host=host)
print(pfx + " Client: " + str(client))
harmony_config = client.get_config()
client.disconnect(send_close=True)
#
# Save the config for reference.
harmony_config_file = key + ".yaml"
with open(harmony_config_file, 'w') as outfile:
yaml.safe_dump(harmony_config, outfile, default_flow_style=False)
#
# Build the activities
#
# PowerOff is always first
ais = ai
nls.write("# The index number is the matching list info->activities index\n")
nls.write("# The activity id's are uniq across all hubs so we share the same list\n")
nls.write(NLS_TMPL % (key.upper(), 0, 'Power Off'))
if ais == 0:
config_data['info']['activities'].append({'label':'Power Off','id':-1});
ai += 1
for a in harmony_config['activity']:
# Skip -1 since we printed it already.
if int(a['id']) != -1:
# Print the Harmony Activities to the log
print("%s Activity: %s Id: %s" % (pfx, a['label'], a['id']))
#aname = "%s (%s)" % (a['label'],a['id'])
aname = str(a['label'])
config_data['info']['activities'].append({'label':aname,'id':int(a['id'])});
nls.write(NLS_TMPL % (key.upper(), ai, aname))
ai += 1
# All activities contain zero which is power off...
if ais == 0:
subset = "%d-%d" % (ais, ai-1)
else:
subset = "0,%d-%d" % (ais, ai-1)
editor.write(EDITOR_TMPL_S % ('Act'+key, subset,key.upper()))
#
# Build all the devices
#
for d in harmony_config['device']:
info = "Device '%s', Type=%s, Manufacturer=%s, Model=%s" % (d['label'],d['type'],d['manufacturer'],d['model'])
subset = []
nodedef.write("\n <!-- === %s -->" % info)
nodedef.write(NODEDEF_TMPL_DEVICE % ('d' + d['id'], 'D' + d['id'], 'Btn' + d['id']))
nls.write("\n# %s" % info)
nls.write(NLS_NODE_TMPL % ('d' + d['id'], d['label'], 'd' + d['id']))
print("%s Device: %s Id: %s" % (pfx, d['label'], d['id']))
#
# Build all the button functions, these are global to all devices
#
for cg in d['controlGroup']:
for f in cg['function']:
a = f['action']
ay = yaml.load(a.replace('\\',''))
bname = f['name']
if not bname in buttons:
cb = bi
buttons[bname] = bi
bi += 1
config_data['info']['functions'].append({'label':str(f['label']),'name':str(f['name']),'command':{str(d['id']):str(ay['command'])}});
else:
cb = buttons[bname]
config_data['info']['functions'][cb]['command'][str(d['id'])] = ay['command']
print("%s Function: Index: %d, Name: %s, Label: %s, Command: %s" % (pfx, cb, f['name'], f['label'], ay['command']))
if bname != f['name']:
warn_string_1 += " device %s has button with label=%s, command=%s\n" % (d['label'],f['label'],ay['command'])
#nls.write("# Button name: %s, label: %s\n" % (f['name'], f['label']))
# This is the list of button numbers in this device.
subset.append(cb)
#
# Turn the list of button numbers, into a compacted subset string for the editor.
#
subset_str = ""
subset.sort()
editor.write("\n <!-- === %s -->\n" % info)
editor.write(" <!-- full subset = %s -->" % ",".join(map(str,subset)))
while len(subset) > 0:
x = subset.pop(0)
if subset_str != "":
subset_str += ","
subset_str += str(x)
if len(subset) > 0 and x == subset[0] - 1:
y = subset.pop(0)
while len(subset) > 0 and y == subset[0] - 1:
y = subset.pop(0)
subset_str += "-" + str(y)
editor.write(EDITOR_TMPL_S % ('Btn' + d['id'], subset_str, 'BTN'))
nls.write("\n\n")
for key in buttons:
nls.write(NLS_TMPL % ('BTN', buttons[key], key))
editor.write("</editors>")
nodedef.write("</nodeDefs>")
nodedef.close()
editor.close()
nls.close()
with open(config_file_name, 'w') as outfile:
yaml.dump(config_data, outfile, default_flow_style=False)
print(pfx + " done.")
#if warn_string_1 != "":
# print "WARNING: If you are upgrading from 0.3.x and using any of the following in an ISY program, you will need to fix them"
# print warn_string_1
exit