Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix moonlight mode detection #119

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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