Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Jun 15, 2024
1 parent 092ffb9 commit e03dedd
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 21 deletions.
39 changes: 39 additions & 0 deletions src/services/chargecurrent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { CharacteristicValue } from "homebridge";
import { debounce } from "../utils/debounce.js";
import { VehicleAccessory } from "../vehicle.js";
import { BaseService } from "./base.js";

export class ChargeCurrentService extends BaseService {
constructor(parent: VehicleAccessory) {
super(parent, parent.platform.Service.Lightbulb, "charge current", "charge_current");

const on = this.service
.getCharacteristic(this.parent.platform.Characteristic.On)
.onGet(this.getOn.bind(this));

const level = this.service
.getCharacteristic(this.parent.platform.Characteristic.ChargingState)
.onGet(this.getLevel.bind(this))
.onSet(debounce(this.setLevel.bind(this), 3000));

this.parent.emitter.on("vehicle_data", () => {
on.updateValue(this.getOn());
level.updateValue(this.getLevel());
});
}

getOn(): boolean {
return !!this.parent.accessory.context?.charge_state;
}

getLevel(): number {
return this.parent.accessory.context?.charge_state?.charge_current_request ?? 16;
}

setLevel(value: CharacteristicValue): Promise<number> {
const min = 2;
const max = this.parent.accessory.context.charge_state.charge_current_request_max ?? 100;
value = Math.max(min, Math.min(max, value as number));
return this.parent.vehicle.set_charge_limit(value).then(() => value);
}
}
39 changes: 39 additions & 0 deletions src/services/chargelimit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { CharacteristicValue } from "homebridge";
import { debounce } from "../utils/debounce.js";
import { VehicleAccessory } from "../vehicle.js";
import { BaseService } from "./base.js";

export class ChargeLimitService extends BaseService {
constructor(parent: VehicleAccessory) {
super(parent, parent.platform.Service.Lightbulb, "charge limit", "charge_limit");

const on = this.service
.getCharacteristic(this.parent.platform.Characteristic.On)
.onGet(this.getOn.bind(this));

const level = this.service
.getCharacteristic(this.parent.platform.Characteristic.ChargingState)
.onGet(this.getLevel.bind(this))
.onSet(debounce(this.setLevel.bind(this), 3000));

this.parent.emitter.on("vehicle_data", () => {
on.updateValue(this.getOn());
level.updateValue(this.getLevel());
});
}

getOn(): boolean {
return !!this.parent.accessory.context?.charge_state;
}

getLevel(): number {
return this.parent.accessory.context?.charge_state?.charge_limit_soc ?? 50;
}

setLevel(value: CharacteristicValue): Promise<number> {
const min = this.parent.accessory.context.charge_state.charge_limit_soc_min ?? 50;
const max = this.parent.accessory.context.charge_state.charge_limit_soc_max ?? 100;
value = Math.max(min, Math.min(max, value as number));
return this.parent.vehicle.set_charge_limit(value).then(() => value);
}
}
43 changes: 22 additions & 21 deletions src/services/chargeport.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
import { CharacteristicValue } from "homebridge";
import { debounce } from "../utils/debounce.js";
import { VehicleAccessory } from "../vehicle.js";
import { BaseService } from "./base.js";

export class ChargePortService extends BaseService {
constructor(parent: VehicleAccessory) {

Check warning on line 6 in src/services/chargeport.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected indentation of 2 spaces but found 4

Check warning on line 6 in src/services/chargeport.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected indentation of 2 spaces but found 4
super(parent, parent.platform.Service.LockMechanism, "charge port", "charge_port");

Check warning on line 7 in src/services/chargeport.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected indentation of 4 spaces but found 8

Check warning on line 7 in src/services/chargeport.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected indentation of 4 spaces but found 8

const on = this.service
.getCharacteristic(this.parent.platform.Characteristic.On)
.onGet(this.getOn.bind(this));
const currentState = this.service

Check warning on line 9 in src/services/chargeport.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected indentation of 4 spaces but found 8

Check warning on line 9 in src/services/chargeport.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected indentation of 4 spaces but found 8
.getCharacteristic(this.parent.platform.Characteristic.LockCurrentState)

Check warning on line 10 in src/services/chargeport.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected indentation of 6 spaces but found 12

Check warning on line 10 in src/services/chargeport.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected indentation of 6 spaces but found 12
.onGet(this.getState.bind(this));

Check warning on line 11 in src/services/chargeport.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected indentation of 6 spaces but found 12

Check warning on line 11 in src/services/chargeport.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected indentation of 6 spaces but found 12

const level = this.service
.getCharacteristic(this.parent.platform.Characteristic.ChargingState)
.onGet(this.getLevel.bind(this))
.onSet(debounce(this.setLevel.bind(this), 3000));
this.service

Check warning on line 13 in src/services/chargeport.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected indentation of 4 spaces but found 8

Check warning on line 13 in src/services/chargeport.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected indentation of 4 spaces but found 8
.getCharacteristic(this.parent.platform.Characteristic.LockTargetState)

Check warning on line 14 in src/services/chargeport.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected indentation of 6 spaces but found 12

Check warning on line 14 in src/services/chargeport.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected indentation of 6 spaces but found 12
.onGet(this.getState.bind(this))

Check warning on line 15 in src/services/chargeport.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected indentation of 6 spaces but found 12

Check warning on line 15 in src/services/chargeport.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected indentation of 6 spaces but found 12
.onSet(this.setState.bind(this));

Check warning on line 16 in src/services/chargeport.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected indentation of 6 spaces but found 12

Check warning on line 16 in src/services/chargeport.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected indentation of 6 spaces but found 12

this.parent.emitter.on("vehicle_data", () => {

Check warning on line 18 in src/services/chargeport.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Expected indentation of 4 spaces but found 8

Check warning on line 18 in src/services/chargeport.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Expected indentation of 4 spaces but found 8
on.updateValue(this.getOn());
level.updateValue(this.getLevel());
currentState.updateValue(this.getState());
});
}

getOn(): boolean {
return this.parent.accessory.context?.charge_state?.user_charge_enable_request
?? this.parent.accessory.context?.charge_state?.charge_enable_request;
getState(): number {
return this.parent.accessory.context?.charge_state?.charge_port_latch ?
this.parent.platform.Characteristic.LockTargetState.SECURED :
this.parent.platform.Characteristic.LockTargetState.UNSECURED;
}

getLevel(): number {
return this.parent.accessory.context?.charge_state?.charge_limit_soc ?? 50;
}
setState(value: CharacteristicValue): Promise<number> {
const open = value === this.parent.platform.Characteristic.LockTargetState.UNSECURED;

setLevel(value: CharacteristicValue): Promise<number> {
const min = this.parent.accessory.context.charge_state.charge_limit_soc_min ?? 50;
const max = this.parent.accessory.context.charge_state.charge_limit_soc_max ?? 100;
value = Math.max(min, Math.min(max, value as number));
return this.parent.vehicle.set_charge_limit(value).then(() => value);
if (open) {
return this.parent.vehicle.charge_port_door_open().then(() =>
this.parent.platform.Characteristic.LockTargetState.SECURED
);
}
return this.parent.vehicle.charge_port_door_close().then(() =>
this.parent.platform.Characteristic.LockTargetState.UNSECURED
);
}
}
File renamed without changes.

0 comments on commit e03dedd

Please sign in to comment.