import { ERR_CONNECTION_LOST } from "./errors.js"; import { MessageBase } from "./types.js"; import { HaWebSocket } from "./socket.js"; import type { Auth } from "./auth.js"; export type ConnectionOptions = { setupRetry: number; auth?: Auth; createSocket: (options: ConnectionOptions) => Promise; }; export type ConnectionEventListener = (conn: Connection, eventData?: any) => void; type Events = "ready" | "disconnected" | "reconnect-error"; type SubscriptionUnsubscribe = () => Promise; interface SubscribeEventCommmandInFlight { resolve: (result?: any) => void; reject: (err: any) => void; callback: (ev: T) => void; subscribe: (() => Promise) | undefined; unsubscribe: SubscriptionUnsubscribe; } type CommandWithAnswerInFlight = { resolve: (result?: any) => void; reject: (err: any) => void; }; type CommandInFlight = SubscribeEventCommmandInFlight | CommandWithAnswerInFlight; export declare class Connection { options: ConnectionOptions; commandId: number; commands: Map; eventListeners: Map; closeRequested: boolean; suspendReconnectPromise?: Promise; oldSubscriptions?: Map; _queuedMessages?: Array<{ resolve: (value?: unknown) => unknown; reject?: (err: typeof ERR_CONNECTION_LOST) => unknown; }>; socket?: HaWebSocket; /** * Version string of the Home Assistant instance. Set to version of last connection while reconnecting. */ haVersion: string; constructor(socket: HaWebSocket, options: ConnectionOptions); get connected(): boolean; private _setSocket; addEventListener(eventType: Events, callback: ConnectionEventListener): void; removeEventListener(eventType: Events, callback: ConnectionEventListener): void; fireEvent(eventType: Events, eventData?: any): void; suspendReconnectUntil(suspendPromise: Promise): void; suspend(): void; /** * Reconnect the websocket connection. * @param force discard old socket instead of gracefully closing it. */ reconnect(force?: boolean): void; close(): void; /** * Subscribe to a specific or all events. * * @param callback Callback to be called when a new event fires * @param eventType * @returns promise that resolves to an unsubscribe function */ subscribeEvents(callback: (ev: EventType) => void, eventType?: string): Promise; ping(): Promise; sendMessage(message: MessageBase, commandId?: number): void; sendMessagePromise(message: MessageBase): Promise; /** * Call a websocket command that starts a subscription on the backend. * * @param message the message to start the subscription * @param callback the callback to be called when a new item arrives * @param [options.resubscribe] re-established a subscription after a reconnect. Defaults to true. * @returns promise that resolves to an unsubscribe function */ subscribeMessage(callback: (result: Result) => void, subscribeMessage: MessageBase, options?: { resubscribe?: boolean; preCheck?: () => boolean | Promise; }): Promise; private _handleMessage; private _handleClose; private _genCmdId; } export {};