Restart the add-on when disconnected from the hub or Home Assistant

This commit is contained in:
Adrian Jagielak 2025-07-28 16:14:36 +02:00
parent 0d993643fb
commit e0b18b304c
No known key found for this signature in database
GPG Key ID: 0818CF7AF6C62BFB
5 changed files with 27 additions and 2 deletions

View File

@ -6,6 +6,7 @@
- Added support for pairing new devices. - Added support for pairing new devices.
- Added support for unpairing devices. - Added support for unpairing devices.
- Fixed using the unit reported by the numerical sensor. - 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) ## 0.1.7 (26.07.2025)

View File

@ -1,15 +1,29 @@
import { exit } from 'process';
import { DemoFimpMqttClient } from './mqtt/demo_client'; import { DemoFimpMqttClient } from './mqtt/demo_client';
import { IMqttClient } from './mqtt/interface'; import { IMqttClient } from './mqtt/interface';
import { RealMqttClient } from './mqtt/real_client'; import { RealMqttClient } from './mqtt/real_client';
import { log } from './logger';
export function connectHub(opts: { export async function connectHub(opts: {
hubIp: string; hubIp: string;
username: string; username: string;
password: string; password: string;
demo: boolean; demo: boolean;
}): Promise<IMqttClient> { }): Promise<IMqttClient> {
const url = `mqtt://${opts.hubIp}`; 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: { export async function connectHA(opts: {
@ -26,6 +40,11 @@ export async function connectHA(opts: {
opts.mqttPassword, opts.mqttPassword,
false, false,
); );
ha.once('disconnect', () => {
log.error("Disconnected from Home Assistant's MQTT broker");
exit(1);
});
const retainedMessages = await waitForHARetainedMessages(ha); const retainedMessages = await waitForHARetainedMessages(ha);
return { ha, retainedMessages }; return { ha, retainedMessages };

View File

@ -124,11 +124,14 @@ export class DemoFimpMqttClient implements IMqttClient {
once(event: 'connect', handler: () => void): void; once(event: 'connect', handler: () => void): void;
once(event: 'error', handler: OnErrorCallback): void; once(event: 'error', handler: OnErrorCallback): void;
once(event: 'disconnect', handler: () => void): void;
once(event: any, handler: any): void { once(event: any, handler: any): void {
if (event === 'connect') { if (event === 'connect') {
this.onceConnectHandlers.push(handler); this.onceConnectHandlers.push(handler);
} else if (event === 'error') { } else if (event === 'error') {
this.onceErrorHandlers.push(handler); this.onceErrorHandlers.push(handler);
} else if (event === 'disconnect') {
// not possible in demo mode
} }
} }

View File

@ -40,4 +40,5 @@ export interface IMqttClient {
once(event: 'connect', handler: () => void): void; once(event: 'connect', handler: () => void): void;
once(event: 'error', handler: OnErrorCallback): void; once(event: 'error', handler: OnErrorCallback): void;
once(event: 'disconnect', handler: () => void): void;
} }

View File

@ -62,6 +62,7 @@ export class RealMqttClient implements IMqttClient {
once(event: 'connect', handler: () => void): void; once(event: 'connect', handler: () => void): void;
once(event: 'error', handler: OnErrorCallback): void; once(event: 'error', handler: OnErrorCallback): void;
once(event: 'disconnect', handler: () => void): void;
once(event: any, handler: any): void { once(event: any, handler: any): void {
this.client.once(event, handler); this.client.once(event, handler);
} }