Skip to content

Commit

Permalink
also update sensor battery state,
Browse files Browse the repository at this point in the history
- and level and ct for single lights via push api
  • Loading branch information
foxriver76 committed Jun 11, 2023
1 parent ed5a18e commit 2f9c50f
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 36 deletions.
6 changes: 6 additions & 0 deletions build/lib/hueHelper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,9 @@ export declare function XYBtoRGB(x: number, y: number, Brightness?: number): {
Green: number;
Blue: number;
};
/**
* Convert Mired to Kelvin
*
* @param mired mired value
*/
export declare function miredToKelvin(mired: number): number;
11 changes: 10 additions & 1 deletion 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.

39 changes: 23 additions & 16 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.

9 changes: 9 additions & 0 deletions src/lib/hueHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,12 @@ export function XYBtoRGB(x: number, y: number, Brightness?: number): { Red: numb
}
return { Red, Green, Blue };
}

/**
* Convert Mired to Kelvin
*
* @param mired mired value
*/
export function miredToKelvin(mired: number) {
return Math.round(1e6 / mired);
}
46 changes: 29 additions & 17 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ interface BridgeUpdate {
id: string;
id_v1: `/${string}/${number}`;
owner: { rid: string; rtype: string };
type: 'grouped_light' | 'light' | 'temperature' | 'motion' | 'light_level' | 'zigbee_connectivity';
type: 'grouped_light' | 'light' | 'temperature' | 'motion' | 'light_level' | 'zigbee_connectivity' | 'device_power';
/** if type is motion */
motion?: { motion: boolean; motion_report: { changed: string; motion: boolean }; motion_valid: boolean };
/** for lights and groups */
Expand All @@ -56,6 +56,8 @@ interface BridgeUpdate {
};
/** For type zigbee_connectivity */
status?: 'connected' | 'connectivity_issue';
/** For type device_power */
power_state: { battery_level: number; battery_state: string };
}

/** IDs currently blocked from polling */
Expand Down Expand Up @@ -577,8 +579,7 @@ class Hue extends utils.Adapter {
}

finalLS.ct = Math.max(2200, Math.min(6500, ls.ct));
// convert kelvin to mired
finalLS.ct = Math.round(1e6 / finalLS.ct);
finalLS.ct = hueHelper.miredToKelvin(finalLS.ct);

lightState = lightState.ct(finalLS.ct);
if (!lampOn && (!('bri' in ls) || ls.bri === 0) && this.config.turnOnWithOthers) {
Expand Down Expand Up @@ -920,7 +921,7 @@ class Hue extends utils.Adapter {
}
if (states.ct !== undefined) {
// convert color temperature from mired to kelvin
states.ct = Math.round(1e6 / states.ct);
states.ct = hueHelper.miredToKelvin(states.ct);
if (!isFinite(states.ct)) {
// issue #234
// invalid value we cannot determine the meant value, fallback to max
Expand Down Expand Up @@ -1003,8 +1004,7 @@ class Hue extends utils.Adapter {
states.hue = Math.round((states.hue / 65535) * 360);
}
if (states.ct !== undefined) {
// convert color temperature from mired to kelvin
states.ct = Math.round(1e6 / states.ct);
states.ct = hueHelper.miredToKelvin(states.ct);
}
for (const stateB of Object.keys(states)) {
values.push({ id: `${this.namespace}.${light.name}.${stateB}`, val: states[stateB] });
Expand All @@ -1024,7 +1024,6 @@ class Hue extends utils.Adapter {
}

/**
<<<<<<< HEAD
* Create a push connection to the Hue bridge, to listen to updates in near real-time
*/
createPushConnection(): void {
Expand Down Expand Up @@ -1083,7 +1082,7 @@ class Hue extends utils.Adapter {
return;
}

if (update.type === 'motion' || update.type === 'temperature' || update.type === 'light_level') {
if (['motion', 'temperature', 'light_level', 'device_power'].includes(update.type)) {
this.handleSensorUpdate(id, update);
return;
}
Expand Down Expand Up @@ -1116,6 +1115,10 @@ class Hue extends utils.Adapter {
if (update.light?.light_level_valid) {
this.setState(`${channelName}.lightlevel`, update.light.light_level, true);
}

if (update.power_state) {
this.setState(`${channelName}.battery`, update.power_state.battery_level, true);
}
}

/**
Expand All @@ -1130,6 +1133,18 @@ class Hue extends utils.Adapter {
if (update.on) {
this.setState(`${channelName}.on`, update.on.on, true);
}

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

if (update.color_temperature?.mirek_valid) {
this.setState(`${channelName}.ct`, hueHelper.miredToKelvin(update.color_temperature.mirek!), true);
}

if (update.color) {
// TODO
}
}

/**
Expand Down Expand Up @@ -1180,8 +1195,6 @@ class Hue extends utils.Adapter {
}

/**
=======
>>>>>>> 7dcd9eb0021aff8763a97d55e0f82468f6fcc8a5
* Connects to the bridge and creates the initial objects
*/
async connect(): Promise<void> {
Expand Down Expand Up @@ -1545,9 +1558,9 @@ class Hue extends utils.Adapter {
lobj.common.type = 'number';
lobj.common.role = 'level.color.temperature';
lobj.common.unit = '°K';
lobj.common.min = Math.round(1e6 / ctObj.max); // this way, because with higher Kelvin -> smaller Mired
lobj.common.max = Math.round(1e6 / ctObj.min);
value = Math.round(1e6 / value);
lobj.common.min = hueHelper.miredToKelvin(ctObj.max);
lobj.common.max = hueHelper.miredToKelvin(ctObj.min);
value = hueHelper.miredToKelvin(value);
if (!isFinite(value)) {
// issue #234
// invalid value we cannot determine the meant value, fallback to max
Expand Down Expand Up @@ -1789,8 +1802,7 @@ class Hue extends utils.Adapter {
gobj.common.unit = '°K';
gobj.common.min = 1802; // normally 500 (2000) but some groups have smaller values due to third pary lights and we cannot get min via api for groups
gobj.common.max = 6536; // 153
// mired to kelvin
group.action[action] = Math.round(1e6 / group.action[action]);
group.action[action] = hueHelper.miredToKelvin(group.action[action]);
if (!isFinite(group.action[action])) {
// issue #234
// invalid value we cannot determine the meant value, fallback to max
Expand Down Expand Up @@ -2282,7 +2294,7 @@ class Hue extends utils.Adapter {
}
if (states.ct !== undefined) {
// convert color temperature from mired to kelvin
states.ct = Math.round(1e6 / states.ct);
states.ct = hueHelper.miredToKelvin(states.ct);

// some devices send 0 -> infinity (issue #234)
if (!isFinite(states.ct)) {
Expand Down Expand Up @@ -2384,7 +2396,7 @@ class Hue extends utils.Adapter {

if (states.ct !== undefined) {
// convert color temperature from mired to kelvin
states.ct = Math.round(1e6 / states.ct);
states.ct = hueHelper.miredToKelvin(states.ct);

// some devices send 0 -> infinity (issue #234)
if (!isFinite(states.ct)) {
Expand Down

0 comments on commit 2f9c50f

Please sign in to comment.