esphome-ts 5.1.1 → 5.1.3
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 +6 -0
- package/dist/index.js +230 -121
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -335,6 +335,12 @@ declare class EspDevice {
|
|
|
335
335
|
options?: {
|
|
336
336
|
connection?: Connection;
|
|
337
337
|
});
|
|
338
|
+
private watchDeviceMessages;
|
|
339
|
+
private handleDeviceMessage;
|
|
340
|
+
private watchConnection;
|
|
341
|
+
private handshake;
|
|
342
|
+
private retryFailedConnections;
|
|
343
|
+
private createLivenessObservable;
|
|
338
344
|
/**
|
|
339
345
|
* Provides the reconnect cadence used while the device is unreachable.
|
|
340
346
|
* Calling this again replaces the previously provided retry signal, so
|
package/dist/index.js
CHANGED
|
@@ -2822,7 +2822,6 @@ function isSet(value) {
|
|
|
2822
2822
|
|
|
2823
2823
|
// src/api/espSocket.ts
|
|
2824
2824
|
import {
|
|
2825
|
-
EMPTY,
|
|
2826
2825
|
filter,
|
|
2827
2826
|
from,
|
|
2828
2827
|
merge,
|
|
@@ -2832,6 +2831,7 @@ import {
|
|
|
2832
2831
|
switchMap,
|
|
2833
2832
|
take as take2,
|
|
2834
2833
|
takeUntil as takeUntil2,
|
|
2834
|
+
tap as tap2,
|
|
2835
2835
|
timeout
|
|
2836
2836
|
} from "rxjs";
|
|
2837
2837
|
|
|
@@ -3028,22 +3028,62 @@ var TcpConnection = class {
|
|
|
3028
3028
|
};
|
|
3029
3029
|
|
|
3030
3030
|
// src/api/framer.ts
|
|
3031
|
+
var MAX_PAYLOAD_SIZE = 65535;
|
|
3032
|
+
var MAX_VARINT_BYTES = 5;
|
|
3033
|
+
var MAX_MESSAGE_TYPE = 65535;
|
|
3034
|
+
var encodeVarint = (value) => {
|
|
3035
|
+
const bytes = [];
|
|
3036
|
+
while (value >= 128) {
|
|
3037
|
+
bytes.push(value & 127 | 128);
|
|
3038
|
+
value = Math.floor(value / 128);
|
|
3039
|
+
}
|
|
3040
|
+
bytes.push(value);
|
|
3041
|
+
return bytes;
|
|
3042
|
+
};
|
|
3031
3043
|
var encodeFrame = (type, payload) => {
|
|
3032
|
-
|
|
3033
|
-
|
|
3044
|
+
const messageType = Number(type);
|
|
3045
|
+
if (payload.length > MAX_PAYLOAD_SIZE) {
|
|
3046
|
+
throw new RangeError(`frame payload exceeds wire limit: ${payload.length} > ${MAX_PAYLOAD_SIZE} bytes`);
|
|
3034
3047
|
}
|
|
3035
|
-
|
|
3048
|
+
if (!Number.isSafeInteger(messageType) || messageType < 0 || messageType > MAX_MESSAGE_TYPE) {
|
|
3049
|
+
throw new RangeError(`frame message type must be an unsigned 16-bit integer: ${messageType}`);
|
|
3050
|
+
}
|
|
3051
|
+
const lengthBytes = encodeVarint(payload.length);
|
|
3052
|
+
const typeBytes = encodeVarint(messageType);
|
|
3053
|
+
const frame = new Uint8Array(1 + lengthBytes.length + typeBytes.length + payload.length);
|
|
3036
3054
|
frame[0] = 0;
|
|
3037
|
-
frame
|
|
3038
|
-
frame
|
|
3039
|
-
frame.set(payload,
|
|
3055
|
+
frame.set(lengthBytes, 1);
|
|
3056
|
+
frame.set(typeBytes, 1 + lengthBytes.length);
|
|
3057
|
+
frame.set(payload, 1 + lengthBytes.length + typeBytes.length);
|
|
3040
3058
|
return frame;
|
|
3041
3059
|
};
|
|
3060
|
+
var decodeVarint = (buffer, offset) => {
|
|
3061
|
+
let value = 0;
|
|
3062
|
+
let multiplier = 1;
|
|
3063
|
+
for (let index = 0; index < MAX_VARINT_BYTES; index++) {
|
|
3064
|
+
const position = offset + index;
|
|
3065
|
+
if (position >= buffer.length) {
|
|
3066
|
+
return { status: "incomplete" };
|
|
3067
|
+
}
|
|
3068
|
+
const byte = buffer[position];
|
|
3069
|
+
if (index === MAX_VARINT_BYTES - 1 && (byte & 240) !== 0) {
|
|
3070
|
+
return { status: "invalid" };
|
|
3071
|
+
}
|
|
3072
|
+
value += (byte & 127) * multiplier;
|
|
3073
|
+
if ((byte & 128) === 0) {
|
|
3074
|
+
return { status: "value", value, next: position + 1 };
|
|
3075
|
+
}
|
|
3076
|
+
multiplier *= 128;
|
|
3077
|
+
}
|
|
3078
|
+
return { status: "invalid" };
|
|
3079
|
+
};
|
|
3042
3080
|
var createFrameParser = () => {
|
|
3043
3081
|
let buffer = Buffer.alloc(0);
|
|
3044
3082
|
return {
|
|
3045
3083
|
push(chunk) {
|
|
3046
|
-
|
|
3084
|
+
if (chunk.length > 0) {
|
|
3085
|
+
buffer = Buffer.concat([buffer, chunk]);
|
|
3086
|
+
}
|
|
3047
3087
|
const frames = [];
|
|
3048
3088
|
while (buffer.length > 0) {
|
|
3049
3089
|
if (buffer[0] !== 0) {
|
|
@@ -3051,18 +3091,33 @@ var createFrameParser = () => {
|
|
|
3051
3091
|
buffer = sync === -1 ? Buffer.alloc(0) : buffer.subarray(sync);
|
|
3052
3092
|
continue;
|
|
3053
3093
|
}
|
|
3054
|
-
|
|
3094
|
+
const length = decodeVarint(buffer, 1);
|
|
3095
|
+
if (length.status === "incomplete") {
|
|
3096
|
+
break;
|
|
3097
|
+
}
|
|
3098
|
+
if (length.status === "invalid" || length.value > MAX_PAYLOAD_SIZE) {
|
|
3099
|
+
const sync = buffer.indexOf(0, 1);
|
|
3100
|
+
buffer = sync === -1 ? Buffer.alloc(0) : buffer.subarray(sync);
|
|
3101
|
+
continue;
|
|
3102
|
+
}
|
|
3103
|
+
const type = decodeVarint(buffer, length.next);
|
|
3104
|
+
if (type.status === "incomplete") {
|
|
3055
3105
|
break;
|
|
3056
3106
|
}
|
|
3057
|
-
|
|
3058
|
-
|
|
3107
|
+
if (type.status === "invalid" || type.value > MAX_MESSAGE_TYPE) {
|
|
3108
|
+
const sync = buffer.indexOf(0, 1);
|
|
3109
|
+
buffer = sync === -1 ? Buffer.alloc(0) : buffer.subarray(sync);
|
|
3110
|
+
continue;
|
|
3111
|
+
}
|
|
3112
|
+
const end = type.next + length.value;
|
|
3113
|
+
if (buffer.length < end) {
|
|
3059
3114
|
break;
|
|
3060
3115
|
}
|
|
3061
3116
|
frames.push({
|
|
3062
|
-
type:
|
|
3063
|
-
payload: new Uint8Array(buffer.subarray(
|
|
3117
|
+
type: type.value,
|
|
3118
|
+
payload: new Uint8Array(buffer.subarray(type.next, end))
|
|
3064
3119
|
});
|
|
3065
|
-
buffer = buffer.subarray(
|
|
3120
|
+
buffer = buffer.subarray(end);
|
|
3066
3121
|
}
|
|
3067
3122
|
return frames;
|
|
3068
3123
|
}
|
|
@@ -3109,10 +3164,9 @@ var EspSocket = class {
|
|
|
3109
3164
|
timeout(5e3),
|
|
3110
3165
|
filter(isTrue),
|
|
3111
3166
|
take2(1),
|
|
3112
|
-
|
|
3167
|
+
tap2(() => {
|
|
3113
3168
|
const final = encodeFrame(type, payload);
|
|
3114
3169
|
this.connection.write(final);
|
|
3115
|
-
return EMPTY;
|
|
3116
3170
|
}),
|
|
3117
3171
|
// terminate() kills a pending send, but open() deliberately does
|
|
3118
3172
|
// NOT: a command issued during a blip rides the reconnect within
|
|
@@ -3128,14 +3182,14 @@ var EspSocket = class {
|
|
|
3128
3182
|
};
|
|
3129
3183
|
|
|
3130
3184
|
// src/api/client.ts
|
|
3131
|
-
import { filter as filter2, map, of, Subscription, take as take3, tap as
|
|
3185
|
+
import { filter as filter2, map, of, Subscription, take as take3, tap as tap3 } from "rxjs";
|
|
3132
3186
|
var Client = class {
|
|
3133
3187
|
constructor(socket) {
|
|
3134
3188
|
this.socket = socket;
|
|
3135
3189
|
this.subscription = new Subscription();
|
|
3136
3190
|
this.subscription.add(
|
|
3137
3191
|
this.socket.espData$.pipe(
|
|
3138
|
-
|
|
3192
|
+
tap3((data) => {
|
|
3139
3193
|
if (data.type === 7 /* PingRequest */) {
|
|
3140
3194
|
this.socket.sendEspMessage(8 /* PingResponse */, PingResponse.encode({}).finish());
|
|
3141
3195
|
}
|
|
@@ -3215,7 +3269,7 @@ import {
|
|
|
3215
3269
|
Subject as Subject3,
|
|
3216
3270
|
take as take4,
|
|
3217
3271
|
takeUntil as takeUntil3,
|
|
3218
|
-
tap as
|
|
3272
|
+
tap as tap4
|
|
3219
3273
|
} from "rxjs";
|
|
3220
3274
|
var shallowEqualStates = (previous, current) => {
|
|
3221
3275
|
const previousKeys = Object.keys(previous);
|
|
@@ -3239,7 +3293,7 @@ var BaseComponent = class {
|
|
|
3239
3293
|
this.commandInPipeline.pipe(
|
|
3240
3294
|
debounceTime(30 * 1e3),
|
|
3241
3295
|
filter3(isTrue),
|
|
3242
|
-
|
|
3296
|
+
tap4(() => this.commandInPipeline.next(false)),
|
|
3243
3297
|
takeUntil3(this.teardown)
|
|
3244
3298
|
).subscribe();
|
|
3245
3299
|
}
|
|
@@ -3253,8 +3307,8 @@ var BaseComponent = class {
|
|
|
3253
3307
|
provideStateObservable(state$) {
|
|
3254
3308
|
this.terminatePreviousStateSubscription.next();
|
|
3255
3309
|
state$.pipe(
|
|
3256
|
-
|
|
3257
|
-
|
|
3310
|
+
tap4((state) => this.state.next(state)),
|
|
3311
|
+
tap4(() => this.commandInPipeline.next(false)),
|
|
3258
3312
|
takeUntil3(this.terminatePreviousStateSubscription),
|
|
3259
3313
|
takeUntil3(this.teardown)
|
|
3260
3314
|
).subscribe();
|
|
@@ -3278,7 +3332,7 @@ var BaseComponent = class {
|
|
|
3278
3332
|
this.commandInPipeline.pipe(
|
|
3279
3333
|
filter3((x) => !x || disableSerialise),
|
|
3280
3334
|
take4(1),
|
|
3281
|
-
|
|
3335
|
+
tap4(() => {
|
|
3282
3336
|
this.commandInterface.sendEspMessage(type, dataFn());
|
|
3283
3337
|
this.commandInPipeline.next(true);
|
|
3284
3338
|
}),
|
|
@@ -3720,65 +3774,118 @@ var stateParser = (data) => {
|
|
|
3720
3774
|
return void 0;
|
|
3721
3775
|
};
|
|
3722
3776
|
var createComponents = (data, stateEvents$, connection, knownComponents, apiVersion = { major: 1, minor: 8 }) => {
|
|
3777
|
+
const identity = (objectId, key) => {
|
|
3778
|
+
const knownById = knownComponents instanceof Set ? void 0 : knownComponents;
|
|
3779
|
+
const knownIds = knownComponents instanceof Set ? knownComponents : void 0;
|
|
3780
|
+
if (knownById) {
|
|
3781
|
+
for (const [knownId, knownKey2] of knownById) {
|
|
3782
|
+
if (knownKey2 === key) {
|
|
3783
|
+
return {
|
|
3784
|
+
id: knownId,
|
|
3785
|
+
fallback: objectId.length === 0,
|
|
3786
|
+
known: true
|
|
3787
|
+
};
|
|
3788
|
+
}
|
|
3789
|
+
}
|
|
3790
|
+
}
|
|
3791
|
+
let fallback = objectId.length === 0;
|
|
3792
|
+
let id = fallback ? fallbackEntityId(key) : objectId;
|
|
3793
|
+
const knownKey = (candidate) => knownById?.get(candidate);
|
|
3794
|
+
const isKnown = (candidate) => knownById ? knownKey(candidate) === key : knownIds?.has(candidate) ?? false;
|
|
3795
|
+
if (knownById && knownById.has(id) && !isKnown(id)) {
|
|
3796
|
+
fallback = true;
|
|
3797
|
+
id = fallbackEntityId(key);
|
|
3798
|
+
let suffix = 1;
|
|
3799
|
+
const baseId = id;
|
|
3800
|
+
while (knownById.has(id)) id = `${baseId}_${suffix++}`;
|
|
3801
|
+
}
|
|
3802
|
+
return { id, fallback, known: isKnown(id) };
|
|
3803
|
+
};
|
|
3723
3804
|
switch (data.type) {
|
|
3724
3805
|
case 12 /* ListEntitiesBinarySensorResponse */: {
|
|
3725
3806
|
const response = ListEntitiesBinarySensorResponse.decode(data.payload);
|
|
3726
3807
|
const state$ = transformStates(stateEvents$, response);
|
|
3727
|
-
|
|
3728
|
-
|
|
3808
|
+
const entity = identity(response.objectId, response.key);
|
|
3809
|
+
return entity.known ? {
|
|
3810
|
+
id: entity.id,
|
|
3811
|
+
key: response.key,
|
|
3812
|
+
fallback: entity.fallback,
|
|
3729
3813
|
state$
|
|
3730
3814
|
} : {
|
|
3731
|
-
id:
|
|
3815
|
+
id: entity.id,
|
|
3816
|
+
key: response.key,
|
|
3817
|
+
fallback: entity.fallback,
|
|
3732
3818
|
component: new BinarySensorComponent(response, state$, emptyCommandInterface)
|
|
3733
3819
|
};
|
|
3734
3820
|
}
|
|
3735
3821
|
case 13 /* ListEntitiesCoverResponse */: {
|
|
3736
3822
|
const response = ListEntitiesCoverResponse.decode(data.payload);
|
|
3737
3823
|
const state$ = transformStates(stateEvents$, response);
|
|
3738
|
-
|
|
3739
|
-
|
|
3824
|
+
const entity = identity(response.objectId, response.key);
|
|
3825
|
+
return entity.known ? {
|
|
3826
|
+
id: entity.id,
|
|
3827
|
+
key: response.key,
|
|
3828
|
+
fallback: entity.fallback,
|
|
3740
3829
|
state$
|
|
3741
3830
|
} : {
|
|
3742
|
-
id:
|
|
3831
|
+
id: entity.id,
|
|
3832
|
+
key: response.key,
|
|
3833
|
+
fallback: entity.fallback,
|
|
3743
3834
|
component: new CoverComponent(response, state$, connection, apiVersion)
|
|
3744
3835
|
};
|
|
3745
3836
|
}
|
|
3746
3837
|
case 17 /* ListEntitiesSwitchResponse */: {
|
|
3747
3838
|
const response = ListEntitiesSwitchResponse.decode(data.payload);
|
|
3748
3839
|
const state$ = transformStates(stateEvents$, response);
|
|
3749
|
-
|
|
3750
|
-
|
|
3840
|
+
const entity = identity(response.objectId, response.key);
|
|
3841
|
+
return entity.known ? {
|
|
3842
|
+
id: entity.id,
|
|
3843
|
+
key: response.key,
|
|
3844
|
+
fallback: entity.fallback,
|
|
3751
3845
|
state$
|
|
3752
3846
|
} : {
|
|
3753
|
-
id:
|
|
3847
|
+
id: entity.id,
|
|
3848
|
+
key: response.key,
|
|
3849
|
+
fallback: entity.fallback,
|
|
3754
3850
|
component: new SwitchComponent(response, state$, connection)
|
|
3755
3851
|
};
|
|
3756
3852
|
}
|
|
3757
3853
|
case 15 /* ListEntitiesLightResponse */: {
|
|
3758
3854
|
const response = ListEntitiesLightResponse.decode(data.payload);
|
|
3759
3855
|
const state$ = transformStates(stateEvents$, response);
|
|
3760
|
-
|
|
3761
|
-
|
|
3856
|
+
const entity = identity(response.objectId, response.key);
|
|
3857
|
+
return entity.known ? {
|
|
3858
|
+
id: entity.id,
|
|
3859
|
+
key: response.key,
|
|
3860
|
+
fallback: entity.fallback,
|
|
3762
3861
|
state$
|
|
3763
3862
|
} : {
|
|
3764
|
-
id:
|
|
3863
|
+
id: entity.id,
|
|
3864
|
+
key: response.key,
|
|
3865
|
+
fallback: entity.fallback,
|
|
3765
3866
|
component: new LightComponent(response, state$, connection)
|
|
3766
3867
|
};
|
|
3767
3868
|
}
|
|
3768
3869
|
case 16 /* ListEntitiesSensorResponse */: {
|
|
3769
3870
|
const response = ListEntitiesSensorResponse.decode(data.payload);
|
|
3770
3871
|
const state$ = transformStates(stateEvents$, response);
|
|
3771
|
-
|
|
3772
|
-
|
|
3872
|
+
const entity = identity(response.objectId, response.key);
|
|
3873
|
+
return entity.known ? {
|
|
3874
|
+
id: entity.id,
|
|
3875
|
+
key: response.key,
|
|
3876
|
+
fallback: entity.fallback,
|
|
3773
3877
|
state$
|
|
3774
3878
|
} : {
|
|
3775
|
-
id:
|
|
3879
|
+
id: entity.id,
|
|
3880
|
+
key: response.key,
|
|
3881
|
+
fallback: entity.fallback,
|
|
3776
3882
|
component: new SensorComponent(response, state$, emptyCommandInterface)
|
|
3777
3883
|
};
|
|
3778
3884
|
}
|
|
3779
3885
|
}
|
|
3780
3886
|
return { id: "" };
|
|
3781
3887
|
};
|
|
3888
|
+
var fallbackEntityId = (key) => `__esphome_entity_key_${key}`;
|
|
3782
3889
|
var emptyCommandInterface = {
|
|
3783
3890
|
sendEspMessage: () => void 0
|
|
3784
3891
|
};
|
|
@@ -3809,7 +3916,7 @@ import {
|
|
|
3809
3916
|
concat,
|
|
3810
3917
|
defer,
|
|
3811
3918
|
distinctUntilChanged as distinctUntilChanged2,
|
|
3812
|
-
EMPTY
|
|
3919
|
+
EMPTY,
|
|
3813
3920
|
filter as filter5,
|
|
3814
3921
|
map as map2,
|
|
3815
3922
|
merge as merge2,
|
|
@@ -3821,7 +3928,7 @@ import {
|
|
|
3821
3928
|
Subscription as Subscription2,
|
|
3822
3929
|
switchMap as switchMap2,
|
|
3823
3930
|
takeUntil as takeUntil4,
|
|
3824
|
-
tap as
|
|
3931
|
+
tap as tap5,
|
|
3825
3932
|
timer
|
|
3826
3933
|
} from "rxjs";
|
|
3827
3934
|
var PING_TIMEOUT = 90 * 1e3;
|
|
@@ -3854,84 +3961,10 @@ var EspDevice = class {
|
|
|
3854
3961
|
// component re-runs the filter+decode chain for the same state frame.
|
|
3855
3962
|
share2()
|
|
3856
3963
|
);
|
|
3857
|
-
this.
|
|
3858
|
-
|
|
3859
|
-
|
|
3860
|
-
|
|
3861
|
-
if (listResponses.has(data.type)) {
|
|
3862
|
-
this.parseListResponse(data);
|
|
3863
|
-
} else if (data.type === 10 /* DeviceInfoResponse */) {
|
|
3864
|
-
this.deviceInfo = DeviceInfoResponse.decode(data.payload);
|
|
3865
|
-
}
|
|
3866
|
-
} catch (error) {
|
|
3867
|
-
this.errors.next(error);
|
|
3868
|
-
}
|
|
3869
|
-
})
|
|
3870
|
-
).subscribe()
|
|
3871
|
-
);
|
|
3872
|
-
this.subscription.add(
|
|
3873
|
-
this.socket.connected$.pipe(
|
|
3874
|
-
distinctUntilChanged2(),
|
|
3875
|
-
tap4((connected) => {
|
|
3876
|
-
if (!connected && !this.passwordRejected) {
|
|
3877
|
-
this.socket.open();
|
|
3878
|
-
}
|
|
3879
|
-
}),
|
|
3880
|
-
filter5((connected) => connected && !this.passwordRejected),
|
|
3881
|
-
switchMap2(
|
|
3882
|
-
() => (
|
|
3883
|
-
// defer() re-runs this factory on every retry:
|
|
3884
|
-
// Client.request() sends eagerly when called (not on
|
|
3885
|
-
// subscription), so without it a retried handshake
|
|
3886
|
-
// would never put a fresh HelloRequest on the wire —
|
|
3887
|
-
// and a real device only answers HelloRequest, never
|
|
3888
|
-
// speaks first.
|
|
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
|
-
}),
|
|
3894
|
-
switchMap2(() => this.client.connect({ password })),
|
|
3895
|
-
map2((response) => {
|
|
3896
|
-
if (response.invalidPassword) {
|
|
3897
|
-
throw new InvalidPasswordError(this.host);
|
|
3898
|
-
}
|
|
3899
|
-
return response;
|
|
3900
|
-
}),
|
|
3901
|
-
switchMap2(() => this.client.deviceInfo()),
|
|
3902
|
-
switchMap2(() => this.client.listEntities()),
|
|
3903
|
-
switchMap2(() => this.client.subscribeStateChange()),
|
|
3904
|
-
retry({
|
|
3905
|
-
delay: (error) => {
|
|
3906
|
-
this.errors.next(error);
|
|
3907
|
-
if (error instanceof InvalidPasswordError) {
|
|
3908
|
-
this.passwordRejected = true;
|
|
3909
|
-
this.socket.terminate();
|
|
3910
|
-
return EMPTY2;
|
|
3911
|
-
}
|
|
3912
|
-
return timer(RETRY_DELAY);
|
|
3913
|
-
}
|
|
3914
|
-
})
|
|
3915
|
-
)
|
|
3916
|
-
)
|
|
3917
|
-
)
|
|
3918
|
-
).subscribe()
|
|
3919
|
-
);
|
|
3920
|
-
this.subscription.add(
|
|
3921
|
-
this.socket.error$.pipe(
|
|
3922
|
-
switchMap2(() => timer(RETRY_DELAY)),
|
|
3923
|
-
filter5(() => !this.passwordRejected && !this.socket.isConnected())
|
|
3924
|
-
).subscribe(() => this.socket.open())
|
|
3925
|
-
);
|
|
3926
|
-
this.alive$ = merge2(
|
|
3927
|
-
this.socket.connected$,
|
|
3928
|
-
this.socket.espData$.pipe(switchMap2(() => concat(of2(true), timer(PING_TIMEOUT).pipe(map2(() => false)))))
|
|
3929
|
-
).pipe(
|
|
3930
|
-
distinctUntilChanged2(),
|
|
3931
|
-
// refCount releases the watchdog source and its pending timer when
|
|
3932
|
-
// the last subscriber leaves, e.g. after terminate().
|
|
3933
|
-
shareReplay({ bufferSize: 1, refCount: true })
|
|
3934
|
-
);
|
|
3964
|
+
this.watchDeviceMessages();
|
|
3965
|
+
this.watchConnection();
|
|
3966
|
+
this.retryFailedConnections();
|
|
3967
|
+
this.alive$ = this.createLivenessObservable();
|
|
3935
3968
|
}
|
|
3936
3969
|
host;
|
|
3937
3970
|
password;
|
|
@@ -3960,6 +3993,80 @@ var EspDevice = class {
|
|
|
3960
3993
|
// auto-reconnect until provideRetryObservable() explicitly re-arms it.
|
|
3961
3994
|
passwordRejected = false;
|
|
3962
3995
|
alive$;
|
|
3996
|
+
watchDeviceMessages() {
|
|
3997
|
+
this.subscription.add(this.socket.espData$.subscribe((data) => this.handleDeviceMessage(data)));
|
|
3998
|
+
}
|
|
3999
|
+
handleDeviceMessage(data) {
|
|
4000
|
+
try {
|
|
4001
|
+
if (listResponses.has(data.type)) {
|
|
4002
|
+
this.parseListResponse(data);
|
|
4003
|
+
} else if (data.type === 10 /* DeviceInfoResponse */) {
|
|
4004
|
+
this.deviceInfo = DeviceInfoResponse.decode(data.payload);
|
|
4005
|
+
} else if (data.type === 4 /* ConnectResponse */) {
|
|
4006
|
+
if (ConnectResponse.decode(data.payload).invalidPassword && !this.passwordRejected) {
|
|
4007
|
+
this.passwordRejected = true;
|
|
4008
|
+
this.errors.next(new InvalidPasswordError(this.host));
|
|
4009
|
+
this.socket.terminate();
|
|
4010
|
+
}
|
|
4011
|
+
}
|
|
4012
|
+
} catch (error) {
|
|
4013
|
+
this.errors.next(error);
|
|
4014
|
+
}
|
|
4015
|
+
}
|
|
4016
|
+
watchConnection() {
|
|
4017
|
+
this.subscription.add(
|
|
4018
|
+
this.socket.connected$.pipe(
|
|
4019
|
+
distinctUntilChanged2(),
|
|
4020
|
+
tap5((connected) => {
|
|
4021
|
+
if (!connected && !this.passwordRejected) {
|
|
4022
|
+
this.socket.open();
|
|
4023
|
+
}
|
|
4024
|
+
}),
|
|
4025
|
+
filter5((connected) => connected && !this.passwordRejected),
|
|
4026
|
+
// A new connection cancels the previous handshake and its retry timer.
|
|
4027
|
+
switchMap2(() => this.handshake())
|
|
4028
|
+
).subscribe()
|
|
4029
|
+
);
|
|
4030
|
+
}
|
|
4031
|
+
handshake() {
|
|
4032
|
+
return defer(() => this.client.hello({ clientInfo: "esphome-ts" })).pipe(
|
|
4033
|
+
tap5((helloResponse) => {
|
|
4034
|
+
this.apiVersion.major = helloResponse.apiVersionMajor;
|
|
4035
|
+
this.apiVersion.minor = helloResponse.apiVersionMinor;
|
|
4036
|
+
}),
|
|
4037
|
+
switchMap2(() => {
|
|
4038
|
+
this.client.connect({ password: this.password });
|
|
4039
|
+
return this.passwordRejected ? EMPTY : this.client.deviceInfo();
|
|
4040
|
+
}),
|
|
4041
|
+
switchMap2(() => this.client.listEntities()),
|
|
4042
|
+
switchMap2(() => this.client.subscribeStateChange()),
|
|
4043
|
+
retry({
|
|
4044
|
+
delay: (error) => {
|
|
4045
|
+
this.errors.next(error);
|
|
4046
|
+
return timer(RETRY_DELAY);
|
|
4047
|
+
}
|
|
4048
|
+
})
|
|
4049
|
+
);
|
|
4050
|
+
}
|
|
4051
|
+
retryFailedConnections() {
|
|
4052
|
+
this.subscription.add(
|
|
4053
|
+
this.socket.error$.pipe(
|
|
4054
|
+
switchMap2(() => timer(RETRY_DELAY)),
|
|
4055
|
+
filter5(() => !this.passwordRejected && !this.socket.isConnected())
|
|
4056
|
+
).subscribe(() => this.socket.open())
|
|
4057
|
+
);
|
|
4058
|
+
}
|
|
4059
|
+
createLivenessObservable() {
|
|
4060
|
+
const trafficLiveness$ = this.socket.espData$.pipe(
|
|
4061
|
+
switchMap2(() => concat(of2(true), timer(PING_TIMEOUT).pipe(map2(() => false))))
|
|
4062
|
+
);
|
|
4063
|
+
return merge2(this.socket.connected$, trafficLiveness$).pipe(
|
|
4064
|
+
distinctUntilChanged2(),
|
|
4065
|
+
// refCount releases the watchdog source and its pending timer when
|
|
4066
|
+
// the last subscriber leaves, e.g. after terminate().
|
|
4067
|
+
shareReplay({ bufferSize: 1, refCount: true })
|
|
4068
|
+
);
|
|
4069
|
+
}
|
|
3963
4070
|
/**
|
|
3964
4071
|
* Provides the reconnect cadence used while the device is unreachable.
|
|
3965
4072
|
* Calling this again replaces the previously provided retry signal, so
|
|
@@ -3973,7 +4080,7 @@ var EspDevice = class {
|
|
|
3973
4080
|
filter5(isFalse),
|
|
3974
4081
|
switchMap2(
|
|
3975
4082
|
() => retryWhen$.pipe(
|
|
3976
|
-
|
|
4083
|
+
tap5(() => this.socket.open()),
|
|
3977
4084
|
takeUntil4(this.alive$.pipe(filter5(isTrue)))
|
|
3978
4085
|
)
|
|
3979
4086
|
)
|
|
@@ -3993,7 +4100,9 @@ var EspDevice = class {
|
|
|
3993
4100
|
if (data.type === 19 /* ListEntitiesDoneResponse */) {
|
|
3994
4101
|
this.discovery.next(true);
|
|
3995
4102
|
} else {
|
|
3996
|
-
const knownComponents = new
|
|
4103
|
+
const knownComponents = new Map(
|
|
4104
|
+
Object.entries(this.components).map(([id2, component2]) => [id2, component2.key])
|
|
4105
|
+
);
|
|
3997
4106
|
const { id, component, state$ } = createComponents(
|
|
3998
4107
|
data,
|
|
3999
4108
|
this.stateEvents$,
|
|
@@ -4004,7 +4113,7 @@ var EspDevice = class {
|
|
|
4004
4113
|
if (component) {
|
|
4005
4114
|
this.components[id] = this.components[id] ?? component;
|
|
4006
4115
|
} else if (state$) {
|
|
4007
|
-
this.components[id]
|
|
4116
|
+
this.components[id]?.provideStateObservable(state$);
|
|
4008
4117
|
}
|
|
4009
4118
|
}
|
|
4010
4119
|
}
|