Skip to content

Commit 98fbb79

Browse files
committed
* added a way to fix bugged properties from the gateway reports
* fix minimum brightness when lights are off * added tests for that and repaired other ones
1 parent e42524d commit 98fbb79

15 files changed

Lines changed: 189 additions & 12 deletions

build/lib/accessory.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ export declare class Accessory extends IPSODevice {
1818
sensorList: Sensor[];
1919
switchList: IPSODevice[];
2020
otaUpdateState: number;
21+
/**
22+
* Fixes property values that are known to be bugged
23+
*/
24+
fixBuggedProperties(): this;
2125
}

build/lib/accessory.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,28 @@ class Accessory extends ipsoDevice_1.IPSODevice {
6363
}
6464
return this;
6565
}
66+
/**
67+
* Fixes property values that are known to be bugged
68+
*/
69+
fixBuggedProperties() {
70+
super.fixBuggedProperties();
71+
if (this.lightList != null) {
72+
this.lightList = this.lightList.map(light => light.fixBuggedProperties());
73+
}
74+
/* istanbul ignore next */
75+
if (this.plugList != null) {
76+
this.plugList = this.plugList.map(plug => plug.fixBuggedProperties());
77+
}
78+
/* istanbul ignore next */
79+
if (this.sensorList != null) {
80+
this.sensorList = this.sensorList.map(sensor => sensor.fixBuggedProperties());
81+
}
82+
/* istanbul ignore next */
83+
if (this.switchList != null) {
84+
this.switchList = this.switchList.map(swtch => swtch.fixBuggedProperties());
85+
}
86+
return this;
87+
}
6688
}
6789
__decorate([
6890
ipsoObject_1.ipsoKey("5750"),

build/lib/conversions.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,6 @@ const brightness_in = (value) => {
216216
if (value === 0)
217217
return 0;
218218
value = value / 254 * 100;
219-
// Any value > 0 should equal at least 1% brightness
220-
if (value < 1)
221-
return 1;
222219
return math_1.roundTo(value, 1);
223220
};
224221
exports.serializers = {

build/lib/ipsoObject.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,8 @@ export declare class IPSOObject {
7777
*/
7878
createProxy(get?: (me: this, key: PropertyKey) => any, set?: (me: this, key: PropertyKey, value: any, receiver: any) => boolean): this;
7979
protected client: OperationProvider;
80+
/**
81+
* Fixes property values that are known to be bugged
82+
*/
83+
fixBuggedProperties(): this;
8084
}

build/lib/ipsoObject.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,13 @@ class IPSOObject {
472472
this.client = client;
473473
return this;
474474
}
475+
/**
476+
* Fixes property values that are known to be bugged
477+
*/
478+
fixBuggedProperties() {
479+
// IPSOObject has none
480+
return this;
481+
}
475482
}
476483
__decorate([
477484
exports.doNotSerialize,

build/lib/light.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,9 @@ export declare class Light extends IPSODevice {
7878
setSaturation(value: number, transitionTime?: number): Promise<boolean>;
7979
/** Turns this object into JSON while leaving out the potential circular reference */
8080
toJSON(): {};
81+
/**
82+
* Fixes property values that are known to be bugged
83+
*/
84+
fixBuggedProperties(): this;
8185
}
8286
export declare type Spectrum = "none" | "white" | "rgb";

build/lib/light.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,16 @@ class Light extends ipsoDevice_1.IPSODevice {
220220
transitionTime: this.transitionTime,
221221
};
222222
}
223+
/**
224+
* Fixes property values that are known to be bugged
225+
*/
226+
fixBuggedProperties() {
227+
super.fixBuggedProperties();
228+
// For some reason the gateway reports lights with brightness 1 after turning off
229+
if (this.onOff === false && this.dimmer === MIN_BRIGHTNESS)
230+
this.dimmer = 0;
231+
return this;
232+
}
223233
}
224234
__decorate([
225235
ipsoObject_1.doNotSerialize,
@@ -305,6 +315,8 @@ __decorate([
305315
__metadata("design:type", String)
306316
], Light.prototype, "unit", void 0);
307317
exports.Light = Light;
318+
// remember the minimum possible non-zero brightness to fix the bugged properties;
319+
const MIN_BRIGHTNESS = conversions_1.deserializers.brightness(1);
308320
const rgbRegex = /^[0-9A-Fa-f]{6}$/;
309321
/**
310322
* Creates a proxy for an RGB lamp,

build/tradfri-client.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,10 @@ class TradfriClient extends events_1.EventEmitter {
255255
const result = parsePayload(response);
256256
logger_1.log(`observeDevice > ` + JSON.stringify(result), "debug");
257257
// parse device info
258-
const accessory = new accessory_1.Accessory(this.ipsoOptions).parse(result).createProxy();
258+
const accessory = new accessory_1.Accessory(this.ipsoOptions)
259+
.parse(result)
260+
.fixBuggedProperties()
261+
.createProxy();
259262
// remember the device object, so we can later use it as a reference for updates
260263
// store a clone, so we don't have to care what the calling library does
261264
this.devices[instanceId] = accessory.clone();
@@ -370,7 +373,10 @@ class TradfriClient extends events_1.EventEmitter {
370373
}
371374
const result = parsePayload(response);
372375
// parse group info
373-
const group = (new group_1.Group(this.ipsoOptions)).parse(result).createProxy();
376+
const group = new group_1.Group(this.ipsoOptions)
377+
.parse(result)
378+
.fixBuggedProperties()
379+
.createProxy();
374380
// remember the group object, so we can later use it as a reference for updates
375381
let groupInfo;
376382
if (!(instanceId in this.groups)) {
@@ -450,7 +456,10 @@ class TradfriClient extends events_1.EventEmitter {
450456
}
451457
const result = parsePayload(response);
452458
// parse scene info
453-
const scene = (new scene_1.Scene(this.ipsoOptions)).parse(result).createProxy();
459+
const scene = new scene_1.Scene(this.ipsoOptions)
460+
.parse(result)
461+
.fixBuggedProperties()
462+
.createProxy();
454463
// remember the scene object, so we can later use it as a reference for updates
455464
// store a clone, so we don't have to care what the calling library does
456465
this.groups[groupId].scenes[instanceId] = scene.clone();

src/lib/accessory.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,27 @@ export class Accessory extends IPSODevice {
8181
return this;
8282
}
8383

84+
/**
85+
* Fixes property values that are known to be bugged
86+
*/
87+
public fixBuggedProperties(): this {
88+
super.fixBuggedProperties();
89+
if (this.lightList != null) {
90+
this.lightList = this.lightList.map(light => light.fixBuggedProperties());
91+
}
92+
/* istanbul ignore next */
93+
if (this.plugList != null) {
94+
this.plugList = this.plugList.map(plug => plug.fixBuggedProperties());
95+
}
96+
/* istanbul ignore next */
97+
if (this.sensorList != null) {
98+
this.sensorList = this.sensorList.map(sensor => sensor.fixBuggedProperties());
99+
}
100+
/* istanbul ignore next */
101+
if (this.switchList != null) {
102+
this.switchList = this.switchList.map(swtch => swtch.fixBuggedProperties());
103+
}
104+
return this;
105+
}
106+
84107
}

src/lib/conversions.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe("tradfri/conversions => brightness() =>", () => {
3535
// tslint:disable-next-line:variable-name
3636
const testSets_in = [
3737
{coap: 0, tradfri: 0},
38-
{coap: 1, tradfri: 1},
38+
{coap: 1, tradfri: 0.4},
3939
{coap: 68, tradfri: 26.8},
4040
{coap: 128, tradfri: 50.4},
4141
{coap: 254, tradfri: 100},

0 commit comments

Comments
 (0)