esphome-ts 5.1.0 → 5.1.1

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/dist/index.d.ts CHANGED
@@ -245,6 +245,7 @@ interface CoverEntity extends ListEntity {
245
245
  assumedState: boolean;
246
246
  supportsPosition: boolean;
247
247
  supportsTilt: boolean;
248
+ supportsStop?: boolean;
248
249
  deviceClass: string;
249
250
  }
250
251
  interface LightEntity extends ListEntity {
@@ -309,6 +310,7 @@ declare class EspDevice {
309
310
  private readonly client;
310
311
  private readonly stateEvents$;
311
312
  deviceInfo?: DeviceInfoResponse;
313
+ private readonly apiVersion;
312
314
  readonly components: {
313
315
  [key: string]: BaseComponent;
314
316
  };
@@ -389,7 +391,13 @@ declare class BinarySensorComponent extends BaseComponent<BinarySensorEntity, Bi
389
391
  get type(): ComponentType;
390
392
  }
391
393
 
394
+ interface ApiVersion {
395
+ major: number;
396
+ minor: number;
397
+ }
392
398
  declare class CoverComponent extends BaseComponent<CoverEntity, CoverStateEvent> {
399
+ private readonly apiVersion;
400
+ constructor(listEntity: CoverEntity, state: Observable<CoverStateEvent>, commandInterface: CommandInterface, apiVersion?: ApiVersion);
393
401
  open(): void;
394
402
  close(): void;
395
403
  stop(): void;
@@ -398,10 +406,12 @@ declare class CoverComponent extends BaseComponent<CoverEntity, CoverStateEvent>
398
406
  get type(): ComponentType;
399
407
  get assumedState(): boolean;
400
408
  get supportsPosition(): boolean;
409
+ get supportsStop(): boolean;
401
410
  get supportsTilt(): boolean;
402
411
  /**
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.
412
+ * Current position between 0 (closed) and 1 (open). This is feedback from
413
+ * the state response and is independent of position-control support. API
414
+ * versions before 1.1 do not report position values.
405
415
  */
406
416
  get position(): number | undefined;
407
417
  /**
@@ -410,15 +420,13 @@ declare class CoverComponent extends BaseComponent<CoverEntity, CoverStateEvent>
410
420
  */
411
421
  get tilt(): number | undefined;
412
422
  /**
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.
423
+ * Whether the cover is fully closed. API versions before 1.1 report this
424
+ * using the legacy state enum; newer versions report the position.
416
425
  */
417
426
  get isClosed(): boolean | undefined;
418
427
  /**
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.
428
+ * Whether the cover is fully open. Uses the legacy state before API 1.1
429
+ * and position thereafter; a partially open cover is neither open nor closed.
422
430
  */
423
431
  get isOpen(): boolean | undefined;
424
432
  /**
@@ -426,6 +434,8 @@ declare class CoverComponent extends BaseComponent<CoverEntity, CoverStateEvent>
426
434
  * received.
427
435
  */
428
436
  get operation(): CoverOperation | undefined;
437
+ private get usesLegacyProtocol();
438
+ private isApiVersionAtLeast;
429
439
  private sendCommand;
430
440
  }
431
441
 
package/dist/index.js CHANGED
@@ -1002,7 +1002,8 @@ function createBaseListEntitiesCoverResponse() {
1002
1002
  assumedState: false,
1003
1003
  supportsPosition: false,
1004
1004
  supportsTilt: false,
1005
- deviceClass: ""
1005
+ deviceClass: "",
1006
+ supportsStop: false
1006
1007
  };
1007
1008
  }
1008
1009
  var ListEntitiesCoverResponse = {
@@ -1031,6 +1032,9 @@ var ListEntitiesCoverResponse = {
1031
1032
  if (message.deviceClass !== "") {
1032
1033
  writer.uint32(66).string(message.deviceClass);
1033
1034
  }
1035
+ if (message.supportsStop !== false) {
1036
+ writer.uint32(96).bool(message.supportsStop);
1037
+ }
1034
1038
  return writer;
1035
1039
  },
1036
1040
  decode(input, length) {
@@ -1102,6 +1106,13 @@ var ListEntitiesCoverResponse = {
1102
1106
  message.deviceClass = reader.string();
1103
1107
  continue;
1104
1108
  }
1109
+ case 12: {
1110
+ if (tag !== 96) {
1111
+ break;
1112
+ }
1113
+ message.supportsStop = reader.bool();
1114
+ continue;
1115
+ }
1105
1116
  }
1106
1117
  if ((tag & 7) === 4 || tag === 0) {
1107
1118
  break;
@@ -1122,7 +1133,8 @@ var ListEntitiesCoverResponse = {
1122
1133
  assumedState: isSet(object.assumedState) ? globalThis.Boolean(object.assumedState) : isSet(object.assumed_state) ? globalThis.Boolean(object.assumed_state) : false,
1123
1134
  supportsPosition: isSet(object.supportsPosition) ? globalThis.Boolean(object.supportsPosition) : isSet(object.supports_position) ? globalThis.Boolean(object.supports_position) : false,
1124
1135
  supportsTilt: isSet(object.supportsTilt) ? globalThis.Boolean(object.supportsTilt) : isSet(object.supports_tilt) ? globalThis.Boolean(object.supports_tilt) : false,
1125
- deviceClass: isSet(object.deviceClass) ? globalThis.String(object.deviceClass) : isSet(object.device_class) ? globalThis.String(object.device_class) : ""
1136
+ deviceClass: isSet(object.deviceClass) ? globalThis.String(object.deviceClass) : isSet(object.device_class) ? globalThis.String(object.device_class) : "",
1137
+ supportsStop: isSet(object.supportsStop) ? globalThis.Boolean(object.supportsStop) : isSet(object.supports_stop) ? globalThis.Boolean(object.supports_stop) : false
1126
1138
  };
1127
1139
  },
1128
1140
  toJSON(message) {
@@ -1151,6 +1163,9 @@ var ListEntitiesCoverResponse = {
1151
1163
  if (message.deviceClass !== "") {
1152
1164
  obj.deviceClass = message.deviceClass;
1153
1165
  }
1166
+ if (message.supportsStop !== false) {
1167
+ obj.supportsStop = message.supportsStop;
1168
+ }
1154
1169
  return obj;
1155
1170
  },
1156
1171
  create(base) {
@@ -1166,6 +1181,7 @@ var ListEntitiesCoverResponse = {
1166
1181
  message.supportsPosition = object.supportsPosition ?? false;
1167
1182
  message.supportsTilt = object.supportsTilt ?? false;
1168
1183
  message.deviceClass = object.deviceClass ?? "";
1184
+ message.supportsStop = object.supportsStop ?? false;
1169
1185
  return message;
1170
1186
  }
1171
1187
  };
@@ -3315,24 +3331,46 @@ var BinarySensorComponent = class extends BaseComponent {
3315
3331
  };
3316
3332
 
3317
3333
  // src/components/cover.ts
3334
+ import "rxjs";
3335
+ var DEFAULT_API_VERSION = { major: 1, minor: 8 };
3318
3336
  var CoverComponent = class extends BaseComponent {
3337
+ constructor(listEntity, state, commandInterface, apiVersion = DEFAULT_API_VERSION) {
3338
+ super(listEntity, state, commandInterface);
3339
+ this.apiVersion = apiVersion;
3340
+ }
3341
+ apiVersion;
3319
3342
  open() {
3320
- this.sendCommand({ position: 1 });
3343
+ if (this.usesLegacyProtocol) {
3344
+ this.sendCommand({ legacyCommand: 0 /* LEGACY_COVER_COMMAND_OPEN */ });
3345
+ } else {
3346
+ this.sendCommand({ position: 1 });
3347
+ }
3321
3348
  }
3322
3349
  close() {
3323
- this.sendCommand({ position: 0 });
3350
+ if (this.usesLegacyProtocol) {
3351
+ this.sendCommand({ legacyCommand: 1 /* LEGACY_COVER_COMMAND_CLOSE */ });
3352
+ } else {
3353
+ this.sendCommand({ position: 0 });
3354
+ }
3324
3355
  }
3325
3356
  stop() {
3326
- this.sendCommand({ stop: true });
3357
+ if (!this.supportsStop) {
3358
+ return;
3359
+ }
3360
+ if (this.usesLegacyProtocol) {
3361
+ this.sendCommand({ legacyCommand: 2 /* LEGACY_COVER_COMMAND_STOP */ });
3362
+ } else {
3363
+ this.sendCommand({ stop: true });
3364
+ }
3327
3365
  }
3328
3366
  setPosition(position) {
3329
- if (!this.supportsPosition) {
3367
+ if (this.usesLegacyProtocol || !this.supportsPosition) {
3330
3368
  return;
3331
3369
  }
3332
3370
  this.sendCommand({ position });
3333
3371
  }
3334
3372
  setTilt(tilt) {
3335
- if (!this.supportsTilt) {
3373
+ if (this.usesLegacyProtocol || !this.supportsTilt) {
3336
3374
  return;
3337
3375
  }
3338
3376
  this.sendCommand({ tilt });
@@ -3344,17 +3382,21 @@ var CoverComponent = class extends BaseComponent {
3344
3382
  return this.listEntity.assumedState;
3345
3383
  }
3346
3384
  get supportsPosition() {
3347
- return this.listEntity.supportsPosition;
3385
+ return this.isApiVersionAtLeast(1, 1) && this.listEntity.supportsPosition;
3386
+ }
3387
+ get supportsStop() {
3388
+ return !this.isApiVersionAtLeast(1, 8) || (this.listEntity.supportsStop ?? false);
3348
3389
  }
3349
3390
  get supportsTilt() {
3350
- return this.listEntity.supportsTilt;
3391
+ return this.isApiVersionAtLeast(1, 1) && this.listEntity.supportsTilt;
3351
3392
  }
3352
3393
  /**
3353
- * Current position between 0 (closed) and 1 (open). Undefined until the
3354
- * first state was received or when the cover does not support positions.
3394
+ * Current position between 0 (closed) and 1 (open). This is feedback from
3395
+ * the state response and is independent of position-control support. API
3396
+ * versions before 1.1 do not report position values.
3355
3397
  */
3356
3398
  get position() {
3357
- if (!this.supportsPosition) {
3399
+ if (this.usesLegacyProtocol) {
3358
3400
  return void 0;
3359
3401
  }
3360
3402
  return this.state.getValue()?.position;
@@ -3370,21 +3412,33 @@ var CoverComponent = class extends BaseComponent {
3370
3412
  return this.state.getValue()?.tilt;
3371
3413
  }
3372
3414
  /**
3373
- * Whether the cover is fully closed. Derived from the position field the
3374
- * device always sends, exactly like aioesphomeapi derives is_closed
3375
- * (position == 0). Undefined until the first state was received.
3415
+ * Whether the cover is fully closed. API versions before 1.1 report this
3416
+ * using the legacy state enum; newer versions report the position.
3376
3417
  */
3377
3418
  get isClosed() {
3378
- const position = this.state.getValue()?.position;
3419
+ const state = this.state.getValue();
3420
+ if (state === void 0) {
3421
+ return void 0;
3422
+ }
3423
+ if (this.usesLegacyProtocol) {
3424
+ return state.legacyState === 1 /* LEGACY_COVER_STATE_CLOSED */;
3425
+ }
3426
+ const position = state.position;
3379
3427
  return position === void 0 ? void 0 : position === 0;
3380
3428
  }
3381
3429
  /**
3382
- * Whether the cover is fully open. Derived from the position field the
3383
- * device always sends. Undefined until the first state was received; a
3384
- * partially open cover is neither open nor closed.
3430
+ * Whether the cover is fully open. Uses the legacy state before API 1.1
3431
+ * and position thereafter; a partially open cover is neither open nor closed.
3385
3432
  */
3386
3433
  get isOpen() {
3387
- const position = this.state.getValue()?.position;
3434
+ const state = this.state.getValue();
3435
+ if (state === void 0) {
3436
+ return void 0;
3437
+ }
3438
+ if (this.usesLegacyProtocol) {
3439
+ return state.legacyState === 0 /* LEGACY_COVER_STATE_OPEN */;
3440
+ }
3441
+ const position = state.position;
3388
3442
  return position === void 0 ? void 0 : position === 1;
3389
3443
  }
3390
3444
  /**
@@ -3394,18 +3448,21 @@ var CoverComponent = class extends BaseComponent {
3394
3448
  get operation() {
3395
3449
  return this.state.getValue()?.currentOperation;
3396
3450
  }
3451
+ get usesLegacyProtocol() {
3452
+ return !this.isApiVersionAtLeast(1, 1);
3453
+ }
3454
+ isApiVersionAtLeast(major, minor) {
3455
+ return this.apiVersion.major > major || this.apiVersion.major === major && this.apiVersion.minor >= minor;
3456
+ }
3397
3457
  sendCommand(command) {
3398
3458
  this.queueCommand(
3399
3459
  30 /* CoverCommandRequest */,
3400
3460
  () => CoverCommandRequest.encode({
3401
3461
  key: this.key,
3402
- // The legacy command fields are dead: Home Assistant last
3403
- // sent them to pre-2019 devices and firmware dropped their
3404
- // handling in 2025.8.0. Only the values below reach the
3405
- // wire; these two are wire-inert defaults that satisfy
3406
- // the generated message type.
3407
- hasLegacyCommand: false,
3408
- legacyCommand: 0 /* LEGACY_COVER_COMMAND_OPEN */,
3462
+ // API 1.0 predates position/stop commands; retain the
3463
+ // legacy enum command for those firmware versions.
3464
+ hasLegacyCommand: command.legacyCommand !== void 0,
3465
+ legacyCommand: command.legacyCommand ?? 0 /* LEGACY_COVER_COMMAND_OPEN */,
3409
3466
  hasPosition: command.position !== void 0,
3410
3467
  position: command.position ?? 0,
3411
3468
  hasTilt: command.tilt !== void 0,
@@ -3662,7 +3719,7 @@ var stateParser = (data) => {
3662
3719
  }
3663
3720
  return void 0;
3664
3721
  };
3665
- var createComponents = (data, stateEvents$, connection, knownComponents) => {
3722
+ var createComponents = (data, stateEvents$, connection, knownComponents, apiVersion = { major: 1, minor: 8 }) => {
3666
3723
  switch (data.type) {
3667
3724
  case 12 /* ListEntitiesBinarySensorResponse */: {
3668
3725
  const response = ListEntitiesBinarySensorResponse.decode(data.payload);
@@ -3683,7 +3740,7 @@ var createComponents = (data, stateEvents$, connection, knownComponents) => {
3683
3740
  state$
3684
3741
  } : {
3685
3742
  id: response.objectId,
3686
- component: new CoverComponent(response, state$, connection)
3743
+ component: new CoverComponent(response, state$, connection, apiVersion)
3687
3744
  };
3688
3745
  }
3689
3746
  case 17 /* ListEntitiesSwitchResponse */: {
@@ -3830,6 +3887,10 @@ var EspDevice = class {
3830
3887
  // and a real device only answers HelloRequest, never
3831
3888
  // speaks first.
3832
3889
  defer(() => this.client.hello({ clientInfo: "esphome-ts" })).pipe(
3890
+ tap4((helloResponse) => {
3891
+ this.apiVersion.major = helloResponse.apiVersionMajor;
3892
+ this.apiVersion.minor = helloResponse.apiVersionMinor;
3893
+ }),
3833
3894
  switchMap2(() => this.client.connect({ password })),
3834
3895
  map2((response) => {
3835
3896
  if (response.invalidPassword) {
@@ -3879,6 +3940,10 @@ var EspDevice = class {
3879
3940
  client;
3880
3941
  stateEvents$;
3881
3942
  deviceInfo;
3943
+ // The HelloResponse selects the protocol version for each connection. Keep
3944
+ // one mutable per-device value so covers created on discovery and reused
3945
+ // after reconnects see the version negotiated on the latest handshake.
3946
+ apiVersion = { major: 0, minor: 0 };
3882
3947
  components = {};
3883
3948
  discovery;
3884
3949
  discovery$;
@@ -3929,7 +3994,13 @@ var EspDevice = class {
3929
3994
  this.discovery.next(true);
3930
3995
  } else {
3931
3996
  const knownComponents = new Set(Object.keys(this.components));
3932
- const { id, component, state$ } = createComponents(data, this.stateEvents$, this.socket, knownComponents);
3997
+ const { id, component, state$ } = createComponents(
3998
+ data,
3999
+ this.stateEvents$,
4000
+ this.socket,
4001
+ knownComponents,
4002
+ this.apiVersion
4003
+ );
3933
4004
  if (component) {
3934
4005
  this.components[id] = this.components[id] ?? component;
3935
4006
  } else if (state$) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "esphome-ts",
3
- "version": "5.1.0",
3
+ "version": "5.1.1",
4
4
  "description": "A TypeScript client for the Esphome Native API interface",
5
5
  "type": "module",
6
6
  "sideEffects": false,