-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement support for Bold Connect Controller
- Loading branch information
1 parent
fdee2eb
commit 08dd4c9
Showing
12 changed files
with
6,810 additions
and
809 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import {Device} from '../device'; | ||
import {BoldPlatform} from '../index'; | ||
import {PlatformAccessory} from 'homebridge'; | ||
import {DeviceConfig} from '../types'; | ||
|
||
export abstract class BaseAccessory extends Device { | ||
|
||
protected constructor( | ||
protected platform: BoldPlatform, | ||
protected accessory: PlatformAccessory, | ||
protected deviceConfig: DeviceConfig | ||
) { | ||
super(deviceConfig, platform.bold); | ||
|
||
this.platform.log.info(`Configuring accessory for device '${deviceConfig.name}'`); | ||
|
||
let informationService = this.accessory.getService(this.platform.hap.Service.AccessoryInformation); | ||
|
||
if (!informationService) { | ||
informationService = this.accessory.addService(this.platform.hap.Service.AccessoryInformation); | ||
} | ||
|
||
informationService.getCharacteristic(this.platform.hap.Characteristic.Name) | ||
.onGet(() => deviceConfig.name || 'Bold Device'); | ||
|
||
informationService.getCharacteristic(this.platform.hap.Characteristic.Manufacturer) | ||
.onGet(() => deviceConfig.model.make || 'Bold'); | ||
|
||
informationService.getCharacteristic(this.platform.hap.Characteristic.Model) | ||
.onGet(() => deviceConfig.model.model || 'Unknown'); | ||
|
||
informationService.getCharacteristic(this.platform.hap.Characteristic.SerialNumber) | ||
.onGet(() => deviceConfig.serial || 'Unknown'); | ||
|
||
informationService.getCharacteristic(this.platform.hap.Characteristic.FirmwareRevision) | ||
.onGet(() => `${deviceConfig.actualFirmwareVersion || 'Unknown'}`); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { BoldPlatform } from '../'; | ||
import { PlatformAccessory } from 'homebridge'; | ||
import { DeviceConfig } from '../types'; | ||
import { BaseAccessory } from './base-accessory'; | ||
|
||
enum LockState { | ||
Unlocked = 0, | ||
Locked = 1 | ||
} | ||
|
||
export class LockAccessory extends BaseAccessory { | ||
|
||
constructor( | ||
protected platform: BoldPlatform, | ||
protected accessory: PlatformAccessory, | ||
protected deviceConfig: DeviceConfig | ||
) { | ||
super(platform, accessory, deviceConfig); | ||
|
||
let lockService = accessory.getService(this.platform.hap.Service.LockMechanism); | ||
|
||
if (!lockService) { | ||
lockService = accessory.addService(this.platform.hap.Service.LockMechanism); | ||
} | ||
|
||
let currentState = lockService.getCharacteristic(this.platform.hap.Characteristic.LockCurrentState); | ||
let targetState = lockService.getCharacteristic(this.platform.hap.Characteristic.LockTargetState); | ||
|
||
currentState.onGet(() => this.isActivated ? LockState.Unlocked : LockState.Locked); | ||
|
||
targetState.onGet(() => this.isActivated ? LockState.Unlocked : LockState.Locked) | ||
.onSet((newState) => this.setState(newState == LockState.Unlocked)); | ||
} | ||
|
||
onStateChange(activated: boolean): void { | ||
let service = this.accessory.getService(this.platform.hap.Service.LockMechanism); | ||
|
||
service?.getCharacteristic(this.platform.hap.Characteristic.LockCurrentState) | ||
.updateValue(activated ? LockState.Unlocked : LockState.Locked); | ||
|
||
service?.getCharacteristic(this.platform.hap.Characteristic.LockTargetState) | ||
.updateValue(activated ? LockState.Unlocked : LockState.Locked); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { BoldPlatform } from '../'; | ||
import { DeviceConfig } from '../types'; | ||
import { BaseAccessory } from './base-accessory'; | ||
import { PlatformAccessory } from 'homebridge'; | ||
|
||
export class SwitchAccessory extends BaseAccessory { | ||
|
||
constructor( | ||
protected platform: BoldPlatform, | ||
protected accessory: PlatformAccessory, | ||
protected deviceConfig: DeviceConfig | ||
) { | ||
super(platform, accessory, deviceConfig); | ||
|
||
let switchService = accessory.getService(this.platform.hap.Service.Switch); | ||
|
||
if (!switchService) { | ||
switchService = accessory.addService(this.platform.hap.Service.Switch); | ||
} | ||
|
||
let on = switchService.getCharacteristic(this.platform.hap.Characteristic.On); | ||
|
||
on.onGet(() => this.isActivated) | ||
.onSet((newState) => this.setState(newState == true)); | ||
} | ||
|
||
onStateChange(activated: boolean): void { | ||
let service = this.accessory.getService(this.platform.hap.Service.Switch); | ||
|
||
service?.getCharacteristic(this.platform.hap.Characteristic.On) | ||
.updateValue(activated); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { BoldAPI } from './bold'; | ||
import {DeviceConfig} from './types'; | ||
|
||
/// Class representing an activatable Bold device. | ||
/// This class is used by each accessory to interact with the Bold API. | ||
export abstract class Device { | ||
|
||
// Getters | ||
|
||
/// Whether the device is currently activated | ||
get isActivated(): boolean { | ||
return this.activatedUntil > new Date(); | ||
} | ||
|
||
// Private properties | ||
|
||
private activatedUntil: Date; | ||
|
||
constructor( | ||
private readonly config: DeviceConfig, | ||
private readonly bold: BoldAPI | ||
) { | ||
// Initialize with current Date, making it not activated. | ||
this.activatedUntil = new Date(); | ||
} | ||
|
||
// Methods | ||
|
||
async setState(activate: boolean): Promise<void> { | ||
if (activate) { | ||
if (await this.bold.activate(this.config.id)) { | ||
// Store activation end date | ||
let date = new Date(); | ||
|
||
date.setSeconds(date.getSeconds() + this.config.settings.activationTime); | ||
|
||
this.activatedUntil = date; | ||
|
||
// Call state change handler | ||
this.onStateChange(true); | ||
|
||
// Deactivate after activation time | ||
setTimeout(() => { | ||
this.setState(false); | ||
}, this.config.settings.activationTime * 1000); | ||
} else { | ||
// If failed to activate, set device state to deactivated. | ||
await this.setState(false); | ||
} | ||
} else { | ||
// Set activation end date to now | ||
this.activatedUntil = new Date(); | ||
|
||
// Call state change handler | ||
this.onStateChange(false); | ||
} | ||
} | ||
|
||
abstract onStateChange(activated: boolean): void; | ||
|
||
} |
Oops, something went wrong.