-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathalias.js
160 lines (144 loc) · 3.19 KB
/
alias.js
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
import { validateStates, getRoom } from '../adapters';
import { locale } from 'src/boot/i18n';
export default 'Alias Devices';
export const namespace = 'alias';
export const deviceObjectType = 'channel';
/**
*
*/
export function parse(deviceStructure, options) {
return new Promise((resolve, reject) => {
const obj = deviceStructure.objects[deviceStructure.root];
if (!obj || !obj.common) {
return reject(new Error('Alias structure incorrect. Could not load role from %state'));
}
const device = {
'name': obj.common.name[locale] || obj.common.name || '',
'function': 'other',
'room': getRoom(deviceStructure),
'states': {
'unreach': '.UNREACH',
'lowBattery': '.LOWBAT'
}
}
// convert alias structure to jarvis
const role = obj && obj.common ? obj.common.role : '';
// LIGHT - SWITCH
if ([ 'light' ].indexOf(role) > -1) {
device.function = 'light';
device.states = {
'power': {
'state': '.SET',
'action': '.SET'
},
...device.states
}
}
// LIGHT - DIMMER
else if ([ 'dimmer' ].indexOf(role) > -1) { // 'rgb' not supported
device.function = 'light';
device.states = {
'level': {
'state': '.ACTUAL',
'action': '.SET'
},
...device.states
}
}
// LIGHT - COLOR
else if ([ 'rgbSingle', 'ct', 'hue' ].indexOf(role) > -1) { // 'rgb' not supported
device.function = 'light';
device.states = {
'power': {
'state': '.ON',
'action': '.ON'
},
'level': {
'state': '.DIMMER',
'action': '.DIMMER'
},
'colorTemperature': {
'state': '.TEMPERATURE',
'action': '.TEMPERATURE'
},
'hue': {
'state': '.HUE',
'action': '.HUE'
},
'rgb': {
'state': '.RGB',
'action': '.RGB'
},
...device.states
}
}
// BLIND
else if ([ 'blind' ].indexOf(role) > -1) {
device.function = 'blind';
device.states = {
'level': {
'state': '.SET',
'action': '.SET'
},
'activity': {
'state': '.WORKING',
'action': '.STOP'
},
...device.states
}
}
// WINDOW
else if ([ 'window' ].indexOf(role) > -1) {
device.function = 'window';
device.states = {
'open': {
'state': '.ACTUAL'
},
...device.states
}
}
// DOOR
else if ([ 'door' ].indexOf(role) > -1) {
device.function = 'door';
device.states = {
'open': {
'state': '.ACTUAL'
},
...device.states
}
}
// THERMOSTAT / TEMPERATURE
else if ([ 'temperature', 'thermostat' ].indexOf(role) > -1) {
device.function = 'heating';
device.states = {
'temperature': {
'state': '.ACTUAL'
},
'setTemperature': {
'state': '.SET',
'action': '.SET'
},
'humidity': {
'state': '.HUMIDITY',
'action': '.HUMIDITY'
},
'boost': {
'state': '.BOOST',
'action': '.BOOST'
},
...device.states
}
}
// adapt all states
deviceStructure.list.forEach(state => {
device.states = {
...device.states,
[state.substring(state.lastIndexOf('.') + 1)]: state.replace(deviceStructure.root, '')
}
});
// validate states
device.states = validateStates(device.states, deviceStructure);
// resolve
resolve(device);
});
}