forked from ActiveByte/homebridge-loxone-connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
157 lines (130 loc) · 5.75 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
let Accessory, Service, Characteristic, UUIDGen;
const request = require("request");
const ItemFactory = require('./libs/ItemFactory.js');
const Utility = require('./libs/Utility.js');
const WSListener = require('./libs/WSListener.js');
module.exports = homebridge => {
console.log(`homebridge API version: ${homebridge.version}`);
// Accessory must be created from PlatformAccessory Constructor
Accessory = homebridge.platformAccessory;
// Keep refference to the passes API object
Homebridge = homebridge;
//Add inheritance of the AbstractItem to the Accessory object
Utility.addSupportTo(ItemFactory.AbstractItem, Accessory);
//All other items are child of the abstractItem
Utility.addSupportTo(ItemFactory.LightControllerV2MoodSwitch, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.Trigger, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.TemperatureSensor, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.HumiditySensor, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.MotionSensor, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.ContactSensor, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.LightSensor, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.LeakSensor, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.Dimmer, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.Colorpicker, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.Gate, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.Doorbell, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.Jalousie, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.TimedSwitch, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.Switch, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.SmokeSensor, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.Alarm, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.Lock, ItemFactory.AbstractItem);
Utility.addSupportTo(ItemFactory.Valve, ItemFactory.AbstractItem);
//Add childs of switch
Utility.addSupportTo(ItemFactory.Lightbulb, ItemFactory.Switch);
Utility.addSupportTo(ItemFactory.Outlet, ItemFactory.Switch);
Utility.addSupportTo(ItemFactory.Pushbutton, ItemFactory.Switch);
Utility.addSupportTo(ItemFactory.Fan, ItemFactory.Switch);
//Add childs of shower
Utility.addSupportTo(ItemFactory.Sprinkler, ItemFactory.Valve);
//Pieter EIBBlinds
Utility.addSupportTo(ItemFactory.EIBBlinds, ItemFactory.AbstractItem);
homebridge.registerPlatform("homebridge-loxoneWs", "LoxoneWs", LoxPlatform);
};
// Platform constructor
function LoxPlatform(log, config) {
const platform = this;
this.log = log;
this.config = config;
this.protocol = "http";
if (!this.config['host']) throw new Error("Configuration missing: loxone host (please provide the IP address here)");
if (!this.config['username']) throw new Error("Configuration missing: loxone username");
if (!this.config['password']) throw new Error("Configuration missing: loxone password");
if (!this.config['port']) this.config['port'] = 80;
this.host = config["host"];
this.port = config["port"];
this.username = config["username"];
this.password = config["password"];
//* Options *//
if (!config['options']) {
config['options'] = "";
}
const options = config['options'];
this.rooms = [];
if (options['rooms']) {
this.rooms = options["rooms"];
}
this.moodSwitches = 'none';
if (options['moodSwitches']) {
this.moodSwitches = options["moodSwitches"];
}
this.timedswitch_method = "pulse";
if (options['StairwellSwitch'] == "on") {
this.timedswitch_method = "on";
}
this.alarmsystem_method = "delayedon";
if (options['alarmSystem'] == "on") {
this.alarmsystem_method = "on";
}
this.alarmsystem_trigger = 5;
if (options['alarmTrigerLevel']) {
if (options['alarmTrigerLevel'] > 0 && options['alarmTrigerLevel'] < 7) {
this.alarmsystem_trigger = options['alarmTrigerLevel'];
} else {
this.log("Info: alarmTrigerLevel must be an integer between 1 and 6");
}
}
this.autoLock = 1;
if (options['autoLock']) {
this.autoLock = options['autoLock'];
}
this.autoLockDelay = 5;
if (options['autoLockDelay']) {
this.autoLockDelay = options['autoLockDelay'];
}
//* Alias *//
if (!config['alias']) {
config['alias'] = "";
}
const alias = config['alias'];
let aliases = ['Outlet', 'Lighting', 'Doorbell', 'Trigger', 'Contact', 'Motion', 'Brightness', 'Temperature', 'Humidity', 'Lock', 'Valve', 'Sprinklers', 'Fan', 'Leak'];
aliases.forEach(function (item) {
if (!alias[item]) {
alias[item] = item;
}
})
this.alias = alias;
//Also make a WS connection
this.ws = new WSListener(platform);
}
LoxPlatform.prototype.accessories = function (callback) {
const that = this;
this.log("Getting Loxone configuration.");
const itemFactory = new ItemFactory.Factory(this, Homebridge);
const url = itemFactory.sitemapUrl();
this.log("Platform - Waiting 8 seconds until initial state is retrieved via WebSocket.");
setTimeout(() => {
that.log(`Platform - Retrieving initial config from ${url}`);
request.get({
url,
json: true
}, (err, response, json) => {
if (!err && response.statusCode === 200) {
callback(itemFactory.parseSitemap(json));
} else {
that.log("Platform - There was a problem connecting to Loxone.");
}
})
}, 8000);
};