Skip to content

Fix device actions in step-flow-form #24539

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/components/device/ha-device-automation-picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -103,6 +104,7 @@ export abstract class HaDeviceAutomationPicker<
.label=${this.label}
.value=${value}
@selected=${this._automationChanged}
@closed=${stopPropagation}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An unrelated bug that causes the config flow form to close every time you select a device action in the action selector.

.disabled=${this._automations.length === 0}
>
${value === NO_AUTOMATION_KEY
Expand Down
37 changes: 35 additions & 2 deletions src/components/ha-selector/ha-selector-action.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,13 +27,38 @@ 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 [];
}
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}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ export class HaDeviceAction extends LitElement {
}

protected firstUpdated() {
this.hass.loadBackendTranslation("device_automation");
if (!this._capabilities) {
this._getCapabilities();
}
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export class HaDeviceCondition extends LitElement {
}

protected firstUpdated() {
this.hass.loadBackendTranslation("device_automation");
if (!this._capabilities) {
this._getCapabilities();
}
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export class HaDeviceTrigger extends LitElement {
}

protected firstUpdated() {
this.hass.loadBackendTranslation("device_automation");
if (!this._capabilities) {
this._getCapabilities();
}
Expand Down
Loading