From 4948d2abbe64d7cee07a779e90b880d45ac2ce2e Mon Sep 17 00:00:00 2001 From: Adrian Jagielak Date: Wed, 24 Sep 2025 21:23:16 +0200 Subject: [PATCH] Stop inclusion/exclusion after the first report --- futurehome/src/ha/admin.ts | 142 ++++++++++++++++++++++++++++++++++++- futurehome/src/index.ts | 18 ++++- 2 files changed, 156 insertions(+), 4 deletions(-) diff --git a/futurehome/src/ha/admin.ts b/futurehome/src/ha/admin.ts index 727c062..eacae46 100644 --- a/futurehome/src/ha/admin.ts +++ b/futurehome/src/ha/admin.ts @@ -400,12 +400,150 @@ export function handleExclusionStatusReport(hubId: string, msg: FimpResponse) { ); } -export function handleInclusionReport() { +export async function handleInclusionReport(parameters: { + hubId: string; + demoMode: boolean; + hubIp: string; + thingsplexUsername: string; + thingsplexPassword: string; + thingsplexAllowEmpty: boolean; +}) { + const topicPrefix = `homeassistant/device/futurehome_${parameters.hubId}_hub`; + pollVinculum('device').catch((e) => log.warn('Failed to request devices', e)); pollVinculum('state').catch((e) => log.warn('Failed to request state', e)); + + if (parameters.demoMode) { + ha?.publish(`${topicPrefix}/inclusion_exclusion_status/state`, 'Done', { + retain: true, + qos: 2, + }); + return; + } + + if ( + !parameters.thingsplexAllowEmpty && + !parameters.thingsplexUsername && + !parameters.thingsplexPassword + ) { + return; + } + + try { + const token = await loginToThingsplex({ + host: parameters.hubIp, + username: parameters.thingsplexUsername, + password: parameters.thingsplexPassword, + }); + await connectThingsplexWebSocketAndSend( + { + host: parameters.hubIp, + token: token, + }, + [ + { + address: 'pt:j1/mt:cmd/rt:ad/rn:zigbee/ad:1', + service: 'zigbee', + cmd: 'cmd.thing.inclusion', + val: false, + val_t: 'bool', + }, + { + address: 'pt:j1/mt:cmd/rt:ad/rn:zw/ad:1', + service: 'zwave-ad', + cmd: 'cmd.thing.inclusion', + val: false, + val_t: 'bool', + }, + ], + ); + ha?.publish(`${topicPrefix}/inclusion_exclusion_status/state`, 'Done', { + retain: true, + qos: 2, + }); + } catch (e) { + log.error('Failed trying to stop inclusion/exclusion', e); + ha?.publish( + `${topicPrefix}/inclusion_exclusion_status/state`, + 'Failed trying to stop inclusion/exclusion.', + { + retain: true, + qos: 2, + }, + ); + } } -export function handleExclusionReport() { +export async function handleExclusionReport(parameters: { + hubId: string; + demoMode: boolean; + hubIp: string; + thingsplexUsername: string; + thingsplexPassword: string; + thingsplexAllowEmpty: boolean; +}) { + const topicPrefix = `homeassistant/device/futurehome_${parameters.hubId}_hub`; + pollVinculum('device').catch((e) => log.warn('Failed to request devices', e)); pollVinculum('state').catch((e) => log.warn('Failed to request state', e)); + + if (parameters.demoMode) { + ha?.publish(`${topicPrefix}/inclusion_exclusion_status/state`, 'Done', { + retain: true, + qos: 2, + }); + return; + } + + if ( + !parameters.thingsplexAllowEmpty && + !parameters.thingsplexUsername && + !parameters.thingsplexPassword + ) { + return; + } + + try { + const token = await loginToThingsplex({ + host: parameters.hubIp, + username: parameters.thingsplexUsername, + password: parameters.thingsplexPassword, + }); + await connectThingsplexWebSocketAndSend( + { + host: parameters.hubIp, + token: token, + }, + [ + { + address: 'pt:j1/mt:cmd/rt:ad/rn:zigbee/ad:1', + service: 'zigbee', + cmd: 'cmd.thing.exclusion', + val: false, + val_t: 'bool', + }, + { + address: 'pt:j1/mt:cmd/rt:ad/rn:zw/ad:1', + service: 'zwave-ad', + cmd: 'cmd.thing.exclusion', + val: false, + val_t: 'bool', + }, + ], + ); + ha?.publish(`${topicPrefix}/inclusion_exclusion_status/state`, 'Done', { + retain: true, + qos: 2, + }); + } catch (e) { + log.error('Failed trying to stop inclusion/exclusion', e); + ha?.publish( + `${topicPrefix}/inclusion_exclusion_status/state`, + 'Failed trying to stop inclusion/exclusion.', + { + retain: true, + qos: 2, + }, + ); + } } diff --git a/futurehome/src/index.ts b/futurehome/src/index.ts index 225bec5..7864b64 100644 --- a/futurehome/src/index.ts +++ b/futurehome/src/index.ts @@ -282,12 +282,26 @@ import { pollVinculum } from './fimp/vinculum'; } case 'evt.thing.inclusion_report': { - handleInclusionReport(); + handleInclusionReport({ + hubId, + demoMode, + hubIp, + thingsplexUsername, + thingsplexPassword, + thingsplexAllowEmpty, + }); break; } case 'evt.thing.exclusion_report': { - handleExclusionReport(); + handleExclusionReport({ + hubId, + demoMode, + hubIp, + thingsplexUsername, + thingsplexPassword, + thingsplexAllowEmpty, + }); break; }