1
0
mirror of https://github.com/home-assistant/frontend.git synced 2025-12-20 02:38:53 +00:00

Add subscribeLabFeature function (#28309)

* Add subscribe to lab feature function

* Add docstrings to exported functions
This commit is contained in:
Aidan Timson
2025-12-03 13:16:09 +00:00
committed by Bram Kragten
parent de5778079e
commit 60724eb952
5 changed files with 77 additions and 35 deletions

View File

@@ -1,7 +1,7 @@
import { css, html, LitElement, nothing } from "lit"; import { css, html, LitElement, nothing } from "lit";
import { customElement, property, state } from "lit/decorators"; import { customElement, property, state } from "lit/decorators";
import type { HomeAssistant } from "../types"; import type { HomeAssistant } from "../types";
import { subscribeLabFeatures } from "../data/labs"; import { subscribeLabFeature } from "../data/labs";
import { SubscribeMixin } from "../mixins/subscribe-mixin"; import { SubscribeMixin } from "../mixins/subscribe-mixin";
interface Snowflake { interface Snowflake {
@@ -27,13 +27,14 @@ export class HaSnowflakes extends SubscribeMixin(LitElement) {
public hassSubscribe() { public hassSubscribe() {
return [ return [
subscribeLabFeatures(this.hass!.connection, (features) => { subscribeLabFeature(
this._enabled = this.hass!.connection,
features.find( "frontend",
(f) => "winter_mode",
f.domain === "frontend" && f.preview_feature === "winter_mode" (enabled) => {
)?.enabled ?? false; this._enabled = enabled;
}), }
),
]; ];
} }

View File

@@ -18,6 +18,11 @@ export interface LabPreviewFeaturesResponse {
features: LabPreviewFeature[]; features: LabPreviewFeature[];
} }
/**
* Fetch all lab features
* @param hass - The Home Assistant instance
* @returns A promise to fetch the lab features
*/
export const fetchLabFeatures = async ( export const fetchLabFeatures = async (
hass: HomeAssistant hass: HomeAssistant
): Promise<LabPreviewFeature[]> => { ): Promise<LabPreviewFeature[]> => {
@@ -27,6 +32,15 @@ export const fetchLabFeatures = async (
return response.features; 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 = ( export const labsUpdatePreviewFeature = (
hass: HomeAssistant, hass: HomeAssistant,
domain: string, domain: string,
@@ -65,6 +79,12 @@ const subscribeLabUpdates = (
"labs_updated" "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 = ( export const subscribeLabFeatures = (
conn: Connection, conn: Connection,
onChange: (features: LabPreviewFeature[]) => void onChange: (features: LabPreviewFeature[]) => void
@@ -76,3 +96,27 @@ export const subscribeLabFeatures = (
conn, conn,
onChange 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);
});

View File

@@ -97,7 +97,7 @@ import {
fetchIntegrationManifests, fetchIntegrationManifests,
} from "../../../data/integration"; } from "../../../data/integration";
import type { LabelRegistryEntry } from "../../../data/label_registry"; import type { LabelRegistryEntry } from "../../../data/label_registry";
import { subscribeLabFeatures } from "../../../data/labs"; import { subscribeLabFeature } from "../../../data/labs";
import { import {
TARGET_SEPARATOR, TARGET_SEPARATOR,
getConditionsForTarget, getConditionsForTarget,
@@ -281,15 +281,12 @@ class DialogAddAutomationElement
this._fetchManifests(); this._fetchManifests();
this._calculateUsedDomains(); this._calculateUsedDomains();
this._unsubscribeLabFeatures = subscribeLabFeatures( this._unsubscribeLabFeatures = subscribeLabFeature(
this.hass.connection, this.hass.connection,
(features) => { "automation",
this._newTriggersAndConditions = "new_triggers_conditions",
features.find( (enabled) => {
(feature) => this._newTriggersAndConditions = enabled;
feature.domain === "automation" &&
feature.preview_feature === "new_triggers_conditions"
)?.enabled ?? false;
this._tab = this._newTriggersAndConditions ? "targets" : "groups"; this._tab = this._newTriggersAndConditions ? "targets" : "groups";
} }
); );

View File

@@ -28,7 +28,7 @@ import {
CONDITION_BUILDING_BLOCKS, CONDITION_BUILDING_BLOCKS,
subscribeConditions, subscribeConditions,
} from "../../../../data/condition"; } from "../../../../data/condition";
import { subscribeLabFeatures } from "../../../../data/labs"; import { subscribeLabFeature } from "../../../../data/labs";
import { SubscribeMixin } from "../../../../mixins/subscribe-mixin"; import { SubscribeMixin } from "../../../../mixins/subscribe-mixin";
import type { HomeAssistant } from "../../../../types"; import type { HomeAssistant } from "../../../../types";
import { import {
@@ -90,14 +90,14 @@ export default class HaAutomationCondition extends SubscribeMixin(LitElement) {
protected hassSubscribe() { protected hassSubscribe() {
return [ return [
subscribeLabFeatures(this.hass!.connection, (features) => { subscribeLabFeature(
this._newTriggersAndConditions = this.hass!.connection,
features.find( "automation",
(feature) => "new_triggers_conditions",
feature.domain === "automation" && (enabled) => {
feature.preview_feature === "new_triggers_conditions" this._newTriggersAndConditions = enabled;
)?.enabled ?? false; }
}), ),
]; ];
} }

View File

@@ -24,7 +24,7 @@ import {
type Trigger, type Trigger,
type TriggerList, type TriggerList,
} from "../../../../data/automation"; } from "../../../../data/automation";
import { subscribeLabFeatures } from "../../../../data/labs"; import { subscribeLabFeature } from "../../../../data/labs";
import type { TriggerDescriptions } from "../../../../data/trigger"; import type { TriggerDescriptions } from "../../../../data/trigger";
import { isTriggerList, subscribeTriggers } from "../../../../data/trigger"; import { isTriggerList, subscribeTriggers } from "../../../../data/trigger";
import { SubscribeMixin } from "../../../../mixins/subscribe-mixin"; import { SubscribeMixin } from "../../../../mixins/subscribe-mixin";
@@ -85,14 +85,14 @@ export default class HaAutomationTrigger extends SubscribeMixin(LitElement) {
protected hassSubscribe() { protected hassSubscribe() {
return [ return [
subscribeLabFeatures(this.hass!.connection, (features) => { subscribeLabFeature(
this._newTriggersAndConditions = this.hass!.connection,
features.find( "automation",
(feature) => "new_triggers_conditions",
feature.domain === "automation" && (enabled) => {
feature.preview_feature === "new_triggers_conditions" this._newTriggersAndConditions = enabled;
)?.enabled ?? false; }
}), ),
]; ];
} }