diff --git a/futurehome/CHANGELOG.md b/futurehome/CHANGELOG.md index 71b97c4..7d7ed45 100644 --- a/futurehome/CHANGELOG.md +++ b/futurehome/CHANGELOG.md @@ -6,6 +6,7 @@ - Added support for pairing new devices. - Added support for unpairing devices. - Fixed using the unit reported by the numerical sensor. +- Added restarting the add-on when disconnected from the hub or Home Assistant ## 0.1.7 (26.07.2025) diff --git a/futurehome/src/client.ts b/futurehome/src/client.ts index 2e2344e..e188294 100644 --- a/futurehome/src/client.ts +++ b/futurehome/src/client.ts @@ -1,15 +1,29 @@ +import { exit } from 'process'; import { DemoFimpMqttClient } from './mqtt/demo_client'; import { IMqttClient } from './mqtt/interface'; import { RealMqttClient } from './mqtt/real_client'; +import { log } from './logger'; -export function connectHub(opts: { +export async function connectHub(opts: { hubIp: string; username: string; password: string; demo: boolean; }): Promise { const url = `mqtt://${opts.hubIp}`; - return makeClient(url, 1884, opts.username, opts.password, opts.demo); + const fimp = await makeClient( + url, + 1884, + opts.username, + opts.password, + opts.demo, + ); + fimp.once('disconnect', () => { + log.error('Disconnected from Futurehome Smarthub'); + exit(1); + }); + + return fimp; } export async function connectHA(opts: { @@ -26,6 +40,11 @@ export async function connectHA(opts: { opts.mqttPassword, false, ); + ha.once('disconnect', () => { + log.error("Disconnected from Home Assistant's MQTT broker"); + exit(1); + }); + const retainedMessages = await waitForHARetainedMessages(ha); return { ha, retainedMessages }; diff --git a/futurehome/src/mqtt/demo_client.ts b/futurehome/src/mqtt/demo_client.ts index 2f4a913..d52b813 100644 --- a/futurehome/src/mqtt/demo_client.ts +++ b/futurehome/src/mqtt/demo_client.ts @@ -124,11 +124,14 @@ export class DemoFimpMqttClient implements IMqttClient { once(event: 'connect', handler: () => void): void; once(event: 'error', handler: OnErrorCallback): void; + once(event: 'disconnect', handler: () => void): void; once(event: any, handler: any): void { if (event === 'connect') { this.onceConnectHandlers.push(handler); } else if (event === 'error') { this.onceErrorHandlers.push(handler); + } else if (event === 'disconnect') { + // not possible in demo mode } } diff --git a/futurehome/src/mqtt/interface.ts b/futurehome/src/mqtt/interface.ts index bf0cbe7..f6dfd6d 100644 --- a/futurehome/src/mqtt/interface.ts +++ b/futurehome/src/mqtt/interface.ts @@ -40,4 +40,5 @@ export interface IMqttClient { once(event: 'connect', handler: () => void): void; once(event: 'error', handler: OnErrorCallback): void; + once(event: 'disconnect', handler: () => void): void; } diff --git a/futurehome/src/mqtt/real_client.ts b/futurehome/src/mqtt/real_client.ts index 6e52dd2..b503f63 100644 --- a/futurehome/src/mqtt/real_client.ts +++ b/futurehome/src/mqtt/real_client.ts @@ -62,6 +62,7 @@ export class RealMqttClient implements IMqttClient { once(event: 'connect', handler: () => void): void; once(event: 'error', handler: OnErrorCallback): void; + once(event: 'disconnect', handler: () => void): void; once(event: any, handler: any): void { this.client.once(event, handler); }