diff --git a/src/components/ha-snowflakes.ts b/src/components/ha-snowflakes.ts index 3cd9398875..45ffbc3e18 100644 --- a/src/components/ha-snowflakes.ts +++ b/src/components/ha-snowflakes.ts @@ -1,7 +1,7 @@ import { css, html, LitElement, nothing } from "lit"; import { customElement, property, state } from "lit/decorators"; import type { HomeAssistant } from "../types"; -import { subscribeLabFeatures } from "../data/labs"; +import { subscribeLabFeature } from "../data/labs"; import { SubscribeMixin } from "../mixins/subscribe-mixin"; interface Snowflake { @@ -27,13 +27,14 @@ export class HaSnowflakes extends SubscribeMixin(LitElement) { public hassSubscribe() { return [ - subscribeLabFeatures(this.hass!.connection, (features) => { - this._enabled = - features.find( - (f) => - f.domain === "frontend" && f.preview_feature === "winter_mode" - )?.enabled ?? false; - }), + subscribeLabFeature( + this.hass!.connection, + "frontend", + "winter_mode", + (enabled) => { + this._enabled = enabled; + } + ), ]; } diff --git a/src/data/labs.ts b/src/data/labs.ts index 64071e2b13..4eda13bc6f 100644 --- a/src/data/labs.ts +++ b/src/data/labs.ts @@ -18,6 +18,11 @@ export interface LabPreviewFeaturesResponse { features: LabPreviewFeature[]; } +/** + * Fetch all lab features + * @param hass - The Home Assistant instance + * @returns A promise to fetch the lab features + */ export const fetchLabFeatures = async ( hass: HomeAssistant ): Promise => { @@ -27,6 +32,15 @@ export const fetchLabFeatures = async ( return response.features; }; +/** + * Update a specific lab feature + * @param hass - The Home Assistant instance + * @param domain - The domain of the lab feature + * @param preview_feature - The preview feature of the lab feature + * @param enabled - Whether the lab feature is enabled + * @param create_backup - Whether to create a backup of the lab feature + * @returns A promise to update the lab feature + */ export const labsUpdatePreviewFeature = ( hass: HomeAssistant, domain: string, @@ -65,6 +79,12 @@ const subscribeLabUpdates = ( "labs_updated" ); +/** + * Subscribe to a collection of lab features + * @param conn - The connection to the Home Assistant instance + * @param onChange - The function to call when the lab features change + * @returns The unsubscribe function + */ export const subscribeLabFeatures = ( conn: Connection, onChange: (features: LabPreviewFeature[]) => void @@ -76,3 +96,27 @@ export const subscribeLabFeatures = ( conn, onChange ); + +/** + * Subscribe to a specific lab feature + * @param conn - The connection to the Home Assistant instance + * @param domain - The domain of the lab feature + * @param previewFeature - The preview feature of the lab feature + * @param onChange - The function to call when the lab feature changes + * @returns The unsubscribe function + */ +export const subscribeLabFeature = ( + conn: Connection, + domain: string, + previewFeature: string, + onChange: (enabled: boolean) => void +) => + subscribeLabFeatures(conn, (features) => { + const enabled = + features.find( + (feature) => + feature.domain === domain && + feature.preview_feature === previewFeature + )?.enabled ?? false; + onChange(enabled); + }); diff --git a/src/panels/config/automation/add-automation-element-dialog.ts b/src/panels/config/automation/add-automation-element-dialog.ts index 4b8ca3f54c..fe7aa4fd9d 100644 --- a/src/panels/config/automation/add-automation-element-dialog.ts +++ b/src/panels/config/automation/add-automation-element-dialog.ts @@ -97,7 +97,7 @@ import { fetchIntegrationManifests, } from "../../../data/integration"; import type { LabelRegistryEntry } from "../../../data/label_registry"; -import { subscribeLabFeatures } from "../../../data/labs"; +import { subscribeLabFeature } from "../../../data/labs"; import { TARGET_SEPARATOR, getConditionsForTarget, @@ -281,15 +281,12 @@ class DialogAddAutomationElement this._fetchManifests(); this._calculateUsedDomains(); - this._unsubscribeLabFeatures = subscribeLabFeatures( + this._unsubscribeLabFeatures = subscribeLabFeature( this.hass.connection, - (features) => { - this._newTriggersAndConditions = - features.find( - (feature) => - feature.domain === "automation" && - feature.preview_feature === "new_triggers_conditions" - )?.enabled ?? false; + "automation", + "new_triggers_conditions", + (enabled) => { + this._newTriggersAndConditions = enabled; this._tab = this._newTriggersAndConditions ? "targets" : "groups"; } ); diff --git a/src/panels/config/automation/condition/ha-automation-condition.ts b/src/panels/config/automation/condition/ha-automation-condition.ts index ea20b92a67..be3df93561 100644 --- a/src/panels/config/automation/condition/ha-automation-condition.ts +++ b/src/panels/config/automation/condition/ha-automation-condition.ts @@ -28,7 +28,7 @@ import { CONDITION_BUILDING_BLOCKS, subscribeConditions, } from "../../../../data/condition"; -import { subscribeLabFeatures } from "../../../../data/labs"; +import { subscribeLabFeature } from "../../../../data/labs"; import { SubscribeMixin } from "../../../../mixins/subscribe-mixin"; import type { HomeAssistant } from "../../../../types"; import { @@ -90,14 +90,14 @@ export default class HaAutomationCondition extends SubscribeMixin(LitElement) { protected hassSubscribe() { return [ - subscribeLabFeatures(this.hass!.connection, (features) => { - this._newTriggersAndConditions = - features.find( - (feature) => - feature.domain === "automation" && - feature.preview_feature === "new_triggers_conditions" - )?.enabled ?? false; - }), + subscribeLabFeature( + this.hass!.connection, + "automation", + "new_triggers_conditions", + (enabled) => { + this._newTriggersAndConditions = enabled; + } + ), ]; } diff --git a/src/panels/config/automation/trigger/ha-automation-trigger.ts b/src/panels/config/automation/trigger/ha-automation-trigger.ts index 0510cfe166..c39be3c1e0 100644 --- a/src/panels/config/automation/trigger/ha-automation-trigger.ts +++ b/src/panels/config/automation/trigger/ha-automation-trigger.ts @@ -24,7 +24,7 @@ import { type Trigger, type TriggerList, } from "../../../../data/automation"; -import { subscribeLabFeatures } from "../../../../data/labs"; +import { subscribeLabFeature } from "../../../../data/labs"; import type { TriggerDescriptions } from "../../../../data/trigger"; import { isTriggerList, subscribeTriggers } from "../../../../data/trigger"; import { SubscribeMixin } from "../../../../mixins/subscribe-mixin"; @@ -85,14 +85,14 @@ export default class HaAutomationTrigger extends SubscribeMixin(LitElement) { protected hassSubscribe() { return [ - subscribeLabFeatures(this.hass!.connection, (features) => { - this._newTriggersAndConditions = - features.find( - (feature) => - feature.domain === "automation" && - feature.preview_feature === "new_triggers_conditions" - )?.enabled ?? false; - }), + subscribeLabFeature( + this.hass!.connection, + "automation", + "new_triggers_conditions", + (enabled) => { + this._newTriggersAndConditions = enabled; + } + ), ]; }