Skip to content

Commit

Permalink
Implement fan light setting
Browse files Browse the repository at this point in the history
  • Loading branch information
JELoohuis committed Aug 30, 2024
1 parent 5b6041f commit b30a3b3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
6 changes: 6 additions & 0 deletions drivers/fan/TuyaFanConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ export const FAN_CAPABILITIES = {
],
read_only: ['temp_current'],
} as const;

export type HomeyFanSettings = {
enable_light_support: boolean;
};

export type TuyaFanSettings = Record<string, never>;
36 changes: 34 additions & 2 deletions drivers/fan/device.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import TuyaOAuth2Device from '../../lib/TuyaOAuth2Device';
import { ParsedColourData, TuyaStatus } from '../../types/TuyaTypes';
import { FAN_CAPABILITIES, FAN_CAPABILITIES_MAPPING } from './TuyaFanConstants';
import { ParsedColourData, SettingsEvent, TuyaStatus } from '../../types/TuyaTypes';
import { FAN_CAPABILITIES, FAN_CAPABILITIES_MAPPING, HomeyFanSettings } from './TuyaFanConstants';
import { constIncludes, getFromMap } from '../../lib/TuyaOAuth2Util';
import * as TuyaFanMigrations from '../../lib/migrations/TuyaFanMigrations';
import { TuyaCommand } from '../../types/TuyaApiTypes';
Expand Down Expand Up @@ -170,6 +170,38 @@ export default class TuyaOAuth2DeviceFan extends TuyaOAuth2Device {
await this.sendCommands(commands);
}
}

async onSettings(event: SettingsEvent<HomeyFanSettings>): Promise<string | void> {
if (event.changedKeys.includes('enable_light_support')) {
if (event.newSettings['enable_light_support']) {
for (const lightTuyaCapability of ['light', 'switch_led', 'bright_value', 'temp_value'] as const) {
if (this.hasTuyaCapability(lightTuyaCapability)) {
const homeyCapability = FAN_CAPABILITIES_MAPPING[lightTuyaCapability];
if (!this.hasCapability(homeyCapability)) await this.addCapability(homeyCapability);
}
}
if (this.hasTuyaCapability('colour')) {
if (!this.hasCapability('light_hue')) await this.addCapability('light_hue');
if (!this.hasCapability('light_saturation')) await this.addCapability('light_saturation');
if (!this.hasCapability('dim.light')) await this.addCapability('dim.light');
}
if (this.hasCapability('light_temperature') && this.hasCapability('light_hue')) {
if (!this.hasCapability('light_mode')) await this.addCapability('light_mode');
}
} else {
for (const lightCapability of [
'onoff.light',
'dim.light',
'light_mode',
'light_temperature',
'light_hue',
'light_saturation',
]) {
if (this.hasCapability(lightCapability)) await this.removeCapability(lightCapability);
}
}
}
}
}

module.exports = TuyaOAuth2DeviceFan;
4 changes: 4 additions & 0 deletions drivers/fan/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ module.exports = class TuyaOAuth2DriverFan extends TuyaOAuth2Driver {
}
}

if (props.store.tuya_capabilities.includes('light') || props.store.tuya_capabilities.includes('switch_led')) {
props.settings['enable_light_support'] = true;
}

if (props.store.tuya_capabilities.includes('colour_data') && !props.capabilities.includes('dim.light')) {
props.capabilities.push('dim.light');
}
Expand Down

0 comments on commit b30a3b3

Please sign in to comment.