Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update fan dp code and light service. #273

Merged
merged 2 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions src/accessory/FanAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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);

Expand All @@ -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);
}
}
}

Expand All @@ -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);
Expand Down
21 changes: 14 additions & 7 deletions src/accessory/characteristic/RotationSpeed.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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(
Expand Down