From 1d4f576f61d0f7bff4ac25540ec55c45121a7914 Mon Sep 17 00:00:00 2001 From: gaosen <0x5e@sina.cn> Date: Mon, 3 Apr 2023 20:24:03 +0800 Subject: [PATCH 1/2] Update dp codes for Fan. --- src/accessory/FanAccessory.ts | 2 +- src/accessory/characteristic/RotationSpeed.ts | 21 ++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/accessory/FanAccessory.ts b/src/accessory/FanAccessory.ts index 85b3fff5..9d792a4e 100644 --- a/src/accessory/FanAccessory.ts +++ b/src/accessory/FanAccessory.ts @@ -10,7 +10,7 @@ import { configureSwingMode } from './characteristic/SwingMode'; const SCHEMA_CODE = { FAN_ON: ['switch_fan', 'fan_switch', 'switch'], FAN_DIRECTION: ['fan_direction'], - FAN_SPEED: ['fan_speed'], + FAN_SPEED: ['fan_speed', 'fan_speed_percent'], FAN_SPEED_LEVEL: ['fan_speed_enum', 'fan_speed'], FAN_LOCK: ['child_lock'], FAN_SWING: ['switch_horizontal', 'switch_vertical'], diff --git a/src/accessory/characteristic/RotationSpeed.ts b/src/accessory/characteristic/RotationSpeed.ts index d8e3898f..7e842f27 100644 --- a/src/accessory/characteristic/RotationSpeed.ts +++ b/src/accessory/characteristic/RotationSpeed.ts @@ -1,6 +1,6 @@ import { Service } from 'homebridge'; import { TuyaDeviceSchema, TuyaDeviceSchemaEnumProperty, TuyaDeviceSchemaIntegerProperty } from '../../device/TuyaDevice'; -import { limit, remap } from '../../util/util'; +import { limit } from '../../util/util'; import BaseAccessory from '../BaseAccessory'; export function configureRotationSpeed( @@ -13,18 +13,25 @@ export function configureRotationSpeed( return; } - const { min, max } = schema.property as TuyaDeviceSchemaIntegerProperty; + const property = schema.property as TuyaDeviceSchemaIntegerProperty; + const multiple = Math.pow(10, property.scale); + const props = { + minValue: property.min / multiple, + maxValue: property.max / multiple, + minStep: Math.max(1, property.step / multiple), + }; service.getCharacteristic(accessory.Characteristic.RotationSpeed) .onGet(() => { const status = accessory.getStatus(schema.code)!; - const value = Math.round(remap(status.value as number, min, max, 0, 100)); - return limit(value, 0, 100); + const value = status.value as number / multiple; + return limit(value, props.minValue, props.maxValue); }) .onSet(value => { - let speed = Math.round(remap(value as number, 0, 100, min, max)); - speed = limit(speed, min, max); + const speed = (value as number) * multiple; accessory.sendCommands([{ code: schema.code, value: speed }], true); - }); + }) + .setProps(props); + } export function configureRotationSpeedLevel( From 16e9bdb5567b167ab64c3941c213a980e3e5377e Mon Sep 17 00:00:00 2001 From: gaosen <0x5e@sina.cn> Date: Mon, 3 Apr 2023 20:58:43 +0800 Subject: [PATCH 2/2] Change fan light to fan light switch, if it don't have any light features. (#264) --- src/accessory/FanAccessory.ts | 43 ++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/src/accessory/FanAccessory.ts b/src/accessory/FanAccessory.ts index 9d792a4e..71045bc6 100644 --- a/src/accessory/FanAccessory.ts +++ b/src/accessory/FanAccessory.ts @@ -29,13 +29,12 @@ export default class FanAccessory extends BaseAccessory { configureServices() { - const serviceType = this.fanServiceType(); - if (serviceType === this.Service.Fan) { + if (this.fanServiceType() === this.Service.Fan) { const unusedService = this.accessory.getService(this.Service.Fanv2); unusedService && this.accessory.removeService(unusedService); configureOn(this, this.fanService(), this.getSchema(...SCHEMA_CODE.FAN_ON)); - } else if (serviceType === this.Service.Fanv2) { + } else if (this.fanServiceType() === this.Service.Fanv2) { const unusedService = this.accessory.getService(this.Service.Fan); unusedService && this.accessory.removeService(unusedService); @@ -57,19 +56,21 @@ export default class FanAccessory extends BaseAccessory { // Light if (this.getSchema(...SCHEMA_CODE.LIGHT_ON)) { - configureLight( - this, - this.lightService(), - this.getSchema(...SCHEMA_CODE.LIGHT_ON), - this.getSchema(...SCHEMA_CODE.LIGHT_BRIGHT), - this.getSchema(...SCHEMA_CODE.LIGHT_TEMP), - this.getSchema(...SCHEMA_CODE.LIGHT_COLOR), - this.getSchema(...SCHEMA_CODE.LIGHT_MODE), - ); - } else { - this.log.warn('Remove Lightbulb Service...'); - const unusedService = this.accessory.getService(this.Service.Lightbulb); - unusedService && this.accessory.removeService(unusedService); + if (this.lightServiceType() === this.Service.Lightbulb) { + configureLight( + this, + this.lightService(), + this.getSchema(...SCHEMA_CODE.LIGHT_ON), + this.getSchema(...SCHEMA_CODE.LIGHT_BRIGHT), + this.getSchema(...SCHEMA_CODE.LIGHT_TEMP), + this.getSchema(...SCHEMA_CODE.LIGHT_COLOR), + this.getSchema(...SCHEMA_CODE.LIGHT_MODE), + ); + } else if (this.lightServiceType() === this.Service.Switch) { + configureOn(this, undefined, this.getSchema(...SCHEMA_CODE.LIGHT_ON)); + const unusedService = this.accessory.getService(this.Service.Lightbulb); + unusedService && this.accessory.removeService(unusedService); + } } } @@ -87,6 +88,16 @@ export default class FanAccessory extends BaseAccessory { || this.accessory.addService(serviceType); } + lightServiceType() { + if (this.getSchema(...SCHEMA_CODE.LIGHT_BRIGHT) + || this.getSchema(...SCHEMA_CODE.LIGHT_TEMP) + || this.getSchema(...SCHEMA_CODE.LIGHT_COLOR) + || this.getSchema(...SCHEMA_CODE.LIGHT_MODE)) { + return this.Service.Lightbulb; + } + return this.Service.Switch; + } + lightService() { return this.accessory.getService(this.Service.Lightbulb) || this.accessory.addService(this.Service.Lightbulb);