-
Notifications
You must be signed in to change notification settings - Fork 2
/
me167.js
307 lines (295 loc) · 12.2 KB
/
me167.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
const fz = require('zigbee-herdsman-converters/converters/fromZigbee');
const tz = require('zigbee-herdsman-converters/converters/toZigbee');
const exposes = require('zigbee-herdsman-converters/lib/exposes');
const reporting = require('zigbee-herdsman-converters/lib/reporting');
const extend = require('zigbee-herdsman-converters/lib/extend');
const e = exposes.presets;
const ea = exposes.access;
const tuya = require("zigbee-herdsman-converters/lib/tuya");
const tuyaLocal = {
dataPoints: {
me167Mode: 2,
me167HeatingSetpoint: 4,
me167LocalTemp: 5,
me167ChildLock: 7,
me167Heating: 3,
me167Schedule1: 28,
me167Schedule2: 29,
me167Schedule3: 30,
me167Schedule4: 31,
me167Schedule5: 32,
me167Schedule6: 33,
me167Schedule7: 34,
me167ErrorCode: 35,
me167FrostGuard: 36,
me167AntiScaling: 39,
me167TempCalibration: 47,
},
};
const fzLocal = {
me167_thermostat: {
cluster: 'manuSpecificTuya',
type: ['commandDataResponse', 'commandDataReport'],
convert: (model, msg, publish, options, meta) => {
const result = {};
function weeklySchedule(day, value) {
// byte 0 - Day of Week (0~7 = Wed ~ Tue) ???
// byte 1 - hour ???
// byte 2 - minute ???
// byte 3 - Temp (temp = value )
// byte 4 - Temperature (temp = value / 10)
const weekDays=[ 'wed', 'thu', 'fri', 'sat', 'sun','mon', 'tue'];
// we get supplied in value only a weekday schedule, so we must add it to
// the weekly schedule from meta.state, if it exists
const weeklySchedule= meta.state.hasOwnProperty('weekly_schedule') ? meta.state.weekly_schedule : {};
meta.logger.info(JSON.stringify({'received day': day, 'received values': value}));
let daySchedule = []; // result array
for (let i=1; i<16 && value[i]; ++i) {
const aHour=value[i];
++i;
const aMinute=value[i];
++i;
const aTemp2=value[i];
++i;
const aTemp=value[i];
daySchedule=[...daySchedule, {
temperature: Math.floor((aTemp+aTemp2*256)/10),
hour: aHour,
minute: aMinute,
}];
}
meta.logger.info(JSON.stringify({'returned weekly schedule: ': daySchedule}));
return {'weekly-schedule': {...weeklySchedule, [weekDays[day]]: daySchedule}};
}
for (const dpValue of msg.data.dpValues) {
const value = tuya.getDataValue(dpValue);
switch (dpValue.dp) {
case tuyaLocal.dataPoints.me167ChildLock:
result.child_lock = value ? 'LOCK' : 'UNLOCK';
break;
case tuyaLocal.dataPoints.me167HeatingSetpoint:
result.current_heating_setpoint = value/10;
break;
case tuyaLocal.dataPoints.me167LocalTemp:
result.local_temperature = value/10;
break;
case tuyaLocal.dataPoints.me167Heating:
switch(value) {
case 0:
result.running_state = "heat"; // valve open
break;
case 1:
result.running_state = "idle"; // valve closed
break;
default:
meta.logger.warn('zigbee-herdsman-converters:me167_thermostat: ' +
`Running state ${value} is not recognized.`);
break;
}
break;
case tuyaLocal.dataPoints.me167Mode:
switch (value) {
case 0: // auto
result.system_mode = 'auto';
break;
case 1: // manu
result.system_mode = 'heat';
break;
case 2: // off
result.system_mode = 'off';
break;
default:
meta.logger.warn('zigbee-herdsman-converters:me167_thermostat: ' +
`Mode ${value} is not recognized.`);
break;
}
break;
case tuyaLocal.dataPoints.me167Schedule1:
weeklySchedule(0,value);
break;
case tuyaLocal.dataPoints.me167Schedule2:
weeklySchedule(1,value);
break;
case tuyaLocal.dataPoints.me167Schedule3:
weeklySchedule(2,value);
break;
case tuyaLocal.dataPoints.me167Schedule4:
weeklySchedule(3,value);
break;
case tuyaLocal.dataPoints.me167Schedule5:
weeklySchedule(4,value);
break;
case tuyaLocal.dataPoints.me167Schedule6:
weeklySchedule(5,value);
break;
case tuyaLocal.dataPoints.me167Schedule7:
weeklySchedule(6,value);
break;
case tuyaLocal.dataPoints.me167TempCalibration:
if (value >= 4294967295 ){
result.local_temperature_calibration = (value-4294967295)-1 // negative values
}else{
result.local_temperature_calibration = value
}
break;
case tuyaLocal.dataPoints.me167ErrorCode:
switch (value) {
case 0: // OK
result.battery_low = false;
meta.logger.info(`zigbee-herdsman-converters:me167_thermostat: BattOK - Error Code: ` +
`${JSON.stringify(dpValue)}`);
break;
case 1: // Empty Battery
result.battery_low = true;
meta.logger.info(`zigbee-herdsman-converters:me167_thermostat: BattEmtpy - Error Code: ` +
`${JSON.stringify(dpValue)}`);
break;
default:
meta.logger.warn(`zigbee-herdsman-converters:me167_thermostat: Error Code not recognized: ` +
`${JSON.stringify(dpValue)}`);
break;
}
break;
case tuyaLocal.dataPoints.me167FrostGuard:
result.frost_guard = value ? 'ON' : 'OFF';
break;
case tuyaLocal.dataPoints.me167AntiScaling:
result.anti_scaling = value ? 'ON' : 'OFF';
break;
default:
meta.logger.warn(`zigbee-herdsman-converters:me167_thermostat: NOT RECOGNIZED ` +
`DP #${dpValue.dp} with data ${JSON.stringify(dpValue)}`);
}
}
return result;
},
},
};
const tzLocal = {
me167_thermostat_current_heating_setpoint: {
key: ['current_heating_setpoint'],
convertSet: async (entity, key, value, meta) => {
const temp = Math.round(value * 10);
await tuya.sendDataPointValue(entity, tuyaLocal.dataPoints.me167HeatingSetpoint, temp);
},
},
me167_thermostat_system_mode: {
key: ['system_mode'],
convertSet: async (entity, key, value, meta) => {
switch (value) {
case 'off':
await tuya.sendDataPointEnum(entity, tuyaLocal.dataPoints.me167Mode, 2 /* off */);
break;
case 'heat':
await tuya.sendDataPointEnum(entity, tuyaLocal.dataPoints.me167Mode, 1 /* manual */);
break;
case 'auto':
await tuya.sendDataPointEnum(entity, tuyaLocal.dataPoints.me167Mode, 0 /* auto */);
break;
}
},
},
me167_thermostat_child_lock: {
key: ['child_lock'],
convertSet: async (entity, key, value, meta) => {
await tuya.sendDataPointBool(entity, tuyaLocal.dataPoints.me167ChildLock, value === 'LOCK');
},
},
me167_thermostat_schedule: {
key: ['weekly_schedule'],
convertSet: async (entity, key, value, meta) => {
const weekDays=['wed', 'thu', 'fri', 'sat', 'sun', 'mon' , 'tue'];
// we overwirte only the received days. The other ones keep stored on the device
const keys = Object.keys(value);
for (const dayName of keys) { // for loop in order to delete the empty day schedules
const output= []; // empty output byte buffer
const dayNo=weekDays.indexOf(dayName);
output[0]=dayNo+1;
const schedule=value[dayName];
schedule.forEach((el, Index) => {
if (Index <4) {
output[1+4*Index]=el.hour;
output[2+4*Index]=el.minute;
output[3+4*Index]=Math.floor((el.temperature*10)/256);
output[4+4*Index]=(el.temperature*10)%256;
} else {
meta.logger.warn('more than 4 schedule points supplied for week-day '+dayName +
' additional schedule points will be ignored');
}
});
meta.logger.info(`zigbee-herdsman-converters:me167_thermostat: Writing Schedule to ` +
`DP #${tuyaLocal.dataPoints.me167Schedule1+dayNo} with data ${JSON.stringify(output)}`);
await tuya.sendDataPointRaw(entity, tuyaLocal.dataPoints.me167Schedule1+dayNo, output);
await new Promise((r) => setTimeout(r, 2000));
// wait 2 seconds between schedule sends in order not to overload the device
}
},
},
me167_thermostat_calibration: {
key: ['local_temperature_calibration'],
convertSet: async (entity, key, value, meta) => {
if (value >= 0) value = value;
if (value < 0) value = value+4294967295+1;
await tuya.sendDataPointValue(entity, tuyaLocal.dataPoints.me167TempCalibration, value);
},
},
me167_thermostat_anti_scaling: {
key: ['anti_scaling'],
convertSet: async (entity, key, value, meta) => {
await tuya.sendDataPointBool(entity, tuyaLocal.dataPoints.me167AntiScaling, value === 'ON');
},
},
me167_thermostat_frost_guard: {
key: ['frost_guard'],
convertSet: async (entity, key, value, meta) => {
await tuya.sendDataPointBool(entity, tuyaLocal.dataPoints.me167FrostGuard, value === 'ON');
},
},
};
const definition = {
// Since a lot of Tuya devices use the same modelID, but use different data points
// it's usually necessary to provide a fingerprint instead of a zigbeeModel
fingerprint: [
{
// The model ID from: Device with modelID 'TS0601' is not supported
// You may need to add \u0000 at the end of the name in some cases
modelID: 'TS0601',
// The manufacturer name from: Device with modelID 'TS0601' is not supported.
manufacturerName: '_TZE200_bvu2wnxz'
},
],
model: 'ME167',
vendor: 'Avatto',
description: 'Thermostatic radiator valve',
fromZigbee: [
fz.ignore_basic_report, // Add this if you are getting no converter for 'genBasic'
//fz.tuya_data_point_dump, // This is a debug converter, it will be described in the next part
fzLocal.me167_thermostat,
],
toZigbee: [
//tz.tuya_data_point_test, // Another debug converter
tzLocal.me167_thermostat_child_lock,
tzLocal.me167_thermostat_current_heating_setpoint,
tzLocal.me167_thermostat_system_mode,
tzLocal.me167_thermostat_schedule,
tzLocal.me167_thermostat_calibration,
tzLocal.me167_thermostat_anti_scaling,
tzLocal.me167_thermostat_frost_guard,
],
onEvent: tuya.onEventSetTime, // Add this if you are getting no converter for 'commandMcuSyncTime'
configure: async (device, coordinatorEndpoint, logger) => {
const endpoint = device.getEndpoint(1);
await reporting.bind(endpoint, coordinatorEndpoint, ['genBasic']);
},
exposes: [
e.child_lock(),
exposes.switch().withState('anti_scaling', true).withDescription('Anti Scaling feature is ON or OFF'),
exposes.switch().withState('frost_guard', true).withDescription('Frost Protection feature is ON or OFF'),
exposes.climate().withSetpoint('current_heating_setpoint', 5, 35, 1)
.withLocalTemperature()
.withSystemMode(['auto','heat','off'])
.withRunningState(['idle', 'heat'], ea.STATE)
.withLocalTemperatureCalibration(-10, 10, 1, ea.STATE_SET)
],
};
module.exports = definition;