-
Notifications
You must be signed in to change notification settings - Fork 0
/
mystrom-switch-adapter.js
154 lines (133 loc) · 4.06 KB
/
mystrom-switch-adapter.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
'use strict';
const {
Adapter,
Device,
Property,
} = require('gateway-addon');
const fetch = require('node-fetch');
const config = require('./config');
const manifest = require('./manifest.json');
const DEVICE_NAME = 'MyStrom Switch';
const REPORT_MAP = {
on: 'relay',
power: 'power',
temperature: 'temperature',
};
class SwitchProperty extends Property {
constructor(device, name, propertyDescription, pollIntervalMS) {
super(device, name, propertyDescription);
this.setCachedValue(propertyDescription.value);
this.device.notifyPropertyChanged(this);
setInterval(() => this.updateProperty(), pollIntervalMS);
this.updateProperty();
}
async setValue(value) {
switch (this.name) {
case 'on': {
await this.device.adapter.sendValue(
this.device.ip,
value
);
break;
}
default:
break;
}
this.setCachedValueAndNotify(value);
setTimeout(() => this.updateProperty(), 2000);
return this.value;
}
async updateProperty() {
const uri = `http://${this.device.ip}/report`;
try {
const response = await fetch(uri);
const data = await response.json();
const resultProperty = REPORT_MAP[this.name];
const reportedValue = data[resultProperty];
if (reportedValue !== this.value) {
this.setCachedValueAndNotify(reportedValue);
}
} catch (error) {
console.error(error);
}
}
}
class MyStromSwitchDevice extends Device {
constructor(adapter, id, deviceDescription) {
super(adapter, id);
this.ip = deviceDescription.ip;
this.title = deviceDescription.title;
this.type = deviceDescription.type;
this['@type'] = deviceDescription['@type'];
this.description = deviceDescription.description;
for (const propertyName in deviceDescription.properties) {
const propertyDescription = deviceDescription.properties[propertyName];
const property = new SwitchProperty(this, propertyName, propertyDescription, deviceDescription.pollIntervalMS);
this.properties.set(propertyName, property);
}
}
}
class MyStromSwitchAdapter extends Adapter {
constructor(addonManager) {
super(addonManager, 'MyStromSwitchAdapter', manifest.id);
addonManager.addAdapter(this);
this.createSwitches(manifest.id);
}
async createSwitches(manifestId) {
const {
devices = [],
pollIntervalMS = 15 * 1000,
} = await config.load(manifestId);
for (const deviceConfig of devices) {
const device = new MyStromSwitchDevice(this, `mystrom-switch-${deviceConfig.ip}`, {
ip: deviceConfig.ip,
title: `${DEVICE_NAME} - ${deviceConfig.ip}`,
'@type': ['OnOffSwitch', 'SmartPlug'],
description: `${DEVICE_NAME} - ${deviceConfig.ip}`,
properties: {
on: {
'@type': 'OnOffProperty',
title: 'On/Off',
type: 'boolean',
value: false,
},
power: {
'@type': 'InstantaneousPowerProperty',
title: 'Power',
type: 'number',
value: 0,
},
},
pollIntervalMS,
});
this.handleDeviceAdded(device);
const temperatureDevice = new MyStromSwitchDevice(this, `mystrom-switch-temperature-${deviceConfig.ip}`, {
ip: deviceConfig.ip,
title: `${DEVICE_NAME} - Temperature - ${deviceConfig.ip}`,
'@type': ['TemperatureSensor'],
description: `${DEVICE_NAME} - Temperature - ${deviceConfig.ip}`,
properties: {
temperature: {
'@type': 'TemperatureProperty',
title: 'Temperature',
type: 'number',
unit: 'degree celsius',
value: -1,
},
},
pollIntervalMS,
});
this.handleDeviceAdded(temperatureDevice);
}
}
async sendValue(deviceIp, state) {
const booleanState = state ? 1 : 0;
const uri = `http://${deviceIp}/relay?state=${booleanState}`;
try {
await fetch(uri);
} catch (error) {
console.error(error);
}
}
}
module.exports = MyStromSwitchAdapter;