Fix device automations from device page (#17145)

This commit is contained in:
Bram Kragten 2023-07-03 15:24:19 +02:00 committed by GitHub
parent 21caac4240
commit 6a2cad1af3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 14 deletions

View File

@ -1,11 +1,9 @@
import { consume } from "@lit-labs/context";
import { css, html, LitElement, nothing } from "lit";
import { property, state } from "lit/decorators";
import { fireEvent } from "../../../../common/dom/fire_event";
import "../../../../components/ha-chip";
import "../../../../components/ha-chip-set";
import { showAutomationEditor } from "../../../../data/automation";
import { fullEntitiesContext } from "../../../../data/context";
import {
DeviceAction,
DeviceAutomation,
@ -32,11 +30,9 @@ export abstract class HaDeviceAutomationCard<
@property({ attribute: false }) public automations: T[] = [];
@state() public _showSecondary = false;
@property({ attribute: false }) entityReg?: EntityRegistryEntry[];
@state()
@consume({ context: fullEntitiesContext, subscribe: true })
_entityReg!: EntityRegistryEntry[];
@state() public _showSecondary = false;
abstract headerKey: Parameters<typeof this.hass.localize>[0];
@ -67,7 +63,7 @@ export abstract class HaDeviceAutomationCard<
}
protected render() {
if (this.automations.length === 0) {
if (this.automations.length === 0 || !this.entityReg) {
return nothing;
}
const automations = this._showSecondary
@ -89,7 +85,7 @@ export abstract class HaDeviceAutomationCard<
>
${this._localizeDeviceAutomation(
this.hass,
this._entityReg,
this.entityReg!,
automation
)}
</ha-chip>

View File

@ -109,6 +109,7 @@ export class DialogDeviceAutomation extends LitElement {
<ha-device-triggers-card
.hass=${this.hass}
.automations=${this._triggers}
.entityReg=${this._params.entityReg}
></ha-device-triggers-card>
`
: ""}
@ -117,6 +118,7 @@ export class DialogDeviceAutomation extends LitElement {
<ha-device-conditions-card
.hass=${this.hass}
.automations=${this._conditions}
.entityReg=${this._params.entityReg}
></ha-device-conditions-card>
`
: ""}
@ -126,6 +128,7 @@ export class DialogDeviceAutomation extends LitElement {
.hass=${this.hass}
.automations=${this._actions}
.script=${this._params.script}
.entityReg=${this._params.entityReg}
></ha-device-actions-card>
`
: ""}

View File

@ -1,8 +1,10 @@
import { fireEvent } from "../../../../common/dom/fire_event";
import { DeviceRegistryEntry } from "../../../../data/device_registry";
import { EntityRegistryEntry } from "../../../../data/entity_registry";
export interface DeviceAutomationDialogParams {
device: DeviceRegistryEntry;
entityReg: EntityRegistryEntry[];
script?: boolean;
}

View File

@ -20,6 +20,7 @@ import {
import { customElement, property, state } from "lit/decorators";
import { ifDefined } from "lit/directives/if-defined";
import memoizeOne from "memoize-one";
import { consume } from "@lit-labs/context";
import { isComponentLoaded } from "../../../common/config/is_component_loaded";
import { SENSOR_ENTITIES } from "../../../common/const";
import { computeDomain } from "../../../common/entity/compute_domain";
@ -84,6 +85,7 @@ import {
loadDeviceRegistryDetailDialog,
showDeviceRegistryDetailDialog,
} from "./device-registry-detail/show-dialog-device-registry-detail";
import { fullEntitiesContext } from "../../../data/context";
export interface EntityRegistryStateEntry extends EntityRegistryEntry {
stateName?: string | null;
@ -137,6 +139,10 @@ export class HaConfigDevicePage extends LitElement {
@state() private _deviceAlerts?: DeviceAlert[];
@state()
@consume({ context: fullEntitiesContext, subscribe: true })
_entityReg!: EntityRegistryEntry[];
private _logbookTime = { recent: 86400 };
private _device = memoizeOne(
@ -422,12 +428,13 @@ export class HaConfigDevicePage extends LitElement {
)
: this.hass.localize(
"ui.panel.config.devices.automation.create",
"type",
this.hass.localize(
`ui.panel.config.devices.type.${
device.entry_type || "device"
}`
)
{
type: this.hass.localize(
`ui.panel.config.devices.type.${
device.entry_type || "device"
}`
),
}
)}
.path=${mdiPlusCircle}
></ha-icon-button>
@ -1180,6 +1187,7 @@ export class HaConfigDevicePage extends LitElement {
private _showScriptDialog() {
showDeviceAutomationDialog(this, {
device: this._device(this.deviceId, this.devices)!,
entityReg: this._entityReg,
script: true,
});
}
@ -1187,6 +1195,7 @@ export class HaConfigDevicePage extends LitElement {
private _showAutomationDialog() {
showDeviceAutomationDialog(this, {
device: this._device(this.deviceId, this.devices)!,
entityReg: this._entityReg,
script: false,
});
}