Skip to content

Commit

Permalink
Remove direct hap-nodejs references
Browse files Browse the repository at this point in the history
  • Loading branch information
milo526 committed Jul 26, 2020
1 parent 8c6bff6 commit 09113dd
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/TuyaWebApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export class TuyaWebApi {
}
}

private debouncedDeviceDiscovery = debounce(this.discoverDevices, 100, {maxWait: 1000})
private debouncedDeviceDiscovery = debounce(this.discoverDevices, 500, {maxWait: 1000})

private outstandingDeviceStateRequests: Map<string, DebouncedPromise<TuyaDeviceState>> = new Map()

Expand Down
26 changes: 13 additions & 13 deletions src/accessories/BaseAccessory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {HomebridgeAccessory, TuyaWebPlatform} from '../platform';
import {Categories, Logger, WithUUID} from 'homebridge';
import {Characteristic, CharacteristicGetCallback, CharacteristicValue, Nullable, Service} from 'hap-nodejs';
import {Characteristic, CharacteristicGetCallback, CharacteristicValue, Nullable, Service} from 'homebridge';
import {TuyaDevice} from '../TuyaWebApi';
import {PLUGIN_NAME} from '../settings';
import {inspect} from 'util';
Expand Down Expand Up @@ -65,19 +65,19 @@ export abstract class BaseAccessory<DeviceConfig extends TuyaDevice = TuyaDevice

switch (categoryType) {
case Categories.LIGHTBULB:
this.serviceType = Service.Lightbulb;
this.serviceType = platform.Service.Lightbulb;
break;
case Categories.SWITCH:
this.serviceType = Service.Switch;
this.serviceType = platform.Service.Switch;
break;
case Categories.OUTLET:
this.serviceType = Service.Outlet;
this.serviceType = platform.Service.Outlet;
break;
case Categories.FAN:
this.serviceType = Service.Fanv2;
this.serviceType = platform.Service.Fanv2;
break;
default:
this.serviceType = Service.AccessoryInformation;
this.serviceType = platform.Service.AccessoryInformation;
}

// Retrieve existing of create new Bridged Accessory
Expand Down Expand Up @@ -106,7 +106,7 @@ export abstract class BaseAccessory<DeviceConfig extends TuyaDevice = TuyaDevice
// Create service
this.service = this.homebridgeAccessory.getService(this.serviceType);
if (this.service) {
this.service.setCharacteristic(Characteristic.Name, this.deviceConfig.name);
this.service.setCharacteristic(platform.Characteristic.Name, this.deviceConfig.name);
} else {
this.log.debug('Creating New Service %s', this.deviceConfig.id);
this.service = this.homebridgeAccessory.addService(this.serviceType, this.deviceConfig.name);
Expand Down Expand Up @@ -151,13 +151,13 @@ export abstract class BaseAccessory<DeviceConfig extends TuyaDevice = TuyaDevice
this.homebridgeAccessory.displayName = device.name;
this.homebridgeAccessory._associatedHAPAccessory.displayName = device.name;
const accessoryInformationService = (
this.homebridgeAccessory.getService(Service.AccessoryInformation) ||
this.homebridgeAccessory.addService(Service.AccessoryInformation));
setCharacteristic(Characteristic.Name, device.name);
this.homebridgeAccessory.getService(this.platform.Service.AccessoryInformation) ||
this.homebridgeAccessory.addService(this.platform.Service.AccessoryInformation));
setCharacteristic(this.platform.Characteristic.Name, device.name);

setCharacteristic(Characteristic.SerialNumber, this.deviceConfig.id);
setCharacteristic(Characteristic.Manufacturer, PLUGIN_NAME);
setCharacteristic(Characteristic.Model, this.categoryType);
setCharacteristic(this.platform.Characteristic.SerialNumber, this.deviceConfig.id);
setCharacteristic(this.platform.Characteristic.Manufacturer, PLUGIN_NAME);
setCharacteristic(this.platform.Characteristic.Model, this.categoryType);

// Update device specific state
this.updateState(device.data);
Expand Down
3 changes: 1 addition & 2 deletions src/accessories/ColorAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ import {TuyaDevice} from '../TuyaWebApi';
import debounce from 'lodash.debounce';
import {DebouncedPromise} from '../helpers/DebouncedPromise';
import {BrightnessCharacteristic, HueCharacteristic, SaturationCharacteristic} from './characteristics';
import {Characteristic} from 'hap-nodejs';


export abstract class ColorAccessory<DeviceConfig extends TuyaDevice = TuyaDevice> extends BaseAccessory<DeviceConfig> {
private async setRemoteColor(color: { hue: number, saturation: number }): Promise<void> {
const cachedBrightness = this.getCachedState(Characteristic.Brightness);
const cachedBrightness = this.getCachedState(this.platform.Characteristic.Brightness);
const brightness = Number(cachedBrightness ? cachedBrightness : BrightnessCharacteristic.DEFAULT_VALUE);
const tuyaData = {
hue: color.hue,
Expand Down
6 changes: 3 additions & 3 deletions src/accessories/characteristics/base.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {BaseAccessory, CharacteristicConstructor} from '../BaseAccessory';
import {LogLevel} from 'homebridge';
import {Characteristic, CharacteristicGetCallback, CharacteristicSetCallback, CharacteristicValue} from 'hap-nodejs';
import {Characteristic, CharacteristicGetCallback, CharacteristicSetCallback, CharacteristicValue} from 'homebridge';

export abstract class TuyaWebCharacteristic<Accessory extends BaseAccessory = BaseAccessory> {
public static Title: string;
public static HomekitCharacteristic: CharacteristicConstructor
public static HomekitCharacteristic: (accessory: BaseAccessory) => CharacteristicConstructor;

public static isSupportedByAccessory<Accessory extends BaseAccessory>(accessory: Accessory): boolean {
accessory.log.error('Method `isSupportedByAccessory must be overwritten by Characteristic, missing for %s', this.Title);
Expand Down Expand Up @@ -34,7 +34,7 @@ export abstract class TuyaWebCharacteristic<Accessory extends BaseAccessory = Ba
}

public get homekitCharacteristic(): CharacteristicConstructor {
return this.staticInstance.HomekitCharacteristic;
return this.staticInstance.HomekitCharacteristic(this.accessory);
}

private log(logLevel: LogLevel, message: string, ...args: unknown[]): void {
Expand Down
8 changes: 6 additions & 2 deletions src/accessories/characteristics/brightness.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import {TuyaDevice, TuyaDeviceState} from '../../TuyaWebApi';
import {Characteristic, CharacteristicGetCallback, CharacteristicSetCallback, CharacteristicValue} from 'hap-nodejs';
import {CharacteristicGetCallback, CharacteristicSetCallback, CharacteristicValue} from 'homebridge';
import {COLOR_MODES, ColorModes} from './index';
import {XOR} from 'ts-xor';
import {inspect} from 'util';
import {TuyaWebCharacteristic} from './base';
import {BaseAccessory} from '../BaseAccessory';

export type BrightnessCharacteristicData =
XOR<{ brightness: string }, { color: { brightness?: string }, color_mode: ColorModes }>
type DeviceWithBrightnessCharacteristic = TuyaDevice<TuyaDeviceState & BrightnessCharacteristicData>

export class BrightnessCharacteristic extends TuyaWebCharacteristic {
public static Title = 'Characteristic.Brightness'
public static HomekitCharacteristic = Characteristic.Brightness;

public static HomekitCharacteristic(accessory: BaseAccessory) {
return accessory.platform.Characteristic.Brightness;
}

public static DEFAULT_VALUE = 100;

Expand Down
8 changes: 6 additions & 2 deletions src/accessories/characteristics/colorTemperature.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {TuyaDevice, TuyaDeviceState} from '../../TuyaWebApi';
import {Characteristic, CharacteristicGetCallback, CharacteristicSetCallback, CharacteristicValue} from 'hap-nodejs';
import {CharacteristicGetCallback, CharacteristicSetCallback, CharacteristicValue} from 'homebridge';
import {TuyaWebCharacteristic} from './base';
import {MapRange} from '../../helpers/MapRange';
import {BaseAccessory} from '../BaseAccessory';

export type ColorTemperatureCharacteristicData = { color_temp: number }
type DeviceWithColorTemperatureCharacteristic = TuyaDevice<TuyaDeviceState & ColorTemperatureCharacteristicData>
Expand All @@ -11,7 +12,10 @@ type DeviceWithColorTemperatureCharacteristic = TuyaDevice<TuyaDeviceState & Col

export class ColorTemperatureCharacteristic extends TuyaWebCharacteristic {
public static Title = 'Characteristic.ColorTemperature'
public static HomekitCharacteristic = Characteristic.ColorTemperature;

public static HomekitCharacteristic(accessory: BaseAccessory) {
return accessory.platform.Characteristic.ColorTemperature;
}

private rangeMapper = MapRange.from(1000000 / 140, 1000000 / 500).to(10000, 1000)

Expand Down
8 changes: 6 additions & 2 deletions src/accessories/characteristics/hue.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import {TuyaDevice, TuyaDeviceState} from '../../TuyaWebApi';
import {Characteristic, CharacteristicGetCallback, CharacteristicSetCallback, CharacteristicValue} from 'hap-nodejs';
import {CharacteristicGetCallback, CharacteristicSetCallback, CharacteristicValue} from 'homebridge';
import {COLOR_MODES, ColorModes} from './index';
import {TuyaWebCharacteristic} from './base';
import {ColorAccessory} from '../ColorAccessory';
import {BaseAccessory} from '../BaseAccessory';

export type HueCharacteristicData = { color: { hue?: string }, color_mode: ColorModes }
type DeviceWithHueCharacteristic = TuyaDevice<TuyaDeviceState & HueCharacteristicData>

export class HueCharacteristic extends TuyaWebCharacteristic<ColorAccessory> {
public static Title = 'Characteristic.Hue'
public static HomekitCharacteristic = Characteristic.Hue;

public static HomekitCharacteristic(accessory: BaseAccessory) {
return accessory.platform.Characteristic.Hue;
}

public static DEFAULT_VALUE = 0;

Expand Down
8 changes: 6 additions & 2 deletions src/accessories/characteristics/on.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import {TuyaDevice, TuyaDeviceState} from '../../TuyaWebApi';
import {Characteristic, CharacteristicGetCallback, CharacteristicSetCallback, CharacteristicValue} from 'hap-nodejs';
import {CharacteristicGetCallback, CharacteristicSetCallback, CharacteristicValue} from 'homebridge';
import {TuyaWebCharacteristic} from './base';
import {BaseAccessory} from '../BaseAccessory';

export type OnCharacteristicData = { state: boolean | 'true' | 'false' }
type DeviceWithOnCharacteristic = TuyaDevice<TuyaDeviceState & OnCharacteristicData>

export class OnCharacteristic extends TuyaWebCharacteristic {
public static Title = 'Characteristic.On'
public static HomekitCharacteristic = Characteristic.On;

public static HomekitCharacteristic(accessory: BaseAccessory) {
return accessory.platform.Characteristic.On;
}

public static isSupportedByAccessory(accessory): boolean {
return accessory.deviceConfig.data.state !== undefined;
Expand Down
10 changes: 6 additions & 4 deletions src/accessories/characteristics/rotationSpeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import {
CharacteristicSetCallback,
CharacteristicValue,
Formats,
} from 'hap-nodejs';
} from 'homebridge';
import {TuyaWebCharacteristic} from './base';
import {inspect} from 'util';
import {BaseAccessory} from '../BaseAccessory';

export type RotationSpeedCharacteristicData = { speed_level: number, speed: string }
type DeviceWithRotationSpeedCharacteristic = TuyaDevice<TuyaDeviceState & RotationSpeedCharacteristicData>

export class RotationSpeedCharacteristic extends TuyaWebCharacteristic {
public static Title = 'Characteristic.RotationSpeed'
public static HomekitCharacteristic = Characteristic.RotationSpeed;

public static HomekitCharacteristic(accessory: BaseAccessory) {
return accessory.platform.Characteristic.RotationSpeed;
}

public setProps(char?: Characteristic): Characteristic | undefined {
return char?.setProps({
Expand All @@ -32,7 +35,6 @@ export class RotationSpeedCharacteristic extends TuyaWebCharacteristic {

public get maxSpeedLevel(): number {
const data = this.accessory.deviceConfig.data as unknown as RotationSpeedCharacteristicData;
this.accessory.log?.info(inspect(data), data.speed_level);
return data.speed_level;
}

Expand Down
8 changes: 6 additions & 2 deletions src/accessories/characteristics/saturation.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import {TuyaDevice, TuyaDeviceState} from '../../TuyaWebApi';
import {Characteristic, CharacteristicGetCallback, CharacteristicSetCallback, CharacteristicValue} from 'hap-nodejs';
import {CharacteristicGetCallback, CharacteristicSetCallback, CharacteristicValue} from 'homebridge';
import {COLOR_MODES, ColorModes} from './index';
import {TuyaWebCharacteristic} from './base';
import {ColorAccessory} from '../ColorAccessory';
import {BaseAccessory} from '../BaseAccessory';

export type SaturationCharacteristicData = { color: { saturation?: string }, color_mode: ColorModes }
type DeviceWithSaturationCharacteristic = TuyaDevice<TuyaDeviceState & SaturationCharacteristicData>

export class SaturationCharacteristic extends TuyaWebCharacteristic<ColorAccessory> {
public static Title = 'Characteristic.Saturation'
public static HomekitCharacteristic = Characteristic.Saturation;

public static HomekitCharacteristic(accessory: BaseAccessory) {
return accessory.platform.Characteristic.Saturation;
}

public static DEFAULT_VALUE = 0;

Expand Down

0 comments on commit 09113dd

Please sign in to comment.