2025-07-22 23:21:34 +02:00
|
|
|
|
import { v4 as uuidv4 } from "uuid";
|
2025-07-23 20:25:01 +02:00
|
|
|
|
import { IMqttClient } from "./mqtt/interface";
|
2025-07-22 00:51:14 +02:00
|
|
|
|
|
|
|
|
|
export function exposeSmarthubTools(
|
2025-07-23 20:25:01 +02:00
|
|
|
|
ha: IMqttClient,
|
|
|
|
|
fimp: IMqttClient,
|
2025-07-22 00:51:14 +02:00
|
|
|
|
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,
|
|
|
|
|
}),
|
2025-07-23 20:25:01 +02:00
|
|
|
|
{ retain: true, qos: 2 }
|
2025-07-22 00:51:14 +02:00
|
|
|
|
);
|
|
|
|
|
|
2025-07-22 23:21:34 +02:00
|
|
|
|
// // keep last known state locally
|
|
|
|
|
// let pairingOn = false;
|
2025-07-22 00:51:14 +02:00
|
|
|
|
|
|
|
|
|
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;
|
2025-07-23 20:25:01 +02:00
|
|
|
|
ha.publish(`${base}/state`, turnOn ? "ON" : "OFF", { retain: true, qos: 2 });
|
2025-07-22 00:51:14 +02:00
|
|
|
|
|
|
|
|
|
// placeholder FIMP message – adjust to real API if different
|
|
|
|
|
fimp.publish(
|
|
|
|
|
hubAddr,
|
|
|
|
|
JSON.stringify({
|
|
|
|
|
type: "cmd.pairing_mode.set",
|
|
|
|
|
service: "zigbee",
|
2025-07-22 23:21:34 +02:00
|
|
|
|
uid: uuidv4(),
|
2025-07-22 00:51:14 +02:00
|
|
|
|
val_t: "str",
|
|
|
|
|
val: turnOn ? "start" : "stop",
|
|
|
|
|
}),
|
|
|
|
|
{ qos: 1 }
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// (optional) listen for hub-side confirmation and correct state here
|
|
|
|
|
}
|