-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
system to make adding configuration easy #11
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,7 +69,16 @@ export class Platform implements DynamicPlatformPlugin { | |
|
||
// create the accessory handler for the restored accessory | ||
// this is imported from `platformAccessory.ts` | ||
new Car(this, existingAccessory, this.kiaConnect, car.name, car.vin, car.targetTemperature, car.refreshInterval); | ||
new Car( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We will pass all other fields in the car object from the configuration to |
||
this, | ||
existingAccessory, | ||
this.kiaConnect, | ||
car.name, | ||
car.vin, | ||
{ | ||
...car, | ||
}, | ||
); | ||
|
||
// it is possible to remove platform accessories at any time using `api.unregisterPlatformAccessories`, eg.: | ||
// remove platform accessories when no longer present | ||
|
@@ -84,7 +93,17 @@ export class Platform implements DynamicPlatformPlugin { | |
|
||
// create the accessory handler for the newly create accessory | ||
// this is imported from `platformAccessory.ts` | ||
new Car(this, accessory, this.kiaConnect, car.name, car.vin, car.targetTemperature, car.refreshInterval); | ||
new Car( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here! |
||
this, | ||
accessory, | ||
this.kiaConnect, | ||
car.name, | ||
car.vin, | ||
{ | ||
targetTemperature: car.targetTemperature, | ||
refreshInterval: car.refreshInterval, | ||
}, | ||
); | ||
|
||
// link the accessory to your platform | ||
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge'; | ||
|
||
import { Platform } from './platform'; | ||
import { VehicleInfoList } from './kiaconnect/types'; | ||
import { OneToTen, VehicleInfoList } from './kiaconnect/types'; | ||
import { KiaConnect } from './kiaconnect/client'; | ||
|
||
type CarConfig = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a typed version of what we expect. |
||
ignitionOnDuration: OneToTen; // in minutes | ||
refreshInterval: number; // in milliseconds | ||
targetTemperature: string; // in farenheit | ||
}; | ||
|
||
type Door = { | ||
id: string; | ||
name: string; | ||
|
@@ -25,16 +31,19 @@ export class Car { | |
private engine: Service; | ||
private lock: Service; | ||
private target: Target; | ||
private config: CarConfig; | ||
|
||
constructor( | ||
private readonly platform: Platform, | ||
private readonly accessory: PlatformAccessory, | ||
private readonly kiaConnect: KiaConnect, | ||
private readonly name: string, | ||
private readonly vin: string, | ||
private readonly targetTemperature: string, | ||
private readonly refreshInterval: number, | ||
config: Partial<CarConfig>, | ||
) { | ||
// Initialize config to default values | ||
this.config = initializeConfig(config); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We will take the input from the user specified config, and make sure every option is either set to the user provided value, or some default. |
||
|
||
// target indicate where we want each value to be. These are sane defaults. | ||
this.target = { | ||
lockState: 0, | ||
|
@@ -287,7 +296,7 @@ export class Car { | |
if (value) { | ||
// Turn on the engine | ||
this.platform.log.info('Turning on the engine'); | ||
xid = await this.kiaConnect.startClimate(this.vin, this.targetTemperature); | ||
xid = await this.kiaConnect.startClimate(this.vin, this.config.targetTemperature, this.config.ignitionOnDuration); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use |
||
} else { | ||
// Turn off the engine | ||
this.platform.log.info('Turning off the engine'); | ||
|
@@ -316,14 +325,22 @@ export class Car { | |
|
||
private async startControlLoop() { | ||
this.platform.log.info('Starting control loop', { | ||
refreshInterval: this.refreshInterval, | ||
refreshInterval: this.config.refreshInterval, | ||
vin: this.vin, | ||
}); | ||
this.refresh(); | ||
setInterval(this.refresh.bind(this), this.refreshInterval || 1000 * 60 * 60); // Every 1 hour | ||
setInterval(this.refresh.bind(this), this.config.refreshInterval || 1000 * 60 * 60); // Every 1 hour | ||
} | ||
} | ||
|
||
const initializeConfig = (config: Partial<CarConfig>): CarConfig => { | ||
return { | ||
ignitionOnDuration: config.ignitionOnDuration || 10, | ||
refreshInterval: config.refreshInterval || 1000 * 60 * 60, // Every 1 hour | ||
targetTemperature: config.targetTemperature || '72', | ||
}; | ||
}; | ||
|
||
type VehicleInfo = { | ||
areDoorsLocked: boolean; | ||
areDoorsClosed: { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will always need to add any new configuration option to this file. This is what Homebridge uses to create the UI.