-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathindex.js
162 lines (134 loc) · 5.94 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
158
159
160
161
162
const TadoApi = require('./tado/api')
const syncHomeKitCache = require('./tado/syncHomeKitCache')
const refreshState = require('./tado/refreshState')
const path = require('path')
const storage = require('node-persist')
const PLUGIN_NAME = 'homebridge-tado-ac'
const PLATFORM_NAME = 'TadoAC'
module.exports = (api) => {
api.registerPlatform(PLUGIN_NAME, PLATFORM_NAME, TadoACPlatform)
}
class TadoACPlatform {
constructor(log, config, api) {
this.cachedAccessories = []
this.activeAccessories = []
this.log = log
this.api = api
this.storage = storage
this.refreshState = refreshState(this)
this.syncHomeKitCache = syncHomeKitCache(this)
this.name = PLATFORM_NAME
this.disableFan = config['disableFan'] || false
this.disableDry = config['disableDry'] || false
this.enableHistoryStorage = config['historyStorage'] || false
this.debug = config['debug'] || false
this.PLUGIN_NAME = PLUGIN_NAME
this.PLATFORM_NAME = PLATFORM_NAME
// ~~~~~~~~~~~~~~~~~~~~~ tado° Specials ~~~~~~~~~~~~~~~~~~~~~ //
this.username = config['username']
this.password = config['password']
if (!this.username || !this.password) {
this.log('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -- ERROR -- XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n')
this.log('Can\'t start homebridge-tado-ac plugin without username and password !!\n')
this.log('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n')
return
}
this.homeId = config['homeID'] || false
this.tadoMode = config['tadoMode'] || 'MANUAL'
this.durationInMinutes = config['durationInMinutes'] || 90
this.weatherSensorsEnabled = config['weatherSensorsEnabled'] || false
this.weatherPollingInterval = !isNaN(config['weatherPollingInterval']) ? (config['weatherPollingInterval'] * 60 * 1000) : 300000 // default is 5 minutes
if (this.weatherPollingInterval < 30000) this.weatherPollingInterval = 60000 // minimum 1 minute to not overload
this.occupancySensorsEnabled = config['occupancySensorsEnabled'] || false
this.occupancyPollingInterval = !isNaN(config['occupancyPollingInterval']) ? (config['occupancyPollingInterval'] * 1000) : 10000 // default is 10 seconds
if (this.occupancyPollingInterval < 3000) this.occupancyPollingInterval = 3000 // minimum 3 seconds to not overload
this.anyoneSensor = config['anyoneSensor'] === false ? false : true
this.extraHumiditySensor = config['extraHumiditySensor'] || false
this.manualControlSwitch = config['manualControl'] || config['manualControlSwitch'] || false
this.extraHumiditySensor = config['extraHumiditySensor'] || false
this.forceThermostat = config['forceThermostat'] || false
this.forceHeaterCooler = config['forceHeaterCooler'] || false //new
this.disableAcAccessory = config['disableAcAccessory'] || false //new
this.persistPath = path.join(this.api.user.persistPath(), '/../tado-persist')
this.emptyState = {devices:{}, weather:{} ,occupancy: {}}
this.CELSIUS_UNIT = 'CELSIUS'
this.FAHRENHEIT_UNIT = 'FAHRENHEIT'
const requestedInterval = config['statePollingInterval'] === 0 ? 0 : (config['statePollingInterval'] || 30) // default polling time is 30 seconds
this.refreshDelay = 2000
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //
this.setProcessing = false
this.pollingTimeout = null
this.processingState = false
this.refreshTimeout = null
this.pollingInterval = requestedInterval ? (requestedInterval * 1000 - this.refreshDelay) : false
// define debug method to output debug logs when enabled in the config
this.log.easyDebug = (...content) => {
if (this.debug) {
this.log(content.reduce((previous, current) => {
return previous + ' ' + current
}))
} else
this.log.debug(content.reduce((previous, current) => {
return previous + ' ' + current
}))
}
this.api.on('didFinishLaunching', async () => {
await this.storage.init({
dir: this.persistPath,
forgiveParseErrors: true
})
this.cachedState = await this.storage.getItem('state') || this.emptyState
try {
this.tadoApi = await TadoApi(this)
} catch(err) {
this.log.easyDebug(err)
return
}
try {
this.devices = await this.tadoApi.getAllDevices()
await this.storage.setItem('tado-devices', this.devices)
} catch(err) {
this.devices = await this.storage.getItem('tado-devices') || []
}
if (this.weatherSensorsEnabled) {
try {
this.weather = await this.tadoApi.getWeather()
await this.storage.setItem('weather', this.weather)
setInterval(this.refreshState.weather, this.weatherPollingInterval)
} catch(err) {
this.log('Can\'t get live Weather information -> Searching in storage...')
this.weather = await this.storage.getItem('weather')
if (this.weather) {
this.log('Found weather information in storage - the plugin will keep scanning for weather changes.')
setInterval(this.refreshState.weather, this.weatherPollingInterval)
} else {
this.log('Can\'t get Weather information -> The plugin will not create weather accessories')
}
}
}
if (this.occupancySensorsEnabled) {
try {
this.users = await this.tadoApi.getUsers()
await this.storage.setItem('users', this.users)
setInterval(this.refreshState.occupancy, this.occupancyPollingInterval)
} catch(err) {
this.log('Can\'t get live Users information -> Searching in storage...')
this.users = await this.storage.getItem('users')
if (this.users) {
this.log('Found users in storage - the plugin will keep scanning for those users.')
setInterval(this.refreshState.occupancy, this.occupancyPollingInterval)
} else {
this.log('Can\'t get Users information -> The plugin will not create occupancy accessories')
this.users = []
}
}
}
if (this.pollingInterval)
this.pollingTimeout = setTimeout(this.refreshState.ac, this.pollingInterval)
this.syncHomeKitCache()
})
}
configureAccessory(accessory) {
this.cachedAccessories.push(accessory)
}
}