Skip to content

Commit

Permalink
also set rgb values and bri values via push api
Browse files Browse the repository at this point in the history
  • Loading branch information
foxriver76 committed Jun 11, 2023
1 parent 38033a5 commit b1189f8
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 61 deletions.
8 changes: 7 additions & 1 deletion build/lib/hueHelper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export declare function GamutXYforModel(Px: number, Py: number, Model: string):
* @param Blue - Range [0..1]
* @returns [Ang, Sat, Bri] - Ranges [0..360] [0..1] [0..1]
*/
export declare function HelperRGBtoHueAngSatBri(Red: number, Green: number, Blue: number): {
export declare function RgbToHsv(Red: number, Green: number, Blue: number): {
Ang: number;
Sat: number;
Bri: number;
Expand All @@ -68,3 +68,9 @@ export declare function XYBtoRGB(x: number, y: number, Brightness?: number): {
* @param mired mired value
*/
export declare function miredToKelvin(mired: number): number;
/**
* Convert level to brightness value
*
* @param level the level value
*/
export declare function levelToBrightness(level: number): number;
49 changes: 26 additions & 23 deletions build/lib/hueHelper.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/lib/hueHelper.js.map

Large diffs are not rendered by default.

37 changes: 31 additions & 6 deletions build/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build/main.js.map

Large diffs are not rendered by default.

50 changes: 27 additions & 23 deletions src/lib/hueHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
*/
export function RgbToXYB(Red: number, Green: number, Blue: number, model: string): { x: number; y: number; b: number } {
const Point = HelperRGBtoXY(Red, Green, Blue);
const HueAngSatBri = HelperRGBtoHueAngSatBri(Red, Green, Blue);
const bri = Math.min(255, Math.round(HueAngSatBri.Bri * 255));
const { Bri } = RgbToHsv(Red, Green, Blue);
const bri = Math.min(255, Math.round(Bri * 255));
const Gamuted = GamutXYforModel(Point.x, Point.y, model)!;
return { x: Gamuted.x, y: Gamuted.y, b: bri };
}
Expand Down Expand Up @@ -162,32 +162,27 @@ export function GamutXYforModel(Px: number, Py: number, Model: string): { x: num
* @param Blue - Range [0..1]
* @returns [Ang, Sat, Bri] - Ranges [0..360] [0..1] [0..1]
*/
export function HelperRGBtoHueAngSatBri(
Red: number,
Green: number,
Blue: number
): { Ang: number; Sat: number; Bri: number } {
export function RgbToHsv(Red: number, Green: number, Blue: number): { Ang: number; Sat: number; Bri: number } {
let Ang;
let Sat;
let Bri;

const Min = Math.min(Red, Green, Blue);
const Max = Math.max(Red, Green, Blue);
if (Min !== Max) {
if (Red === Max) {
Ang = ((Green - Blue) / (Max - Min)) * 60;
} else if (Green === Max) {
Ang = (2 + (Blue - Red) / (Max - Min)) * 60;
} else {
Ang = (4 + (Red - Green) / (Max - Min)) * 60;
}
Sat = (Max - Min) / Max;
Bri = Max;

if (Max === Min) {
return { Ang: 0, Sat: 0, Bri: Max };
}

const delta = Max - Min;

if (Red === Max) {
Ang = ((Green - Blue) / delta) * 60;
} else if (Green === Max) {
Ang = (2 + (Blue - Red) / delta) * 60;
} else {
// Max == Min
Ang = 0;
Sat = 0;
Bri = Max;
Ang = (4 + (Red - Green) / delta) * 60;
}
const Sat = delta / Max;
const Bri = Max;
return { Ang, Sat, Bri };
}

Expand Down Expand Up @@ -296,3 +291,12 @@ export function XYBtoRGB(x: number, y: number, Brightness?: number): { Red: numb
export function miredToKelvin(mired: number): number {
return Math.round(1e6 / mired);
}

/**
* Convert level to brightness value
*
* @param level the level value
*/
export function levelToBrightness(level: number): number {
return Math.min(254, Math.max(0, Math.round(level * 2.54)));
}
43 changes: 37 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ class Hue extends utils.Adapter {
}
// if .level changed instead change .bri to level.val*254
if (dp === 'level' && typeof state.val === 'number') {
bri = Math.max(Math.min(Math.round(state.val * 2.54), 254), 0);
bri = hueHelper.levelToBrightness(state.val);
this.setState([id, 'bri'].join('.'), { val: bri, ack: false });
return;
}
Expand Down Expand Up @@ -463,7 +463,7 @@ class Hue extends utils.Adapter {
} else if (command === 'level') {
//convert level to bri
if (!Object.prototype.hasOwnProperty.call(commands, 'bri')) {
ls.bri = Math.min(254, Math.max(0, Math.round(parseInt(commands[command]) * 2.54)));
ls.bri = hueHelper.levelToBrightness(parseInt(commands[command]));
} else {
ls.bri = 254;
}
Expand Down Expand Up @@ -1067,13 +1067,13 @@ class Hue extends utils.Adapter {
*
* @param update update received by bridge
*/
handleUpdate(update: BridgeUpdate): void {
async handleUpdate(update: BridgeUpdate): Promise<void> {
this.log.debug(`New push connection update: ${JSON.stringify(update)}`);

const id = parseInt(update.id_v1.split('/')[2]);

if (update.type === 'light') {
this.handleLightUpdate(id, update);
await this.handleLightUpdate(id, update);
return;
}

Expand Down Expand Up @@ -1127,7 +1127,7 @@ class Hue extends utils.Adapter {
* @param id id of the light
* @param update the update sent by bridge
*/
handleLightUpdate(id: number, update: BridgeUpdate): void {
async handleLightUpdate(id: number, update: BridgeUpdate): Promise<void> {
const channelName = this.getLightChannelById(id);

if (update.on) {
Expand All @@ -1136,6 +1136,7 @@ class Hue extends utils.Adapter {

if (update.dimming) {
this.setState(`${channelName}.level`, Math.round(update.dimming.brightness), true);
this.setState(`${channelName}.bri`, hueHelper.levelToBrightness(update.dimming.brightness), true);
}

if (update.color_temperature?.mirek_valid) {
Expand All @@ -1144,10 +1145,40 @@ class Hue extends utils.Adapter {

if (update.color) {
this.setState(`${channelName}.xy`, `${update.color.xy.x},${update.color.xy.y}`, true);
// TODO: also update rgb values (and maybe hue sat)
await this.updateColorStatesByXY(channelName, update.color.xy.x, update.color.xy.y);
}
}

/**
* Update the RGB, Hue and sat states of a channel by given x, y values
*
* @param channelName ioBroker channel name
* @param x x-value
* @param y y-value
*/
async updateColorStatesByXY(channelName: string, x: number, y: number): Promise<void> {
const state = await this.getStateAsync(`${channelName}.bri`);

if (!state || typeof state.val !== 'number') {
return;
}

const bri = state.val;

const { Red, Green, Blue } = hueHelper.XYBtoRGB(x, y, bri / 254);

this.setState(`${channelName}.r`, Math.round(Red * 254), true);
this.setState(`${channelName}.g`, Math.round(Green * 254), true);
this.setState(`${channelName}.b`, Math.round(Blue * 254), true);

/** TODO: this converts to wrong HS values
const { Ang, Sat } = hueHelper.RgbToHsv(Red, Green, Blue);
this.setState(`${channelName}.hue`, Math.round(Ang), true);
this.setState(`${channelName}.sat`, Math.round(Sat * 254), true);
*/
}

/**
* Handle group specific update
*
Expand Down

0 comments on commit b1189f8

Please sign in to comment.