UNPKG

@webarray/esphome-native-api

Version:

TypeScript/Node.js client for ESPHome native API with encryption and deep sleep support

133 lines 3.58 kB
/** * Encrypted Connection Handler for ESPHome Native API * Based on the official Python aioesphomeapi implementation * Uses Noise Protocol Framework (Noise_NNpsk0_25519_ChaChaPoly_SHA256) */ import { EventEmitter } from 'eventemitter3'; import { DecodedMessage } from '../utils/protocol'; import { ConnectionOptions } from '../types'; export interface ConnectionState { connected: boolean; authenticated: boolean; apiVersion?: { major: number; minor: number; }; serverInfo?: string; } 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; /** * Perform the noise protocol handshake based on ESPHome protocol spec * Protocol: Noise_NNpsk0_25519_ChaChaPoly_SHA256 * Reference: https://developers.esphome.io/architecture/api/protocol_details/ */ private performNoiseHandshake; /** * Create a handshake frame */ private createHandshakeFrame; /** * Create an encrypted data frame */ private createDataFrame; /** * Setup socket event handlers */ private setupSocketHandlers; /** * Handle incoming messages */ private handleMessage; /** * Handle disconnect request from device (deep sleep) */ private handleDisconnectRequest; disconnect(): void; /** * Send a message to the device */ sendMessage(type: number, data: Buffer): void; getState(): ConnectionState; isConnected(): boolean; isAuthenticated(): boolean; isEncrypted(): boolean; setAuthenticated(authenticated: boolean): void; setApiVersion(major: number, minor: number): void; setServerInfo(info: string): void; destroy(): 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 */ 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; private updateState; } //# sourceMappingURL=encrypted-connection.d.ts.map