esphome-ts 3.3.1 → 4.0.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.
Files changed (60) hide show
  1. package/README.md +33 -1
  2. package/dist/index.d.ts +379 -2
  3. package/dist/index.js +3355 -18
  4. package/package.json +58 -39
  5. package/dist/api/bytePositions.d.ts +0 -8
  6. package/dist/api/bytePositions.js +0 -12
  7. package/dist/api/client.d.ts +0 -22
  8. package/dist/api/client.js +0 -60
  9. package/dist/api/connection.d.ts +0 -18
  10. package/dist/api/connection.js +0 -51
  11. package/dist/api/espDevice.d.ts +0 -23
  12. package/dist/api/espDevice.js +0 -76
  13. package/dist/api/espSocket.d.ts +0 -13
  14. package/dist/api/espSocket.js +0 -35
  15. package/dist/api/helpers.d.ts +0 -17
  16. package/dist/api/helpers.js +0 -102
  17. package/dist/api/index.d.ts +0 -10
  18. package/dist/api/index.js +0 -26
  19. package/dist/api/interfaces.d.ts +0 -4
  20. package/dist/api/interfaces.js +0 -2
  21. package/dist/api/protobuf/api.d.ts +0 -922
  22. package/dist/api/protobuf/api.js +0 -6678
  23. package/dist/api/protobuf/api_options.d.ts +0 -24
  24. package/dist/api/protobuf/api_options.js +0 -76
  25. package/dist/api/protobuf/google_descriptor.d.ts +0 -1022
  26. package/dist/api/protobuf/google_descriptor.js +0 -3980
  27. package/dist/api/requestResponseMatching.d.ts +0 -50
  28. package/dist/api/requestResponseMatching.js +0 -54
  29. package/dist/api/responses.d.ts +0 -3
  30. package/dist/api/responses.js +0 -23
  31. package/dist/api/socket.d.ts +0 -29
  32. package/dist/api/socket.js +0 -109
  33. package/dist/api/typeGuards.d.ts +0 -5
  34. package/dist/api/typeGuards.js +0 -7
  35. package/dist/components/base.d.ts +0 -23
  36. package/dist/components/base.js +0 -51
  37. package/dist/components/binarySensor.d.ts +0 -9
  38. package/dist/components/binarySensor.js +0 -19
  39. package/dist/components/binarySensorTypes.d.ts +0 -26
  40. package/dist/components/binarySensorTypes.js +0 -30
  41. package/dist/components/commandInterface.d.ts +0 -4
  42. package/dist/components/commandInterface.js +0 -2
  43. package/dist/components/entities.d.ts +0 -21
  44. package/dist/components/entities.js +0 -2
  45. package/dist/components/helpers.d.ts +0 -1
  46. package/dist/components/helpers.js +0 -12
  47. package/dist/components/index.d.ts +0 -11
  48. package/dist/components/index.js +0 -27
  49. package/dist/components/interfaces.d.ts +0 -10
  50. package/dist/components/interfaces.js +0 -2
  51. package/dist/components/light.d.ts +0 -22
  52. package/dist/components/light.js +0 -159
  53. package/dist/components/sensor.d.ts +0 -10
  54. package/dist/components/sensor.js +0 -23
  55. package/dist/components/states.d.ts +0 -18
  56. package/dist/components/states.js +0 -2
  57. package/dist/components/switch.d.ts +0 -9
  58. package/dist/components/switch.js +0 -28
  59. package/dist/run.d.ts +0 -1
  60. package/dist/run.js +0 -17
package/README.md CHANGED
@@ -5,7 +5,7 @@ This is a client library for use with [esphome](https://esphome.io).
5
5
  ## Example use
6
6
 
7
7
  ```typescript
8
- import { EspDevice, SwitchComponent } from 'esphome-ts/dist';
8
+ import { EspDevice, SwitchComponent } from 'esphome-ts';
9
9
  import { filter, tap } from 'rxjs/operators';
10
10
 
11
11
  const device = new EspDevice('my_esp.local');
@@ -22,8 +22,40 @@ device.discovery$
22
22
  .subscribe();
23
23
  ```
24
24
 
25
+ `rxjs` (>= 7.8) is a peer dependency: install it next to the library (`npm install esphome-ts rxjs`).
26
+
25
27
  Please see more [here](docs/index.md)
26
28
 
29
+ ## Examples
30
+
31
+ See [examples/kitchenLights.ts](examples/kitchenLights.ts) for a runnable demo script (`npx tsx examples/kitchenLights.ts <device-host>`).
32
+
33
+ ## Upgrading to v4
34
+
35
+ v4 is a breaking modernisation release. The package is now ESM-only (`"type": "module"`) and
36
+ requires Node.js >= 22.12.0; CommonJS consumers should stay on v3 (or use `require(esm)` on
37
+ Node >= 22.12). Import from the package root (e.g. `import { EspDevice } from 'esphome-ts'`) —
38
+ deep imports such as the previously documented `esphome-ts/dist` no longer resolve under the
39
+ exports map. The legacy `Connection` class, the `isLightComponent` type guard and internal helpers
40
+ (`decode`, `stateParser`, `convertNumbers`, `BytePositions`, …) are no longer exported; the public
41
+ surface is `EspDevice` (with `InvalidPasswordError`), `Client`, `EspSocket`, `RxjsSocket`,
42
+ `MessageTypes`, `ReadData`, the `isSwitchComponent` type guard, the `isTrue`/`isFalse` filters and
43
+ 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()` /
46
+ `Client.subscribeStateChange()` emit `void` instead of a placeholder message. The
47
+ generated protobuf layer now depends on `@bufbuild/protobuf` instead of `protobufjs`.
48
+
49
+ ## Development
50
+
51
+ The protobuf layer in `src/api/protobuf/` is generated from the `.proto` files alongside it. After changing a `.proto` file, regenerate with:
52
+
53
+ ```bash
54
+ npm run proto
55
+ ```
56
+
57
+ This requires `protoc` on your `PATH` (e.g. `brew install protobuf`). Do not edit the generated files by hand.
58
+
27
59
  ## Contribution
28
60
 
29
61
  Please, feel free to make a PR and contribute to this project. Esphome is a good project, and this expands their
package/dist/index.d.ts CHANGED
@@ -1,2 +1,379 @@
1
- export * from './api';
2
- export * from './components';
1
+ import { BinaryWriter, BinaryReader } from '@bufbuild/protobuf/wire';
2
+ import { Observable, Subject, BehaviorSubject } from 'rxjs';
3
+
4
+ /**
5
+ * Message sent at the beginning of each connection
6
+ * Can only be sent by the client and only at the beginning of the connection
7
+ */
8
+ interface HelloRequest {
9
+ /**
10
+ * Description of client (like User Agent)
11
+ * For example "Home Assistant"
12
+ * Not strictly necessary to send but nice for debugging
13
+ * purposes.
14
+ */
15
+ clientInfo: string;
16
+ }
17
+ declare const HelloRequest: MessageFns<HelloRequest>;
18
+ /**
19
+ * Confirmation of successful connection request.
20
+ * Can only be sent by the server and only at the beginning of the connection
21
+ */
22
+ interface HelloResponse {
23
+ /**
24
+ * The version of the API to use. The _client_ (for example Home Assistant) needs to check
25
+ * for compatibility and if necessary adopt to an older API.
26
+ * Major is for breaking changes in the base protocol - a mismatch will lead to immediate disconnect_client_
27
+ * Minor is for breaking changes in individual messages - a mismatch will lead to a warning message
28
+ */
29
+ apiVersionMajor: number;
30
+ apiVersionMinor: number;
31
+ /**
32
+ * A string identifying the server (ESP); like client info this may be empty
33
+ * and only exists for debugging/logging purposes.
34
+ * For example "ESPHome v1.10.0 on ESP8266"
35
+ */
36
+ serverInfo: string;
37
+ }
38
+ declare const HelloResponse: MessageFns<HelloResponse>;
39
+ /**
40
+ * Message sent at the beginning of each connection to authenticate the client
41
+ * Can only be sent by the client and only at the beginning of the connection
42
+ */
43
+ interface ConnectRequest {
44
+ /** The password to log in with */
45
+ password: string;
46
+ }
47
+ declare const ConnectRequest: MessageFns<ConnectRequest>;
48
+ /**
49
+ * Confirmation of successful connection. After this the connection is available for all traffic.
50
+ * Can only be sent by the server and only at the beginning of the connection
51
+ */
52
+ interface ConnectResponse {
53
+ invalidPassword: boolean;
54
+ }
55
+ declare const ConnectResponse: MessageFns<ConnectResponse>;
56
+ interface PingResponse {
57
+ }
58
+ declare const PingResponse: MessageFns<PingResponse>;
59
+ interface DeviceInfoResponse {
60
+ usesPassword: boolean;
61
+ /** The name of the node, given by "App.set_name()" */
62
+ name: string;
63
+ /** The mac address of the device. For example "AC:BC:32:89:0E:A9" */
64
+ macAddress: string;
65
+ /** A string describing the ESPHome version. For example "1.10.0" */
66
+ esphomeVersion: string;
67
+ /**
68
+ * A string describing the date of compilation, this is generated by the compiler
69
+ * and therefore may not be in the same format all the time.
70
+ * If the user isn't using ESPHome, this will also not be set.
71
+ */
72
+ compilationTime: string;
73
+ /** The model of the board. For example NodeMCU */
74
+ model: string;
75
+ hasDeepSleep: boolean;
76
+ }
77
+ declare const DeviceInfoResponse: MessageFns<DeviceInfoResponse>;
78
+ type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
79
+ type DeepPartial<T> = T extends Builtin ? T : T extends globalThis.Array<infer U> ? globalThis.Array<DeepPartial<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<DeepPartial<U>> : T extends {} ? {
80
+ [K in keyof T]?: DeepPartial<T[K]>;
81
+ } : Partial<T>;
82
+ type KeysOfUnion<T> = T extends T ? keyof T : never;
83
+ type Exact<P, I extends P> = P extends Builtin ? P : P & {
84
+ [K in keyof P]: Exact<P[K], I[K]>;
85
+ } & {
86
+ [K in Exclude<keyof I, KeysOfUnion<P>>]: never;
87
+ };
88
+ interface MessageFns<T> {
89
+ encode(message: T, writer?: BinaryWriter): BinaryWriter;
90
+ decode(input: BinaryReader | Uint8Array, length?: number): T;
91
+ fromJSON(object: any): T;
92
+ toJSON(message: T): unknown;
93
+ create<I extends Exact<DeepPartial<T>, I>>(base?: I): T;
94
+ fromPartial<I extends Exact<DeepPartial<T>, I>>(object: I): T;
95
+ }
96
+
97
+ declare enum MessageTypes {
98
+ HelloRequest = 1,
99
+ HelloResponse = 2,
100
+ ConnectRequest = 3,
101
+ ConnectResponse = 4,
102
+ DisconnectRequest = 5,
103
+ DisconnectResponse = 6,
104
+ PingRequest = 7,
105
+ PingResponse = 8,
106
+ DeviceInfoRequest = 9,
107
+ DeviceInfoResponse = 10,
108
+ ListEntitiesRequest = 11,
109
+ ListEntitiesBinarySensorResponse = 12,
110
+ ListEntitiesCoverResponse = 13,
111
+ ListEntitiesFanResponse = 14,
112
+ ListEntitiesLightResponse = 15,
113
+ ListEntitiesSensorResponse = 16,
114
+ ListEntitiesSwitchResponse = 17,
115
+ ListEntitiesTextSensorResponse = 18,
116
+ ListEntitiesDoneResponse = 19,
117
+ SubscribeStatesRequest = 20,
118
+ BinarySensorStateResponse = 21,
119
+ CoverStateResponse = 22,
120
+ FanStateResponse = 23,
121
+ LightStateResponse = 24,
122
+ SensorStateResponse = 25,
123
+ SwitchStateResponse = 26,
124
+ TextSensorStateResponse = 27,
125
+ SubscribeLogsRequest = 28,
126
+ SubscribeLogsResponse = 29,
127
+ CoverCommandRequest = 30,
128
+ FanCommandRequest = 31,
129
+ LightCommandRequest = 32,
130
+ SwitchCommandRequest = 33,
131
+ SubscribeHomeassistantServicesRequest = 34,
132
+ HomeassistantServiceResponse = 35,
133
+ GetTimeRequest = 36,
134
+ GetTimeResponse = 37,
135
+ SubscribeHomeAssistantStatesRequest = 38,
136
+ SubscribeHomeAssistantStateResponse = 39,
137
+ HomeAssistantStateResponse = 40,
138
+ ListEntitiesServicesResponse = 41,
139
+ ExecuteServiceRequest = 42,
140
+ ListEntitiesCameraResponse = 43,
141
+ CameraImageResponse = 44,
142
+ CameraImageRequest = 45,
143
+ ListEntitiesClimateResponse = 46,
144
+ ClimateStateResponse = 47,
145
+ ClimateCommandRequest = 48
146
+ }
147
+
148
+ interface CommandInterface {
149
+ sendEspMessage(type: MessageTypes, data: Uint8Array): void;
150
+ }
151
+
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);
172
+ open(): void;
173
+ close(forceDestroy?: boolean): void;
174
+ isConnected(): boolean;
175
+ send(data: string | Uint8Array, encoding?: BufferEncoding): Observable<void>;
176
+ private setupSocketEvents;
177
+ private reconnect;
178
+ private destroySocket;
179
+ }
180
+
181
+ interface ReadData {
182
+ type: MessageTypes;
183
+ payload: Uint8Array;
184
+ }
185
+
186
+ declare class EspSocket extends RxjsSocket implements CommandInterface {
187
+ readonly espData$: Observable<ReadData>;
188
+ constructor(host: string, port: number, config?: RxjsSocketConfiguration);
189
+ sendEspMessage(type: MessageTypes, payload: Uint8Array): void;
190
+ }
191
+
192
+ declare class Client {
193
+ private readonly socket;
194
+ private readonly subscription;
195
+ constructor(socket: EspSocket);
196
+ terminate(): void;
197
+ hello(request: HelloRequest): Observable<HelloResponse>;
198
+ connect(request: ConnectRequest): Observable<ConnectResponse>;
199
+ ping(): Observable<PingResponse>;
200
+ deviceInfo(): Observable<DeviceInfoResponse>;
201
+ listEntities(): Observable<void>;
202
+ subscribeStateChange(): Observable<void>;
203
+ private request;
204
+ }
205
+
206
+ interface ListEntity {
207
+ key: number;
208
+ name: string;
209
+ uniqueId: string;
210
+ objectId: string;
211
+ }
212
+ interface SensorEntity extends ListEntity {
213
+ accuracyDecimals: number;
214
+ deviceClass?: string;
215
+ icon: string;
216
+ unitOfMeasurement: string;
217
+ }
218
+ interface BinarySensorEntity extends ListEntity {
219
+ deviceClass: string;
220
+ }
221
+ interface LightEntity extends ListEntity {
222
+ effects: string[];
223
+ supportsBrightness: boolean;
224
+ supportsRgb: boolean;
225
+ }
226
+ type ComponentType = 'light' | 'binarySensor' | 'sensor' | 'switch';
227
+
228
+ interface StateEvent {
229
+ key: number;
230
+ }
231
+ interface SensorStateEvent extends StateEvent {
232
+ state?: number;
233
+ }
234
+ interface BinarySensorStateEvent extends StateEvent {
235
+ state?: boolean;
236
+ }
237
+ type SwitchStateEvent = BinarySensorStateEvent;
238
+ interface LightStateEvent extends StateEvent {
239
+ state?: boolean;
240
+ brightness?: number;
241
+ red?: number;
242
+ green?: number;
243
+ blue?: number;
244
+ effect?: string;
245
+ }
246
+
247
+ declare abstract class BaseComponent<L extends ListEntity = ListEntity, S extends StateEvent = StateEvent> {
248
+ protected readonly listEntity: L;
249
+ private readonly commandInterface;
250
+ protected readonly state: BehaviorSubject<S | undefined>;
251
+ readonly state$: Observable<S>;
252
+ private readonly terminatePreviousStateSubscription;
253
+ private readonly commandInPipeline;
254
+ protected readonly teardown: Subject<void>;
255
+ constructor(listEntity: L, state: Observable<S>, commandInterface: CommandInterface);
256
+ provideStateObservable(state$: Observable<S>): void;
257
+ get ready(): boolean;
258
+ get name(): string;
259
+ get key(): number;
260
+ toString(): string;
261
+ terminate(): void;
262
+ protected queueCommand(type: MessageTypes, dataFn: () => Uint8Array, disableSerialise?: boolean): void;
263
+ abstract get type(): ComponentType;
264
+ }
265
+
266
+ declare class InvalidPasswordError extends Error {
267
+ constructor(host: string);
268
+ }
269
+ declare class EspDevice {
270
+ private readonly host;
271
+ private readonly password;
272
+ private readonly port;
273
+ private readonly socket;
274
+ private readonly client;
275
+ private readonly stateEvents$;
276
+ deviceInfo?: DeviceInfoResponse;
277
+ readonly components: {
278
+ [key: string]: BaseComponent;
279
+ };
280
+ private readonly discovery;
281
+ readonly discovery$: Observable<boolean>;
282
+ private readonly errors;
283
+ /**
284
+ * Device level failures: frames that could not be decoded and handshake
285
+ * problems such as an `InvalidPasswordError`. Socket level errors are
286
+ * available on the socket itself.
287
+ */
288
+ readonly error$: Observable<unknown>;
289
+ private readonly subscription;
290
+ readonly alive$: Observable<boolean>;
291
+ constructor(host: string, password?: string, port?: number);
292
+ provideRetryObservable(retryWhen$: Observable<unknown>): void;
293
+ terminate(): void;
294
+ private parseListResponse;
295
+ }
296
+
297
+ declare const isTrue: (val: unknown) => val is true;
298
+ declare const isFalse: (val: unknown) => val is false;
299
+
300
+ declare class SwitchComponent extends BaseComponent<ListEntity, SwitchStateEvent> {
301
+ get status(): boolean;
302
+ turnOn(): void;
303
+ turnOff(): void;
304
+ get type(): ComponentType;
305
+ }
306
+
307
+ declare const isSwitchComponent: (component: BaseComponent) => component is SwitchComponent;
308
+
309
+ declare enum BinarySensorTypes {
310
+ NONE = "None",
311
+ BATTERY = "battery",
312
+ COLD = "cold",
313
+ CONNECTIVITY = "connectivity",
314
+ DOOR = "door",
315
+ GARAGE_DOOR = "garage_door",
316
+ GAS = "gas",
317
+ HEAT = "heat",
318
+ LIGHT = "light",
319
+ LOCK = "lock",
320
+ MOISTURE = "moisture",
321
+ MOTION = "motion",
322
+ MOVING = "moving",
323
+ OCCUPANCY = "occupancy",
324
+ OPENING = "opening",
325
+ PLUG = "plug",
326
+ POWER = "power",
327
+ PRESENCE = "presence",
328
+ PROBLEM = "problem",
329
+ SAFETY = "safety",
330
+ SMOKE = "smoke",
331
+ SOUND = "sound",
332
+ VIBRATION = "vibration",
333
+ WINDOW = "window"
334
+ }
335
+
336
+ declare class BinarySensorComponent extends BaseComponent<BinarySensorEntity, BinarySensorStateEvent> {
337
+ get deviceClass(): BinarySensorTypes;
338
+ get status(): boolean;
339
+ get type(): ComponentType;
340
+ }
341
+
342
+ interface Rgb {
343
+ red: number;
344
+ green: number;
345
+ blue: number;
346
+ }
347
+ interface Hsv {
348
+ hue: number;
349
+ saturation: number;
350
+ value: number;
351
+ }
352
+
353
+ declare class LightComponent extends BaseComponent<LightEntity, LightStateEvent> {
354
+ turnOn(): void;
355
+ turnOff(): void;
356
+ getBrightness(): number | undefined;
357
+ setBrightness(brightness: number): void;
358
+ get hsv(): Hsv;
359
+ set hsv(hsv: Hsv);
360
+ get rgb(): Rgb;
361
+ set rgb({ red, green, blue }: Rgb);
362
+ get type(): ComponentType;
363
+ get supportsRgb(): boolean;
364
+ get supportsBrightness(): boolean;
365
+ availableEffects(): string[];
366
+ get effect(): string;
367
+ set effect(effect: string);
368
+ private generateState;
369
+ }
370
+
371
+ declare class SensorComponent extends BaseComponent<SensorEntity, SensorStateEvent> {
372
+ get value(): number | undefined;
373
+ get type(): ComponentType;
374
+ get deviceClass(): string | undefined;
375
+ get unitOfMeasurement(): string;
376
+ get icon(): string;
377
+ }
378
+
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 };