Skip to content

Commit 4a0222f

Browse files
authored
Update fan dp code and light service. (#273)
* Update dp codes for Fan. * Change fan light to fan light switch, if it don't have any light features. (#264)
1 parent 6b49bff commit 4a0222f

2 files changed

Lines changed: 42 additions & 24 deletions

File tree

src/accessory/FanAccessory.ts

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { configureSwingMode } from './characteristic/SwingMode';
1010
const SCHEMA_CODE = {
1111
FAN_ON: ['switch_fan', 'fan_switch', 'switch'],
1212
FAN_DIRECTION: ['fan_direction'],
13-
FAN_SPEED: ['fan_speed'],
13+
FAN_SPEED: ['fan_speed', 'fan_speed_percent'],
1414
FAN_SPEED_LEVEL: ['fan_speed_enum', 'fan_speed'],
1515
FAN_LOCK: ['child_lock'],
1616
FAN_SWING: ['switch_horizontal', 'switch_vertical'],
@@ -29,13 +29,12 @@ export default class FanAccessory extends BaseAccessory {
2929

3030
configureServices() {
3131

32-
const serviceType = this.fanServiceType();
33-
if (serviceType === this.Service.Fan) {
32+
if (this.fanServiceType() === this.Service.Fan) {
3433
const unusedService = this.accessory.getService(this.Service.Fanv2);
3534
unusedService && this.accessory.removeService(unusedService);
3635

3736
configureOn(this, this.fanService(), this.getSchema(...SCHEMA_CODE.FAN_ON));
38-
} else if (serviceType === this.Service.Fanv2) {
37+
} else if (this.fanServiceType() === this.Service.Fanv2) {
3938
const unusedService = this.accessory.getService(this.Service.Fan);
4039
unusedService && this.accessory.removeService(unusedService);
4140

@@ -57,19 +56,21 @@ export default class FanAccessory extends BaseAccessory {
5756

5857
// Light
5958
if (this.getSchema(...SCHEMA_CODE.LIGHT_ON)) {
60-
configureLight(
61-
this,
62-
this.lightService(),
63-
this.getSchema(...SCHEMA_CODE.LIGHT_ON),
64-
this.getSchema(...SCHEMA_CODE.LIGHT_BRIGHT),
65-
this.getSchema(...SCHEMA_CODE.LIGHT_TEMP),
66-
this.getSchema(...SCHEMA_CODE.LIGHT_COLOR),
67-
this.getSchema(...SCHEMA_CODE.LIGHT_MODE),
68-
);
69-
} else {
70-
this.log.warn('Remove Lightbulb Service...');
71-
const unusedService = this.accessory.getService(this.Service.Lightbulb);
72-
unusedService && this.accessory.removeService(unusedService);
59+
if (this.lightServiceType() === this.Service.Lightbulb) {
60+
configureLight(
61+
this,
62+
this.lightService(),
63+
this.getSchema(...SCHEMA_CODE.LIGHT_ON),
64+
this.getSchema(...SCHEMA_CODE.LIGHT_BRIGHT),
65+
this.getSchema(...SCHEMA_CODE.LIGHT_TEMP),
66+
this.getSchema(...SCHEMA_CODE.LIGHT_COLOR),
67+
this.getSchema(...SCHEMA_CODE.LIGHT_MODE),
68+
);
69+
} else if (this.lightServiceType() === this.Service.Switch) {
70+
configureOn(this, undefined, this.getSchema(...SCHEMA_CODE.LIGHT_ON));
71+
const unusedService = this.accessory.getService(this.Service.Lightbulb);
72+
unusedService && this.accessory.removeService(unusedService);
73+
}
7374
}
7475
}
7576

@@ -87,6 +88,16 @@ export default class FanAccessory extends BaseAccessory {
8788
|| this.accessory.addService(serviceType);
8889
}
8990

91+
lightServiceType() {
92+
if (this.getSchema(...SCHEMA_CODE.LIGHT_BRIGHT)
93+
|| this.getSchema(...SCHEMA_CODE.LIGHT_TEMP)
94+
|| this.getSchema(...SCHEMA_CODE.LIGHT_COLOR)
95+
|| this.getSchema(...SCHEMA_CODE.LIGHT_MODE)) {
96+
return this.Service.Lightbulb;
97+
}
98+
return this.Service.Switch;
99+
}
100+
90101
lightService() {
91102
return this.accessory.getService(this.Service.Lightbulb)
92103
|| this.accessory.addService(this.Service.Lightbulb);

src/accessory/characteristic/RotationSpeed.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Service } from 'homebridge';
22
import { TuyaDeviceSchema, TuyaDeviceSchemaEnumProperty, TuyaDeviceSchemaIntegerProperty } from '../../device/TuyaDevice';
3-
import { limit, remap } from '../../util/util';
3+
import { limit } from '../../util/util';
44
import BaseAccessory from '../BaseAccessory';
55

66
export function configureRotationSpeed(
@@ -13,18 +13,25 @@ export function configureRotationSpeed(
1313
return;
1414
}
1515

16-
const { min, max } = schema.property as TuyaDeviceSchemaIntegerProperty;
16+
const property = schema.property as TuyaDeviceSchemaIntegerProperty;
17+
const multiple = Math.pow(10, property.scale);
18+
const props = {
19+
minValue: property.min / multiple,
20+
maxValue: property.max / multiple,
21+
minStep: Math.max(1, property.step / multiple),
22+
};
1723
service.getCharacteristic(accessory.Characteristic.RotationSpeed)
1824
.onGet(() => {
1925
const status = accessory.getStatus(schema.code)!;
20-
const value = Math.round(remap(status.value as number, min, max, 0, 100));
21-
return limit(value, 0, 100);
26+
const value = status.value as number / multiple;
27+
return limit(value, props.minValue, props.maxValue);
2228
})
2329
.onSet(value => {
24-
let speed = Math.round(remap(value as number, 0, 100, min, max));
25-
speed = limit(speed, min, max);
30+
const speed = (value as number) * multiple;
2631
accessory.sendCommands([{ code: schema.code, value: speed }], true);
27-
});
32+
})
33+
.setProps(props);
34+
2835
}
2936

3037
export function configureRotationSpeedLevel(

0 commit comments

Comments
 (0)