From 3beb5d69b9ab22e8c3965a45ffe8c83600d720db Mon Sep 17 00:00:00 2001 From: karwosts Date: Fri, 7 Mar 2025 02:00:55 +0000 Subject: [PATCH 1/4] Fix device actions in step-flow-form --- .../device/ha-device-automation-picker.ts | 2 + .../ha-selector/ha-selector-action.ts | 38 ++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/components/device/ha-device-automation-picker.ts b/src/components/device/ha-device-automation-picker.ts index b79ac97e41ca..e0847f3ba77b 100644 --- a/src/components/device/ha-device-automation-picker.ts +++ b/src/components/device/ha-device-automation-picker.ts @@ -12,6 +12,7 @@ import { import type { EntityRegistryEntry } from "../../data/entity_registry"; import type { HomeAssistant } from "../../types"; 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 255340b787d0..575d9d5992c3 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,24 @@ export class HaActionSelector extends LitElement { return migrateAutomationAction(action); }); + protected firstUpdated() { + if (!this._entityReg) { + this.hass.loadBackendTranslation("device_automation"); + 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} From 16ec60c5db0d3eeb4f1a6a6267c472ab09e1accd Mon Sep 17 00:00:00 2001 From: karwosts Date: Mon, 10 Mar 2025 23:53:35 +0000 Subject: [PATCH 2/4] Move xlation fetch to device action --- src/components/ha-selector/ha-selector-action.ts | 1 - .../action/types/ha-automation-action-device_id.ts | 7 +++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/components/ha-selector/ha-selector-action.ts b/src/components/ha-selector/ha-selector-action.ts index 575d9d5992c3..90c0b9bd621e 100644 --- a/src/components/ha-selector/ha-selector-action.ts +++ b/src/components/ha-selector/ha-selector-action.ts @@ -44,7 +44,6 @@ export class HaActionSelector extends SubscribeMixin(LitElement) { protected firstUpdated() { if (!this._entityReg) { - this.hass.loadBackendTranslation("device_automation"); this._entitiesContext = new ContextProvider(this, { context: fullEntitiesContext, initialValue: [], 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 e5f4f3d9d4b5..f54fdf83bc49 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 @@ -135,8 +135,11 @@ export class HaDeviceAction extends LitElement { } } - protected updated(changedPros) { - const prevAction = changedPros.get("action"); + protected updated(changedProps) { + if (this.hass && changedProps.has("hass") && !changedProps.get("hass")) { + this.hass.loadBackendTranslation("device_automation"); + } + const prevAction = changedProps.get("action"); if ( prevAction && !deviceAutomationsEqual(this._entityReg, prevAction, this.action) From ead5b801c874ba99691686069ffaa5d037ecb3ed Mon Sep 17 00:00:00 2001 From: karwosts Date: Mon, 10 Mar 2025 23:58:33 +0000 Subject: [PATCH 3/4] also conditions & triggers --- .../condition/types/ha-automation-condition-device.ts | 7 +++++-- .../trigger/types/ha-automation-trigger-device.ts | 3 +++ 2 files changed, 8 insertions(+), 2 deletions(-) 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 70ae2128e171..a50867779537 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 @@ -136,8 +136,11 @@ export class HaDeviceCondition extends LitElement { } } - protected updated(changedPros) { - const prevCondition = changedPros.get("condition"); + protected updated(changedProps) { + if (this.hass && changedProps.has("hass") && !changedProps.get("hass")) { + this.hass.loadBackendTranslation("device_automation"); + } + 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 ad72162fd7b6..91f96018da24 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 @@ -141,6 +141,9 @@ export class HaDeviceTrigger extends LitElement { } protected updated(changedProps) { + if (this.hass && changedProps.has("hass") && !changedProps.get("hass")) { + this.hass.loadBackendTranslation("device_automation"); + } if (!changedProps.has("trigger")) { return; } From 42b5f36fe7a3eeda3d2ebfb053c47cac462ccdba Mon Sep 17 00:00:00 2001 From: karwosts Date: Thu, 13 Mar 2025 03:15:36 +0000 Subject: [PATCH 4/4] move to firstUpdated --- .../automation/action/types/ha-automation-action-device_id.ts | 4 +--- .../condition/types/ha-automation-condition-device.ts | 4 +--- .../automation/trigger/types/ha-automation-trigger-device.ts | 4 +--- 3 files changed, 3 insertions(+), 9 deletions(-) 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 f54fdf83bc49..dd5f7e649231 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(); } @@ -136,9 +137,6 @@ export class HaDeviceAction extends LitElement { } protected updated(changedProps) { - if (this.hass && changedProps.has("hass") && !changedProps.get("hass")) { - this.hass.loadBackendTranslation("device_automation"); - } const prevAction = changedProps.get("action"); if ( prevAction && 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 a50867779537..de33f9817b8d 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(); } @@ -137,9 +138,6 @@ export class HaDeviceCondition extends LitElement { } protected updated(changedProps) { - if (this.hass && changedProps.has("hass") && !changedProps.get("hass")) { - this.hass.loadBackendTranslation("device_automation"); - } const prevCondition = changedProps.get("condition"); if ( prevCondition && 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 91f96018da24..d0bc48b39946 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(); } @@ -141,9 +142,6 @@ export class HaDeviceTrigger extends LitElement { } protected updated(changedProps) { - if (this.hass && changedProps.has("hass") && !changedProps.get("hass")) { - this.hass.loadBackendTranslation("device_automation"); - } if (!changedProps.has("trigger")) { return; }