esphome-ts 5.0.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
@@ -1,6 +1,17 @@
1
1
  import { BinaryWriter, BinaryReader } from '@bufbuild/protobuf/wire';
2
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
@@ -230,12 +241,19 @@ interface SensorEntity extends ListEntity {
230
241
  interface BinarySensorEntity extends ListEntity {
231
242
  deviceClass: string;
232
243
  }
244
+ interface CoverEntity extends ListEntity {
245
+ assumedState: boolean;
246
+ supportsPosition: boolean;
247
+ supportsTilt: boolean;
248
+ supportsStop?: boolean;
249
+ deviceClass: string;
250
+ }
233
251
  interface LightEntity extends ListEntity {
234
252
  effects: string[];
235
253
  supportsBrightness: boolean;
236
254
  supportsRgb: boolean;
237
255
  }
238
- type ComponentType = 'light' | 'binarySensor' | 'sensor' | 'switch';
256
+ type ComponentType = 'light' | 'binarySensor' | 'sensor' | 'switch' | 'cover';
239
257
 
240
258
  interface StateEvent {
241
259
  key: number;
@@ -247,6 +265,12 @@ interface BinarySensorStateEvent extends StateEvent {
247
265
  state?: boolean;
248
266
  }
249
267
  type SwitchStateEvent = BinarySensorStateEvent;
268
+ interface CoverStateEvent extends StateEvent {
269
+ legacyState?: LegacyCoverState;
270
+ position?: number;
271
+ tilt?: number;
272
+ currentOperation?: CoverOperation;
273
+ }
250
274
  interface LightStateEvent extends StateEvent {
251
275
  state?: boolean;
252
276
  brightness?: number;
@@ -286,6 +310,7 @@ declare class EspDevice {
286
310
  private readonly client;
287
311
  private readonly stateEvents$;
288
312
  deviceInfo?: DeviceInfoResponse;
313
+ private readonly apiVersion;
289
314
  readonly components: {
290
315
  [key: string]: BaseComponent;
291
316
  };
@@ -366,6 +391,54 @@ declare class BinarySensorComponent extends BaseComponent<BinarySensorEntity, Bi
366
391
  get type(): ComponentType;
367
392
  }
368
393
 
394
+ interface ApiVersion {
395
+ major: number;
396
+ minor: number;
397
+ }
398
+ declare class CoverComponent extends BaseComponent<CoverEntity, CoverStateEvent> {
399
+ private readonly apiVersion;
400
+ constructor(listEntity: CoverEntity, state: Observable<CoverStateEvent>, commandInterface: CommandInterface, apiVersion?: ApiVersion);
401
+ open(): void;
402
+ close(): void;
403
+ stop(): void;
404
+ setPosition(position: number): void;
405
+ setTilt(tilt: number): void;
406
+ get type(): ComponentType;
407
+ get assumedState(): boolean;
408
+ get supportsPosition(): boolean;
409
+ get supportsStop(): boolean;
410
+ get supportsTilt(): boolean;
411
+ /**
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.
415
+ */
416
+ get position(): number | undefined;
417
+ /**
418
+ * Current tilt between 0 and 1. Undefined until the first state was
419
+ * received or when the cover does not support tilt.
420
+ */
421
+ get tilt(): number | undefined;
422
+ /**
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.
425
+ */
426
+ get isClosed(): boolean | undefined;
427
+ /**
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.
430
+ */
431
+ get isOpen(): boolean | undefined;
432
+ /**
433
+ * What the cover is currently doing. Undefined until the first state was
434
+ * received.
435
+ */
436
+ get operation(): CoverOperation | undefined;
437
+ private get usesLegacyProtocol();
438
+ private isApiVersionAtLeast;
439
+ private sendCommand;
440
+ }
441
+
369
442
  interface Rgb {
370
443
  red: number;
371
444
  green: number;
@@ -403,4 +476,4 @@ declare class SensorComponent extends BaseComponent<SensorEntity, SensorStateEve
403
476
  get icon(): string;
404
477
  }
405
478
 
406
- export { BaseComponent, BinarySensorComponent, type BinarySensorEntity, type BinarySensorStateEvent, BinarySensorTypes, Client, type CommandInterface, type ComponentType, type Connection, EspDevice, EspSocket, type Hsv, InvalidPasswordError, 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 };
479
+ 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 };
package/dist/index.js CHANGED
@@ -1,5 +1,11 @@
1
1
  // src/api/protobuf/api.ts
2
2
  import { BinaryReader, BinaryWriter } from "@bufbuild/protobuf/wire";
3
+ var LegacyCoverState = /* @__PURE__ */ ((LegacyCoverState2) => {
4
+ LegacyCoverState2[LegacyCoverState2["LEGACY_COVER_STATE_OPEN"] = 0] = "LEGACY_COVER_STATE_OPEN";
5
+ LegacyCoverState2[LegacyCoverState2["LEGACY_COVER_STATE_CLOSED"] = 1] = "LEGACY_COVER_STATE_CLOSED";
6
+ LegacyCoverState2[LegacyCoverState2["UNRECOGNIZED"] = -1] = "UNRECOGNIZED";
7
+ return LegacyCoverState2;
8
+ })(LegacyCoverState || {});
3
9
  function legacyCoverStateFromJSON(object) {
4
10
  switch (object) {
5
11
  case 0:
@@ -25,6 +31,13 @@ function legacyCoverStateToJSON(object) {
25
31
  return "UNRECOGNIZED";
26
32
  }
27
33
  }
34
+ var CoverOperation = /* @__PURE__ */ ((CoverOperation2) => {
35
+ CoverOperation2[CoverOperation2["COVER_OPERATION_IDLE"] = 0] = "COVER_OPERATION_IDLE";
36
+ CoverOperation2[CoverOperation2["COVER_OPERATION_IS_OPENING"] = 1] = "COVER_OPERATION_IS_OPENING";
37
+ CoverOperation2[CoverOperation2["COVER_OPERATION_IS_CLOSING"] = 2] = "COVER_OPERATION_IS_CLOSING";
38
+ CoverOperation2[CoverOperation2["UNRECOGNIZED"] = -1] = "UNRECOGNIZED";
39
+ return CoverOperation2;
40
+ })(CoverOperation || {});
28
41
  function coverOperationFromJSON(object) {
29
42
  switch (object) {
30
43
  case 0:
@@ -55,6 +68,36 @@ function coverOperationToJSON(object) {
55
68
  return "UNRECOGNIZED";
56
69
  }
57
70
  }
71
+ function legacyCoverCommandFromJSON(object) {
72
+ switch (object) {
73
+ case 0:
74
+ case "LEGACY_COVER_COMMAND_OPEN":
75
+ return 0 /* LEGACY_COVER_COMMAND_OPEN */;
76
+ case 1:
77
+ case "LEGACY_COVER_COMMAND_CLOSE":
78
+ return 1 /* LEGACY_COVER_COMMAND_CLOSE */;
79
+ case 2:
80
+ case "LEGACY_COVER_COMMAND_STOP":
81
+ return 2 /* LEGACY_COVER_COMMAND_STOP */;
82
+ case -1:
83
+ case "UNRECOGNIZED":
84
+ default:
85
+ return -1 /* UNRECOGNIZED */;
86
+ }
87
+ }
88
+ function legacyCoverCommandToJSON(object) {
89
+ switch (object) {
90
+ case 0 /* LEGACY_COVER_COMMAND_OPEN */:
91
+ return "LEGACY_COVER_COMMAND_OPEN";
92
+ case 1 /* LEGACY_COVER_COMMAND_CLOSE */:
93
+ return "LEGACY_COVER_COMMAND_CLOSE";
94
+ case 2 /* LEGACY_COVER_COMMAND_STOP */:
95
+ return "LEGACY_COVER_COMMAND_STOP";
96
+ case -1 /* UNRECOGNIZED */:
97
+ default:
98
+ return "UNRECOGNIZED";
99
+ }
100
+ }
58
101
  function createBaseHelloRequest() {
59
102
  return { clientInfo: "" };
60
103
  }
@@ -950,6 +993,198 @@ var BinarySensorStateResponse = {
950
993
  return message;
951
994
  }
952
995
  };
996
+ function createBaseListEntitiesCoverResponse() {
997
+ return {
998
+ objectId: "",
999
+ key: 0,
1000
+ name: "",
1001
+ uniqueId: "",
1002
+ assumedState: false,
1003
+ supportsPosition: false,
1004
+ supportsTilt: false,
1005
+ deviceClass: "",
1006
+ supportsStop: false
1007
+ };
1008
+ }
1009
+ var ListEntitiesCoverResponse = {
1010
+ encode(message, writer = new BinaryWriter()) {
1011
+ if (message.objectId !== "") {
1012
+ writer.uint32(10).string(message.objectId);
1013
+ }
1014
+ if (message.key !== 0) {
1015
+ writer.uint32(21).fixed32(message.key);
1016
+ }
1017
+ if (message.name !== "") {
1018
+ writer.uint32(26).string(message.name);
1019
+ }
1020
+ if (message.uniqueId !== "") {
1021
+ writer.uint32(34).string(message.uniqueId);
1022
+ }
1023
+ if (message.assumedState !== false) {
1024
+ writer.uint32(40).bool(message.assumedState);
1025
+ }
1026
+ if (message.supportsPosition !== false) {
1027
+ writer.uint32(48).bool(message.supportsPosition);
1028
+ }
1029
+ if (message.supportsTilt !== false) {
1030
+ writer.uint32(56).bool(message.supportsTilt);
1031
+ }
1032
+ if (message.deviceClass !== "") {
1033
+ writer.uint32(66).string(message.deviceClass);
1034
+ }
1035
+ if (message.supportsStop !== false) {
1036
+ writer.uint32(96).bool(message.supportsStop);
1037
+ }
1038
+ return writer;
1039
+ },
1040
+ decode(input, length) {
1041
+ const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
1042
+ const previousRecursionDepth = reader.__tsProtoDecodeDepth ?? 0;
1043
+ if (previousRecursionDepth >= 100) {
1044
+ throw new globalThis.Error("protobuf decode recursion limit exceeded");
1045
+ }
1046
+ reader.__tsProtoDecodeDepth = previousRecursionDepth + 1;
1047
+ try {
1048
+ const end = length === void 0 ? reader.len : reader.pos + length;
1049
+ const message = createBaseListEntitiesCoverResponse();
1050
+ while (reader.pos < end) {
1051
+ const tag = reader.uint32();
1052
+ switch (tag >>> 3) {
1053
+ case 1: {
1054
+ if (tag !== 10) {
1055
+ break;
1056
+ }
1057
+ message.objectId = reader.string();
1058
+ continue;
1059
+ }
1060
+ case 2: {
1061
+ if (tag !== 21) {
1062
+ break;
1063
+ }
1064
+ message.key = reader.fixed32();
1065
+ continue;
1066
+ }
1067
+ case 3: {
1068
+ if (tag !== 26) {
1069
+ break;
1070
+ }
1071
+ message.name = reader.string();
1072
+ continue;
1073
+ }
1074
+ case 4: {
1075
+ if (tag !== 34) {
1076
+ break;
1077
+ }
1078
+ message.uniqueId = reader.string();
1079
+ continue;
1080
+ }
1081
+ case 5: {
1082
+ if (tag !== 40) {
1083
+ break;
1084
+ }
1085
+ message.assumedState = reader.bool();
1086
+ continue;
1087
+ }
1088
+ case 6: {
1089
+ if (tag !== 48) {
1090
+ break;
1091
+ }
1092
+ message.supportsPosition = reader.bool();
1093
+ continue;
1094
+ }
1095
+ case 7: {
1096
+ if (tag !== 56) {
1097
+ break;
1098
+ }
1099
+ message.supportsTilt = reader.bool();
1100
+ continue;
1101
+ }
1102
+ case 8: {
1103
+ if (tag !== 66) {
1104
+ break;
1105
+ }
1106
+ message.deviceClass = reader.string();
1107
+ continue;
1108
+ }
1109
+ case 12: {
1110
+ if (tag !== 96) {
1111
+ break;
1112
+ }
1113
+ message.supportsStop = reader.bool();
1114
+ continue;
1115
+ }
1116
+ }
1117
+ if ((tag & 7) === 4 || tag === 0) {
1118
+ break;
1119
+ }
1120
+ reader.skip(tag & 7);
1121
+ }
1122
+ return message;
1123
+ } finally {
1124
+ reader.__tsProtoDecodeDepth = previousRecursionDepth;
1125
+ }
1126
+ },
1127
+ fromJSON(object) {
1128
+ return {
1129
+ objectId: isSet(object.objectId) ? globalThis.String(object.objectId) : isSet(object.object_id) ? globalThis.String(object.object_id) : "",
1130
+ key: isSet(object.key) ? globalThis.Number(object.key) : 0,
1131
+ name: isSet(object.name) ? globalThis.String(object.name) : "",
1132
+ uniqueId: isSet(object.uniqueId) ? globalThis.String(object.uniqueId) : isSet(object.unique_id) ? globalThis.String(object.unique_id) : "",
1133
+ assumedState: isSet(object.assumedState) ? globalThis.Boolean(object.assumedState) : isSet(object.assumed_state) ? globalThis.Boolean(object.assumed_state) : false,
1134
+ supportsPosition: isSet(object.supportsPosition) ? globalThis.Boolean(object.supportsPosition) : isSet(object.supports_position) ? globalThis.Boolean(object.supports_position) : false,
1135
+ supportsTilt: isSet(object.supportsTilt) ? globalThis.Boolean(object.supportsTilt) : isSet(object.supports_tilt) ? globalThis.Boolean(object.supports_tilt) : false,
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
1138
+ };
1139
+ },
1140
+ toJSON(message) {
1141
+ const obj = {};
1142
+ if (message.objectId !== "") {
1143
+ obj.objectId = message.objectId;
1144
+ }
1145
+ if (message.key !== 0) {
1146
+ obj.key = Math.round(message.key);
1147
+ }
1148
+ if (message.name !== "") {
1149
+ obj.name = message.name;
1150
+ }
1151
+ if (message.uniqueId !== "") {
1152
+ obj.uniqueId = message.uniqueId;
1153
+ }
1154
+ if (message.assumedState !== false) {
1155
+ obj.assumedState = message.assumedState;
1156
+ }
1157
+ if (message.supportsPosition !== false) {
1158
+ obj.supportsPosition = message.supportsPosition;
1159
+ }
1160
+ if (message.supportsTilt !== false) {
1161
+ obj.supportsTilt = message.supportsTilt;
1162
+ }
1163
+ if (message.deviceClass !== "") {
1164
+ obj.deviceClass = message.deviceClass;
1165
+ }
1166
+ if (message.supportsStop !== false) {
1167
+ obj.supportsStop = message.supportsStop;
1168
+ }
1169
+ return obj;
1170
+ },
1171
+ create(base) {
1172
+ return ListEntitiesCoverResponse.fromPartial(base ?? {});
1173
+ },
1174
+ fromPartial(object) {
1175
+ const message = createBaseListEntitiesCoverResponse();
1176
+ message.objectId = object.objectId ?? "";
1177
+ message.key = object.key ?? 0;
1178
+ message.name = object.name ?? "";
1179
+ message.uniqueId = object.uniqueId ?? "";
1180
+ message.assumedState = object.assumedState ?? false;
1181
+ message.supportsPosition = object.supportsPosition ?? false;
1182
+ message.supportsTilt = object.supportsTilt ?? false;
1183
+ message.deviceClass = object.deviceClass ?? "";
1184
+ message.supportsStop = object.supportsStop ?? false;
1185
+ return message;
1186
+ }
1187
+ };
953
1188
  function createBaseCoverStateResponse() {
954
1189
  return { key: 0, legacyState: 0, position: 0, tilt: 0, currentOperation: 0 };
955
1190
  }
@@ -1072,6 +1307,182 @@ var CoverStateResponse = {
1072
1307
  return message;
1073
1308
  }
1074
1309
  };
1310
+ function createBaseCoverCommandRequest() {
1311
+ return {
1312
+ key: 0,
1313
+ hasLegacyCommand: false,
1314
+ legacyCommand: 0,
1315
+ hasPosition: false,
1316
+ position: 0,
1317
+ hasTilt: false,
1318
+ tilt: 0,
1319
+ stop: false
1320
+ };
1321
+ }
1322
+ var CoverCommandRequest = {
1323
+ encode(message, writer = new BinaryWriter()) {
1324
+ if (message.key !== 0) {
1325
+ writer.uint32(13).fixed32(message.key);
1326
+ }
1327
+ if (message.hasLegacyCommand !== false) {
1328
+ writer.uint32(16).bool(message.hasLegacyCommand);
1329
+ }
1330
+ if (message.legacyCommand !== 0) {
1331
+ writer.uint32(24).int32(message.legacyCommand);
1332
+ }
1333
+ if (message.hasPosition !== false) {
1334
+ writer.uint32(32).bool(message.hasPosition);
1335
+ }
1336
+ if (message.position !== 0) {
1337
+ writer.uint32(45).float(message.position);
1338
+ }
1339
+ if (message.hasTilt !== false) {
1340
+ writer.uint32(48).bool(message.hasTilt);
1341
+ }
1342
+ if (message.tilt !== 0) {
1343
+ writer.uint32(61).float(message.tilt);
1344
+ }
1345
+ if (message.stop !== false) {
1346
+ writer.uint32(64).bool(message.stop);
1347
+ }
1348
+ return writer;
1349
+ },
1350
+ decode(input, length) {
1351
+ const reader = input instanceof BinaryReader ? input : new BinaryReader(input);
1352
+ const previousRecursionDepth = reader.__tsProtoDecodeDepth ?? 0;
1353
+ if (previousRecursionDepth >= 100) {
1354
+ throw new globalThis.Error("protobuf decode recursion limit exceeded");
1355
+ }
1356
+ reader.__tsProtoDecodeDepth = previousRecursionDepth + 1;
1357
+ try {
1358
+ const end = length === void 0 ? reader.len : reader.pos + length;
1359
+ const message = createBaseCoverCommandRequest();
1360
+ while (reader.pos < end) {
1361
+ const tag = reader.uint32();
1362
+ switch (tag >>> 3) {
1363
+ case 1: {
1364
+ if (tag !== 13) {
1365
+ break;
1366
+ }
1367
+ message.key = reader.fixed32();
1368
+ continue;
1369
+ }
1370
+ case 2: {
1371
+ if (tag !== 16) {
1372
+ break;
1373
+ }
1374
+ message.hasLegacyCommand = reader.bool();
1375
+ continue;
1376
+ }
1377
+ case 3: {
1378
+ if (tag !== 24) {
1379
+ break;
1380
+ }
1381
+ message.legacyCommand = reader.int32();
1382
+ continue;
1383
+ }
1384
+ case 4: {
1385
+ if (tag !== 32) {
1386
+ break;
1387
+ }
1388
+ message.hasPosition = reader.bool();
1389
+ continue;
1390
+ }
1391
+ case 5: {
1392
+ if (tag !== 45) {
1393
+ break;
1394
+ }
1395
+ message.position = reader.float();
1396
+ continue;
1397
+ }
1398
+ case 6: {
1399
+ if (tag !== 48) {
1400
+ break;
1401
+ }
1402
+ message.hasTilt = reader.bool();
1403
+ continue;
1404
+ }
1405
+ case 7: {
1406
+ if (tag !== 61) {
1407
+ break;
1408
+ }
1409
+ message.tilt = reader.float();
1410
+ continue;
1411
+ }
1412
+ case 8: {
1413
+ if (tag !== 64) {
1414
+ break;
1415
+ }
1416
+ message.stop = reader.bool();
1417
+ continue;
1418
+ }
1419
+ }
1420
+ if ((tag & 7) === 4 || tag === 0) {
1421
+ break;
1422
+ }
1423
+ reader.skip(tag & 7);
1424
+ }
1425
+ return message;
1426
+ } finally {
1427
+ reader.__tsProtoDecodeDepth = previousRecursionDepth;
1428
+ }
1429
+ },
1430
+ fromJSON(object) {
1431
+ return {
1432
+ key: isSet(object.key) ? globalThis.Number(object.key) : 0,
1433
+ hasLegacyCommand: isSet(object.hasLegacyCommand) ? globalThis.Boolean(object.hasLegacyCommand) : isSet(object.has_legacy_command) ? globalThis.Boolean(object.has_legacy_command) : false,
1434
+ legacyCommand: isSet(object.legacyCommand) ? legacyCoverCommandFromJSON(object.legacyCommand) : isSet(object.legacy_command) ? legacyCoverCommandFromJSON(object.legacy_command) : 0,
1435
+ hasPosition: isSet(object.hasPosition) ? globalThis.Boolean(object.hasPosition) : isSet(object.has_position) ? globalThis.Boolean(object.has_position) : false,
1436
+ position: isSet(object.position) ? globalThis.Number(object.position) : 0,
1437
+ hasTilt: isSet(object.hasTilt) ? globalThis.Boolean(object.hasTilt) : isSet(object.has_tilt) ? globalThis.Boolean(object.has_tilt) : false,
1438
+ tilt: isSet(object.tilt) ? globalThis.Number(object.tilt) : 0,
1439
+ stop: isSet(object.stop) ? globalThis.Boolean(object.stop) : false
1440
+ };
1441
+ },
1442
+ toJSON(message) {
1443
+ const obj = {};
1444
+ if (message.key !== 0) {
1445
+ obj.key = Math.round(message.key);
1446
+ }
1447
+ if (message.hasLegacyCommand !== false) {
1448
+ obj.hasLegacyCommand = message.hasLegacyCommand;
1449
+ }
1450
+ if (message.legacyCommand !== 0) {
1451
+ obj.legacyCommand = legacyCoverCommandToJSON(message.legacyCommand);
1452
+ }
1453
+ if (message.hasPosition !== false) {
1454
+ obj.hasPosition = message.hasPosition;
1455
+ }
1456
+ if (message.position !== 0) {
1457
+ obj.position = message.position;
1458
+ }
1459
+ if (message.hasTilt !== false) {
1460
+ obj.hasTilt = message.hasTilt;
1461
+ }
1462
+ if (message.tilt !== 0) {
1463
+ obj.tilt = message.tilt;
1464
+ }
1465
+ if (message.stop !== false) {
1466
+ obj.stop = message.stop;
1467
+ }
1468
+ return obj;
1469
+ },
1470
+ create(base) {
1471
+ return CoverCommandRequest.fromPartial(base ?? {});
1472
+ },
1473
+ fromPartial(object) {
1474
+ const message = createBaseCoverCommandRequest();
1475
+ message.key = object.key ?? 0;
1476
+ message.hasLegacyCommand = object.hasLegacyCommand ?? false;
1477
+ message.legacyCommand = object.legacyCommand ?? 0;
1478
+ message.hasPosition = object.hasPosition ?? false;
1479
+ message.position = object.position ?? 0;
1480
+ message.hasTilt = object.hasTilt ?? false;
1481
+ message.tilt = object.tilt ?? 0;
1482
+ message.stop = object.stop ?? false;
1483
+ return message;
1484
+ }
1485
+ };
1075
1486
  function createBaseListEntitiesLightResponse() {
1076
1487
  return {
1077
1488
  objectId: "",
@@ -2919,6 +3330,154 @@ var BinarySensorComponent = class extends BaseComponent {
2919
3330
  }
2920
3331
  };
2921
3332
 
3333
+ // src/components/cover.ts
3334
+ import "rxjs";
3335
+ var DEFAULT_API_VERSION = { major: 1, minor: 8 };
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;
3342
+ open() {
3343
+ if (this.usesLegacyProtocol) {
3344
+ this.sendCommand({ legacyCommand: 0 /* LEGACY_COVER_COMMAND_OPEN */ });
3345
+ } else {
3346
+ this.sendCommand({ position: 1 });
3347
+ }
3348
+ }
3349
+ close() {
3350
+ if (this.usesLegacyProtocol) {
3351
+ this.sendCommand({ legacyCommand: 1 /* LEGACY_COVER_COMMAND_CLOSE */ });
3352
+ } else {
3353
+ this.sendCommand({ position: 0 });
3354
+ }
3355
+ }
3356
+ stop() {
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
+ }
3365
+ }
3366
+ setPosition(position) {
3367
+ if (this.usesLegacyProtocol || !this.supportsPosition) {
3368
+ return;
3369
+ }
3370
+ this.sendCommand({ position });
3371
+ }
3372
+ setTilt(tilt) {
3373
+ if (this.usesLegacyProtocol || !this.supportsTilt) {
3374
+ return;
3375
+ }
3376
+ this.sendCommand({ tilt });
3377
+ }
3378
+ get type() {
3379
+ return "cover";
3380
+ }
3381
+ get assumedState() {
3382
+ return this.listEntity.assumedState;
3383
+ }
3384
+ get supportsPosition() {
3385
+ return this.isApiVersionAtLeast(1, 1) && this.listEntity.supportsPosition;
3386
+ }
3387
+ get supportsStop() {
3388
+ return !this.isApiVersionAtLeast(1, 8) || (this.listEntity.supportsStop ?? false);
3389
+ }
3390
+ get supportsTilt() {
3391
+ return this.isApiVersionAtLeast(1, 1) && this.listEntity.supportsTilt;
3392
+ }
3393
+ /**
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.
3397
+ */
3398
+ get position() {
3399
+ if (this.usesLegacyProtocol) {
3400
+ return void 0;
3401
+ }
3402
+ return this.state.getValue()?.position;
3403
+ }
3404
+ /**
3405
+ * Current tilt between 0 and 1. Undefined until the first state was
3406
+ * received or when the cover does not support tilt.
3407
+ */
3408
+ get tilt() {
3409
+ if (!this.supportsTilt) {
3410
+ return void 0;
3411
+ }
3412
+ return this.state.getValue()?.tilt;
3413
+ }
3414
+ /**
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.
3417
+ */
3418
+ get isClosed() {
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;
3427
+ return position === void 0 ? void 0 : position === 0;
3428
+ }
3429
+ /**
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.
3432
+ */
3433
+ get isOpen() {
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;
3442
+ return position === void 0 ? void 0 : position === 1;
3443
+ }
3444
+ /**
3445
+ * What the cover is currently doing. Undefined until the first state was
3446
+ * received.
3447
+ */
3448
+ get operation() {
3449
+ return this.state.getValue()?.currentOperation;
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
+ }
3457
+ sendCommand(command) {
3458
+ this.queueCommand(
3459
+ 30 /* CoverCommandRequest */,
3460
+ () => CoverCommandRequest.encode({
3461
+ key: this.key,
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 */,
3466
+ hasPosition: command.position !== void 0,
3467
+ position: command.position ?? 0,
3468
+ hasTilt: command.tilt !== void 0,
3469
+ tilt: command.tilt ?? 0,
3470
+ stop: command.stop ?? false
3471
+ }).finish(),
3472
+ // Covers without position feedback never confirm a command with a
3473
+ // state response, so a serialized command would wait for the 30
3474
+ // second pipeline fallback. Like switch commands, cover commands
3475
+ // are cheap idempotent intents and go out immediately.
3476
+ true
3477
+ );
3478
+ }
3479
+ };
3480
+
2922
3481
  // src/components/helpers.ts
2923
3482
  var convertNumbers = (num, max = 255, fromEsp = false) => {
2924
3483
  if (fromEsp) {
@@ -3160,7 +3719,7 @@ var stateParser = (data) => {
3160
3719
  }
3161
3720
  return void 0;
3162
3721
  };
3163
- var createComponents = (data, stateEvents$, connection, knownComponents) => {
3722
+ var createComponents = (data, stateEvents$, connection, knownComponents, apiVersion = { major: 1, minor: 8 }) => {
3164
3723
  switch (data.type) {
3165
3724
  case 12 /* ListEntitiesBinarySensorResponse */: {
3166
3725
  const response = ListEntitiesBinarySensorResponse.decode(data.payload);
@@ -3173,6 +3732,17 @@ var createComponents = (data, stateEvents$, connection, knownComponents) => {
3173
3732
  component: new BinarySensorComponent(response, state$, emptyCommandInterface)
3174
3733
  };
3175
3734
  }
3735
+ case 13 /* ListEntitiesCoverResponse */: {
3736
+ const response = ListEntitiesCoverResponse.decode(data.payload);
3737
+ const state$ = transformStates(stateEvents$, response);
3738
+ return knownComponents.has(response.objectId) ? {
3739
+ id: response.objectId,
3740
+ state$
3741
+ } : {
3742
+ id: response.objectId,
3743
+ component: new CoverComponent(response, state$, connection, apiVersion)
3744
+ };
3745
+ }
3176
3746
  case 17 /* ListEntitiesSwitchResponse */: {
3177
3747
  const response = ListEntitiesSwitchResponse.decode(data.payload);
3178
3748
  const state$ = transformStates(stateEvents$, response);
@@ -3219,6 +3789,7 @@ var transformStates = (stateEvents$, listEntityResponse) => {
3219
3789
  // src/api/responses.ts
3220
3790
  var listResponses = /* @__PURE__ */ new Set([
3221
3791
  12 /* ListEntitiesBinarySensorResponse */,
3792
+ 13 /* ListEntitiesCoverResponse */,
3222
3793
  15 /* ListEntitiesLightResponse */,
3223
3794
  16 /* ListEntitiesSensorResponse */,
3224
3795
  17 /* ListEntitiesSwitchResponse */,
@@ -3316,6 +3887,10 @@ var EspDevice = class {
3316
3887
  // and a real device only answers HelloRequest, never
3317
3888
  // speaks first.
3318
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
+ }),
3319
3894
  switchMap2(() => this.client.connect({ password })),
3320
3895
  map2((response) => {
3321
3896
  if (response.invalidPassword) {
@@ -3365,6 +3940,10 @@ var EspDevice = class {
3365
3940
  client;
3366
3941
  stateEvents$;
3367
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 };
3368
3947
  components = {};
3369
3948
  discovery;
3370
3949
  discovery$;
@@ -3415,7 +3994,13 @@ var EspDevice = class {
3415
3994
  this.discovery.next(true);
3416
3995
  } else {
3417
3996
  const knownComponents = new Set(Object.keys(this.components));
3418
- 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
+ );
3419
4004
  if (component) {
3420
4005
  this.components[id] = this.components[id] ?? component;
3421
4006
  } else if (state$) {
@@ -3432,9 +4017,12 @@ export {
3432
4017
  BinarySensorComponent,
3433
4018
  BinarySensorTypes,
3434
4019
  Client,
4020
+ CoverComponent,
4021
+ CoverOperation,
3435
4022
  EspDevice,
3436
4023
  EspSocket,
3437
4024
  InvalidPasswordError,
4025
+ LegacyCoverState,
3438
4026
  LightComponent,
3439
4027
  MessageTypes,
3440
4028
  SensorComponent,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "esphome-ts",
3
- "version": "5.0.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,
@@ -38,7 +38,8 @@
38
38
  "test:watch": "vitest",
39
39
  "test:coverage": "vitest run --coverage",
40
40
  "prepack": "npm run cbuild",
41
- "semantic-release": "semantic-release"
41
+ "semantic-release": "semantic-release",
42
+ "prepare": "husky"
42
43
  },
43
44
  "keywords": [
44
45
  "esphome",
@@ -66,6 +67,8 @@
66
67
  "eslint-import-resolver-typescript": "4.4.5",
67
68
  "eslint-plugin-import-x": "4.17.1",
68
69
  "eslint-plugin-unused-imports": "4.4.1",
70
+ "husky": "9.1.7",
71
+ "lint-staged": "17.6.0",
69
72
  "prettier": "3.9.9",
70
73
  "rxjs": "7.8.2",
71
74
  "semantic-release": "25.0.9",