From 5d687f90e6e557c78a9206f6f443dd89545a83e6 Mon Sep 17 00:00:00 2001 From: Adrian Jagielak Date: Tue, 22 Jul 2025 00:51:14 +0200 Subject: [PATCH] Add mock admin tools --- futurehome/config.yaml | 2 +- futurehome/src/admin.ts | 55 +++++++++++++++++++++++++++++++++++++++++ futurehome/src/index.ts | 3 +++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 futurehome/src/admin.ts diff --git a/futurehome/config.yaml b/futurehome/config.yaml index 54f565f..be5aa1f 100644 --- a/futurehome/config.yaml +++ b/futurehome/config.yaml @@ -1,6 +1,6 @@ # https://developers.home-assistant.io/docs/add-ons/configuration#add-on-config name: Futurehome -version: "0.0.12" +version: "0.0.13" slug: futurehome description: Local Futurehome Smarthub integration url: "https://github.com/adrianjagielak/home-assistant-futurehome" diff --git a/futurehome/src/admin.ts b/futurehome/src/admin.ts new file mode 100644 index 0000000..419b44d --- /dev/null +++ b/futurehome/src/admin.ts @@ -0,0 +1,55 @@ +import { MqttClient } from "mqtt"; +import { v4 as uuid } from "uuid"; + +export function exposeSmarthubTools( + ha: MqttClient, + fimp: MqttClient, + hubAddr = "pt:j1/mt:cmd/rt:app/rn:zb/ad:1" +) { + const base = "homeassistant/switch/fh_zb_pairing"; + const device = { + identifiers: ["futurehome_hub"], + name: "Futurehome Hub", + model: "Smarthub", + }; + + ha.publish( + `${base}/config`, + JSON.stringify({ + name: "Zigbee Pairing", + uniq_id: "fh_zb_pairing", + cmd_t: `${base}/set`, + stat_t: `${base}/state`, + device, + }), + { retain: true } + ); + +// // keep last known state locally +// let pairingOn = false; + + ha.subscribe(`${base}/set`); + ha.on("message", (topic, payload) => { + if (topic !== `${base}/set`) return; + const turnOn = payload.toString() === "ON"; + + // // optimistic update so the UI flips instantly + // pairingOn = turnOn; + ha.publish(`${base}/state`, turnOn ? "ON" : "OFF", { retain: true }); + + // placeholder FIMP message – adjust to real API if different + fimp.publish( + hubAddr, + JSON.stringify({ + type: "cmd.pairing_mode.set", + service: "zigbee", + uid: uuid(), + val_t: "str", + val: turnOn ? "start" : "stop", + }), + { qos: 1 } + ); + }); + + // (optional) listen for hub-side confirmation and correct state here +} diff --git a/futurehome/src/index.ts b/futurehome/src/index.ts index d96e71c..46697be 100644 --- a/futurehome/src/index.ts +++ b/futurehome/src/index.ts @@ -1,6 +1,7 @@ import { v4 as uuid } from "uuid"; import { connectHub, connectHA } from "./client"; import { publishDiscovery } from "./discovery"; +import { exposeSmarthubTools } from "./admin"; (async () => { const hubIp = process.env.FH_HUB_IP || ""; @@ -30,6 +31,8 @@ import { publishDiscovery } from "./discovery"; const fimp = await connectHub({ hubIp, username: hubUsername, password: hubPassword }); console.log("Connected to Futurehome hub"); + exposeSmarthubTools(ha, fimp); + // -- subscribe to FIMP events ----------------------------------------- fimp.subscribe("#"); fimp.on("message", (topic, buf) => {