generated from homebridge/homebridge-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 13
/
platformAccessory.ts
57 lines (54 loc) · 1.91 KB
/
platformAccessory.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { VehicleStatus } from 'bluelinky/dist/interfaces/common.interfaces'
import { Vehicle } from 'bluelinky/dist/vehicles/vehicle'
import { EventEmitter } from 'events'
import { PlatformAccessory } from 'homebridge'
import { HyundaiConfig } from './config'
import { HyundaiPlatform } from './platform'
import initServices from './services'
export class VehicleAccessory extends EventEmitter {
private isFetching = false
constructor(
public readonly platform: HyundaiPlatform,
public readonly accessory: PlatformAccessory,
public readonly vehicle: Vehicle
) {
super()
this.setInformation()
initServices(this)
setInterval(
this.fetchStatus.bind(this),
(<HyundaiConfig>this.platform.config).refreshInterval * 1000
)
}
fetchStatus(): void {
if (!this.isFetching) {
this.isFetching = true
this.vehicle
.status({ refresh: false, parsed: true })
.then((response) => {
this.platform.log.debug('Received status update', response)
this.emit('update', <VehicleStatus>response)
this.isFetching = false
})
.catch((error) => {
this.platform.log.error('Status fetch error', error)
})
}
}
setInformation(): void {
this.accessory
?.getService(this.platform.Service.AccessoryInformation)
?.setCharacteristic(
this.platform.Characteristic.Manufacturer,
'Hyundai'
)
.setCharacteristic(
this.platform.Characteristic.Model,
this.accessory.context.device.name
)
.setCharacteristic(
this.platform.Characteristic.SerialNumber,
this.accessory.context.device.vin
)
}
}