Skip to content

Commit b9c8815

Browse files
authored
Merge pull request #19 from sajmonr/6-hubspace-outlet
6 hubspace outlet
2 parents 3ad6cce + a9c289a commit b9c8815

File tree

6 files changed

+61
-4
lines changed

6 files changed

+61
-4
lines changed

src/accessories/device-accessory-factory.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { HubspacePlatform } from '../platform';
55
import { FanAccessory } from './fan-accessory';
66
import { HubspaceAccessory } from './hubspace-accessory';
77
import { LightAccessory } from './light-accessory';
8+
import { OutletAccessory } from './outlet-accessory';
89

910
/**
1011
* Creates {@link HubspaceAccessory} for a specific {@link DeviceType}
@@ -20,6 +21,8 @@ export function createAccessoryForDevice(device: Device, platform: HubspacePlatf
2021
return new LightAccessory(platform, accessory);
2122
case DeviceType.Fan:
2223
return new FanAccessory(platform, accessory);
24+
case DeviceType.Outlet:
25+
return new OutletAccessory(platform, accessory);
2326
default:
2427
throw new Error(`Accessory of type '${device.type}' is not supported.`);
2528
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}

src/models/device-functions.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export enum DeviceFunction{
88
Brightness,
99
FanPower,
1010
FanSpeed
11+
OutletPower,
1112
}
1213

1314
/**
@@ -37,6 +38,11 @@ export const DeviceFunctions: DeviceFunctionDef[] = [
3738
attributeId: 6,
3839
functionClass: 'fan-speed',
3940
functionInstanceName: 'fan-speed'
41+
},
42+
{
43+
type: DeviceFunction.OutletPower,
44+
attributeId: 2,
45+
functionClass: 'power'
4046
}
4147
];
4248

src/models/device-type.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
/**
1+
/**
22
* Type of a device
33
*/
44
export enum DeviceType{
55
Light = 'light',
6-
Fan = 'fan'
6+
Fan = 'fan',
7+
Outlet = 'power-outlet'
78
}
89

910
/**
@@ -17,6 +18,8 @@ export function getDeviceTypeForKey(key: string): DeviceType | undefined{
1718
return DeviceType.Light;
1819
case 'fan':
1920
return DeviceType.Fan;
21+
case 'power-outlet':
22+
return DeviceType.Outlet;
2023
default:
2124
return undefined;
2225
}

src/responses/devices-response.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ export interface DeviceResponse{
1010
typeId: string;
1111
friendlyName: string;
1212
description: {
13-
key: string;
1413
device: {
1514
manufacturerName: string;
1615
model: string;
16+
deviceClass: string;
1717
};
1818
functions: DeviceFunctionResponse[];
1919
};

src/services/discovery.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export class DiscoveryService{
108108
}
109109

110110
private mapDeviceResponseToModel(response: DeviceResponse): Device | undefined{
111-
const type = getDeviceTypeForKey(response.description.key);
111+
const type = getDeviceTypeForKey(response.description.device.deviceClass);
112112

113113
if(!type) return undefined;
114114

0 commit comments

Comments
 (0)