-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
32906cd
commit 3be6377
Showing
21 changed files
with
857 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
'use strict'; | ||
|
||
const Homey = require('homey'); | ||
const miio = require('miio'); | ||
|
||
class MiAirMonitorDevice extends Homey.Device { | ||
|
||
onInit() { | ||
this.createDevice(); | ||
|
||
this.registerCapabilityListener('onoff', this.onCapabilityOnoff.bind(this)); | ||
} | ||
|
||
onDeleted() { | ||
clearInterval(this.pollingInterval); | ||
this.miio.destroy(); | ||
} | ||
|
||
// LISTENERS FOR UPDATING CAPABILITIES | ||
onCapabilityOnoff(value, opts, callback) { | ||
this.miio.setPower(value) | ||
.then(result => { callback(null, value) }) | ||
.catch(error => { callback(error, false) }); | ||
} | ||
|
||
// HELPER FUNCTIONS | ||
createDevice() { | ||
miio.device({ | ||
address: this.getSetting('address'), | ||
token: this.getSetting('token') | ||
}).then(miiodevice => { | ||
this.miio = miiodevice; | ||
|
||
var interval = this.getSetting('polling') || 60; | ||
this.pollDevice(interval); | ||
}).catch(function (error) { | ||
return reject(error); | ||
}); | ||
} | ||
|
||
pollDevice(interval) { | ||
clearInterval(this.pollingInterval); | ||
|
||
this.pollingInterval = setInterval(() => { | ||
const getData = async () => { | ||
try { | ||
const power = await this.miio.power(); | ||
const battery = await this.miio.batteryLevel(); | ||
const aqi = await this.miio.pm2_5(); | ||
|
||
if (this.getCapabilityValue('onoff') != power) { | ||
this.setCapabilityValue('onoff', power); | ||
} | ||
if (this.getCapabilityValue('measure_battery') != battery) { | ||
this.setCapabilityValue('measure_battery', battery); | ||
} | ||
if (this.getCapabilityValue('measure_pm25') != aqi) { | ||
this.setCapabilityValue('measure_pm25', aqi); | ||
} | ||
if (!this.getAvailable()) { | ||
this.setAvailable(); | ||
} | ||
} catch (error) { | ||
this.setUnavailable(Homey.__('unreachable')); | ||
this.log(error); | ||
} | ||
} | ||
getData(); | ||
}, 1000 * interval); | ||
} | ||
} | ||
|
||
module.exports = MiAirMonitorDevice; |
Oops, something went wrong.