feat: Implement type casting for channel states and events, refine Shelly device online/offline logic, and improve event handling on server startup.

This commit is contained in:
sebseb7
2026-01-17 00:50:32 -05:00
parent 55e4dc56c3
commit 2468f7b5e0
3 changed files with 67 additions and 13 deletions

View File

@@ -71,28 +71,31 @@ function getState(mac) {
export default {
async run(ctx) {
// Auto-on for water button when it connects
if (ctx.trigger.mac === '08A6F773510C' && ctx.trigger.field === 'connected' && ctx.trigger.value === true) {
ctx.log('Water button connected - turning light on');
await setLight(ctx, ctx.trigger.mac, true, 20);
// Double flash if remote switch is already connected
const remoteSwitchConnected = await ctx.getState(REMOTE_SWITCH_MAC, 'sys', 'connected');
// 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) {
const remoteSwitchConnected = await ctx.getState(REMOTE_SWITCH_MAC, 'system', 'online');
if (remoteSwitchConnected === true) {
ctx.log('Remote switch already connected - double flashing');
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');
for (let i = 0; i < 2; i++) {
await ctx.sendRPC('08A6F773510C', 'Light.Set', { id: 0, on: true, brightness: 20 });
await sleep(200);
await ctx.sendRPC('08A6F773510C', 'Light.Set', { id: 0, on: false, brightness: 0 });
await sleep(200);
}
} else {
ctx.log('Water button connected - remote switch offline, keeping light off');
await ctx.sendRPC('08A6F773510C', 'Light.Set', { id: 0, on: false, brightness: 0 });
}
return;
}
// Auto-off for remote switch when it connects
if (ctx.trigger.mac === REMOTE_SWITCH_MAC && ctx.trigger.field === 'connected' && ctx.trigger.value === true) {
ctx.log('Remote switch connected - turning off and flashing light');
if (ctx.trigger.mac === REMOTE_SWITCH_MAC && ctx.trigger.field === 'online' && ctx.trigger.event === true) {
ctx.log('Remote switch connected - turning switch off, flashing light, then turning light on');
await ctx.sendRPC(REMOTE_SWITCH_MAC, 'Switch.Set', { id: 0, on: false });
// Double flash the light
@@ -102,11 +105,36 @@ export default {
await ctx.sendRPC('08A6F773510C', 'Light.Set', { id: 0, on: false, brightness: 0 });
await sleep(200);
}
// Turn light on after flash
await setLight(ctx, '08A6F773510C', true, 20);
return;
}
// Turn off light when remote switch goes offline
if (ctx.trigger.mac === REMOTE_SWITCH_MAC && ctx.trigger.field === 'online' && ctx.trigger.event === false) {
ctx.log('Remote switch went offline - turning light off');
await ctx.sendRPC('08A6F773510C', 'Light.Set', { id: 0, on: false, brightness: 0 });
// Clear any pending timer
const state = getState('08A6F773510C');
if (state.timer) {
clearTimeout(state.timer);
state.timer = null;
}
state.countMode = false;
return;
}
if (ctx.trigger.field !== 'button') return;
// Ignore button events if remote switch is offline
const remoteSwitchOnline = await ctx.getState(REMOTE_SWITCH_MAC, 'system', 'online');
if (remoteSwitchOnline !== true) {
ctx.log('Button ignored - remote switch is offline');
return;
}
const mac = ctx.trigger.mac;
const state = getState(mac);