Skip to content

Commit

Permalink
Fix moonlight mode detection
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vieira committed Nov 26, 2023
1 parent a44be2c commit cc6284e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
30 changes: 17 additions & 13 deletions bulbs/moonlight.js
Original file line number Diff line number Diff line change
@@ -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) ||
Expand All @@ -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);
Expand All @@ -40,21 +44,21 @@ 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) {
if (prop === 'active_mode') {
this.activeMode = value;
this.moonlightModeService
.getCharacteristic(global.Characteristic.On)
.updateValue(this.activeMode === 1);
.updateValue(this.activeMode === MOONLIGHT_MODE);
return;
}

Expand Down
4 changes: 1 addition & 3 deletions platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit cc6284e

Please sign in to comment.