From 02b4b8e334774f436d49a367d187da5d147f5da5 Mon Sep 17 00:00:00 2001 From: karwosts <32912880+karwosts@users.noreply.github.com> Date: Mon, 26 May 2025 05:19:09 -0700 Subject: [PATCH] Fix device actions in step-flow-form (#24539) * Fix device actions in step-flow-form * Move xlation fetch to device action * also conditions & triggers * move to firstUpdated --- .../device/ha-device-automation-picker.ts | 2 + .../ha-selector/ha-selector-action.ts | 37 ++++++++++++++++++- .../types/ha-automation-action-device_id.ts | 5 ++- .../types/ha-automation-condition-device.ts | 5 ++- .../types/ha-automation-trigger-device.ts | 1 + 5 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/components/device/ha-device-automation-picker.ts b/src/components/device/ha-device-automation-picker.ts index ea3da6fba4..9a424d82fc 100644 --- a/src/components/device/ha-device-automation-picker.ts +++ b/src/components/device/ha-device-automation-picker.ts @@ -12,6 +12,7 @@ import type { EntityRegistryEntry } from "../../data/entity_registry"; import type { HomeAssistant } from "../../types"; import "../ha-list-item"; import "../ha-select"; +import { stopPropagation } from "../../common/dom/stop_propagation"; const NO_AUTOMATION_KEY = "NO_AUTOMATION"; const UNKNOWN_AUTOMATION_KEY = "UNKNOWN_AUTOMATION"; @@ -103,6 +104,7 @@ export abstract class HaDeviceAutomationPicker< .label=${this.label} .value=${value} @selected=${this._automationChanged} + @closed=${stopPropagation} .disabled=${this._automations.length === 0} > ${value === NO_AUTOMATION_KEY diff --git a/src/components/ha-selector/ha-selector-action.ts b/src/components/ha-selector/ha-selector-action.ts index a3d94b9742..d6f531544c 100644 --- a/src/components/ha-selector/ha-selector-action.ts +++ b/src/components/ha-selector/ha-selector-action.ts @@ -1,14 +1,22 @@ +import { ContextProvider, consume } from "@lit-labs/context"; import { css, html, LitElement, nothing } from "lit"; -import { customElement, property } from "lit/decorators"; +import { customElement, property, state } from "lit/decorators"; import memoizeOne from "memoize-one"; +import type { UnsubscribeFunc } from "home-assistant-js-websocket"; +import { fullEntitiesContext } from "../../data/context"; import type { Action } from "../../data/script"; import { migrateAutomationAction } from "../../data/script"; import type { ActionSelector } from "../../data/selector"; import "../../panels/config/automation/action/ha-automation-action"; import type { HomeAssistant } from "../../types"; +import { + subscribeEntityRegistry, + type EntityRegistryEntry, +} from "../../data/entity_registry"; +import { SubscribeMixin } from "../../mixins/subscribe-mixin"; @customElement("ha-selector-action") -export class HaActionSelector extends LitElement { +export class HaActionSelector extends SubscribeMixin(LitElement) { @property({ attribute: false }) public hass!: HomeAssistant; @property({ attribute: false }) public selector!: ActionSelector; @@ -19,6 +27,14 @@ export class HaActionSelector extends LitElement { @property({ type: Boolean, reflect: true }) public disabled = false; + @state() + @consume({ context: fullEntitiesContext, subscribe: true }) + _entityReg: EntityRegistryEntry[] | undefined; + + @state() private _entitiesContext; + + protected hassSubscribeRequiredHostProps = ["_entitiesContext"]; + private _actions = memoizeOne((action: Action | undefined) => { if (!action) { return []; @@ -26,6 +42,23 @@ export class HaActionSelector extends LitElement { return migrateAutomationAction(action); }); + protected firstUpdated() { + if (!this._entityReg) { + this._entitiesContext = new ContextProvider(this, { + context: fullEntitiesContext, + initialValue: [], + }); + } + } + + public hassSubscribe(): UnsubscribeFunc[] { + return [ + subscribeEntityRegistry(this.hass.connection!, (entities) => { + this._entitiesContext.setValue(entities); + }), + ]; + } + protected render() { return html` ${this.label ? html`` : nothing} diff --git a/src/panels/config/automation/action/types/ha-automation-action-device_id.ts b/src/panels/config/automation/action/types/ha-automation-action-device_id.ts index 0cc01e80c6..a1637cccfe 100644 --- a/src/panels/config/automation/action/types/ha-automation-action-device_id.ts +++ b/src/panels/config/automation/action/types/ha-automation-action-device_id.ts @@ -127,6 +127,7 @@ export class HaDeviceAction extends LitElement { } protected firstUpdated() { + this.hass.loadBackendTranslation("device_automation"); if (!this._capabilities) { this._getCapabilities(); } @@ -135,8 +136,8 @@ export class HaDeviceAction extends LitElement { } } - protected updated(changedPros) { - const prevAction = changedPros.get("action"); + protected updated(changedProps) { + const prevAction = changedProps.get("action"); if ( prevAction && !deviceAutomationsEqual(this._entityReg, prevAction, this.action) diff --git a/src/panels/config/automation/condition/types/ha-automation-condition-device.ts b/src/panels/config/automation/condition/types/ha-automation-condition-device.ts index 7b03f5164d..c6cea685f3 100644 --- a/src/panels/config/automation/condition/types/ha-automation-condition-device.ts +++ b/src/panels/config/automation/condition/types/ha-automation-condition-device.ts @@ -128,6 +128,7 @@ export class HaDeviceCondition extends LitElement { } protected firstUpdated() { + this.hass.loadBackendTranslation("device_automation"); if (!this._capabilities) { this._getCapabilities(); } @@ -136,8 +137,8 @@ export class HaDeviceCondition extends LitElement { } } - protected updated(changedPros) { - const prevCondition = changedPros.get("condition"); + protected updated(changedProps) { + const prevCondition = changedProps.get("condition"); if ( prevCondition && !deviceAutomationsEqual(this._entityReg, prevCondition, this.condition) diff --git a/src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts b/src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts index d329bd5221..238454a8a2 100644 --- a/src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts +++ b/src/panels/config/automation/trigger/types/ha-automation-trigger-device.ts @@ -132,6 +132,7 @@ export class HaDeviceTrigger extends LitElement { } protected firstUpdated() { + this.hass.loadBackendTranslation("device_automation"); if (!this._capabilities) { this._getCapabilities(); }