From b07a6c08dc9d6adaa626c33e5c709c33c311d658 Mon Sep 17 00:00:00 2001 From: Ollie Date: Sun, 15 Dec 2024 18:33:24 +1100 Subject: [PATCH] Feature: Add "bzyd" White Noise Night Light (#510) * Add 'bzyd' White Noise Machine --- SUPPORTED_DEVICES.md | 1 + src/accessory/AccessoryFactory.ts | 4 ++ src/accessory/WhiteNoiseLightAccessory.ts | 70 +++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 src/accessory/WhiteNoiseLightAccessory.ts diff --git a/SUPPORTED_DEVICES.md b/SUPPORTED_DEVICES.md index 430db98..67a0b7f 100644 --- a/SUPPORTED_DEVICES.md +++ b/SUPPORTED_DEVICES.md @@ -20,6 +20,7 @@ Most category code is pinyin abbreviation of Chinese name. | Dimmer | 调光器 | tgq | Lightbulb | ✅ | [Documentation](https://developer.tuya.com/en/docs/iot/tgq?id=Kaof8ke9il4k4) | | Remote Control | 遥控器 | ykq | | | [Documentation](https://developer.tuya.com/en/docs/iot/ykq?id=Kaof8ljn81aov) | | Spotlight | 射灯 | sxd | Lightbulb | ✅ | [Documentation](https://developer.tuya.com/en/docs/iot/sxd?id=Kb7jayalltstu) | +| White Noise Light | 白噪音灯 | bzyd | Lightbulb
Switch | ✅ | Documentation | ## Electrical Products diff --git a/src/accessory/AccessoryFactory.ts b/src/accessory/AccessoryFactory.ts index 62e46cc..c9fcfd8 100644 --- a/src/accessory/AccessoryFactory.ts +++ b/src/accessory/AccessoryFactory.ts @@ -43,6 +43,7 @@ import VibrationSensorAccessory from './VibrationSensorAccessory'; import WeatherStationAccessory from './WeatherStationAccessory'; import DoorbellAccessory from './DoorbellAccessory'; import PetFeederAccessory from './PetFeederAccessory'; +import WhiteNoiseLightAccessory from './WhiteNoiseLightAccessory'; export default class AccessoryFactory { @@ -91,6 +92,9 @@ export default class AccessoryFactory { case 'cjkg': handler = new SceneSwitchAccessory(platform, accessory); break; + case 'bzyd': + handler = new WhiteNoiseLightAccessory(platform, accessory); + break; // Large Home Appliances case 'kt': diff --git a/src/accessory/WhiteNoiseLightAccessory.ts b/src/accessory/WhiteNoiseLightAccessory.ts new file mode 100644 index 0000000..ba7fd74 --- /dev/null +++ b/src/accessory/WhiteNoiseLightAccessory.ts @@ -0,0 +1,70 @@ +import BaseAccessory from './BaseAccessory'; +import { configureOn } from './characteristic/On'; +import { configureLight } from './characteristic/Light'; + +const SCHEMA_CODE = { + LIGHT_ON: ['switch_led'], + LIGHT_COLOR: ['colour_data'], + MUSIC_ON: ['switch_music'], +}; + +export default class WhiteNoiseLightAccessory extends BaseAccessory { + requiredSchema() { + return [SCHEMA_CODE.LIGHT_ON, SCHEMA_CODE.MUSIC_ON]; + } + + configureServices() { + // Light + if (this.lightServiceType() === this.Service.Lightbulb) { + configureLight( + this, + this.lightService(), + this.getSchema(...SCHEMA_CODE.LIGHT_ON), + undefined, + undefined, + this.lightColorSchema(), + undefined, + ); + } 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); + } + + // White Noise + configureOn(this, undefined, this.getSchema(...SCHEMA_CODE.MUSIC_ON)); + } + + lightColorSchema() { + const colorSchema = this.getSchema(...SCHEMA_CODE.LIGHT_COLOR); + if (!colorSchema) { + return; + } + + const { h, s, v } = (colorSchema.property || {}) as never; + if (!h || !s || !v) { + // Set sensible defaults for missing properties + colorSchema.property = { + h: { min: 0, scale: 0, unit: '', max: 360, step: 1 }, + s: { min: 0, scale: 0, unit: '', max: 1000, step: 1 }, + v: { min: 0, scale: 0, unit: '', max: 1000, step: 1 }, + }; + } + + return colorSchema; + } + + lightServiceType() { + if (this.lightColorSchema()) { + return this.Service.Lightbulb; + } + return this.Service.Switch; + } + + lightService() { + return ( + this.accessory.getService(this.Service.Lightbulb) || + this.accessory.addService(this.Service.Lightbulb) + ); + } +}