Skip to content

Commit 9710142

Browse files
authored
Resubscribe to descriptions when labs feat changes (#28145)
1 parent 57640d1 commit 9710142

File tree

3 files changed

+119
-25
lines changed

3 files changed

+119
-25
lines changed

src/panels/config/automation/add-automation-element-dialog.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,31 @@ class DialogAddAutomationElement
246246
) {
247247
this._calculateUsedDomains();
248248
}
249+
250+
if (changedProps.has("_newTriggersAndConditions")) {
251+
this._subscribeDescriptions();
252+
}
253+
}
254+
255+
private _subscribeDescriptions() {
256+
this._unsubscribe();
257+
if (this._params?.type === "trigger") {
258+
this._triggerDescriptions = {};
259+
this._unsub = subscribeTriggers(this.hass, (triggers) => {
260+
this._triggerDescriptions = {
261+
...this._triggerDescriptions,
262+
...triggers,
263+
};
264+
});
265+
} else if (this._params?.type === "condition") {
266+
this._conditionDescriptions = {};
267+
this._unsub = subscribeConditions(this.hass, (conditions) => {
268+
this._conditionDescriptions = {
269+
...this._conditionDescriptions,
270+
...conditions,
271+
};
272+
});
273+
}
249274
}
250275

251276
public hassSubscribe() {
@@ -279,21 +304,11 @@ class DialogAddAutomationElement
279304
} else if (this._params?.type === "trigger") {
280305
this.hass.loadBackendTranslation("triggers");
281306
getTriggerIcons(this.hass);
282-
this._unsub = subscribeTriggers(this.hass, (triggers) => {
283-
this._triggerDescriptions = {
284-
...this._triggerDescriptions,
285-
...triggers,
286-
};
287-
});
307+
this._subscribeDescriptions();
288308
} else if (this._params?.type === "condition") {
289309
this.hass.loadBackendTranslation("conditions");
290310
getConditionIcons(this.hass);
291-
this._unsub = subscribeConditions(this.hass, (conditions) => {
292-
this._conditionDescriptions = {
293-
...this._conditionDescriptions,
294-
...conditions,
295-
};
296-
});
311+
this._subscribeDescriptions();
297312
}
298313

299314
window.addEventListener("resize", this._updateNarrow);

src/panels/config/automation/condition/ha-automation-condition.ts

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { mdiDragHorizontalVariant, mdiPlus } from "@mdi/js";
22
import deepClone from "deep-clone-simple";
3-
import type { HassServiceTarget } from "home-assistant-js-websocket";
3+
import type {
4+
HassServiceTarget,
5+
UnsubscribeFunc,
6+
} from "home-assistant-js-websocket";
47
import type { PropertyValues } from "lit";
58
import { html, LitElement, nothing } from "lit";
69
import { customElement, property, queryAll, state } from "lit/decorators";
@@ -25,6 +28,7 @@ import {
2528
CONDITION_BUILDING_BLOCKS,
2629
subscribeConditions,
2730
} from "../../../../data/condition";
31+
import { subscribeLabFeatures } from "../../../../data/labs";
2832
import { SubscribeMixin } from "../../../../mixins/subscribe-mixin";
2933
import type { HomeAssistant } from "../../../../types";
3034
import {
@@ -74,19 +78,52 @@ export default class HaAutomationCondition extends SubscribeMixin(LitElement) {
7478

7579
private _conditionKeys = new WeakMap<Condition, string>();
7680

81+
private _unsub?: Promise<UnsubscribeFunc>;
82+
83+
// @ts-ignore
84+
@state() private _newTriggersAndConditions = false;
85+
86+
public disconnectedCallback() {
87+
super.disconnectedCallback();
88+
this._unsubscribe();
89+
}
90+
7791
protected hassSubscribe() {
7892
return [
79-
subscribeConditions(this.hass, (conditions) =>
80-
this._addConditions(conditions)
81-
),
93+
subscribeLabFeatures(this.hass!.connection, (features) => {
94+
this._newTriggersAndConditions =
95+
features.find(
96+
(feature) =>
97+
feature.domain === "automation" &&
98+
feature.preview_feature === "new_triggers_conditions"
99+
)?.enabled ?? false;
100+
}),
82101
];
83102
}
84103

85-
private _addConditions(conditions: ConditionDescriptions) {
86-
this._conditionDescriptions = {
87-
...this._conditionDescriptions,
88-
...conditions,
89-
};
104+
private _subscribeDescriptions() {
105+
this._unsubscribe();
106+
this._conditionDescriptions = {};
107+
this._unsub = subscribeConditions(this.hass, (descriptions) => {
108+
this._conditionDescriptions = {
109+
...this._conditionDescriptions,
110+
...descriptions,
111+
};
112+
});
113+
}
114+
115+
private _unsubscribe() {
116+
if (this._unsub) {
117+
this._unsub.then((unsub) => unsub());
118+
this._unsub = undefined;
119+
}
120+
}
121+
122+
protected willUpdate(changedProperties: PropertyValues): void {
123+
super.willUpdate(changedProperties);
124+
if (changedProperties.has("_newTriggersAndConditions")) {
125+
this._subscribeDescriptions();
126+
}
90127
}
91128

92129
protected firstUpdated(changedProps: PropertyValues) {

src/panels/config/automation/trigger/ha-automation-trigger.ts

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { mdiDragHorizontalVariant, mdiPlus } from "@mdi/js";
22
import deepClone from "deep-clone-simple";
3-
import type { HassServiceTarget } from "home-assistant-js-websocket";
3+
import type {
4+
HassServiceTarget,
5+
UnsubscribeFunc,
6+
} from "home-assistant-js-websocket";
47
import type { PropertyValues } from "lit";
58
import { html, LitElement, nothing } from "lit";
69
import { customElement, property, state } from "lit/decorators";
@@ -21,6 +24,7 @@ import {
2124
type Trigger,
2225
type TriggerList,
2326
} from "../../../../data/automation";
27+
import { subscribeLabFeatures } from "../../../../data/labs";
2428
import type { TriggerDescriptions } from "../../../../data/trigger";
2529
import { isTriggerList, subscribeTriggers } from "../../../../data/trigger";
2630
import { SubscribeMixin } from "../../../../mixins/subscribe-mixin";
@@ -67,16 +71,54 @@ export default class HaAutomationTrigger extends SubscribeMixin(LitElement) {
6771

6872
private _triggerKeys = new WeakMap<Trigger, string>();
6973

74+
private _unsub?: Promise<UnsubscribeFunc>;
75+
7076
@state() private _triggerDescriptions: TriggerDescriptions = {};
7177

78+
// @ts-ignore
79+
@state() private _newTriggersAndConditions = false;
80+
81+
public disconnectedCallback() {
82+
super.disconnectedCallback();
83+
this._unsubscribe();
84+
}
85+
7286
protected hassSubscribe() {
7387
return [
74-
subscribeTriggers(this.hass, (triggers) => this._addTriggers(triggers)),
88+
subscribeLabFeatures(this.hass!.connection, (features) => {
89+
this._newTriggersAndConditions =
90+
features.find(
91+
(feature) =>
92+
feature.domain === "automation" &&
93+
feature.preview_feature === "new_triggers_conditions"
94+
)?.enabled ?? false;
95+
}),
7596
];
7697
}
7798

78-
private _addTriggers(triggers: TriggerDescriptions) {
79-
this._triggerDescriptions = { ...this._triggerDescriptions, ...triggers };
99+
private _subscribeDescriptions() {
100+
this._unsubscribe();
101+
this._triggerDescriptions = {};
102+
this._unsub = subscribeTriggers(this.hass, (descriptions) => {
103+
this._triggerDescriptions = {
104+
...this._triggerDescriptions,
105+
...descriptions,
106+
};
107+
});
108+
}
109+
110+
private _unsubscribe() {
111+
if (this._unsub) {
112+
this._unsub.then((unsub) => unsub());
113+
this._unsub = undefined;
114+
}
115+
}
116+
117+
protected willUpdate(changedProperties: PropertyValues): void {
118+
super.willUpdate(changedProperties);
119+
if (changedProperties.has("_newTriggersAndConditions")) {
120+
this._subscribeDescriptions();
121+
}
80122
}
81123

82124
protected firstUpdated(changedProps: PropertyValues) {

0 commit comments

Comments
 (0)