Skip to content

Commit

Permalink
Add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Jul 21, 2024
1 parent 9dc04b2 commit 5d93468
Show file tree
Hide file tree
Showing 19 changed files with 67 additions and 33 deletions.
4 changes: 3 additions & 1 deletion src/energy-services/autonomous.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export class Autonomous extends BaseService {
const on = this.service
.getCharacteristic(this.parent.platform.Characteristic.On)
.onSet(async (value) => {
await this.energy.operation(value ? "autonomous" : "self_consumption").then(() => on.updateValue(value));
await this.energy.operation(value ? "autonomous" : "self_consumption")
.then(() => on.updateValue(value))
.catch((e) => this.log.error(`${this.name} energy operation failed: ${e}`));
});

this.parent.emitter.on("site_info", (data) => {
Expand Down
9 changes: 5 additions & 4 deletions src/energy-services/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export abstract class BaseService {
protected accessory: PlatformAccessory<EnergyContext>;
protected emitter: EventEmitter<EnergyDataEvent>;
protected energy: EnergySpecific;
protected name: string;

constructor(
protected parent: EnergyAccessory,
Expand All @@ -24,17 +25,17 @@ export abstract class BaseService {
this.emitter = parent.emitter;
this.energy = parent.energy;

name = parent.platform.config.prefixName ? `${this.parent.accessory.displayName} ${name}` : name;
this.name = parent.platform.config.prefixName ? `${this.parent.accessory.displayName} ${name}` : name;

this.service =
this.accessory.getServiceById(definition, subtype) ||
this.accessory.addService(definition, name, subtype);
this.accessory.addService(definition, this.name, subtype);

// Set the configured name if it's not already set since Homekit wont use the display name
const ConfiguredName = this.service.getCharacteristic(this.platform.Characteristic.ConfiguredName);
if (!ConfiguredName.value) {
this.log.debug(`Configured name changing to ${name}`);
ConfiguredName.updateValue(name);
this.log.debug(`Configured name changing to ${this.name}`);
ConfiguredName.updateValue(this.name);
}
}
}
4 changes: 3 additions & 1 deletion src/energy-services/changefromgrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export class ChargeFromGrid extends BaseService {
.onSet(async (value) => {
if (typeof value === "boolean") {
// This switch is the inverse of the API value
await this.energy.grid_import_export(!value).then(() => on.updateValue(value));
await this.energy.grid_import_export(!value)
.then(() => on.updateValue(value))
.catch((e) => this.log.error(`${this.name} energy grid import export failed: ${e}`));
}
});

Expand Down
4 changes: 3 additions & 1 deletion src/energy-services/exportbattery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export class ExportBattery extends BaseService {
const on = this.service
.getCharacteristic(this.parent.platform.Characteristic.On)
.onSet(async (value) => {
await this.energy.grid_import_export(null, value ? "battery_ok" : "pv_only").then(() => on.updateValue(value));
await this.energy.grid_import_export(null, value ? "battery_ok" : "pv_only")
.then(() => on.updateValue(value))
.catch((e) => this.log.error(`${this.name} energy grid_import_export failed: ${e}`));
});

this.parent.emitter.on("site_info", (data) => {
Expand Down
4 changes: 3 additions & 1 deletion src/energy-services/stormwatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export class StormWatch extends BaseService {
const on = this.service
.getCharacteristic(this.parent.platform.Characteristic.On)
.onSet(async (value) => {
await this.energy.storm_mode(!!value).then(() => on.updateValue(value));
await this.energy.storm_mode(!!value)
.then(() => on.updateValue(value))
.catch((e) => this.log.error(`${this.name} energy storm_mode failed: ${e}`));
});

this.parent.emitter.on("site_info", (data) => {
Expand Down
7 changes: 4 additions & 3 deletions src/vehicle-services/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export abstract class BaseService {
protected accessory: PlatformAccessory<VehicleContext>;
protected emitter: EventEmitter<VehicleDataEvent>;
protected vehicle: VehicleSpecific;
protected name: string;

constructor(
protected parent: VehicleAccessory,
Expand All @@ -24,7 +25,7 @@ export abstract class BaseService {
this.emitter = parent.emitter;
this.vehicle = parent.vehicle;

name = parent.platform.config.prefixName ? `${this.parent.accessory.displayName} ${name}` : name;
this.name = parent.platform.config.prefixName ? `${this.parent.accessory.displayName} ${name}` : name;

this.service =
this.accessory.getServiceById(definition, subtype) ||
Expand All @@ -33,8 +34,8 @@ export abstract class BaseService {
// Set the configured name if it's not already set since Homekit wont use the display name
const ConfiguredName = this.service.getCharacteristic(this.platform.Characteristic.ConfiguredName);
if (!ConfiguredName.value) {
this.log.debug(`Configured name changing to ${name}`);
ConfiguredName.updateValue(name);
this.log.debug(`Configured name changing to ${this.name}`);
ConfiguredName.updateValue(this.name);
}
}
}
3 changes: 2 additions & 1 deletion src/vehicle-services/chargecurrent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class ChargeCurrentService extends BaseService {

await this.parent.wakeUpAndWait()
.then(() => this.parent.vehicle.set_charging_amps(value))
.then(() => characteristic.updateValue(value));
.then(() => characteristic.updateValue(value))
.catch((e) => this.log.error(`${this.name} vehicle set_charging_amps failed: ${e}`));
}
}
3 changes: 2 additions & 1 deletion src/vehicle-services/chargelimit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class ChargeLimitService extends BaseService {
value = Math.max(this.min, Math.min(this.max, value as number));
await this.parent.wakeUpAndWait()
.then(() => this.vehicle.set_charge_limit(value))
.then(() => characteristic.updateValue(value));
.then(() => characteristic.updateValue(value))
.catch((e) => this.log.error(`${this.name} vehicle set_charge_limit failed: ${e}`));
}
}
7 changes: 5 additions & 2 deletions src/vehicle-services/chargeport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ export class ChargePortService extends BaseService {
this.vehicle.charge_port_door_close()
.then(() =>
currentState.updateValue(1)
) :
)
.catch((e) => this.log.error(`${this.name} vehicle charge_port_door_close failed: ${e}`))
:
this.vehicle.charge_port_door_open()
.then(() =>
currentState.updateValue(0)
)
);
.catch((e) => this.log.error(`${this.name} vehicle charge_port_door_open failed: ${e}`))
)

Check warning on line 28 in src/vehicle-services/chargeport.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Missing semicolon

Check warning on line 28 in src/vehicle-services/chargeport.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Missing semicolon
});

this.parent.emitter.on("vehicle_data", (data) => {
Expand Down
3 changes: 2 additions & 1 deletion src/vehicle-services/chargeswitch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export class ChargeSwitchService extends BaseService {
.onSet(async (value) => {
this.parent.wakeUpAndWait()
.then(() => value ? this.parent.vehicle.charge_start() : this.parent.vehicle.charge_stop())
.then(() => on.updateValue(value));
.then(() => on.updateValue(value))
.catch((e) => this.log.error(`${this.name} vehicle charge_start failed: ${e}`));
});

this.parent.emitter.on("vehicle_data", (data) => {
Expand Down
14 changes: 8 additions & 6 deletions src/vehicle-services/climate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ export class ClimateService extends BaseService {
);
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)
));
? this.vehicle.auto_conditioning_start()
.then(() => currentState.updateValue(this.assumedState))
.catch((e) => this.log.error(`${this.name} vehicle auto_conditioning_start failed: ${e}`))
:
this.vehicle.auto_conditioning_stop()
.then(() => currentState.updateValue(this.parent.platform.Characteristic.CurrentHeatingCoolingState.OFF))
.catch((e) => this.log.error(`${this.name} vehicle auto_conditioning_stop failed: ${e}`))
);
});

const currentTemp = this.service
Expand Down
3 changes: 2 additions & 1 deletion src/vehicle-services/defrost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export class DefrostService extends BaseService {
.onSet(async (value) => {
await this.parent.wakeUpAndWait()
.then(() => this.vehicle.set_preconditioning_max(value as boolean, false))
.then(() => on.updateValue(value));
.then(() => on.updateValue(value))
.catch((e) => this.log.error(`${this.name} vehicle set_preconditioning_max failed: ${e}`))

Check warning on line 14 in src/vehicle-services/defrost.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Missing semicolon

Check warning on line 14 in src/vehicle-services/defrost.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Missing semicolon
});

this.parent.emitter.on("vehicle_data", (data) => {
Expand Down
5 changes: 2 additions & 3 deletions src/vehicle-services/door.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ export class DoorService extends BaseService {
) {
await this.parent.wakeUpAndWait()
.then(() => this.parent.vehicle.actuate_truck(this.trunk))
.then(() => {
currentPosition.updateValue(value);
});
.then(() => currentPosition.updateValue(value))
.catch((e) => this.log.error(`${this.name} vehicle actuate_truck failed: ${e}`))

Check warning on line 39 in src/vehicle-services/door.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Missing semicolon

Check warning on line 39 in src/vehicle-services/door.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Missing semicolon
}
});

Expand Down
3 changes: 2 additions & 1 deletion src/vehicle-services/homelink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export class HomelinkService extends BaseService {
.then(() => {
current.updateValue(false);
target.updateValue(false);
});
})
.catch((e) => this.log.error(`${this.name} vehicle actuate_truck failed: ${e}`))

Check warning on line 23 in src/vehicle-services/homelink.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Missing semicolon

Check warning on line 23 in src/vehicle-services/homelink.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Missing semicolon
}
})
.updateValue(this.platform.Characteristic.TargetDoorState.CLOSED);
Expand Down
12 changes: 10 additions & 2 deletions src/vehicle-services/information.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
// https://developers.homebridge.io/#/service/AccessoryInformation

import { Service } from "homebridge";
import { Logging, Service } from "homebridge";
import { VehicleAccessory } from "../vehicle.js";

export class AccessoryInformationService {
service: Service;
name: string;
log: Logging;


constructor(private parent: VehicleAccessory) {
this.name = this.parent.accessory.displayName

Check warning on line 13 in src/vehicle-services/information.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Missing semicolon

Check warning on line 13 in src/vehicle-services/information.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Missing semicolon
this.log = this.parent.platform.log;

this.service = this.parent.accessory.getService(this.parent.platform.Service.AccessoryInformation)!
.setCharacteristic(
this.parent.platform.Characteristic.Manufacturer,
Expand Down Expand Up @@ -38,6 +44,8 @@ export class AccessoryInformationService {
}

async setIdentify(): Promise<void> {
await this.parent.wakeUpAndWait().then(() => this.parent.vehicle.flash_lights());
await this.parent.wakeUpAndWait()
.then(() => this.parent.vehicle.flash_lights())
.catch((e) => this.log.error(`${this.name} vehicle flash_lights failed: ${e}`));
}
}
5 changes: 4 additions & 1 deletion src/vehicle-services/lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ export class LockService extends BaseService {
await this.parent.wakeUpAndWait().then(() =>
value ?
this.parent.vehicle.door_lock()
.then(() => currentState.updateValue(1)) :
.then(() => currentState.updateValue(1))
.catch((e) => this.log.error(`${this.name} vehicle door_lock failed: ${e}`))
:
this.parent.vehicle.door_unlock()
.then(() => currentState.updateValue(0))
.catch((e) => this.log.error(`${this.name} vehicle door_unlock failed: ${e}`))
);
});

Expand Down
3 changes: 2 additions & 1 deletion src/vehicle-services/sentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export class SentryService extends BaseService {
target.updateValue(value);
await this.parent.wakeUpAndWait()
.then(() => this.vehicle.set_sentry_mode(value !== 3))
.then(() => current.updateValue(value));
.then(() => current.updateValue(value))
.catch((e) => this.log.error(`${this.name} vehicle set_sentry_mode failed: ${e}`))

Check warning on line 19 in src/vehicle-services/sentry.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Missing semicolon

Check warning on line 19 in src/vehicle-services/sentry.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Missing semicolon
});

this.parent.emitter.on("vehicle_data", (data) => {
Expand Down
4 changes: 3 additions & 1 deletion src/vehicle-services/wake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export class WakeService extends BaseService {
.getCharacteristic(this.parent.platform.Characteristic.On)
.onSet(async (value) => {
if (value) {
await this.parent.wakeUpAndWait().then(() => on.updateValue(true));
await this.parent.wakeUpAndWait()
.then(() => on.updateValue(true))
.catch((e) => this.log.error(`${this.name} vehicle wake_up failed: ${e}`))

Check warning on line 14 in src/vehicle-services/wake.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Missing semicolon

Check warning on line 14 in src/vehicle-services/wake.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Missing semicolon
}
});

Expand Down
3 changes: 2 additions & 1 deletion src/vehicle-services/windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class WindowService extends BaseService {

await this.parent.wakeUpAndWait()
.then(() => this.parent.vehicle.window_control(value === 100 ? "vent" : "close", this.latitude, this.longitude))
.then(() => characteristic.updateValue(value));
.then(() => characteristic.updateValue(value))
.catch((e) => this.log.error(`${this.name} vehicle window_control failed: ${e}`))

Check warning on line 45 in src/vehicle-services/windows.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Missing semicolon

Check warning on line 45 in src/vehicle-services/windows.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Missing semicolon
}
}

0 comments on commit 5d93468

Please sign in to comment.