Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Jun 18, 2024
1 parent e59e683 commit 390e8cf
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 62 deletions.
4 changes: 2 additions & 2 deletions src/vehicle-services/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export abstract class BaseService {
name = `${this.parent.accessory.displayName} ${name}`;

if (this.parent.accessory.getServiceById(definition, subtype)) {
this.log.info(`Restoring ${this.accessory.displayName} service ${name}`);
this.log.info(`Restoring service ${name}`);
} else {
this.log.info(`Creating ${this.accessory.displayName} service ${name}`);
this.log.info(`Creating service ${name}`);
}

this.service =
Expand Down
2 changes: 0 additions & 2 deletions src/vehicle-services/chargelimit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ export class ChargeLimitService extends BaseService {

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

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

this.parent.emitter.on("vehicle_data", (data) => {
Expand Down
73 changes: 30 additions & 43 deletions src/vehicle-services/climate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,82 +6,69 @@ import { BaseService } from "./base.js";

export class ClimateService extends BaseService {
displayUnits: number = 0; // Celsius by default
assumedState: number = 0;

constructor(parent: VehicleAccessory) {
super(parent, parent.platform.Service.Thermostat, "Climate", "climate");

const currentState = this.service
.getCharacteristic(
this.parent.platform.Characteristic.CurrentHeatingCoolingState
);
//.onGet(this.getCurrentState.bind(this));

const targetState = this.service
.getCharacteristic(
this.parent.platform.Characteristic.TargetHeatingCoolingState
)
//.onGet(this.getTargetState.bind(this))
.onSet((value) => this.setTargetState(value, targetState));
.onSet(async (value) => {
targetState.updateValue(value ?
this.parent.platform.Characteristic.TargetHeatingCoolingState.AUTO :
this.parent.platform.Characteristic.TargetHeatingCoolingState.OFF
);
await this.parent.wakeUpAndWait()
.then(() => value
? this.vehicle.auto_conditioning_start().then(
() => currentState.updateValue(this.assumedState)
)
: this.vehicle.auto_conditioning_stop().then(
() => currentState.updateValue(this.parent.platform.Characteristic.CurrentHeatingCoolingState.OFF)
));
});

const currentTemp = this.service
.getCharacteristic(this.parent.platform.Characteristic.CurrentTemperature);
//.onGet(this.getCurrentTemp.bind(this));

const targetTemp = this.service
.getCharacteristic(this.parent.platform.Characteristic.TargetTemperature)
//.onGet(this.getTargetTemp.bind(this))
.onSet((value) => this.setTargetTemp(value, targetTemp));
.onSet(async (value) => {
await this.parent.wakeUpAndWait()
.then(() => this.vehicle.set_temps(value as number, value as number))
.then(() => targetTemp.updateValue(value));
});

this.service
.getCharacteristic(this.parent.platform.Characteristic.TemperatureDisplayUnits)
//.onGet(() => this.displayUnits)
.onSet((value: CharacteristicValue) => {
this.displayUnits = value as number;
});

/*this.service.setCharacteristic(
this.parent.platform.Characteristic.TemperatureDisplayUnits,
this.parent.platform.Characteristic.TemperatureDisplayUnits.CELSIUS
);*/

this.parent.emitter.on("vehicle_data", (data) => {
if (data.climate_state.is_climate_on === false) {
this.assumedState = data.climate_state.inside_temp < data.climate_state.driver_temp_setting
? this.platform.Characteristic.CurrentHeatingCoolingState.HEAT
: this.platform.Characteristic.CurrentHeatingCoolingState.COOL;

if (data.climate_state.is_climate_on) {
// On
currentState.updateValue(this.assumedState);
targetState.updateValue(this.platform.Characteristic.TargetHeatingCoolingState.AUTO);
} else {
// Off
currentState.updateValue(this.platform.Characteristic.CurrentHeatingCoolingState.OFF);
targetState.updateValue(this.platform.Characteristic.TargetHeatingCoolingState.OFF);
} else {
// On
currentState.updateValue(data.climate_state.inside_temp < data.climate_state.driver_temp_setting
? this.platform.Characteristic.CurrentHeatingCoolingState.HEAT
: this.platform.Characteristic.CurrentHeatingCoolingState.COOL
);
targetState.updateValue(this.platform.Characteristic.TargetHeatingCoolingState.AUTO);
}

currentTemp.updateValue(data.climate_state.inside_temp);
targetTemp.updateValue(data.climate_state.driver_temp_setting);
});
}

async setTargetState(value: CharacteristicValue, characteristic: Characteristic): Promise<void> {
await this.parent.wakeUpAndWait()
.then(() => value
? this.vehicle
.auto_conditioning_start()
.then(
() =>
characteristic.updateValue(this.parent.platform.Characteristic.TargetHeatingCoolingState.AUTO)
)
: this.vehicle
.auto_conditioning_stop()
.then(
() =>
characteristic.updateValue(this.parent.platform.Characteristic.TargetHeatingCoolingState.OFF)
));
}

async setTargetTemp(value: CharacteristicValue, characteristic: Characteristic): Promise<void> {
await this.parent.wakeUpAndWait().then(() =>
this.vehicle.set_temps(value as number, value as number)
.then(() => characteristic.updateValue(value)));
}
}
29 changes: 14 additions & 15 deletions src/vehicle-services/door.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,27 @@ export class DoorService extends BaseService {

const currentPosition = this.service
.getCharacteristic(this.parent.platform.Characteristic.CurrentPosition);
//.onGet(this.getPosition.bind(this));

/*const positionState = this.service
.getCharacteristic(this.parent.platform.Characteristic.PositionState)
.onGet(() => this.getChargingState());*/

const targetPosition = this.service
.getCharacteristic(this.parent.platform.Characteristic.TargetPosition)
//.onGet(this.getPosition.bind(this))
.onSet((value) => this.setPosition(value, targetPosition));
.onSet(async (value) => {
targetPosition.updateValue(value);
value = value as number;
if (
(!this.open && value > 50) ||
(this.open && value < 50 && this.trunk === "rear")
) {
await this.parent.wakeUpAndWait()
.then(() => this.parent.vehicle.actuate_truck(this.trunk))
.then(() => {
currentPosition.updateValue(value);
});
}
});

this.parent.emitter.on("vehicle_data", (data) => {
this.open = data.vehicle_state[this.key] === 1;
Expand All @@ -38,16 +49,4 @@ export class DoorService extends BaseService {
targetPosition.updateValue(position);
});
}

async setPosition(value: CharacteristicValue, characteristic: Characteristic): Promise<void> {
value = value as number;
if (
(!this.open && value > 50) ||
(this.open && value < 50 && this.trunk === "rear")
) {
this.parent.wakeUpAndWait()
.then(() => this.parent.vehicle.actuate_truck(this.trunk))
.then(() => characteristic.updateValue(value));
}
}
}

0 comments on commit 390e8cf

Please sign in to comment.