1 | import { ConnectionOptions, MessageBase } from "./types.js";
|
2 | import { HaWebSocket } from "./socket.js";
|
3 | export declare type ConnectionEventListener = (conn: Connection, eventData?: any) => void;
|
4 | declare type Events = "ready" | "disconnected" | "reconnect-error";
|
5 | declare type SubscriptionUnsubscribe = () => Promise<void>;
|
6 | interface SubscribeEventCommmandInFlight<T> {
|
7 | resolve: (result?: any) => void;
|
8 | reject: (err: any) => void;
|
9 | callback: (ev: T) => void;
|
10 | subscribe: () => Promise<SubscriptionUnsubscribe>;
|
11 | unsubscribe: SubscriptionUnsubscribe;
|
12 | }
|
13 | declare type CommandWithAnswerInFlight = {
|
14 | resolve: (result?: any) => void;
|
15 | reject: (err: any) => void;
|
16 | };
|
17 | declare type CommandInFlight = SubscribeEventCommmandInFlight<any> | CommandWithAnswerInFlight;
|
18 | export declare class Connection {
|
19 | options: ConnectionOptions;
|
20 | commandId: number;
|
21 | commands: Map<number, CommandInFlight>;
|
22 | eventListeners: Map<string, ConnectionEventListener[]>;
|
23 | closeRequested: boolean;
|
24 | socket: HaWebSocket;
|
25 | constructor(socket: HaWebSocket, options: ConnectionOptions);
|
26 | get haVersion(): string;
|
27 | setSocket(socket: HaWebSocket): void;
|
28 | addEventListener(eventType: Events, callback: ConnectionEventListener): void;
|
29 | removeEventListener(eventType: Events, callback: ConnectionEventListener): void;
|
30 | fireEvent(eventType: Events, eventData?: any): void;
|
31 | close(): void;
|
32 | /**
|
33 | * Subscribe to a specific or all events.
|
34 | *
|
35 | * @param callback Callback to be called when a new event fires
|
36 | * @param eventType
|
37 | * @returns promise that resolves to an unsubscribe function
|
38 | */
|
39 | subscribeEvents<EventType>(callback: (ev: EventType) => void, eventType?: string): Promise<SubscriptionUnsubscribe>;
|
40 | ping(): Promise<unknown>;
|
41 | sendMessage(message: MessageBase, commandId?: number): void;
|
42 | sendMessagePromise<Result>(message: MessageBase): Promise<Result>;
|
43 | /**
|
44 | * Call a websocket command that starts a subscription on the backend.
|
45 | *
|
46 | * @param message the message to start the subscription
|
47 | * @param callback the callback to be called when a new item arrives
|
48 | * @returns promise that resolves to an unsubscribe function
|
49 | */
|
50 | subscribeMessage<Result>(callback: (result: Result) => void, subscribeMessage: MessageBase): Promise<SubscriptionUnsubscribe>;
|
51 | private _handleMessage;
|
52 | private _handleClose;
|
53 | private _genCmdId;
|
54 | }
|
55 | export {};
|