diff --git a/lib/devices/philips-light-downlight.js b/lib/devices/philips-light-downlight.js new file mode 100644 index 0000000..fca7802 --- /dev/null +++ b/lib/devices/philips-light-downlight.js @@ -0,0 +1,73 @@ +'use strict'; + +const { LightBulb, ColorTemperature } = require('abstract-things/lights'); +const { color } = require('abstract-things/values'); +const MiioApi = require('../device'); + +const Power = require('./capabilities/power'); +const Dimmable = require('./capabilities/dimmable'); +const Colorable = require('./capabilities/colorable'); + +const MIN_TEMP = 3000; +const MAX_TEMP = 5700; + +module.exports = class BallLamp extends LightBulb + .with(MiioApi, Power, Dimmable, Colorable, ColorTemperature) +{ + static get type() { + return 'miio: philips-light-downlight'; + } + + constructor(options) { + super(options); + + this.defineProperty('power', { + name: 'power', + mapper: v => v === 'on' + }); + + this.defineProperty('bright', { + name: 'brightness', + mapper: parseInt + }); + + this.defineProperty('cct', { + name: 'color', + mapper: v => { + v = parseInt(v); + return color.temperature(MIN_TEMP + (v / 100) * (MAX_TEMP - MIN_TEMP)); + } + }); + + this.updateColorTemperatureRange(MIN_TEMP, MAX_TEMP); + } + + changePower(power) { + return this.call('set_power', [ power ? 'on' : 'off' ], { + refresh: [ 'power' ] + }).then(MiioApi.checkOk); + } + + changeBrightness(brightness) { + return this.call('set_bright', [ brightness ], { + refresh: [ 'brightness' ] + }).then(MiioApi.checkOk); + } + + changeColor(color) { + const kelvins = color.temperature.kelvins; + let temp; + if(kelvins <= MIN_TEMP) { + temp = 1; + } else if(kelvins >= MAX_TEMP) { + temp = 100; + } else { + temp = Math.round((kelvins - MIN_TEMP) / (MAX_TEMP - MIN_TEMP) * 100); + } + + return this.call('set_cct', [ temp ], { + refresh: [ 'color'] + }).then(MiioApi.checkOk); + } + +}; diff --git a/lib/models.js b/lib/models.js index 635c453..a78ff43 100644 --- a/lib/models.js +++ b/lib/models.js @@ -57,6 +57,7 @@ module.exports = { 'yeelink.light.strip1': YeelightColor, 'philips.light.sread1': require('./devices/eyecare-lamp2'), - 'philips.light.bulb': require('./devices/philips-light-bulb') + 'philips.light.bulb': require('./devices/philips-light-bulb'), + 'philips.light.downlight': require('./devices/philips-light-downlight') };