-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathdevice.js
More file actions
183 lines (168 loc) · 7.23 KB
/
Copy pathdevice.js
File metadata and controls
183 lines (168 loc) · 7.23 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
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
'use strict';
const KNXGenericDevice = require('../../lib/GenericKNXDevice');
const DatapointTypeParser = require('../../lib/DatapointTypeParser');
class KNXWindowCovering extends KNXGenericDevice {
async onInit() {
// One-time migration for devices paired before the invert_position setting existed:
// enable the legacy position mapping so existing flows keep working unchanged.
// Newly paired devices get the store flag from the pair view and follow the KNX standard.
if (this.getStoreValue('windowcoverings_set_migrated') !== true) {
try {
// Only devices with a position groupaddress have legacy position
// behaviour to preserve; the rest just adopt the KNX standard mapping.
if (this.settings.ga_height) {
await this.setSettings({ invert_position: true });
}
await this.setStoreValue('windowcoverings_set_migrated', true);
} catch (err) {
// Flag not set, so the migration is retried on the next init.
this.error('invert_position migration failed', err);
}
}
this.registerCapabilityListener('windowcoverings_state', this.onCapabilityWindowCovering.bind(this));
if (this.settings.ga_height) {
await this.addCapabilityIfNotExists('windowcoverings_set');
this.registerCapabilityListener('windowcoverings_set', this.onCapabilityWindowCoveringSet.bind(this));
} else {
await this.removeCapabilityIfExists('windowcoverings_set');
}
super.onInit();
}
async onKNXEvent(groupaddress, data) {
super.onKNXEvent(groupaddress, data);
if (groupaddress === this.settings.ga_status) {
const state = DatapointTypeParser.bitFormat(data);
if (state) {
if (this.settings.invert_updown === true) {
await this.setCapabilityValue('windowcoverings_state', 'up')
.catch((knxerror) => {
this.error('Set windowcoverings_state error', knxerror);
});
} else {
await this.setCapabilityValue('windowcoverings_state', 'down')
.catch((knxerror) => {
this.error('Set windowcoverings_state error', knxerror);
});
}
} else if (this.settings.invert_updown === true) {
await this.setCapabilityValue('windowcoverings_state', 'down')
.catch((knxerror) => {
this.error('Set windowcoverings_state error', knxerror);
});
} else {
await this.setCapabilityValue('windowcoverings_state', 'up')
.catch((knxerror) => {
this.error('Set windowcoverings_state error', knxerror);
});
}
this.log(state);
}
if (groupaddress === this.getStatusAddress('ga_height')) {
// KNX DPT 5.001 for shutter position: 0% = fully open (up), 100% = fully closed (down).
// Homey windowcoverings_set: 1.0 = open, 0.0 = closed. Invert to match,
// unless invert_position keeps the legacy mapping for this device.
const position = DatapointTypeParser.dim(data);
await this.setCapabilityValue('windowcoverings_set', this.settings.invert_position === true ? position : 1 - position)
.catch((knxerror) => {
this.error('Set windowcoverings_set error', knxerror);
});
}
}
onKNXConnection(connectionStatus) {
super.onKNXConnection(connectionStatus);
if (connectionStatus !== 'connected') {
return;
}
// Reading the groupaddress will trigger an event on the bus.
// This will be catched by onKNXEvent, hence the return value is not used.
this.getWindowCoveringHeight();
}
onCapabilityWindowCovering(value) {
this.log(value);
if (this.knxInterface) {
switch (value) {
case 'up':
if (this.settings.invert_updown === true) {
return this.knxInterface.writeKNXGroupAddress(this.settings.ga_up_down, 1, 'DPT1')
.catch((knxerror) => {
throw new Error(this.homey.__('errors.windowcovering_failed'), knxerror);
});
}
return this.knxInterface.writeKNXGroupAddress(this.settings.ga_up_down, 0, 'DPT1')
.catch((knxerror) => {
throw new Error(this.homey.__('errors.windowcovering_failed'), knxerror);
});
case 'down':
if (this.settings.invert_updown === true) {
return this.knxInterface.writeKNXGroupAddress(this.settings.ga_up_down, 0, 'DPT1')
.catch((knxerror) => {
throw new Error(this.homey.__('errors.windowcovering_failed'), knxerror);
});
}
return this.knxInterface.writeKNXGroupAddress(this.settings.ga_up_down, 1, 'DPT1')
.catch((knxerror) => {
throw new Error(this.homey.__('errors.windowcovering_failed'), knxerror);
});
case 'idle':
this.log('stop selected, stop address', this.settings.ga_stop);
if (this.settings.ga_stop) {
return this.knxInterface.writeKNXGroupAddress(this.settings.ga_stop, 1, 'DPT1')
.catch((knxerror) => {
throw new Error(this.homey.__('errors.windowcovering_failed'), knxerror);
});
}
break;
default:
throw new Error(this.homey.__('errors.windowcovering_failed'));
}
}
return null;
}
getWindowCoveringHeight() {
if (!this.knxInterface) {
return;
}
const statusAddress = this.getStatusAddress('ga_height');
if (!statusAddress) {
return;
}
this.knxInterface.readKNXGroupAddress(statusAddress)
.catch(this.error);
}
onCapabilityWindowCoveringSet(value) {
if (!this.knxInterface || !this.settings.ga_height) {
return null;
}
// KNX DPT 5.001 for shutter position: 0% = fully open (up), 100% = fully closed (down).
// Homey windowcoverings_set: 1.0 = open, 0.0 = closed. Invert to match,
// unless invert_position keeps the legacy mapping for this device.
const position = this.settings.invert_position === true ? value : 1 - value;
return this.knxInterface.writeKNXGroupAddress(this.settings.ga_height, position * 255, 'DPT5')
.catch((knxerror) => {
throw new Error(this.homey.__('errors.windowcovering_failed'), knxerror);
});
}
async onSettings({ oldSettings, newSettings, changedKeys }) {
if (changedKeys.includes('ga_height')) {
if (typeof newSettings.ga_height === 'string' && newSettings.ga_height !== '') {
await this.addCapabilityIfNotExists('windowcoverings_set');
this.registerCapabilityListener('windowcoverings_set', this.onCapabilityWindowCoveringSet.bind(this));
} else {
await this.removeCapabilityIfExists('windowcoverings_set');
}
}
await super.onSettings({ oldSettings, newSettings, changedKeys });
if (changedKeys.includes('invert_position')) {
// The stored capability value was written under the old mapping;
// flip it so the slider keeps showing the same physical position.
const position = this.getCapabilityValue('windowcoverings_set');
if (typeof position === 'number') {
await this.setCapabilityValue('windowcoverings_set', 1 - position)
.catch((knxerror) => {
this.error('Set windowcoverings_set error', knxerror);
});
}
}
}
}
module.exports = KNXWindowCovering;