-
Notifications
You must be signed in to change notification settings - Fork 1
/
uc_entity_climate.js
executable file
·136 lines (127 loc) · 6.45 KB
/
uc_entity_climate.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
'use strict';
const SCALE_DEFAULT = 255;
let eventListeners = [];
async function exposeHomeyDevice(uc, homeyDevice) {
const deviceName = new Map([['en', `${homeyDevice.zoneName}, ${homeyDevice.name}`]]);
const deviceFeatures = await ucFeaturesFromHomeyDevice(uc, homeyDevice);
const deviceStates = await ucStatesFromHomeyDevice(uc, homeyDevice);
const ucEntityId = `climate|${homeyDevice.id}`;
const deviceClass = undefined;
const deviceOptions = null;
const deviceArea = homeyDevice.zoneName;
const ucEntity = new uc.Entities.Climate(ucEntityId, deviceName, deviceFeatures, deviceStates, deviceClass, deviceOptions, deviceArea);
uc.availableEntities.addEntity(ucEntity);
}
async function subscribeHomeyDevice(uc, homeyDevice, ucEntityId) {
if (!eventListeners.includes(ucEntityId)) {
if (homeyDevice.capabilities.includes('onoff')) {
homeyDevice.makeCapabilityInstance('onoff', (value) => {
let newState = new Map([]);
newState.set([uc.Entities.Climate.ATTRIBUTES.STATE], value ? uc.Entities.Climate.STATES.ON : uc.Entities.Climate.STATES.OFF);
uc.configuredEntities.updateEntityAttributes(ucEntityId, newState);
});
}
if (homeyDevice.capabilities.includes('fan_speed')) {
homeyDevice.makeCapabilityInstance('fan_speed', (value) => {
let newState = new Map([]);
newState.set([uc.Entities.Climate.ATTRIBUTES.FAN], value);
uc.configuredEntities.updateEntityAttributes(ucEntityId, newState);
});
}
if (homeyDevice.capabilities.includes('target_temperature')) {
homeyDevice.makeCapabilityInstance('target_temperature', (value) => {
let newState = new Map([]);
newState.set([uc.Entities.Climate.ATTRIBUTES.TARGET_TEMPERATURE], uctemp(value));
uc.configuredEntities.updateEntityAttributes(ucEntityId, newState);
});
}
if (homeyDevice.capabilities.includes('measure_temperature')) {
homeyDevice.makeCapabilityInstance('measure_temperature', (value) => {
let newState = new Map([]);
newState.set([uc.Entities.Climate.ATTRIBUTES.CURRENT_TEMPERATURE], value);
uc.configuredEntities.updateEntityAttributes(ucEntityId, newState);
});
}
}
eventListeners.push(ucEntityId);
}
async function commandHomeyDevice(uc, homeyDevice, wsHandle, ucEntityId, ucEntityType, cmdId, params) {
if (cmdId == 'on') {
await homeyDevice
.setCapabilityValue('onoff', true)
.then((r) => {
uc.acknowledgeCommand(wsHandle);
})
.catch((e) => {
uc.acknowledgeCommand(wsHandle, uc.STATUS_CODES.SERVER_ERROR);
});
}
if (cmdId == 'off') {
homeyDevice
.setCapabilityValue('onoff', false)
.then((r) => {
uc.acknowledgeCommand(wsHandle);
})
.catch((e) => {
uc.acknowledgeCommand(wsHandle, uc.STATUS_CODES.SERVER_ERROR);
});
}
if (cmdId == 'target_temperature') {
homeyDevice
.setCapabilityValue('target_temperature', params.temperature)
.then((r) => {
uc.acknowledgeCommand(wsHandle);
})
.catch((e) => {
uc.acknowledgeCommand(wsHandle, uc.STATUS_CODES.SERVER_ERROR);
});
}
}
async function ucFeaturesFromHomeyDevice(uc, homeyDevice) {
let deviceFeatures = [];
if (homeyDevice.capabilities.includes('onoff')) deviceFeatures.push(uc.Entities.Climate.FEATURES.ON_OFF);
if (homeyDevice.capabilities.includes('fan_speed')) deviceFeatures.push(uc.Entities.Climate.FEATURES.FAN);
if (homeyDevice.capabilities.includes('target_temperature')) {
deviceFeatures.push(uc.Entities.Climate.FEATURES.TARGET_TEMPERATURE);
deviceFeatures.push(uc.Entities.Climate.FEATURES.TARGET_TEMPERATURE_RANGE);
}
if (homeyDevice.capabilities.includes('measure_temperature')) deviceFeatures.push(uc.Entities.Climate.FEATURES.CURRENT_TEMPERATURE);
return deviceFeatures;
}
async function ucStatesFromHomeyDevice(uc, homeyDevice) {
let deviceStates = new Map([]);
if (homeyDevice.capabilities.includes('onoff') && typeof homeyDevice?.capabilitiesObj?.onoff?.value == 'boolean') {
deviceStates.set([uc.Entities.Climate.ATTRIBUTES.STATE], homeyDevice.capabilitiesObj.onoff.value ? uc.Entities.Climate.STATES.AUTO : uc.Entities.Climate.STATES.OFF);
}
if (homeyDevice.capabilities.includes('fan_speed') && typeof homeyDevice?.capabilitiesObj?.fan_speed?.value == 'number') {
deviceStates.set([uc.Entities.Climate.ATTRIBUTES.FAN], parseInt(homeyDevice.capabilitiesObj.fan_speed.value * SCALE_DEFAULT));
}
if (homeyDevice.capabilities.includes('target_temperature') && typeof homeyDevice?.capabilitiesObj?.target_temperature?.value == 'number') {
deviceStates.set([uc.Entities.Climate.ATTRIBUTES.TARGET_TEMPERATURE], uctemp(homeyDevice.capabilitiesObj.target_temperature.value));
}
if (homeyDevice.capabilities.includes('target_temperature') && typeof homeyDevice?.capabilitiesObj?.target_temperature?.max == 'number') {
deviceStates.set([uc.Entities.Climate.ATTRIBUTES.TARGET_TEMPERATURE_HIGH], homeyDevice.capabilitiesObj.target_temperature.max);
deviceStates.set([uc.Entities.Climate.OPTIONS.MAX_TEMPERATURE], homeyDevice.capabilitiesObj.target_temperature.max);
}
if (homeyDevice.capabilities.includes('target_temperature') && typeof homeyDevice?.capabilitiesObj?.target_temperature?.min == 'number') {
deviceStates.set([uc.Entities.Climate.ATTRIBUTES.TARGET_TEMPERATURE_LOW], homeyDevice.capabilitiesObj.target_temperature.min);
deviceStates.set([uc.Entities.Climate.OPTIONS.MIN_TEMPERATURE], homeyDevice.capabilitiesObj.target_temperature.min);
}
if (homeyDevice.capabilities.includes('measure_temperature') && typeof homeyDevice?.capabilitiesObj?.measure_temperature?.value == 'number') {
deviceStates.set([uc.Entities.Climate.ATTRIBUTES.CURRENT_TEMPERATURE], homeyDevice.capabilitiesObj.measure_temperature.value);
}
deviceStates.set([uc.Entities.Climate.ATTRIBUTES.STATE], uc.Entities.Climate.STATES.AUTO);
return deviceStates;
}
function uctemp(temperature) {
let newTemperature = parseInt(temperature);
let digit = temperature - newTemperature;
if (digit >= 0.25 && digit <= 0.75) newTemperature = newTemperature + 0.5;
if (digit > 0.75) newTemperature = newTemperature + 1;
if (digit <= -0.25 && digit >= -0.75) newTemperature = newTemperature - 0.5;
if (digit < -0.75) newTemperature = newTemperature - 1;
return newTemperature;
}
module.exports.exposeHomeyDevice = exposeHomeyDevice;
module.exports.subscribeHomeyDevice = subscribeHomeyDevice;
module.exports.commandHomeyDevice = commandHomeyDevice;