Skip to content

Commit a87e573

Browse files
authored
fix(homebridge): bound charge-limit Brightness characteristic to 50-100 (#121)
Set the Brightness characteristic's min/max/step to match the telemetry clamp so HomeKit rejects an out-of-range write before it reaches the API.
1 parent 1cc7033 commit a87e573

3 files changed

Lines changed: 51 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"homebridge-teslemetry": patch
3+
---
4+
5+
Set the Brightness characteristic's min/max/step on the charge-limit control to 50-100 (step 1), matching the telemetry clamp already applied to `ChargeLimitSoc` readings, so HomeKit itself rejects a client write outside the vehicle's supported charge-limit range instead of forwarding it to the API.

packages/homebridge-teslemetry/src/vehicle-services/charge-limit.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ export class ChargeLimitService extends BaseService {
3030
// Charge limit is always "on" (we just control the brightness/percentage)
3131
this.service.updateCharacteristic(this.platform.Characteristic.On, true);
3232

33+
// Match the Brightness characteristic's bounds to the telemetry clamp
34+
// below so HomeKit rejects/bounds writes outside the vehicle's supported range.
35+
this.service.getCharacteristic(this.platform.Characteristic.Brightness).setProps({
36+
minValue: 50,
37+
maxValue: 100,
38+
minStep: 1,
39+
});
40+
3341
// Subscribe to charge limit updates
3442
this.subscribeSignal(
3543
"ChargeLimitSoc",

packages/homebridge-teslemetry/test/chargeLimit.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,44 @@ test("ChargeLimitSoc below 50 is clamped to 50", () => {
3131
assert.equal(hapService.getCharacteristic(Characteristic.Brightness).value, 50);
3232
});
3333

34+
test("the Brightness characteristic's props are bounded to the vehicle's supported charge limit range", () => {
35+
const { hapService } = setup();
36+
const props = hapService.getCharacteristic(Characteristic.Brightness).props;
37+
assert.equal(props.minValue, 50);
38+
assert.equal(props.maxValue, 100);
39+
assert.equal(props.minStep, 1);
40+
});
41+
42+
test("a client write above the max is rejected by HAP before reaching setChargeLimit()", async () => {
43+
const { hapService, vehicle } = setup();
44+
let apiCalled = false;
45+
(vehicle.api as any).setChargeLimit = () => {
46+
apiCalled = true;
47+
return Promise.resolve({});
48+
};
49+
50+
await assert.rejects(
51+
hapService.getCharacteristic(Characteristic.Brightness).handleSetRequest(150 as never, {} as never),
52+
);
53+
54+
assert.equal(apiCalled, false);
55+
});
56+
57+
test("a client write below the min is rejected by HAP before reaching setChargeLimit()", async () => {
58+
const { hapService, vehicle } = setup();
59+
let apiCalled = false;
60+
(vehicle.api as any).setChargeLimit = () => {
61+
apiCalled = true;
62+
return Promise.resolve({});
63+
};
64+
65+
await assert.rejects(
66+
hapService.getCharacteristic(Characteristic.Brightness).handleSetRequest(10 as never, {} as never),
67+
);
68+
69+
assert.equal(apiCalled, false);
70+
});
71+
3472
test("setting Brightness rounds the value and calls setChargeLimit()", async () => {
3573
const { hapService, vehicle } = setup();
3674
let received: unknown;

0 commit comments

Comments
 (0)