-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
161 lines (137 loc) · 4.85 KB
/
index.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
161
'use strict';
const URTSI = require('urtsi');
module.exports = homebridge => {
const Accessory = homebridge.platformAccessory;
const Characteristic = homebridge.hap.Characteristic;
const Service = homebridge.hap.Service;
const UUIDGen = homebridge.hap.uuid;
/**
* Platform "Somfy URTSI"
*/
class URTSIPlatform {
constructor(log, config) {
this.log = log;
const channelConfigs = config.channels;
if (!Array.isArray(channelConfigs)) {
this.log('Bad `config.channels` value, must be an array.');
this.channelConfigs = [];
} else {
this.channelConfigs = channelConfigs;
}
this.urtsi = new URTSI(config.serialPath);
}
accessories(callback) {
const accessories = this.channelConfigs.map((channelConfig, index) => {
if (channelConfig === null) {
return null;
}
if (typeof channelConfig !== 'object') {
this.log(`Bad channel at index ${index}, must be an object or null.`);
return null;
}
const name = channelConfig.name;
if (typeof name !== 'string') {
this.log(`Bad channel name at index ${index}, must be a string.`);
return null;
}
const orientation = channelConfig.orientation || {
closed: 'down',
middle: 'stop',
opened: 'up'
};
if (typeof orientation !== 'object' ||
['closed', 'middle', 'opened'].some(
state => ['down', 'stop', 'up'].indexOf(orientation[state]) < 0
)) {
this.log(`Bad channel orientation at index ${index}.`);
return null;
}
return new URTSIChannelAccessory(this, index + 1, {name, orientation});
});
callback(accessories.filter(accessory => accessory !== null));
}
}
/**
* Accessory "Somfy URTSI Channel"
*/
class URTSIChannelAccessory extends Accessory {
constructor(platform, channelNumber, channelConfig) {
const displayName = `Somfy ${channelConfig.name}`;
const uuid = UUIDGen.generate(`urtsi.channel.${channelNumber}`);
super(displayName, uuid);
// Homebridge reqiures these.
this.name = displayName;
this.uuid_base = uuid;
this.log = platform.log;
this.urtsi = platform.urtsi;
this.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, 'Somfy')
.setCharacteristic(Characteristic.Model, 'Universal RTS Interface II');
this.addService(
this.createWindowCoveringService(channelNumber, channelConfig)
);
}
createWindowCoveringService(channelNumber, channelConfig) {
const name = channelConfig.name;
const orientation = channelConfig.orientation;
const service = new Service.WindowCovering(name);
const currentPosition =
service.getCharacteristic(Characteristic.CurrentPosition);
const positionState =
service.getCharacteristic(Characteristic.PositionState);
const targetPosition =
service.getCharacteristic(Characteristic.TargetPosition);
targetPosition.on('set', (targetValue, callback) => {
const logError = error => {
this.log(
'Encountered an error setting target position of %s: %s',
`channel ${channelNumber} (${name})`,
error.message
);
};
currentPosition.getValue((error, currentValue) => {
if (error) {
logError(error);
callback(error);
return;
}
this.log(
'Setting target position of %s from %s to %s.',
`channel ${channelNumber} (${name})`,
`${currentValue}%`,
`${targetValue}%`
);
positionState.setValue(
targetValue < currentValue
? Characteristic.PositionState.DECREASING
: targetValue > currentValue
? Characteristic.PositionState.INCREASING
: Characteristic.PositionState.STOPPED
);
callback();
const channel = this.urtsi.getChannel(channelNumber);
const promise =
targetValue === 0
? channel[orientation.closed]()
: targetValue === 100
? channel[orientation.opened]()
: channel[orientation.middle]();
promise.then(
() => {
currentPosition.setValue(targetValue);
positionState.setValue(Characteristic.PositionState.STOPPED);
},
logError
);
});
});
// Set a more sane default value for the current position.
currentPosition.setValue(currentPosition.getDefaultValue());
return service;
}
getServices() {
return this.services;
}
}
homebridge.registerPlatform('homebridge-urtsi', 'Somfy URTSI', URTSIPlatform);
};