feat: Introduce rule configuration from the status dashboard, allowing storedDuration editing for the water button rule and improving its timer handling.

This commit is contained in:
sebseb7
2026-01-20 04:07:49 -05:00
parent 75a4d1cbc0
commit b6a25a53fc
3 changed files with 103 additions and 7 deletions

View File

@@ -79,13 +79,27 @@ export default {
timerActive: state.timer !== null
};
},
setConfig(key, value) {
if (key === 'storedDuration') {
const duration = parseInt(value, 10);
if (duration > 0 && duration <= 300000) { // Max 5 minutes
const state = getState(WATER_BUTTON_MAC);
state.storedDuration = duration;
// Persist
persistedState[WATER_BUTTON_MAC] = { storedDuration: duration };
saveState(persistedState);
console.log(`[Rule] storedDuration set to ${duration}ms`);
return true;
}
}
return false;
},
async run(ctx) {
// Auto-on for water button when it connects (only if remote switch is online)
if (ctx.trigger.mac === WATER_BUTTON_MAC && ctx.trigger.field === 'online' && ctx.trigger.event === true) {
const remoteSwitchConnected = await ctx.getState(REMOTE_SWITCH_MAC, 'system', 'online');
if (remoteSwitchConnected === true) {
ctx.log('Water button connected - remote switch online, turning light on');
await setLight(ctx, ctx.trigger.mac, true, 20);
// Double flash to indicate both devices are connected
ctx.log('Double flashing to confirm connection');
@@ -95,6 +109,9 @@ export default {
await ctx.sendRPC(WATER_BUTTON_MAC, 'Light.Set', { id: 0, on: false, brightness: 0 });
await sleep(200);
}
// Turn light on after flash (ready state)
await setLight(ctx, ctx.trigger.mac, true, 20);
} else {
ctx.log('Water button connected - remote switch offline, keeping light off');
await ctx.sendRPC(WATER_BUTTON_MAC, 'Light.Set', { id: 0, on: false, brightness: 0 });
@@ -151,17 +168,20 @@ export default {
// Handle btn_down
if (ctx.trigger.event === 'btn_down') {
// Check if timer was active before clearing
const timerWasActive = state.timer !== null;
// Clear any pending timer
if (state.timer) {
clearTimeout(state.timer);
state.timer = null;
}
// Turn light off (remote switch turns on)
await setLight(ctx, mac, false, 0);
if (state.countMode) {
// We're in count mode - calculate elapsed time and turn light on
// Turn light off first (remote switch turns on)
await setLight(ctx, mac, false, 0);
const elapsed = Date.now() - state.countStart;
state.storedDuration = elapsed;
state.countMode = false;
@@ -181,8 +201,21 @@ export default {
// Turn light on immediately (remote switch turns off)
await setLight(ctx, mac, true, 20);
} else if (timerWasActive) {
// Timer was running - cancel it and turn light on immediately
ctx.log('Timer cancelled by button press. Turning light on.');
await setLight(ctx, mac, true, 20);
// Push status update to dashboard
ctx.updateStatus({
storedDuration: state.storedDuration,
countMode: false,
timerActive: false,
lastAction: 'Timer cancelled'
});
} else {
// Normal mode - schedule light to turn on after stored duration
// Normal mode - turn off light and schedule it to turn on after stored duration
await setLight(ctx, mac, false, 0);
ctx.log(`Light off. Will turn on in ${state.storedDuration}ms`);
// Capture updateStatus for use in timer callback