This repository has been archived by the owner on Apr 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
neeo-adapter.js
94 lines (78 loc) · 2.41 KB
/
neeo-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
'use strict';
const {
Adapter,
Device,
Property,
} = require('gateway-addon');
const fetch = require('node-fetch');
const config = require('./config');
const manifest = require('./manifest.json');
class NeeoProperty extends Property {
constructor(device, name, propertyDescription) {
super(device, name, propertyDescription);
this.setCachedValue(propertyDescription.value);
this.device.notifyPropertyChanged(this);
}
}
class NeeoDevice extends Device {
constructor(adapter, id, deviceDescription) {
super(adapter, id);
this.title = deviceDescription.title;
this.type = deviceDescription.type;
this['@type'] = deviceDescription['@type'];
this.description = deviceDescription.description;
this.config = deviceDescription.config;
this.neeoIP = deviceDescription.neeoIP;
for (const propertyName in deviceDescription.properties) {
const propertyDescription = deviceDescription.properties[propertyName];
const property = new NeeoProperty(this, propertyName, propertyDescription);
this.properties.set(propertyName, property);
}
this.createActions();
}
createActions() {
const actions = this.config.macros || [];
for (const action of actions) {
this.addAction(action.macro, {
title: action.macro,
});
}
}
async performAction(action) {
const {
macros,
neeoRoomID,
neeoDeviceID,
} = this.config;
action.start();
const macroConfig = macros.find((macroConf) => macroConf.macro === action.name);
const url = `http://${this.neeoIP}:3000/projects/home/rooms/${neeoRoomID}/devices/${neeoDeviceID}/macros/${macroConfig.macroID}/trigger`;
try {
await fetch(url);
} catch (error) {
console.error(error);
}
action.finish();
}
}
class NeeoAdapter extends Adapter {
constructor(addonManager) {
super(addonManager, 'NeeoAdapter', manifest.id);
addonManager.addAdapter(this);
this.init(manifest.id)
}
async init(manifestId) {
const { devices = [], neeoIP } = await config.load(manifestId);
for (const device of devices) {
const neeoDevice = new NeeoDevice(this, device.name, {
'@type': device.type && device.type.replace(', ', ',').split(','),
title: device.title,
description: device.description,
config: device,
neeoIP,
});
this.handleDeviceAdded(neeoDevice);
}
}
}
module.exports = NeeoAdapter;