-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
fc48e86
commit 3472ddb
Showing
10 changed files
with
426 additions
and
17 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,87 @@ | ||
'use strict'; | ||
|
||
const Homey = require('homey'); | ||
const util = require('/lib/util.js'); | ||
const miio = require('miio'); | ||
|
||
class PhilipsEyecareDevice extends Homey.Device { | ||
|
||
onInit() { | ||
this.createDevice(); | ||
|
||
this.registerCapabilityListener('onoff', this.onCapabilityOnoff.bind(this)); | ||
this.registerCapabilityListener('dim', this.onCapabilityDim.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) }); | ||
} | ||
|
||
onCapabilityDim(value, opts, callback) { | ||
var brightness = value * 100; | ||
this.miio.setBrightness(brightness) | ||
.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) { | ||
this.log(error); | ||
}); | ||
} | ||
|
||
pollDevice(interval) { | ||
clearInterval(this.pollingInterval); | ||
|
||
this.pollingInterval = setInterval(() => { | ||
const getData = async () => { | ||
try { | ||
const power = await this.miio.power(); | ||
const brightness = await this.miio.brightness() | ||
const mode = await this.miio.mode(); | ||
const eyecare = await this.miio.eyeCareMode(); | ||
|
||
if (this.getCapabilityValue('onoff') != power) { | ||
this.setCapabilityValue('onoff', power); | ||
} | ||
var dim = brightness / 100; | ||
if (this.getCapabilityValue('dim') != dim) { | ||
this.setCapabilityValue('dim', dim); | ||
} | ||
if (this.getStoreValue('mode') != mode) { | ||
this.setStoreValue('mode', mode); | ||
} | ||
if (this.getStoreValue('eyecare') != eyecare) { | ||
this.setStoreValue('eyecare', eyecare); | ||
} | ||
if (!this.getAvailable()) { | ||
this.setAvailable(); | ||
} | ||
} catch (error) { | ||
this.setUnavailable(Homey.__('unreachable')); | ||
this.log(error); | ||
} | ||
} | ||
getData(); | ||
}, 1000 * interval); | ||
} | ||
} | ||
|
||
module.exports = PhilipsEyecareDevice; |
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,42 @@ | ||
"use strict"; | ||
|
||
const Homey = require('homey'); | ||
const miio = require('miio'); | ||
|
||
class PhilipsEyecareDriver extends Homey.Driver { | ||
|
||
onPair(socket) { | ||
socket.on('testConnection', function(data, callback) { | ||
miio.device({ | ||
address: data.address, | ||
token: data.token | ||
}).then(device => { | ||
const getData = async () => { | ||
try { | ||
const power = await device.power(); | ||
const brightness = await device.brightness() | ||
const mode = await device.mode(); | ||
const eyecare = await device.eyeCareMode(); | ||
|
||
let result = { | ||
onoff: power, | ||
brightness: brightness, | ||
mode: mode, | ||
eyecare: eyecare | ||
} | ||
|
||
callback(null, result); | ||
} catch (error) { | ||
callback(error, null); | ||
} | ||
} | ||
getData(); | ||
}).catch(function (error) { | ||
callback(error, null); | ||
}); | ||
}); | ||
} | ||
|
||
} | ||
|
||
module.exports = PhilipsEyecareDriver; |
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,156 @@ | ||
<script type="text/javascript"> | ||
|
||
Homey.setTitle( __('pair.title') ); | ||
|
||
$(function() { | ||
$('#test-connection').click(function() { | ||
$('.mi-device-test').hide(); | ||
$('.mi-device-error').hide(); | ||
$('.mi-device-ok').hide(); | ||
|
||
var inputaddress = $('#address').val(); | ||
var inputtoken = $('#token').val(); | ||
|
||
if (inputaddress != '' && inputtoken != '') { | ||
var device_data = { | ||
address: inputaddress, | ||
token: inputtoken | ||
}; | ||
|
||
Homey.emit('testConnection', device_data, function(error, result) { | ||
if(error) { | ||
$('.mi-device-error').show(); | ||
$('.mi-device-error-msg').html(error.message || error.toString()); | ||
} else if (result) { | ||
$('#mode').val(result.mode); | ||
var powered = ((result.onoff) ? "On" : "Off"); | ||
var eyecare = ((result.eyecare) ? "On" : "Off"); | ||
var mideviceinfo = "<h2 data-i18n='pair.philipseyecare'>Philips Eyecare Lamp</h2><div class='info-row'><span class='info-label' data-i18n='pair.powered'>Powered:</span><span class='info-value'> "+ powered +"</span></div><div class='info-row'><span class='info-label' data-i18n='pair.dim'>Dim Level:</span><span class='info-value'> "+ result.brightness +"</span></div><div class='info-row'><span class='info-label' data-i18n='pair.mode'>Mode:</span><span class='info-value'> "+ result.mode +"</span></div><div class='info-row'><span class='info-label' data-i18n='pair.eyecare'>Eyecare:</span><span class='info-value'> "+ result.eyecare +"</span></div>"; | ||
$('.mi-device-info').show(); | ||
$('.mi-device-info').html(mideviceinfo); | ||
$('.mi-device-test').show(); | ||
$('#connect').prop('disabled', false); | ||
} else { | ||
$('.mi-device-error').show(); | ||
$('.mi-device-error-msg').html( __('pair.unknownerror') ); | ||
} | ||
}); | ||
|
||
} else { | ||
$('.mi-device-error').show(); | ||
$('.mi-device-error-msg').html( __('pair.nosettings') ); | ||
} | ||
|
||
}); | ||
|
||
$('#connect').click(function(){ | ||
var inputaddress = $('#address').val(); | ||
var inputtoken = $('#token').val(); | ||
var inputpolling = Number($('#polling').val()) || 10; | ||
var inputmode = $('#mode').val() || 'reading'; | ||
|
||
if( inputaddress != '' && inputtoken != '' && inputpolling != '') { | ||
var device_data = { | ||
id: inputtoken | ||
}; | ||
var settings = { | ||
address: inputaddress, | ||
token: inputtoken, | ||
polling: inputpolling | ||
}; | ||
var store = { | ||
mode: inputmode, | ||
eyecare: false | ||
}; | ||
|
||
Homey.addDevice({ | ||
name : 'Philips Eyecare Lamp', | ||
data : device_data, | ||
settings : settings, | ||
store : store | ||
}, function(error, result) { | ||
if (error) { | ||
$('.mi-device-test').hide(); | ||
$('.mi-device-error').show(); | ||
$('.mi-device-error-msg').html(error.message || error.toString()); | ||
} | ||
$('.mi-device-test').hide(); | ||
$('.mi-device-ok').show(); | ||
|
||
Homey.done(); | ||
}) | ||
|
||
} else { | ||
$('.mi-device-test').hide(); | ||
$('.mi-device-error').show(); | ||
$('.mi-device-error-msg').html( __('pair.nosettings') ); | ||
} | ||
|
||
}); | ||
}) | ||
</script> | ||
|
||
<style type="text/css"> | ||
.mi-device-pairing { | ||
display: flex; | ||
justify-content: space-between; | ||
flex-wrap: wrap; | ||
} | ||
.mi-device-pairing .col1, .mi-device-pairing .col2 { | ||
width: 48%; | ||
} | ||
.mi-device-status, .mi-device-info { | ||
display: none; | ||
} | ||
.mi-device-info { | ||
font-size: 12px; | ||
} | ||
.mi-device-info .info-row { | ||
min-width: 150px; | ||
padding-bottom: 4px; | ||
font-weight: 700; | ||
} | ||
.mi-device-info .info-label { | ||
display: inline-block; | ||
min-width: 100px; | ||
font-weight: 700; | ||
} | ||
.form-group input[type="text"] { | ||
width: 80% !important; | ||
} | ||
.buttons, .messages { | ||
padding-top: 14px; | ||
} | ||
</style> | ||
|
||
<p data-i18n="pair.intro">Enter the details of your Mi Home device.</p> | ||
<div class="mi-device-pairing"> | ||
<div class="col1"> | ||
<div class="form-group"> | ||
<label for="address" data-i18n="pair.address">IP address</label> | ||
<input type="text" class="form-control" id="address" placeholder="192.168.0.100"> | ||
</div> | ||
<div class="form-group"> | ||
<label for="token" data-i18n="pair.token">Token (32 characters of numbers and letters)</label> | ||
<input type="text" class="form-control" id="token" pattern="[a-zA-Z0-9]{32}" placeholder="FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"> | ||
</div> | ||
<div class="form-group"> | ||
<label for="polling" data-i18n="pair.polling">Polling Frequency</label> | ||
<input type="number" class="form-control" id="polling" min="5" max="3600" placeholder="10"> | ||
</div> | ||
<input type="hidden" id="mode" value=""> | ||
<div class="form-group buttons"> | ||
<button id="test-connection" class="button" data-i18n="pair.test">Test Connection</button> | ||
<button id="connect" class="button" data-i18n="pair.connect" disabled>Connect</button> | ||
</div> | ||
</div> | ||
<div class="col2"> | ||
<div class="mi-device-info"></div> | ||
</div> | ||
</div> | ||
|
||
<div class="messages"> | ||
<p class="mi-device-status mi-device-test" style="color: #008C23;"><i class="fa fa-check"></i> <span data-i18n="pair.testing">Connection test successfull, you can now connect the device.</span></p> | ||
<p class="mi-device-status mi-device-ok" style="color: #008C23;"><i class="fa fa-check"></i> <span data-i18n="pair.success">Mi Home device added succesfully.</span></p> | ||
<p class="mi-device-status mi-device-error" style="color: #ff6300;"><i class="fa fa-times"></i> <span class="mi-device-error-msg"></span></p> | ||
</div> |
Oops, something went wrong.
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