@webarray/esphome-native-api
Version:
TypeScript/Node.js client for ESPHome native API with encryption and deep sleep support
147 lines • 3.62 kB
TypeScript
/**
* Encrypted Connection Handler for ESPHome Native API
* Extends the base connection with noise protocol encryption
*/
import { EventEmitter } from 'eventemitter3';
import { DecodedMessage } from '../utils/protocol';
import { ConnectionOptions, ConnectionState, MessageType } from '../types';
export interface EncryptedConnectionEvents {
connect: () => void;
disconnect: (error?: Error) => void;
message: (message: DecodedMessage) => void;
error: (error: Error) => void;
stateChange: (state: ConnectionState) => void;
encryptionEstablished: () => void;
}
export declare class EncryptedConnection extends EventEmitter<EncryptedConnectionEvents> {
private socket;
private protocol;
private noise?;
private encryptor?;
private decryptor?;
private state;
private options;
private reconnectTimer?;
private pingTimer?;
private pingTimeoutTimer?;
private isReconnecting;
private isDestroyed;
private encryptionBuffer;
private encryptionEstablished;
private expectedDisconnect;
private hasDeepSleep;
constructor(options: ConnectionOptions);
/**
* Connect to the ESPHome device
*/
connect(): Promise<void>;
/**
* Establish the actual TCP connection
*/
private establishConnection;
/**
* Create a handshake frame
*/
private createHandshakeFrame;
/**
* Create an encrypted data frame
*/
private createDataFrame;
/**
* Setup socket event handlers (for non-encrypted connections)
*/
private setupSocketHandlers;
/**
* Handle incoming messages
*/
private handleMessage;
/**
* Send a message to the device
*/
sendMessage(type: MessageType, data: Buffer): void;
/**
* Handle disconnect request from device (deep sleep)
*/
private handleDisconnectRequest;
/**
* Disconnect from the device
*/
disconnect(): void;
/**
* Handle disconnection and potential reconnection
*/
private handleDisconnect;
/**
* Schedule a reconnection attempt
*/
private scheduleReconnect;
/**
* Enable deep sleep mode for this connection
*/
setDeepSleepMode(enabled: boolean): void;
/**
* Check if this is an expected disconnect (e.g., deep sleep)
*/
isExpectedDisconnect(): boolean;
/**
* Start the ping timer
*/
private startPingTimer;
/**
* Start the ping timeout timer
*/
private startPingTimeoutTimer;
/**
* Handle pong response
*/
private handlePongResponse;
/**
* Stop the ping timer
*/
private stopPingTimer;
/**
* Stop the ping timeout timer
*/
private stopPingTimeoutTimer;
/**
* Clean up resources
*/
private cleanup;
/**
* Update connection state
*/
private updateState;
/**
* Destroy the connection
*/
destroy(): void;
/**
* Get the current connection state
*/
getState(): ConnectionState;
/**
* Check if connected
*/
isConnected(): boolean;
/**
* Check if authenticated
*/
isAuthenticated(): boolean;
/**
* Check if encryption is established
*/
isEncrypted(): boolean;
/**
* Set authentication state
*/
setAuthenticated(authenticated: boolean): void;
/**
* Set API version
*/
setApiVersion(major: number, minor: number): void;
/**
* Set server info
*/
setServerInfo(info: string): void;
}
//# sourceMappingURL=encrypted-connection-broken.d.ts.map