Allow Multiple Entities for Numeric State Trigger (#18064)

This commit is contained in:
Simon Lamon 2023-10-12 09:37:19 +02:00 committed by GitHub
parent 79f3759756
commit c5056eb4d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 20 deletions

View File

@ -99,7 +99,7 @@ export interface HassTrigger extends BaseTrigger {
export interface NumericStateTrigger extends BaseTrigger {
platform: "numeric_state";
entity_id: string;
entity_id: string | string[];
attribute?: string;
above?: number;
below?: number;

View File

@ -141,8 +141,26 @@ const tryDescribeTrigger = (
// Numeric State Trigger
if (trigger.platform === "numeric_state" && trigger.entity_id) {
const stateObj = hass.states[trigger.entity_id];
const entity = stateObj ? computeStateName(stateObj) : trigger.entity_id;
const entities: string[] = [];
const states = hass.states;
const stateObj = Array.isArray(trigger.entity_id)
? hass.states[trigger.entity_id[0]]
: hass.states[trigger.entity_id];
if (Array.isArray(trigger.entity_id)) {
for (const entity of trigger.entity_id.values()) {
if (states[entity]) {
entities.push(computeStateName(states[entity]) || entity);
}
}
} else if (trigger.entity_id) {
entities.push(
states[trigger.entity_id]
? computeStateName(states[trigger.entity_id])
: trigger.entity_id
);
}
const attribute = trigger.attribute
? computeAttributeNameDisplay(
@ -157,35 +175,38 @@ const tryDescribeTrigger = (
? describeDuration(hass.locale, trigger.for)
: undefined;
if (trigger.above && trigger.below) {
if (trigger.above !== undefined && trigger.below !== undefined) {
return hass.localize(
`${triggerTranslationBaseKey}.numeric_state.description.above-below`,
{
attribute: attribute,
entity: entity,
entity: formatListWithOrs(hass.locale, entities),
numberOfEntities: entities.length,
above: trigger.above,
below: trigger.below,
duration: duration,
}
);
}
if (trigger.above) {
if (trigger.above !== undefined) {
return hass.localize(
`${triggerTranslationBaseKey}.numeric_state.description.above`,
{
attribute: attribute,
entity: entity,
entity: formatListWithOrs(hass.locale, entities),
numberOfEntities: entities.length,
above: trigger.above,
duration: duration,
}
);
}
if (trigger.below) {
if (trigger.below !== undefined) {
return hass.localize(
`${triggerTranslationBaseKey}.numeric_state.description.below`,
{
attribute: attribute,
entity: entity,
entity: formatListWithOrs(hass.locale, entities),
numberOfEntities: entities.length,
below: trigger.below,
duration: duration,
}

View File

@ -152,9 +152,7 @@ export default class HaAutomationTriggerRow extends LitElement {
class="trigger-icon"
.path=${TRIGGER_TYPES[this.trigger.platform]}
></ha-svg-icon>
${capitalizeFirstLetter(
describeTrigger(this.trigger, this.hass, this._entityReg)
)}
${describeTrigger(this.trigger, this.hass, this._entityReg)}
</h3>
<slot name="icons" slot="icons"></slot>

View File

@ -9,6 +9,7 @@ import "../../../../../components/ha-form/ha-form";
import type { SchemaUnion } from "../../../../../components/ha-form/types";
import type { NumericStateTrigger } from "../../../../../data/automation";
import type { HomeAssistant } from "../../../../../types";
import { ensureArray } from "../../../../../common/array/ensure-array";
@customElement("ha-automation-trigger-numeric_state")
export class HaNumericStateTrigger extends LitElement {
@ -25,15 +26,21 @@ export class HaNumericStateTrigger extends LitElement {
private _schema = memoizeOne(
(
localize: LocalizeFunc,
entityId: string | string[],
inputAboveIsEntity?: boolean,
inputBelowIsEntity?: boolean
) =>
[
{ name: "entity_id", required: true, selector: { entity: {} } },
{
name: "entity_id",
required: true,
selector: { entity: { multiple: true } },
},
{
name: "attribute",
selector: {
attribute: {
entity_id: entityId ? entityId[0] : undefined,
hide_attributes: [
"access_token",
"auto_update",
@ -125,9 +132,6 @@ export class HaNumericStateTrigger extends LitElement {
],
},
},
context: {
filter_entity: "entity_id",
},
},
{
name: "mode_above",
@ -235,7 +239,7 @@ export class HaNumericStateTrigger extends LitElement {
public static get defaultConfig() {
return {
entity_id: "",
entity_id: [],
};
}
@ -257,6 +261,7 @@ export class HaNumericStateTrigger extends LitElement {
const schema = this._schema(
this.hass.localize,
this.trigger.entity_id,
inputAboveIsEntity,
inputBelowIsEntity
);
@ -265,6 +270,7 @@ export class HaNumericStateTrigger extends LitElement {
mode_above: inputAboveIsEntity ? "input" : "value",
mode_below: inputBelowIsEntity ? "input" : "value",
...this.trigger,
entity_id: ensureArray(this.trigger.entity_id),
for: trgFor,
};

View File

@ -2455,9 +2455,9 @@
"type_value": "Fixed number",
"type_input": "Numeric value of another entity",
"description": {
"above": "When {attribute, select, \n undefined {} \n other {{attribute} from }\n }{entity} is above {above}{duration, select, \n undefined {} \n other { for {duration}}\n }",
"below": "When {attribute, select, \n undefined {} \n other {{attribute} from }\n }{entity} is below {below}{duration, select, \n undefined {} \n other { for {duration}}\n }",
"above-below": "When {attribute, select, \n undefined {} \n other {{attribute} from }\n }{entity} is above {above} and below {below}{duration, select, \n undefined {} \n other { for {duration}}\n }"
"above": "When {attribute, select, \n undefined {} \n other {{attribute} from }\n }{entity} {numberOfEntities, plural,\n one {is}\n other {are}\n} above {above}{duration, select, \n undefined {} \n other { for {duration}}\n }",
"below": "When {attribute, select, \n undefined {} \n other {{attribute} from }\n }{entity} {numberOfEntities, plural,\n one {is}\n other {are}\n} below {below}{duration, select, \n undefined {} \n other { for {duration}}\n }",
"above-below": "When {attribute, select, \n undefined {} \n other {{attribute} from }\n }{entity} {numberOfEntities, plural,\n one {is}\n other {are}\n} above {above} and below {below}{duration, select, \n undefined {} \n other { for {duration}}\n }"
}
},
"persistent_notification": {