esphome-ts 4.0.0 → 5.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -6,10 +6,10 @@ This is a client library for use with [esphome](https://esphome.io).
6
6
 
7
7
  ```typescript
8
8
  import { EspDevice, SwitchComponent } from 'esphome-ts';
9
- import { filter, tap } from 'rxjs/operators';
9
+ import { filter, tap } from 'rxjs';
10
10
 
11
11
  const device = new EspDevice('my_esp.local');
12
- device.discovery$
12
+ const subscription = device.discovery$
13
13
  .pipe(
14
14
  filter((value) => value),
15
15
  tap(() => {
@@ -20,6 +20,9 @@ device.discovery$
20
20
  }),
21
21
  )
22
22
  .subscribe();
23
+
24
+ // Call `subscription.unsubscribe()` to stop watching, and `device.terminate()`
25
+ // to tear the device down for good.
23
26
  ```
24
27
 
25
28
  `rxjs` (>= 7.8) is a peer dependency: install it next to the library (`npm install esphome-ts rxjs`).
@@ -39,13 +42,29 @@ deep imports such as the previously documented `esphome-ts/dist` no longer resol
39
42
  exports map. The legacy `Connection` class, the `isLightComponent` type guard and internal helpers
40
43
  (`decode`, `stateParser`, `convertNumbers`, `BytePositions`, …) are no longer exported; the public
41
44
  surface is `EspDevice` (with `InvalidPasswordError`), `Client`, `EspSocket`, `RxjsSocket`,
42
- `MessageTypes`, `ReadData`, the `isSwitchComponent` type guard, the `isTrue`/`isFalse` filters and
45
+ `MessageTypes`, `ReadData` (type), the `isSwitchComponent` type guard, the `isTrue`/`isFalse` filters and
43
46
  the component classes with their entity and state types. `EspDevice` now reconnects when a
44
- connection attempt fails, reports invalid passwords and undecodable frames on its new `error$`
45
- observable instead of stalling or crashing, and `Client.listEntities()` /
47
+ connection attempt fails, reports invalid passwords and undecodable discovery frames on its
48
+ new `error$` observable instead of stalling or crashing, and `Client.listEntities()` /
46
49
  `Client.subscribeStateChange()` emit `void` instead of a placeholder message. The
47
50
  generated protobuf layer now depends on `@bufbuild/protobuf` instead of `protobufjs`.
48
51
 
52
+ ## Upgrading to v5
53
+
54
+ v5 replaces the exported inheritance-based socket stack with the `Connection` seam:
55
+
56
+ - `RxjsSocket` is internal now — it became `TcpConnection` and is no longer exported.
57
+ - `EspSocket` no longer exposes `data$`, `send()`, `timeout$`, `close(force)` — use
58
+ `sendEspMessage` and `terminate()`.
59
+ - `reconnectOnTimeout`/`disconnectOnTimeout` are gone — an idle timeout always tears the
60
+ connection down, and reconnection stays driven by `EspDevice`.
61
+ - If you need a custom transport, inject your own `Connection` via `EspDevice`'s new options
62
+ parameter or via `EspSocket`'s config. The new `Connection` is an exported interface TYPE for
63
+ custom transports — unrelated to the legacy `Connection` class that v4 removed.
64
+ - ESPHome re-sends identical states; `state$` now de-duplicates them (shallow compare,
65
+ `Object.is` per field) and emits only on actual changes — subscribers no longer see phantom
66
+ re-emissions.
67
+
49
68
  ## Development
50
69
 
51
70
  The protobuf layer in `src/api/protobuf/` is generated from the `.proto` files alongside it. After changing a `.proto` file, regenerate with:
package/dist/index.d.ts CHANGED
@@ -1,6 +1,17 @@
1
1
  import { BinaryWriter, BinaryReader } from '@bufbuild/protobuf/wire';
2
- import { Observable, Subject, BehaviorSubject } from 'rxjs';
2
+ import { Observable, BehaviorSubject, Subject } from 'rxjs';
3
3
 
4
+ declare enum LegacyCoverState {
5
+ LEGACY_COVER_STATE_OPEN = 0,
6
+ LEGACY_COVER_STATE_CLOSED = 1,
7
+ UNRECOGNIZED = -1
8
+ }
9
+ declare enum CoverOperation {
10
+ COVER_OPERATION_IDLE = 0,
11
+ COVER_OPERATION_IS_OPENING = 1,
12
+ COVER_OPERATION_IS_CLOSING = 2,
13
+ UNRECOGNIZED = -1
14
+ }
4
15
  /**
5
16
  * Message sent at the beginning of each connection
6
17
  * Can only be sent by the client and only at the beginning of the connection
@@ -149,33 +160,32 @@ interface CommandInterface {
149
160
  sendEspMessage(type: MessageTypes, data: Uint8Array): void;
150
161
  }
151
162
 
152
- interface RxjsSocketConfiguration {
153
- reconnectOnTimeout?: boolean;
154
- timeout?: number;
155
- disconnectOnTimeout?: boolean;
156
- }
157
- declare class RxjsSocket {
158
- private readonly host;
159
- private readonly port;
160
- private socket?;
161
- private readonly config;
162
- private readonly data;
163
- readonly data$: Observable<Buffer>;
164
- private readonly connected;
165
- readonly connected$: Observable<boolean>;
166
- private readonly timeout;
167
- readonly timeout$: Observable<void>;
168
- protected readonly error: Subject<Error>;
169
- readonly error$: Observable<Error>;
170
- protected readonly terminate: Subject<void>;
171
- constructor(host: string, port: number, config?: RxjsSocketConfiguration);
163
+ /**
164
+ * Connection seam: the live path between the library and a device, along which
165
+ * frames travel as byte chunks. The TCP path is the production Connection; any
166
+ * implementation satisfying the same interface may substitute for it.
167
+ *
168
+ * Contract per member:
169
+ * - `open()`: repeatable — a no-op while a connect attempt is already in flight;
170
+ * otherwise a call with a live path tears it down and opens a fresh one.
171
+ * - `terminate()`: with a path or failed attempt open, tears it down and emits `false` on
172
+ * `connected$`; with nothing open it is a no-op — first or repeat call alike.
173
+ * - `connected$`: hot state stream that MUST emit its current value synchronously to every new
174
+ * subscriber (e.g. a BehaviorSubject seeded false pre-connect) — `EspDevice` drives its initial
175
+ * `open()` from the synchronous seed; a plain hot Subject or cold observable silently never
176
+ * connects. Never completes.
177
+ * - `chunks$`: hot, no emission while disconnected, never completes.
178
+ * - `error$`: hot, never completes — late errors after `terminate()` keep arriving.
179
+ */
180
+ interface Connection {
172
181
  open(): void;
173
- close(forceDestroy?: boolean): void;
182
+ terminate(): void;
174
183
  isConnected(): boolean;
175
- send(data: string | Uint8Array, encoding?: BufferEncoding): Observable<void>;
176
- private setupSocketEvents;
177
- private reconnect;
178
- private destroySocket;
184
+ readonly connected$: Observable<boolean>;
185
+ readonly chunks$: Observable<Buffer>;
186
+ readonly error$: Observable<unknown>;
187
+ /** Fire-and-forget. Silently drops when not connected. Write errors surface on error$. */
188
+ write(data: Uint8Array): void;
179
189
  }
180
190
 
181
191
  interface ReadData {
@@ -183,9 +193,22 @@ interface ReadData {
183
193
  payload: Uint8Array;
184
194
  }
185
195
 
186
- declare class EspSocket extends RxjsSocket implements CommandInterface {
196
+ interface EspSocketConfiguration {
197
+ /** Ignored when `connection` is set: idle-timeout teardown is owned by the injected adapter. */
198
+ timeout?: number;
199
+ connection?: Connection;
200
+ }
201
+ declare class EspSocket implements CommandInterface {
202
+ private readonly connection;
203
+ private readonly teardown;
204
+ private readonly errors;
205
+ readonly connected$: Observable<boolean>;
187
206
  readonly espData$: Observable<ReadData>;
188
- constructor(host: string, port: number, config?: RxjsSocketConfiguration);
207
+ readonly error$: Observable<unknown>;
208
+ constructor(host: string, port: number, config?: EspSocketConfiguration);
209
+ open(): void;
210
+ isConnected(): boolean;
211
+ terminate(): void;
189
212
  sendEspMessage(type: MessageTypes, payload: Uint8Array): void;
190
213
  }
191
214
 
@@ -218,12 +241,18 @@ interface SensorEntity extends ListEntity {
218
241
  interface BinarySensorEntity extends ListEntity {
219
242
  deviceClass: string;
220
243
  }
244
+ interface CoverEntity extends ListEntity {
245
+ assumedState: boolean;
246
+ supportsPosition: boolean;
247
+ supportsTilt: boolean;
248
+ deviceClass: string;
249
+ }
221
250
  interface LightEntity extends ListEntity {
222
251
  effects: string[];
223
252
  supportsBrightness: boolean;
224
253
  supportsRgb: boolean;
225
254
  }
226
- type ComponentType = 'light' | 'binarySensor' | 'sensor' | 'switch';
255
+ type ComponentType = 'light' | 'binarySensor' | 'sensor' | 'switch' | 'cover';
227
256
 
228
257
  interface StateEvent {
229
258
  key: number;
@@ -235,6 +264,12 @@ interface BinarySensorStateEvent extends StateEvent {
235
264
  state?: boolean;
236
265
  }
237
266
  type SwitchStateEvent = BinarySensorStateEvent;
267
+ interface CoverStateEvent extends StateEvent {
268
+ legacyState?: LegacyCoverState;
269
+ position?: number;
270
+ tilt?: number;
271
+ currentOperation?: CoverOperation;
272
+ }
238
273
  interface LightStateEvent extends StateEvent {
239
274
  state?: boolean;
240
275
  brightness?: number;
@@ -287,8 +322,23 @@ declare class EspDevice {
287
322
  */
288
323
  readonly error$: Observable<unknown>;
289
324
  private readonly subscription;
325
+ private retrySubscription?;
326
+ private passwordRejected;
290
327
  readonly alive$: Observable<boolean>;
291
- constructor(host: string, password?: string, port?: number);
328
+ constructor(host: string, password?: string, port?: number,
329
+ /**
330
+ * An injected Connection owns its own liveness/idle teardown — the
331
+ * socket-level `PING_TIMEOUT` does not apply to it.
332
+ */
333
+ options?: {
334
+ connection?: Connection;
335
+ });
336
+ /**
337
+ * Provides the reconnect cadence used while the device is unreachable.
338
+ * Calling this again replaces the previously provided retry signal, so
339
+ * repeated calls cannot stack parallel reconnect loops. Providing a cadence
340
+ * is also the explicit opt-in re-arm after a password rejection.
341
+ */
292
342
  provideRetryObservable(retryWhen$: Observable<unknown>): void;
293
343
  terminate(): void;
294
344
  private parseListResponse;
@@ -339,6 +389,46 @@ declare class BinarySensorComponent extends BaseComponent<BinarySensorEntity, Bi
339
389
  get type(): ComponentType;
340
390
  }
341
391
 
392
+ declare class CoverComponent extends BaseComponent<CoverEntity, CoverStateEvent> {
393
+ open(): void;
394
+ close(): void;
395
+ stop(): void;
396
+ setPosition(position: number): void;
397
+ setTilt(tilt: number): void;
398
+ get type(): ComponentType;
399
+ get assumedState(): boolean;
400
+ get supportsPosition(): boolean;
401
+ get supportsTilt(): boolean;
402
+ /**
403
+ * Current position between 0 (closed) and 1 (open). Undefined until the
404
+ * first state was received or when the cover does not support positions.
405
+ */
406
+ get position(): number | undefined;
407
+ /**
408
+ * Current tilt between 0 and 1. Undefined until the first state was
409
+ * received or when the cover does not support tilt.
410
+ */
411
+ get tilt(): number | undefined;
412
+ /**
413
+ * Whether the cover is fully closed. Derived from the position field the
414
+ * device always sends, exactly like aioesphomeapi derives is_closed
415
+ * (position == 0). Undefined until the first state was received.
416
+ */
417
+ get isClosed(): boolean | undefined;
418
+ /**
419
+ * Whether the cover is fully open. Derived from the position field the
420
+ * device always sends. Undefined until the first state was received; a
421
+ * partially open cover is neither open nor closed.
422
+ */
423
+ get isOpen(): boolean | undefined;
424
+ /**
425
+ * What the cover is currently doing. Undefined until the first state was
426
+ * received.
427
+ */
428
+ get operation(): CoverOperation | undefined;
429
+ private sendCommand;
430
+ }
431
+
342
432
  interface Rgb {
343
433
  red: number;
344
434
  green: number;
@@ -376,4 +466,4 @@ declare class SensorComponent extends BaseComponent<SensorEntity, SensorStateEve
376
466
  get icon(): string;
377
467
  }
378
468
 
379
- export { BaseComponent, BinarySensorComponent, type BinarySensorEntity, type BinarySensorStateEvent, BinarySensorTypes, Client, type CommandInterface, type ComponentType, EspDevice, EspSocket, type Hsv, InvalidPasswordError, LightComponent, type LightEntity, type LightStateEvent, type ListEntity, MessageTypes, type ReadData, type Rgb, RxjsSocket, type RxjsSocketConfiguration, SensorComponent, type SensorEntity, type SensorStateEvent, type StateEvent, SwitchComponent, type SwitchStateEvent, isFalse, isSwitchComponent, isTrue };
469
+ export { BaseComponent, BinarySensorComponent, type BinarySensorEntity, type BinarySensorStateEvent, BinarySensorTypes, Client, type CommandInterface, type ComponentType, type Connection, CoverComponent, type CoverEntity, CoverOperation, type CoverStateEvent, EspDevice, EspSocket, type Hsv, InvalidPasswordError, LegacyCoverState, LightComponent, type LightEntity, type LightStateEvent, type ListEntity, MessageTypes, type ReadData, type Rgb, SensorComponent, type SensorEntity, type SensorStateEvent, type StateEvent, SwitchComponent, type SwitchStateEvent, isFalse, isSwitchComponent, isTrue };