This commit is contained in:
sebseb7
2026-01-17 01:21:44 -05:00
parent 2468f7b5e0
commit c512ca9549

View File

@@ -17,6 +17,7 @@ const __dirname = path.dirname(__filename);
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms)); const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const STATE_FILE = path.join(__dirname, 'timer_state.json'); const STATE_FILE = path.join(__dirname, 'timer_state.json');
const WATER_BUTTON_MAC = '08A6F773510C';
const REMOTE_SWITCH_MAC = 'CC8DA243B0A0'; const REMOTE_SWITCH_MAC = 'CC8DA243B0A0';
// Helper to set local light and sync remote switch inversely // Helper to set local light and sync remote switch inversely
@@ -72,7 +73,7 @@ function getState(mac) {
export default { export default {
async run(ctx) { async run(ctx) {
// Auto-on for water button when it connects (only if remote switch is online) // Auto-on for water button when it connects (only if remote switch is online)
if (ctx.trigger.mac === '08A6F773510C' && ctx.trigger.field === 'online' && ctx.trigger.event === true) { 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'); const remoteSwitchConnected = await ctx.getState(REMOTE_SWITCH_MAC, 'system', 'online');
if (remoteSwitchConnected === true) { if (remoteSwitchConnected === true) {
ctx.log('Water button connected - remote switch online, turning light on'); ctx.log('Water button connected - remote switch online, turning light on');
@@ -81,14 +82,14 @@ export default {
// Double flash to indicate both devices are connected // Double flash to indicate both devices are connected
ctx.log('Double flashing to confirm connection'); ctx.log('Double flashing to confirm connection');
for (let i = 0; i < 2; i++) { for (let i = 0; i < 2; i++) {
await ctx.sendRPC('08A6F773510C', 'Light.Set', { id: 0, on: true, brightness: 20 }); await ctx.sendRPC(WATER_BUTTON_MAC, 'Light.Set', { id: 0, on: true, brightness: 20 });
await sleep(200); await sleep(200);
await ctx.sendRPC('08A6F773510C', 'Light.Set', { id: 0, on: false, brightness: 0 }); await ctx.sendRPC(WATER_BUTTON_MAC, 'Light.Set', { id: 0, on: false, brightness: 0 });
await sleep(200); await sleep(200);
} }
} else { } else {
ctx.log('Water button connected - remote switch offline, keeping light off'); ctx.log('Water button connected - remote switch offline, keeping light off');
await ctx.sendRPC('08A6F773510C', 'Light.Set', { id: 0, on: false, brightness: 0 }); await ctx.sendRPC(WATER_BUTTON_MAC, 'Light.Set', { id: 0, on: false, brightness: 0 });
} }
return; return;
} }
@@ -100,24 +101,24 @@ export default {
// Double flash the light // Double flash the light
for (let i = 0; i < 2; i++) { for (let i = 0; i < 2; i++) {
await ctx.sendRPC('08A6F773510C', 'Light.Set', { id: 0, on: true, brightness: 20 }); await ctx.sendRPC(WATER_BUTTON_MAC, 'Light.Set', { id: 0, on: true, brightness: 20 });
await sleep(200); await sleep(200);
await ctx.sendRPC('08A6F773510C', 'Light.Set', { id: 0, on: false, brightness: 0 }); await ctx.sendRPC(WATER_BUTTON_MAC, 'Light.Set', { id: 0, on: false, brightness: 0 });
await sleep(200); await sleep(200);
} }
// Turn light on after flash // Turn light on after flash
await setLight(ctx, '08A6F773510C', true, 20); await setLight(ctx, WATER_BUTTON_MAC, true, 20);
return; return;
} }
// Turn off light when remote switch goes offline // Turn off light when remote switch goes offline
if (ctx.trigger.mac === REMOTE_SWITCH_MAC && ctx.trigger.field === 'online' && ctx.trigger.event === false) { if (ctx.trigger.mac === REMOTE_SWITCH_MAC && ctx.trigger.field === 'online' && ctx.trigger.event === false) {
ctx.log('Remote switch went offline - turning light off'); ctx.log('Remote switch went offline - turning light off');
await ctx.sendRPC('08A6F773510C', 'Light.Set', { id: 0, on: false, brightness: 0 }); await ctx.sendRPC(WATER_BUTTON_MAC, 'Light.Set', { id: 0, on: false, brightness: 0 });
// Clear any pending timer // Clear any pending timer
const state = getState('08A6F773510C'); const state = getState(WATER_BUTTON_MAC);
if (state.timer) { if (state.timer) {
clearTimeout(state.timer); clearTimeout(state.timer);
state.timer = null; state.timer = null;