Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add CTLight for adjusting color temperatur lights #132

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions lib/addins/CTLight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/* Sample module - colour temperature light (Homekit)
*
* Source for brightness fix: https://knx-user-forum.de/forum/öffentlicher-bereich/knx-eib-forum/diy-do-it-yourself/998030-homebridge-knx-0-3-0-alpha-apple-homekit-interface?p=1182183#post1182183
*
* EXPERIMENTAL maturity state!
*/
/* jshint esversion: 6, strict: true, node: true */
'use strict';
/**
* @type {./handlerpattern.js~HandlerPattern}
*/
var HandlerPattern = require('./handlerpattern.js');
var log = require('debug')('CTLight');

/**
* @classdesc A custom handler for a color temperature Light with 3 group addresses: "On" DPT1 (0,1), "Brightness" and "ColorTemperature" DPT7.600 (0..65535)
* @extends HandlerPattern
*/
class CTLight extends HandlerPattern {

/****
* onKNXValueChange is invoked if a Bus value for one of the bound addresses is received
*
*/
onKNXValueChange(field, oldValue, knxValue) {
log('INFO: onKNXValueChange(' + field + ", "+ oldValue + ", "+ knxValue+ ")");
switch (field) {
case "Brightness":
this.myAPI.setValue(field, parseInt((knxValue) / 255 * 100) + 1);
break;
case "On":
this.myAPI.setValue(field, knxValue);
break;
case "ColorTemperature":
this.myAPI.setValue(field, this.convertBetweenKelvinAndMired(knxValue));
break;
}
}

convertBetweenKelvinAndMired(value) {
return Math.pow(10, 6) / value;
}

// onBusValueChange

/****
* onHKValueChange is invoked if HomeKit is changing characteristic values
*
*/
onHKValueChange(field, oldValue, newValue) {
log('INFO: onHKValueChange(' + field + ", "+ oldValue + ", "+ newValue + ")");
switch (field) {
case "Brightness":
this.setBrightnessToKNX(field, newValue)
break;
case "On":
this.setSwitchToKNX(field, newValue)
break;
case "ColorTemperature":
this.setColorTemperatureToKNX(field, newValue);
break;
}
}

setBrightnessToKNX(field, newValue) {
// to make 1% -> 1/255 (more precise dimming)
this.myAPI.knxWrite(field, parseInt(newValue / 100 * 255 - 1), "DPT5");

// set "brightness has just been set" flag to true for next 0.2s
this.brightnessSet = true;
var that = this;
if (this.timer) clearTimeout(this.timer);
this.timer = setTimeout(function () {
that.brightnessSet = false;
}, 200);
}


setSwitchToKNX(field, newValue) {
//skip "turn on" knx message if brightness has just been set
if (newValue !== 1 || !this.brightnessSet) {
this.myAPI.knxWrite(field, newValue, "DPT1");
}
}

setColorTemperatureToKNX(field, newValue) {
let kelvin = this.convertBetweenKelvinAndMired(newValue);
log('INFO Writing color temperature in kelvin value of ' + kelvin + ' to KNX bus');
this.myAPI.knxWrite(field, kelvin, 'DPT7.600');
}

// onHKValueChange
} // class
module.exports = CTLight;


/* **********************************************************************************************************************
* The config for that should look like this
* Reverse keyword is not allowed for custom handlers
*
*
"Services": [
{
"ServiceType": "Lightbulb",
"Handler": "CTLight",
"ServiceName": "ikea",
"Characteristics": [
{
"Type": "On",
"Set": [
"0/0/13"
],
"Listen": [
"0/0/14"
],
"DPT": "DPT1"
},
{
"Type": "Brightness",
"Set": [
"0/0/20"
],
"Listen": [
"0/0/17"
],
"DPT": "DPT5.001"
},
{
"Type": "ColorTemperature",
"Set": [
"0/0/19"
],
"Listen": [
"0/0/16"
],
"DPT": "DPT7.600"
}
],
"KNXReadRequests": [
"0/0/14",
"0/0/17",
"0/0/16"
],
"subtype": "SUB_d0dc9d33-5f71-4e67-a127-b7a3b7849d33"
}
]
*
*
*
*/
77 changes: 77 additions & 0 deletions lib/addins/millis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict';
/** * @type {HandlerPattern}
* Source for brightness fix: https://knx-user-forum.de/forum/öffentlicher-bereich/knx-eib-forum/diy-do-it-yourself/998030-homebridge-knx-0-3-0-alpha-apple-homekit-interface?p=1182183#post1182183
*/
var HandlerPattern = require('./handlerpattern.js');
var log = require('debug')('dltDIM');


/**
* @class A custom handler to fix brightness/on messages and to make 1% eq 1/255 brightness
* @extends HandlerPattern
*/
class dltDIM extends HandlerPattern {

constructor(knxAPI) {
super(knxAPI); // call the super constructor first. Always.
}


/****
* onKNXValueChange is invoked if a Bus value for one of the bound addresses is received
*
*/
onKNXValueChange(field, oldValue, knxValue) {
console.log('INFO: on KNX Value Change(' + field + ", old="+ oldValue + ", new="+ knxValue+ ")");

switch (field)
{
case "Brightness":
this.myAPI.setValue("Brightness", parseInt((knxValue)/255*100)+1);
break;
case "On":
this.myAPI.setValue("On", knxValue);
break;
}
return true;
} // onBusValueChange

/****
* onHKValueChange is invoked if HomeKit is changing characteristic values
*
*/
onHKValueChange(field, oldValue, newValue) {
console.log('INFO: on HK Value Change (' + field + ", old="+ oldValue + ", new="+ newValue + ")");

switch (field)
{
case "Brightness":

// to make 1% -> 1/255 (more precise dimming)
this.myAPI.knxWrite(field, parseInt(newValue/100*255-1), "DPT5");

// set "brightness has just been set" flag to true for next 0.2s
this.brightnessSet=true;
var that = this;
if (this.timer) clearTimeout(this.timer);
this.timer = setTimeout(function () {
that.brightnessSet=false;
},200);
return true;
break;
case "On":
//skip "turn on" knx message if brightness has just been set
if (newValue == 1 && this.brightnessSet)
{
return true;
}
this.myAPI.knxWrite(field, newValue, "DPT1");
break;
}

return true;


} // onHKValueChange
} // class
module.exports= dltDIM;