From cc6284e2b2ddfaaeeddf6cae1654021e4d1a147a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Vieira?= Date: Sun, 26 Nov 2023 15:27:18 +0000 Subject: [PATCH] Fix moonlight mode detection As reported in #118 there is no value in the `support` array or attribute in the initial advertisement that allows for dynamically detecting if a device supports moonlight mode. To work around this limitation we try to proactively send a `get_prop` request for `active_mode` and will initialize the feature accordingly. Fixes #118. --- bulbs/moonlight.js | 30 +++++++++++++++++------------- platform.js | 4 +--- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/bulbs/moonlight.js b/bulbs/moonlight.js index cf9ab65..85b4cf1 100644 --- a/bulbs/moonlight.js +++ b/bulbs/moonlight.js @@ -1,15 +1,19 @@ -/** Support for "moonlight" mode for ceiling lamps - * active_mode: - * 0 - Daylight mode | - * 1 - Moonlight mode - */ -const MoonlightMode = Device => +const DAYLIGHT_MODE = 0; +const MOONLIGHT_MODE = 1; + +const MoonlightMode = (Device) => class extends Device { constructor(props, platform) { super(props, platform); - const { bright, active_mode } = props; - this.bright = bright; - this.activeMode = Number(active_mode) || 0; + this.initMoonlight(); + } + + async initMoonlight() { + const [activeMode] = await this.getProperty(['active_mode']); + if (!activeMode) return; + + this.log(`Device ${this.name} supports moonlight mode`); + this.activeMode = Number(activeMode) || DAYLIGHT_MODE; this.moonlightModeService = this.accessory.getService(global.Service.Switch) || @@ -25,7 +29,7 @@ const MoonlightMode = Device => callback(err); } }) - .on('get', async callback => { + .on('get', async (callback) => { try { const [value] = await this.getProperty(['active_mode']); this.activeMode = Number(value); @@ -40,13 +44,13 @@ const MoonlightMode = Device => async setMoonlightMode(state) { const { brightness: transition = 400 } = this.config.transitions || {}; this.log.debug( - `Setting ${state ? '🌙' : '☀️'} mode on device ${this.did}` + `Setting ${state ? 'moon' : 'day'}light mode on device ${this.did}` ); await this.sendCmd({ method: 'set_power', params: ['on', 'smooth', transition, state ? 5 : 1], }); - this.activeMode = state ? 1 : 0; + this.activeMode = state ? MOONLIGHT_MODE : DAYLIGHT_MODE; } updateStateFromProp(prop, value) { @@ -54,7 +58,7 @@ const MoonlightMode = Device => this.activeMode = value; this.moonlightModeService .getCharacteristic(global.Characteristic.On) - .updateValue(this.activeMode === 1); + .updateValue(this.activeMode === MOONLIGHT_MODE); return; } diff --git a/platform.js b/platform.js index 9145511..6a55bc7 100644 --- a/platform.js +++ b/platform.js @@ -128,9 +128,7 @@ class YeePlatform { const mixins = []; const limits = devices[model] || devices['default']; - // Lamps that support moonlight mode - if (features.includes('active_mode')) { - this.log(`Device ${name} supports moonlight mode`); + if (!hidden.includes('active_mode')) { mixins.push(MoonlightMode); }