Skip to content

Commit

Permalink
chore: improved readability
Browse files Browse the repository at this point in the history
  • Loading branch information
jasper-seinhorst committed Mar 20, 2023
1 parent 160b777 commit 0a9341e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 37 deletions.
6 changes: 3 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended" // uses the recommended rules from the @typescript-eslint/eslint-plugin
"plugin:@typescript-eslint/recommended"
],
"parserOptions": {
"ecmaVersion": 2018,
Expand All @@ -22,8 +22,8 @@
"curly": ["warn", "all"],
"brace-style": ["warn"],
"prefer-arrow-callback": ["warn"],
"max-len": ["warn", 140],
"no-console": ["warn"], // use the provided Homebridge log method instead
"max-len": ["warn", 175],
"no-console": ["warn"],
"no-non-null-assertion": ["off"],
"comma-spacing": ["error"],
"no-multi-spaces": ["warn", { "ignoreEOLComments": true }],
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"displayName": "Homebridge Porsche Taycan",
"name": "homebridge-porsche-taycan",
"version": "0.7.0",
"version": "0.7.1",
"description": "Control your Porsche Taycan through the home app",
"license": "Apache-2.0",
"author": "Jasper Seinhorst",
Expand Down
21 changes: 5 additions & 16 deletions src/ChargerAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { PorscheAccessory } from './PlatformTypes';
export class PorscheChargerAccessory implements PorscheAccessory {
public readonly Service: typeof Service = this.api.hap.Service;
public readonly Characteristic: typeof Characteristic = this.api.hap.Characteristic;
private chargerSensorService: Service;
private chargerService: Service;
private batteryService: Service;
private batteryLevel = 100;
private lowBatteryLevel: number;
Expand All @@ -15,16 +15,9 @@ export class PorscheChargerAccessory implements PorscheAccessory {
constructor(public config: PlatformConfig, public readonly log: Logger, public readonly api: API, public accessory: PlatformAccessory) {
this.lowBatteryLevel = this.config.lowBattery || 35;
this.sensorType = this.config.chargerDevice || 'occupancy';

if (this.sensorType === 'occupancy') {
this.chargerSensorService = this.accessory.getService(this.Service.OccupancySensor)
|| this.accessory.addService(this.Service.OccupancySensor);
} else {
this.chargerSensorService = this.accessory.getService(this.Service.ContactSensor)
|| this.accessory.addService(this.Service.ContactSensor);
}
this.batteryService = this.accessory.getService(this.Service.Battery)
|| this.accessory.addService(this.Service.Battery);
this.chargerService = this.accessory.getService(this.sensorType === 'occupancy' ? this.Service.OccupancySensor : this.Service.ContactSensor)
|| this.accessory.addService(this.sensorType === 'occupancy' ? this.Service.OccupancySensor : this.Service.ContactSensor);
this.batteryService = this.accessory.getService(this.Service.Battery) || this.accessory.addService(this.Service.Battery);
}

public update(emobilityInfo) {
Expand All @@ -46,11 +39,7 @@ export class PorscheChargerAccessory implements PorscheAccessory {

// Charging state
const isCharging = emobilityInfo.batteryChargeStatus.chargingState === 'CHARGING';
if (this.sensorType === 'occupancy') {
this.chargerSensorService.setCharacteristic(this.Characteristic.OccupancyDetected, isCharging);
} else {
this.chargerSensorService.setCharacteristic(this.Characteristic.ContactSensorState, isCharging);
}
this.chargerService.setCharacteristic(this.sensorType === 'occupancy' ? this.Characteristic.OccupancyDetected : this.Characteristic.ContactSensorState, isCharging);
this.batteryService.setCharacteristic(this.Characteristic.ChargingState, isCharging);
if (isCharging !== this.isCharging) {
this.isCharging = isCharging;
Expand Down
33 changes: 18 additions & 15 deletions src/Platform.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { API, DynamicPlatformPlugin, Logger, PlatformAccessory, PlatformConfig, Service, Characteristic } from 'homebridge';
import { PorscheChargerAccessory } from './ChargerAccessory';
import PorscheConnect, { EngineType, Vehicle } from './porsche-connect';
import { PlatformVehicle } from './PlatformTypes';
import { PlatformVehicle, PorscheAccessory } from './PlatformTypes';

export class PorscheTaycanPlatform implements DynamicPlatformPlugin {
public readonly Service: typeof Service = this.api.hap.Service;
public readonly Characteristic: typeof Characteristic = this.api.hap.Characteristic;
public readonly accessories: PlatformAccessory[] = [];
public readonly PorscheConnectAuth = new PorscheConnect({ username: this.config.username, password: this.config.password });
private platformVehicles: PlatformVehicle[] = [];
private pollInterval: number;
private heartBeatInterval: number;

constructor(public readonly log: Logger, public readonly config: PlatformConfig, public readonly api: API) {
this.pollInterval = config.pollInterval || 15;
this.heartBeatInterval = (config.pollInterval || 15) * 60 * 1000;
this.api.on('didFinishLaunching', () => {
this.initialise();
});
Expand All @@ -25,42 +25,45 @@ export class PorscheTaycanPlatform implements DynamicPlatformPlugin {
private async initialise() {
await this.discoverVehicles();

this.hearthBeat();
this.heartBeat();
setInterval(() => {
this.hearthBeat();
}, (this.pollInterval * 60 * 1000));
this.heartBeat();
}, this.heartBeatInterval);
}

private async discoverVehicles() {
const vehicles = await this.PorscheConnectAuth.getVehicles();
for (const vehicle of vehicles) {
if (vehicle.engineType === EngineType.BatteryPowered) {
const platformVehicle: PlatformVehicle = { vehicle, accessories: [] };
const uuidCharger = this.api.hap.uuid.generate(`${vehicle.vin}-charger`);
const existingAccessoryCharger = this.accessories.find(accessory => accessory.UUID === uuidCharger);
const uuid = this.api.hap.uuid.generate(`${vehicle.vin}-charger`);
const existingAccessory = this.accessories.find(accessory => accessory.UUID === uuid);
const accessoryName = `${vehicle.modelDescription} Charger`;

if (existingAccessoryCharger) {
platformVehicle.accessories.push(new PorscheChargerAccessory(this.config, this.log, this.api, existingAccessoryCharger));
if (existingAccessory) {
this.log.info(`Porsche ${vehicle.modelDescription} is ready`);
platformVehicle.accessories.push(new PorscheChargerAccessory(this.config, this.log, this.api, existingAccessory));
} else {
this.log.info(`Your Porsche ${vehicle.modelDescription} is added as accessory`);
const accessory = new this.api.platformAccessory(accessoryName, uuidCharger);
this.log.info(`Porsche ${vehicle.modelDescription} is added as accessory`);
const accessory = new this.api.platformAccessory(accessoryName, uuid);
accessory.context.device = vehicle;
platformVehicle.accessories.push(new PorscheChargerAccessory(this.config, this.log, this.api, accessory));
this.api.registerPlatformAccessories('homebridge-porsche-taycan', 'PorscheTaycan', [accessory]);
}

this.platformVehicles.push(platformVehicle);
} else {
this.log.info(`Your Porsche ${vehicle.modelDescription} is not supported, it has an unsupported engine type ${vehicle.engineType}`);
}
}
}

private async hearthBeat() {
this.platformVehicles.forEach(async platformVehicle => {
private async heartBeat() {
this.platformVehicles.forEach(async (platformVehicle: PlatformVehicle) => {
this.log.info(`Updating vehicle data for ${platformVehicle.vehicle.modelDescription}`);
const vehicle = new Vehicle(this.PorscheConnectAuth, platformVehicle.vehicle);
const emobilityInfo = await vehicle.getEmobilityInfo();
platformVehicle.accessories.forEach(accessory => {
platformVehicle.accessories.forEach((accessory: PorscheAccessory) => {
accessory.update(emobilityInfo);
});
});
Expand Down

0 comments on commit 0a9341e

Please sign in to comment.