Support entity reg id in device automations (#15387)
This commit is contained in:
parent
85acafe8a8
commit
dde27c3524
|
@ -63,7 +63,7 @@ export class DemoAutomationDescribeCondition extends LitElement {
|
|||
<div class="condition">
|
||||
<span>
|
||||
${this._condition
|
||||
? describeCondition(this._condition, this.hass)
|
||||
? describeCondition(this._condition, this.hass, [])
|
||||
: "<invalid YAML>"}
|
||||
</span>
|
||||
<ha-yaml-editor
|
||||
|
@ -76,7 +76,7 @@ export class DemoAutomationDescribeCondition extends LitElement {
|
|||
${conditions.map(
|
||||
(conf) => html`
|
||||
<div class="condition">
|
||||
<span>${describeCondition(conf as any, this.hass)}</span>
|
||||
<span>${describeCondition(conf as any, this.hass, [])}</span>
|
||||
<pre>${dump(conf)}</pre>
|
||||
</div>
|
||||
`
|
||||
|
|
|
@ -74,7 +74,7 @@ export class DemoAutomationDescribeTrigger extends LitElement {
|
|||
<div class="trigger">
|
||||
<span>
|
||||
${this._trigger
|
||||
? describeTrigger(this._trigger, this.hass)
|
||||
? describeTrigger(this._trigger, this.hass, [])
|
||||
: "<invalid YAML>"}
|
||||
</span>
|
||||
<ha-yaml-editor
|
||||
|
@ -86,7 +86,7 @@ export class DemoAutomationDescribeTrigger extends LitElement {
|
|||
${triggers.map(
|
||||
(conf) => html`
|
||||
<div class="trigger">
|
||||
<span>${describeTrigger(conf as any, this.hass)}</span>
|
||||
<span>${describeTrigger(conf as any, this.hass, [])}</span>
|
||||
<pre>${dump(conf)}</pre>
|
||||
</div>
|
||||
`
|
||||
|
|
|
@ -1,12 +1,15 @@
|
|||
import { consume } from "@lit-labs/context";
|
||||
import "@material/mwc-list/mwc-list-item";
|
||||
import { css, CSSResultGroup, html, LitElement, nothing } from "lit";
|
||||
import { property, state } from "lit/decorators";
|
||||
import { fireEvent } from "../../common/dom/fire_event";
|
||||
import { fullEntitiesContext } from "../../data/context";
|
||||
import {
|
||||
DeviceAutomation,
|
||||
deviceAutomationsEqual,
|
||||
sortDeviceAutomations,
|
||||
} from "../../data/device_automation";
|
||||
import { EntityRegistryEntry } from "../../data/entity_registry";
|
||||
import { HomeAssistant } from "../../types";
|
||||
import "../ha-select";
|
||||
|
||||
|
@ -30,6 +33,10 @@ export abstract class HaDeviceAutomationPicker<
|
|||
// paper-listbox does not like changing things around.
|
||||
@state() private _renderEmpty = false;
|
||||
|
||||
@state()
|
||||
@consume({ context: fullEntitiesContext, subscribe: true })
|
||||
_entityReg!: EntityRegistryEntry[];
|
||||
|
||||
protected get NO_AUTOMATION_TEXT() {
|
||||
return this.hass.localize(
|
||||
"ui.panel.config.devices.automation.actions.no_actions"
|
||||
|
@ -44,6 +51,7 @@ export abstract class HaDeviceAutomationPicker<
|
|||
|
||||
private _localizeDeviceAutomation: (
|
||||
hass: HomeAssistant,
|
||||
entityRegistry: EntityRegistryEntry[],
|
||||
automation: T
|
||||
) => string;
|
||||
|
||||
|
@ -75,7 +83,7 @@ export abstract class HaDeviceAutomationPicker<
|
|||
}
|
||||
|
||||
const idx = this._automations.findIndex((automation) =>
|
||||
deviceAutomationsEqual(automation, this.value!)
|
||||
deviceAutomationsEqual(this._entityReg, automation, this.value!)
|
||||
);
|
||||
|
||||
if (idx === -1) {
|
||||
|
@ -110,7 +118,11 @@ export abstract class HaDeviceAutomationPicker<
|
|||
${this._automations.map(
|
||||
(automation, idx) => html`
|
||||
<mwc-list-item .value=${`${automation.device_id}_${idx}`}>
|
||||
${this._localizeDeviceAutomation(this.hass, automation)}
|
||||
${this._localizeDeviceAutomation(
|
||||
this.hass,
|
||||
this._entityReg,
|
||||
automation
|
||||
)}
|
||||
</mwc-list-item>
|
||||
`
|
||||
)}
|
||||
|
@ -161,7 +173,10 @@ export abstract class HaDeviceAutomationPicker<
|
|||
}
|
||||
|
||||
private _setValue(automation: T) {
|
||||
if (this.value && deviceAutomationsEqual(automation, this.value)) {
|
||||
if (
|
||||
this.value &&
|
||||
deviceAutomationsEqual(this._entityReg, automation, this.value)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
const value = { ...automation };
|
||||
|
|
|
@ -15,6 +15,7 @@ import {
|
|||
computeAttributeValueDisplay,
|
||||
} from "../common/entity/compute_attribute_display";
|
||||
import { computeStateDisplay } from "../common/entity/compute_state_display";
|
||||
import { EntityRegistryEntry } from "./entity_registry";
|
||||
|
||||
const describeDuration = (forTime: number | string | ForDict) => {
|
||||
let duration: string | null;
|
||||
|
@ -48,6 +49,7 @@ const ordinalSuffix = (n: number) => {
|
|||
export const describeTrigger = (
|
||||
trigger: Trigger,
|
||||
hass: HomeAssistant,
|
||||
entityRegistry: EntityRegistryEntry[],
|
||||
ignoreAlias = false
|
||||
) => {
|
||||
if (trigger.alias && !ignoreAlias) {
|
||||
|
@ -561,7 +563,11 @@ export const describeTrigger = (
|
|||
return "Device trigger";
|
||||
}
|
||||
const config = trigger as DeviceTrigger;
|
||||
const localized = localizeDeviceAutomationTrigger(hass, config);
|
||||
const localized = localizeDeviceAutomationTrigger(
|
||||
hass,
|
||||
entityRegistry,
|
||||
config
|
||||
);
|
||||
if (localized) {
|
||||
return localized;
|
||||
}
|
||||
|
@ -579,6 +585,7 @@ export const describeTrigger = (
|
|||
export const describeCondition = (
|
||||
condition: Condition,
|
||||
hass: HomeAssistant,
|
||||
entityRegistry: EntityRegistryEntry[],
|
||||
ignoreAlias = false
|
||||
) => {
|
||||
if (condition.alias && !ignoreAlias) {
|
||||
|
@ -873,7 +880,11 @@ export const describeCondition = (
|
|||
return "Device condition";
|
||||
}
|
||||
const config = condition as DeviceCondition;
|
||||
const localized = localizeDeviceAutomationCondition(hass, config);
|
||||
const localized = localizeDeviceAutomationCondition(
|
||||
hass,
|
||||
entityRegistry,
|
||||
config
|
||||
);
|
||||
if (localized) {
|
||||
return localized;
|
||||
}
|
||||
|
|
|
@ -20,5 +20,5 @@ export const userDataContext =
|
|||
createContext<HomeAssistant["userData"]>("userData");
|
||||
export const panelsContext = createContext<HomeAssistant["panels"]>("panels");
|
||||
|
||||
export const extendedEntitiesContext =
|
||||
export const fullEntitiesContext =
|
||||
createContext<EntityRegistryEntry[]>("extendedEntities");
|
||||
|
|
|
@ -2,6 +2,12 @@ import { computeStateName } from "../common/entity/compute_state_name";
|
|||
import type { HaFormSchema } from "../components/ha-form/types";
|
||||
import { HomeAssistant } from "../types";
|
||||
import { BaseTrigger } from "./automation";
|
||||
import {
|
||||
computeEntityRegistryName,
|
||||
entityRegistryByEntityId,
|
||||
entityRegistryById,
|
||||
EntityRegistryEntry,
|
||||
} from "./entity_registry";
|
||||
|
||||
export interface DeviceAutomation {
|
||||
alias?: string;
|
||||
|
@ -89,6 +95,7 @@ const deviceAutomationIdentifiers = [
|
|||
];
|
||||
|
||||
export const deviceAutomationsEqual = (
|
||||
entityRegistry: EntityRegistryEntry[],
|
||||
a: DeviceAutomation,
|
||||
b: DeviceAutomation
|
||||
) => {
|
||||
|
@ -100,6 +107,22 @@ export const deviceAutomationsEqual = (
|
|||
if (!deviceAutomationIdentifiers.includes(property)) {
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
property === "entity_id" &&
|
||||
a[property]?.includes(".") !== b[property]?.includes(".")
|
||||
) {
|
||||
// both entity_id and entity_reg_id could be used, we should compare the entity_reg_id
|
||||
if (
|
||||
!compareEntityIdWithEntityRegId(
|
||||
entityRegistry,
|
||||
a[property],
|
||||
b[property]
|
||||
)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (!Object.is(a[property], b[property])) {
|
||||
return false;
|
||||
}
|
||||
|
@ -108,6 +131,22 @@ export const deviceAutomationsEqual = (
|
|||
if (!deviceAutomationIdentifiers.includes(property)) {
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
property === "entity_id" &&
|
||||
a[property]?.includes(".") !== b[property]?.includes(".")
|
||||
) {
|
||||
// both entity_id and entity_reg_id could be used, we should compare the entity_reg_id
|
||||
if (
|
||||
!compareEntityIdWithEntityRegId(
|
||||
entityRegistry,
|
||||
a[property],
|
||||
b[property]
|
||||
)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (!Object.is(a[property], b[property])) {
|
||||
return false;
|
||||
}
|
||||
|
@ -116,71 +155,99 @@ export const deviceAutomationsEqual = (
|
|||
return true;
|
||||
};
|
||||
|
||||
const compareEntityIdWithEntityRegId = (
|
||||
entityRegistry: EntityRegistryEntry[],
|
||||
entityIdA?: string,
|
||||
entityIdB?: string
|
||||
) => {
|
||||
if (!entityIdA || !entityIdB) {
|
||||
return false;
|
||||
}
|
||||
if (entityIdA.includes(".")) {
|
||||
entityIdA = entityRegistryByEntityId(entityRegistry)[entityIdA].id;
|
||||
}
|
||||
if (entityIdB.includes(".")) {
|
||||
entityIdB = entityRegistryByEntityId(entityRegistry)[entityIdB].id;
|
||||
}
|
||||
return entityIdA === entityIdB;
|
||||
};
|
||||
|
||||
const getEntityName = (
|
||||
hass: HomeAssistant,
|
||||
entityRegistry: EntityRegistryEntry[],
|
||||
entityId: string | undefined
|
||||
): string => {
|
||||
if (!entityId) {
|
||||
return "<unknown entity>";
|
||||
}
|
||||
if (entityId.includes(".")) {
|
||||
const state = hass.states[entityId];
|
||||
if (state) {
|
||||
return computeStateName(state);
|
||||
}
|
||||
return entityId;
|
||||
}
|
||||
const entityReg = entityRegistryById(entityRegistry)[entityId];
|
||||
if (entityReg) {
|
||||
return computeEntityRegistryName(hass, entityReg) || entityId;
|
||||
}
|
||||
return "<unknown entity>";
|
||||
};
|
||||
|
||||
export const localizeDeviceAutomationAction = (
|
||||
hass: HomeAssistant,
|
||||
entityRegistry: EntityRegistryEntry[],
|
||||
action: DeviceAction
|
||||
): string => {
|
||||
const state = action.entity_id ? hass.states[action.entity_id] : undefined;
|
||||
return (
|
||||
hass.localize(
|
||||
`component.${action.domain}.device_automation.action_type.${action.type}`,
|
||||
"entity_name",
|
||||
state ? computeStateName(state) : action.entity_id || "<unknown>",
|
||||
"subtype",
|
||||
action.subtype
|
||||
? hass.localize(
|
||||
`component.${action.domain}.device_automation.action_subtype.${action.subtype}`
|
||||
) || action.subtype
|
||||
: ""
|
||||
) || (action.subtype ? `"${action.subtype}" ${action.type}` : action.type!)
|
||||
);
|
||||
};
|
||||
): string =>
|
||||
hass.localize(
|
||||
`component.${action.domain}.device_automation.action_type.${action.type}`,
|
||||
"entity_name",
|
||||
getEntityName(hass, entityRegistry, action.entity_id),
|
||||
"subtype",
|
||||
action.subtype
|
||||
? hass.localize(
|
||||
`component.${action.domain}.device_automation.action_subtype.${action.subtype}`
|
||||
) || action.subtype
|
||||
: ""
|
||||
) || (action.subtype ? `"${action.subtype}" ${action.type}` : action.type!);
|
||||
|
||||
export const localizeDeviceAutomationCondition = (
|
||||
hass: HomeAssistant,
|
||||
entityRegistry: EntityRegistryEntry[],
|
||||
condition: DeviceCondition
|
||||
): string => {
|
||||
const state = condition.entity_id
|
||||
? hass.states[condition.entity_id]
|
||||
: undefined;
|
||||
return (
|
||||
hass.localize(
|
||||
`component.${condition.domain}.device_automation.condition_type.${condition.type}`,
|
||||
"entity_name",
|
||||
state ? computeStateName(state) : condition.entity_id || "<unknown>",
|
||||
"subtype",
|
||||
condition.subtype
|
||||
? hass.localize(
|
||||
`component.${condition.domain}.device_automation.condition_subtype.${condition.subtype}`
|
||||
) || condition.subtype
|
||||
: ""
|
||||
) ||
|
||||
(condition.subtype
|
||||
? `"${condition.subtype}" ${condition.type}`
|
||||
: condition.type!)
|
||||
);
|
||||
};
|
||||
): string =>
|
||||
hass.localize(
|
||||
`component.${condition.domain}.device_automation.condition_type.${condition.type}`,
|
||||
"entity_name",
|
||||
getEntityName(hass, entityRegistry, condition.entity_id),
|
||||
"subtype",
|
||||
condition.subtype
|
||||
? hass.localize(
|
||||
`component.${condition.domain}.device_automation.condition_subtype.${condition.subtype}`
|
||||
) || condition.subtype
|
||||
: ""
|
||||
) ||
|
||||
(condition.subtype
|
||||
? `"${condition.subtype}" ${condition.type}`
|
||||
: condition.type!);
|
||||
|
||||
export const localizeDeviceAutomationTrigger = (
|
||||
hass: HomeAssistant,
|
||||
entityRegistry: EntityRegistryEntry[],
|
||||
trigger: DeviceTrigger
|
||||
): string => {
|
||||
const state = trigger.entity_id ? hass.states[trigger.entity_id] : undefined;
|
||||
return (
|
||||
hass.localize(
|
||||
`component.${trigger.domain}.device_automation.trigger_type.${trigger.type}`,
|
||||
"entity_name",
|
||||
state ? computeStateName(state) : trigger.entity_id || "<unknown>",
|
||||
"subtype",
|
||||
trigger.subtype
|
||||
? hass.localize(
|
||||
`component.${trigger.domain}.device_automation.trigger_subtype.${trigger.subtype}`
|
||||
) || trigger.subtype
|
||||
: ""
|
||||
) ||
|
||||
(trigger.subtype ? `"${trigger.subtype}" ${trigger.type}` : trigger.type!)
|
||||
);
|
||||
};
|
||||
): string =>
|
||||
hass.localize(
|
||||
`component.${trigger.domain}.device_automation.trigger_type.${trigger.type}`,
|
||||
"entity_name",
|
||||
getEntityName(hass, entityRegistry, trigger.entity_id),
|
||||
"subtype",
|
||||
trigger.subtype
|
||||
? hass.localize(
|
||||
`component.${trigger.domain}.device_automation.trigger_subtype.${trigger.subtype}`
|
||||
) || trigger.subtype
|
||||
: ""
|
||||
) ||
|
||||
(trigger.subtype ? `"${trigger.subtype}" ${trigger.type}` : trigger.type!);
|
||||
|
||||
export const sortDeviceAutomations = (
|
||||
automationA: DeviceAutomation,
|
||||
|
|
|
@ -186,7 +186,7 @@ export const describeAction = <T extends ActionType>(
|
|||
return "Wait for a trigger";
|
||||
}
|
||||
return `Wait for ${triggers
|
||||
.map((trigger) => describeTrigger(trigger, hass))
|
||||
.map((trigger) => describeTrigger(trigger, hass, entityRegistry))
|
||||
.join(", ")}`;
|
||||
}
|
||||
|
||||
|
@ -208,7 +208,7 @@ export const describeAction = <T extends ActionType>(
|
|||
}
|
||||
|
||||
if (actionType === "check_condition") {
|
||||
return describeCondition(action as Condition, hass);
|
||||
return describeCondition(action as Condition, hass, entityRegistry);
|
||||
}
|
||||
|
||||
if (actionType === "stop") {
|
||||
|
@ -226,7 +226,7 @@ export const describeAction = <T extends ActionType>(
|
|||
: ensureArray(config.if).length > 1
|
||||
? `${ensureArray(config.if).length} conditions`
|
||||
: ensureArray(config.if).length
|
||||
? describeCondition(ensureArray(config.if)[0], hass)
|
||||
? describeCondition(ensureArray(config.if)[0], hass, entityRegistry)
|
||||
: ""
|
||||
}${config.else ? " (or else!)" : ""}`;
|
||||
}
|
||||
|
@ -252,11 +252,11 @@ export const describeAction = <T extends ActionType>(
|
|||
base += ` ${count} time${Number(count) === 1 ? "" : "s"}`;
|
||||
} else if ("while" in config.repeat) {
|
||||
base += ` while ${ensureArray(config.repeat.while)
|
||||
.map((condition) => describeCondition(condition, hass))
|
||||
.map((condition) => describeCondition(condition, hass, entityRegistry))
|
||||
.join(", ")} is true`;
|
||||
} else if ("until" in config.repeat) {
|
||||
base += ` until ${ensureArray(config.repeat.until)
|
||||
.map((condition) => describeCondition(condition, hass))
|
||||
.map((condition) => describeCondition(condition, hass, entityRegistry))
|
||||
.join(", ")} is true`;
|
||||
} else if ("for_each" in config.repeat) {
|
||||
base += ` for every item: ${ensureArray(config.repeat.for_each)
|
||||
|
@ -267,7 +267,11 @@ export const describeAction = <T extends ActionType>(
|
|||
}
|
||||
|
||||
if (actionType === "check_condition") {
|
||||
return `Test ${describeCondition(action as Condition, hass)}`;
|
||||
return `Test ${describeCondition(
|
||||
action as Condition,
|
||||
hass,
|
||||
entityRegistry
|
||||
)}`;
|
||||
}
|
||||
|
||||
if (actionType === "device_action") {
|
||||
|
@ -275,7 +279,11 @@ export const describeAction = <T extends ActionType>(
|
|||
if (!config.device_id) {
|
||||
return "Device action";
|
||||
}
|
||||
const localized = localizeDeviceAutomationAction(hass, config);
|
||||
const localized = localizeDeviceAutomationAction(
|
||||
hass,
|
||||
entityRegistry,
|
||||
config
|
||||
);
|
||||
if (localized) {
|
||||
return localized;
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import { consume } from "@lit-labs/context";
|
||||
import { css, html, LitElement } from "lit";
|
||||
import { customElement, property, state } from "lit/decorators";
|
||||
import memoizeOne from "memoize-one";
|
||||
|
@ -5,12 +6,14 @@ import { fireEvent } from "../../../../../common/dom/fire_event";
|
|||
import "../../../../../components/device/ha-device-action-picker";
|
||||
import "../../../../../components/device/ha-device-picker";
|
||||
import "../../../../../components/ha-form/ha-form";
|
||||
import { fullEntitiesContext } from "../../../../../data/context";
|
||||
import {
|
||||
DeviceAction,
|
||||
deviceAutomationsEqual,
|
||||
DeviceCapabilities,
|
||||
fetchDeviceActionCapabilities,
|
||||
} from "../../../../../data/device_automation";
|
||||
import { EntityRegistryEntry } from "../../../../../data/entity_registry";
|
||||
import { HomeAssistant } from "../../../../../types";
|
||||
|
||||
@customElement("ha-automation-action-device_id")
|
||||
|
@ -25,6 +28,10 @@ export class HaDeviceAction extends LitElement {
|
|||
|
||||
@state() private _capabilities?: DeviceCapabilities;
|
||||
|
||||
@state()
|
||||
@consume({ context: fullEntitiesContext, subscribe: true })
|
||||
_entityReg!: EntityRegistryEntry[];
|
||||
|
||||
private _origAction?: DeviceAction;
|
||||
|
||||
public static get defaultConfig() {
|
||||
|
@ -98,7 +105,10 @@ export class HaDeviceAction extends LitElement {
|
|||
|
||||
protected updated(changedPros) {
|
||||
const prevAction = changedPros.get("action");
|
||||
if (prevAction && !deviceAutomationsEqual(prevAction, this.action)) {
|
||||
if (
|
||||
prevAction &&
|
||||
!deviceAutomationsEqual(this._entityReg, prevAction, this.action)
|
||||
) {
|
||||
this._deviceId = undefined;
|
||||
this._getCapabilities();
|
||||
}
|
||||
|
@ -123,7 +133,10 @@ export class HaDeviceAction extends LitElement {
|
|||
private _deviceActionPicked(ev) {
|
||||
ev.stopPropagation();
|
||||
let action = ev.detail.value;
|
||||
if (this._origAction && deviceAutomationsEqual(this._origAction, action)) {
|
||||
if (
|
||||
this._origAction &&
|
||||
deviceAutomationsEqual(this._entityReg, this._origAction, action)
|
||||
) {
|
||||
action = this._origAction;
|
||||
}
|
||||
fireEvent(this, "value-changed", { value: action });
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import { consume } from "@lit-labs/context";
|
||||
import { ActionDetail } from "@material/mwc-list/mwc-list-foundation";
|
||||
import "@material/mwc-list/mwc-list-item";
|
||||
import {
|
||||
|
@ -28,6 +29,8 @@ import type { Clipboard } from "../../../../data/automation";
|
|||
import { describeCondition } from "../../../../data/automation_i18n";
|
||||
import { CONDITION_TYPES } from "../../../../data/condition";
|
||||
import { validateConfig } from "../../../../data/config";
|
||||
import { fullEntitiesContext } from "../../../../data/context";
|
||||
import { EntityRegistryEntry } from "../../../../data/entity_registry";
|
||||
import {
|
||||
showAlertDialog,
|
||||
showConfirmationDialog,
|
||||
|
@ -90,6 +93,10 @@ export default class HaAutomationConditionRow extends LitElement {
|
|||
|
||||
@state() private _testingResult?: boolean;
|
||||
|
||||
@state()
|
||||
@consume({ context: fullEntitiesContext, subscribe: true })
|
||||
_entityReg!: EntityRegistryEntry[];
|
||||
|
||||
protected render() {
|
||||
if (!this.condition) {
|
||||
return nothing;
|
||||
|
@ -111,7 +118,7 @@ export default class HaAutomationConditionRow extends LitElement {
|
|||
.path=${CONDITION_TYPES[this.condition.condition]}
|
||||
></ha-svg-icon>
|
||||
${capitalizeFirstLetter(
|
||||
describeCondition(this.condition, this.hass)
|
||||
describeCondition(this.condition, this.hass, this._entityReg)
|
||||
)}
|
||||
</h3>
|
||||
|
||||
|
@ -458,7 +465,7 @@ export default class HaAutomationConditionRow extends LitElement {
|
|||
),
|
||||
inputType: "string",
|
||||
placeholder: capitalizeFirstLetter(
|
||||
describeCondition(this.condition, this.hass, true)
|
||||
describeCondition(this.condition, this.hass, this._entityReg, true)
|
||||
),
|
||||
defaultValue: this.condition.alias,
|
||||
confirmText: this.hass.localize("ui.common.submit"),
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import { consume } from "@lit-labs/context";
|
||||
import { css, html, LitElement } from "lit";
|
||||
import { customElement, property, state } from "lit/decorators";
|
||||
import memoizeOne from "memoize-one";
|
||||
|
@ -5,12 +6,14 @@ import { fireEvent } from "../../../../../common/dom/fire_event";
|
|||
import "../../../../../components/device/ha-device-condition-picker";
|
||||
import "../../../../../components/device/ha-device-picker";
|
||||
import "../../../../../components/ha-form/ha-form";
|
||||
import { fullEntitiesContext } from "../../../../../data/context";
|
||||
import {
|
||||
deviceAutomationsEqual,
|
||||
DeviceCapabilities,
|
||||
DeviceCondition,
|
||||
fetchDeviceConditionCapabilities,
|
||||
} from "../../../../../data/device_automation";
|
||||
import { EntityRegistryEntry } from "../../../../../data/entity_registry";
|
||||
import type { HomeAssistant } from "../../../../../types";
|
||||
|
||||
@customElement("ha-automation-condition-device")
|
||||
|
@ -25,6 +28,10 @@ export class HaDeviceCondition extends LitElement {
|
|||
|
||||
@state() private _capabilities?: DeviceCapabilities;
|
||||
|
||||
@state()
|
||||
@consume({ context: fullEntitiesContext, subscribe: true })
|
||||
_entityReg!: EntityRegistryEntry[];
|
||||
|
||||
private _origCondition?: DeviceCondition;
|
||||
|
||||
public static get defaultConfig() {
|
||||
|
@ -100,7 +107,7 @@ export class HaDeviceCondition extends LitElement {
|
|||
const prevCondition = changedPros.get("condition");
|
||||
if (
|
||||
prevCondition &&
|
||||
!deviceAutomationsEqual(prevCondition, this.condition)
|
||||
!deviceAutomationsEqual(this._entityReg, prevCondition, this.condition)
|
||||
) {
|
||||
this._getCapabilities();
|
||||
}
|
||||
|
@ -129,7 +136,7 @@ export class HaDeviceCondition extends LitElement {
|
|||
let condition = ev.detail.value;
|
||||
if (
|
||||
this._origCondition &&
|
||||
deviceAutomationsEqual(this._origCondition, condition)
|
||||
deviceAutomationsEqual(this._entityReg, this._origCondition, condition)
|
||||
) {
|
||||
condition = this._origCondition;
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import { consume } from "@lit-labs/context";
|
||||
import { ActionDetail } from "@material/mwc-list/mwc-list-foundation";
|
||||
import "@material/mwc-list/mwc-list-item";
|
||||
import {
|
||||
|
@ -32,6 +33,8 @@ import { HaYamlEditor } from "../../../../components/ha-yaml-editor";
|
|||
import { subscribeTrigger, Trigger } from "../../../../data/automation";
|
||||
import { describeTrigger } from "../../../../data/automation_i18n";
|
||||
import { validateConfig } from "../../../../data/config";
|
||||
import { fullEntitiesContext } from "../../../../data/context";
|
||||
import { EntityRegistryEntry } from "../../../../data/entity_registry";
|
||||
import { TRIGGER_TYPES } from "../../../../data/trigger";
|
||||
import {
|
||||
showAlertDialog,
|
||||
|
@ -106,6 +109,10 @@ export default class HaAutomationTriggerRow extends LitElement {
|
|||
|
||||
@query("ha-yaml-editor") private _yamlEditor?: HaYamlEditor;
|
||||
|
||||
@state()
|
||||
@consume({ context: fullEntitiesContext, subscribe: true })
|
||||
_entityReg!: EntityRegistryEntry[];
|
||||
|
||||
private _triggerUnsub?: Promise<UnsubscribeFunc>;
|
||||
|
||||
protected render() {
|
||||
|
@ -133,7 +140,9 @@ export default class HaAutomationTriggerRow extends LitElement {
|
|||
class="trigger-icon"
|
||||
.path=${TRIGGER_TYPES[this.trigger.platform]}
|
||||
></ha-svg-icon>
|
||||
${capitalizeFirstLetter(describeTrigger(this.trigger, this.hass))}
|
||||
${capitalizeFirstLetter(
|
||||
describeTrigger(this.trigger, this.hass, this._entityReg)
|
||||
)}
|
||||
</h3>
|
||||
|
||||
<slot name="icons" slot="icons"></slot>
|
||||
|
@ -565,7 +574,7 @@ export default class HaAutomationTriggerRow extends LitElement {
|
|||
),
|
||||
inputType: "string",
|
||||
placeholder: capitalizeFirstLetter(
|
||||
describeTrigger(this.trigger, this.hass, true)
|
||||
describeTrigger(this.trigger, this.hass, this._entityReg, true)
|
||||
),
|
||||
defaultValue: this.trigger.alias,
|
||||
confirmText: this.hass.localize("ui.common.submit"),
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import { consume } from "@lit-labs/context";
|
||||
import { css, html, LitElement } from "lit";
|
||||
import { customElement, property, state } from "lit/decorators";
|
||||
import memoizeOne from "memoize-one";
|
||||
|
@ -5,12 +6,14 @@ import { fireEvent } from "../../../../../common/dom/fire_event";
|
|||
import "../../../../../components/device/ha-device-picker";
|
||||
import "../../../../../components/device/ha-device-trigger-picker";
|
||||
import "../../../../../components/ha-form/ha-form";
|
||||
import { fullEntitiesContext } from "../../../../../data/context";
|
||||
import {
|
||||
deviceAutomationsEqual,
|
||||
DeviceCapabilities,
|
||||
DeviceTrigger,
|
||||
fetchDeviceTriggerCapabilities,
|
||||
} from "../../../../../data/device_automation";
|
||||
import { EntityRegistryEntry } from "../../../../../data/entity_registry";
|
||||
import { HomeAssistant } from "../../../../../types";
|
||||
|
||||
@customElement("ha-automation-trigger-device")
|
||||
|
@ -25,6 +28,10 @@ export class HaDeviceTrigger extends LitElement {
|
|||
|
||||
@state() private _capabilities?: DeviceCapabilities;
|
||||
|
||||
@state()
|
||||
@consume({ context: fullEntitiesContext, subscribe: true })
|
||||
_entityReg!: EntityRegistryEntry[];
|
||||
|
||||
private _origTrigger?: DeviceTrigger;
|
||||
|
||||
public static get defaultConfig() {
|
||||
|
@ -56,7 +63,7 @@ export class HaDeviceTrigger extends LitElement {
|
|||
@value-changed=${this._devicePicked}
|
||||
.hass=${this.hass}
|
||||
.disabled=${this.disabled}
|
||||
label=${this.hass.localize(
|
||||
.label=${this.hass.localize(
|
||||
"ui.panel.config.automation.editor.triggers.type.device.label"
|
||||
)}
|
||||
></ha-device-picker>
|
||||
|
@ -66,7 +73,7 @@ export class HaDeviceTrigger extends LitElement {
|
|||
@value-changed=${this._deviceTriggerPicked}
|
||||
.hass=${this.hass}
|
||||
.disabled=${this.disabled}
|
||||
label=${this.hass.localize(
|
||||
.label=${this.hass.localize(
|
||||
"ui.panel.config.automation.editor.triggers.type.device.trigger"
|
||||
)}
|
||||
></ha-device-trigger-picker>
|
||||
|
@ -96,12 +103,15 @@ export class HaDeviceTrigger extends LitElement {
|
|||
}
|
||||
}
|
||||
|
||||
protected updated(changedPros) {
|
||||
if (!changedPros.has("trigger")) {
|
||||
protected updated(changedProps) {
|
||||
if (!changedProps.has("trigger")) {
|
||||
return;
|
||||
}
|
||||
const prevTrigger = changedPros.get("trigger");
|
||||
if (prevTrigger && !deviceAutomationsEqual(prevTrigger, this.trigger)) {
|
||||
const prevTrigger = changedProps.get("trigger");
|
||||
if (
|
||||
prevTrigger &&
|
||||
!deviceAutomationsEqual(this._entityReg, prevTrigger, this.trigger)
|
||||
) {
|
||||
this._getCapabilities();
|
||||
}
|
||||
}
|
||||
|
@ -129,7 +139,7 @@ export class HaDeviceTrigger extends LitElement {
|
|||
let trigger = ev.detail.value;
|
||||
if (
|
||||
this._origTrigger &&
|
||||
deviceAutomationsEqual(this._origTrigger, trigger)
|
||||
deviceAutomationsEqual(this._entityReg, this._origTrigger, trigger)
|
||||
) {
|
||||
trigger = this._origTrigger;
|
||||
}
|
||||
|
|
|
@ -1,13 +1,16 @@
|
|||
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,
|
||||
} from "../../../../data/device_automation";
|
||||
import { EntityRegistryEntry } from "../../../../data/entity_registry";
|
||||
import { showScriptEditor } from "../../../../data/script";
|
||||
import { buttonLinkStyle } from "../../../../resources/styles";
|
||||
import { HomeAssistant } from "../../../../types";
|
||||
|
@ -31,12 +34,17 @@ export abstract class HaDeviceAutomationCard<
|
|||
|
||||
@state() public _showSecondary = false;
|
||||
|
||||
@state()
|
||||
@consume({ context: fullEntitiesContext, subscribe: true })
|
||||
_entityReg!: EntityRegistryEntry[];
|
||||
|
||||
abstract headerKey: Parameters<typeof this.hass.localize>[0];
|
||||
|
||||
abstract type: "action" | "condition" | "trigger";
|
||||
|
||||
private _localizeDeviceAutomation: (
|
||||
hass: HomeAssistant,
|
||||
entityRegistry: EntityRegistryEntry[],
|
||||
automation: T
|
||||
) => string;
|
||||
|
||||
|
@ -79,7 +87,11 @@ export abstract class HaDeviceAutomationCard<
|
|||
@click=${this._handleAutomationClicked}
|
||||
class=${automation.metadata?.secondary ? "secondary" : ""}
|
||||
>
|
||||
${this._localizeDeviceAutomation(this.hass, automation)}
|
||||
${this._localizeDeviceAutomation(
|
||||
this.hass,
|
||||
this._entityReg,
|
||||
automation
|
||||
)}
|
||||
</ha-chip>
|
||||
`
|
||||
)}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import { ContextProvider } from "@lit-labs/context";
|
||||
import {
|
||||
mdiAccount,
|
||||
mdiBackupRestore,
|
||||
|
@ -28,13 +29,21 @@ import {
|
|||
mdiViewDashboard,
|
||||
} from "@mdi/js";
|
||||
import { PolymerElement } from "@polymer/polymer";
|
||||
import { UnsubscribeFunc } from "home-assistant-js-websocket";
|
||||
import { PropertyValues } from "lit";
|
||||
import { customElement, property, state } from "lit/decorators";
|
||||
import { isComponentLoaded } from "../../common/config/is_component_loaded";
|
||||
import { listenMediaQuery } from "../../common/dom/media_query";
|
||||
import { CloudStatus, fetchCloudStatus } from "../../data/cloud";
|
||||
import { fullEntitiesContext } from "../../data/context";
|
||||
import {
|
||||
entityRegistryByEntityId,
|
||||
entityRegistryById,
|
||||
subscribeEntityRegistry,
|
||||
} from "../../data/entity_registry";
|
||||
import { HassRouterPage, RouterOptions } from "../../layouts/hass-router-page";
|
||||
import { PageNavigation } from "../../layouts/hass-tabs-subpage";
|
||||
import { SubscribeMixin } from "../../mixins/subscribe-mixin";
|
||||
import { HomeAssistant, Route } from "../../types";
|
||||
|
||||
declare global {
|
||||
|
@ -349,13 +358,26 @@ export const configSections: { [name: string]: PageNavigation[] } = {
|
|||
};
|
||||
|
||||
@customElement("ha-panel-config")
|
||||
class HaPanelConfig extends HassRouterPage {
|
||||
class HaPanelConfig extends SubscribeMixin(HassRouterPage) {
|
||||
@property({ attribute: false }) public hass!: HomeAssistant;
|
||||
|
||||
@property() public narrow!: boolean;
|
||||
|
||||
@property() public route!: Route;
|
||||
|
||||
private _entitiesContext = new ContextProvider(this, {
|
||||
context: fullEntitiesContext,
|
||||
initialValue: [],
|
||||
});
|
||||
|
||||
public hassSubscribe(): UnsubscribeFunc[] {
|
||||
return [
|
||||
subscribeEntityRegistry(this.hass.connection!, (entities) => {
|
||||
this._entitiesContext.setValue(entities);
|
||||
}),
|
||||
];
|
||||
}
|
||||
|
||||
protected routerOptions: RouterOptions = {
|
||||
defaultPage: "dashboard",
|
||||
routes: {
|
||||
|
@ -545,6 +567,8 @@ class HaPanelConfig extends HassRouterPage {
|
|||
while (this._listeners.length) {
|
||||
this._listeners.pop()!();
|
||||
}
|
||||
entityRegistryByEntityId.clear();
|
||||
entityRegistryById.clear();
|
||||
}
|
||||
|
||||
protected firstUpdated(changedProps: PropertyValues) {
|
||||
|
|
|
@ -1,19 +1,17 @@
|
|||
import { HassEntities, UnsubscribeFunc } from "home-assistant-js-websocket";
|
||||
import { consume } from "@lit-labs/context";
|
||||
import { HassEntities } from "home-assistant-js-websocket";
|
||||
import { PropertyValues } from "lit";
|
||||
import { customElement, property, state } from "lit/decorators";
|
||||
import memoizeOne from "memoize-one";
|
||||
import { computeStateDomain } from "../../../common/entity/compute_state_domain";
|
||||
import { debounce } from "../../../common/util/debounce";
|
||||
import {
|
||||
EntityRegistryEntry,
|
||||
subscribeEntityRegistry,
|
||||
} from "../../../data/entity_registry";
|
||||
import { fullEntitiesContext } from "../../../data/context";
|
||||
import { EntityRegistryEntry } from "../../../data/entity_registry";
|
||||
import { ScriptEntity } from "../../../data/script";
|
||||
import {
|
||||
HassRouterPage,
|
||||
RouterOptions,
|
||||
} from "../../../layouts/hass-router-page";
|
||||
import { SubscribeMixin } from "../../../mixins/subscribe-mixin";
|
||||
import { HomeAssistant } from "../../../types";
|
||||
import "./ha-script-editor";
|
||||
import "./ha-script-picker";
|
||||
|
@ -26,7 +24,7 @@ const equal = (a: ScriptEntity[], b: ScriptEntity[]): boolean => {
|
|||
};
|
||||
|
||||
@customElement("ha-config-script")
|
||||
class HaConfigScript extends SubscribeMixin(HassRouterPage) {
|
||||
class HaConfigScript extends HassRouterPage {
|
||||
@property({ attribute: false }) public hass!: HomeAssistant;
|
||||
|
||||
@property() public narrow!: boolean;
|
||||
|
@ -37,15 +35,9 @@ class HaConfigScript extends SubscribeMixin(HassRouterPage) {
|
|||
|
||||
@property() public scripts: ScriptEntity[] = [];
|
||||
|
||||
@state() private _entityReg: EntityRegistryEntry[] = [];
|
||||
|
||||
public hassSubscribe(): UnsubscribeFunc[] {
|
||||
return [
|
||||
subscribeEntityRegistry(this.hass.connection!, (entities) => {
|
||||
this._entityReg = entities;
|
||||
}),
|
||||
];
|
||||
}
|
||||
@state()
|
||||
@consume({ context: fullEntitiesContext, subscribe: true })
|
||||
_entityReg!: EntityRegistryEntry[];
|
||||
|
||||
protected routerOptions: RouterOptions = {
|
||||
defaultPage: "dashboard",
|
||||
|
|
Loading…
Reference in New Issue