Skip to content

Added support for lightbulbs that require color temperature updates via percent #9

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,11 @@ The configuration can contain the following properties:
are available:
* **"mired"**: Using mired (more specifically _microreciprocal degree_) to calculate color temperature
* **"kelvin"**: Using Kelvin to calculate color temperature
* **"percent"**: Using percent to calculate color temperature
* `minValue` \<number\> **optional** \(Default: **50**\): Defines the minimum supported temperature in the
given `unit`. The defaut is **50** mired or **20.000** Kelvin.
given `unit`. The default is **50** mired or **20.000** Kelvin. If unit is defined as percent, `minValue` must be defined in mired. 100% maps to `minValue` (**50** mired by default).
* `maxValue` \<number\> **optional** \(Default: **400**\): Defines the maximum supported temperature in the
given `unit`. The fault is **400** mired or **2.500** Kelvin.
given `unit`. The default is **400** mired or **2.500** Kelvin. If unit is defined as percent, `minValue` must be defined in mired. 0% maps to `maxValue` (**400** mired by default).
* `statusPattern` \<string\> **optional** \(Default: **"([0-9]{2,3})"** \[**"([0-9]{4,5})"** when using Kelvin\]): Defines a regex pattern with which the
color temperature is extracted from the body of the http response from the `colorTemperature.statusUrl`.
The group which should be extracted can be configured with the `colorTemperature.patternGroupToExtract` property.
Expand Down
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const SaturationUnit = Object.freeze({
const TemperatureUnit = Object.freeze({
MICRORECIPROCAL_DEGREE: "mired",
KELVIN: "kelvin",
PERCENT: "percent"
});

/*
Expand Down Expand Up @@ -500,6 +501,10 @@ HTTP_LIGHTBULB.prototype = {

if (this.colorTemperature.unit === TemperatureUnit.KELVIN)
minValue = Math.floor(1000000 / minValue);

else if (this.colorTemperature.unit === TemperatureUnit.PERCENT)
minValue = minValue

this.colorTemperature.minValue = minValue;
}
else
Expand All @@ -511,6 +516,10 @@ HTTP_LIGHTBULB.prototype = {

if (this.colorTemperature.unit === TemperatureUnit.KELVIN)
maxValue = Math.floor(1000000 / maxValue);

else if (this.colorTemperature.unit === TemperatureUnit.PERCENT)
maxValue = maxValue

this.colorTemperature.maxValue = maxValue;
}
else
Expand Down Expand Up @@ -940,6 +949,8 @@ HTTP_LIGHTBULB.prototype = {

if (this.colorTemperature.unit === TemperatureUnit.KELVIN)
colorTemperature = Math.round(1000000 / colorTemperature); // converting Kelvin to mired
else if (this.colorTemperature.unit === TemperatureUnit.PERCENT)
colorTemperature = Math.round((0.01 * colorTemperature * (this.colorTemperature.minValue - this.colorTemperature.maxValue)) + this.colorTemperature.maxValue); // converting percent to mired

if (colorTemperature >= this.colorTemperature.minValue && colorTemperature <= this.colorTemperature.maxValue) {
if (this.debug)
Expand All @@ -960,6 +971,8 @@ HTTP_LIGHTBULB.prototype = {
const colorTemperatureMired = colorTemperature;
if (this.colorTemperature.unit === TemperatureUnit.KELVIN)
colorTemperature = Math.round(1000000 / colorTemperature); // converting mired to Kelvin
else if (this.colorTemperature.unit === TemperatureUnit.PERCENT)
colorTemperature = Math.round((100 * (colorTemperature - this.colorTemperature.maxValue)) / (this.colorTemperature.minValue - this.colorTemperature.maxValue)); // converting percent to mired

http.httpRequest(this.colorTemperature.setUrl, (error, response, body) => {
if (error) {
Expand Down Expand Up @@ -1013,6 +1026,8 @@ HTTP_LIGHTBULB.prototype = {
let colorTemperature = this.homebridgeService.getCharacteristic(Characteristic.ColorTemperature).value;
if (this.colorTemperature.unit === TemperatureUnit.KELVIN)
colorTemperature = Math.round(1000000 / colorTemperature);
else if (this.colorTemperature.unit === TemperatureUnit.PERCENT)
colorTemperature = Math.round((100 * (colorTemperature - this.colorTemperature.maxValue)) / (this.colorTemperature.minValue - this.colorTemperature.maxValue)); // converting percent to mired

args.push({searchValue: "%colorTemperature", replacer: `${colorTemperature}`});
}
Expand Down