1+ import { CharacteristicValue , PlatformAccessory } from 'homebridge' ;
2+ import { DeviceFunction } from '../models/device-functions' ;
3+ import { HubspacePlatform } from '../platform' ;
4+ import { isNullOrUndefined } from '../utils' ;
5+ import { HubspaceAccessory } from './hubspace-accessory' ;
6+
7+ export class OutletAccessory extends HubspaceAccessory {
8+
9+ /**
10+ * Crates a new instance of the accessory
11+ * @param platform Hubspace platform
12+ * @param accessory Platform accessory
13+ */
14+ constructor ( platform : HubspacePlatform , accessory : PlatformAccessory ) {
15+ super ( platform , accessory , platform . Service . Outlet ) ;
16+
17+ this . configurePower ( ) ;
18+ }
19+
20+ private configurePower ( ) : void {
21+ if ( this . supportsFunction ( DeviceFunction . OutletPower ) ) {
22+ this . service . getCharacteristic ( this . platform . Characteristic . On )
23+ . onGet ( this . getOn . bind ( this ) )
24+ . onSet ( this . setOn . bind ( this ) ) ;
25+ }
26+ }
27+
28+ private async getOn ( ) : Promise < CharacteristicValue > {
29+ // Try to get the value
30+ const value = await this . deviceService . getValueAsBoolean ( this . device . deviceId , DeviceFunction . OutletPower ) ;
31+
32+ // If the value is not defined then show 'Not Responding'
33+ if ( isNullOrUndefined ( value ) ) {
34+ throw new this . platform . api . hap . HapStatusError ( this . platform . api . hap . HAPStatus . SERVICE_COMMUNICATION_FAILURE ) ;
35+ }
36+
37+ // Otherwise return the value
38+ return value ! ;
39+ }
40+
41+ private async setOn ( value : CharacteristicValue ) : Promise < void > {
42+ await this . deviceService . setValue ( this . device . deviceId , DeviceFunction . OutletPower , value ) ;
43+ }
44+
45+ }
0 commit comments