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
This commit is contained in:
karwosts 2025-05-26 05:19:09 -07:00 committed by GitHub
parent da8d43f5d1
commit 02b4b8e334
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 44 additions and 6 deletions

View File

@ -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

View File

@ -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`<label>${this.label}</label>` : nothing}

View File

@ -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)

View File

@ -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)

View File

@ -132,6 +132,7 @@ export class HaDeviceTrigger extends LitElement {
}
protected firstUpdated() {
this.hass.loadBackendTranslation("device_automation");
if (!this._capabilities) {
this._getCapabilities();
}