Hide and sort secondary device automations (#12496)

This commit is contained in:
Bram Kragten 2022-04-28 15:53:56 +02:00 committed by GitHub
parent 1617a9dfed
commit 7ca379e0a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 15 deletions

View File

@ -5,6 +5,7 @@ import { fireEvent } from "../../common/dom/fire_event";
import { import {
DeviceAutomation, DeviceAutomation,
deviceAutomationsEqual, deviceAutomationsEqual,
sortDeviceAutomations,
} from "../../data/device_automation"; } from "../../data/device_automation";
import { HomeAssistant } from "../../types"; import { HomeAssistant } from "../../types";
import "../ha-select"; import "../ha-select";
@ -127,7 +128,9 @@ export abstract class HaDeviceAutomationPicker<
private async _updateDeviceInfo() { private async _updateDeviceInfo() {
this._automations = this.deviceId this._automations = this.deviceId
? await this._fetchDeviceAutomations(this.hass, this.deviceId) ? (await this._fetchDeviceAutomations(this.hass, this.deviceId)).sort(
sortDeviceAutomations
)
: // No device, clear the list of automations : // No device, clear the list of automations
[]; [];
@ -161,8 +164,9 @@ export abstract class HaDeviceAutomationPicker<
if (this.value && deviceAutomationsEqual(automation, this.value)) { if (this.value && deviceAutomationsEqual(automation, this.value)) {
return; return;
} }
fireEvent(this, "change"); const value = { ...automation };
fireEvent(this, "value-changed", { value: automation }); delete value.metadata;
fireEvent(this, "value-changed", { value });
} }
static get styles(): CSSResultGroup { static get styles(): CSSResultGroup {

View File

@ -11,6 +11,7 @@ export interface DeviceAutomation {
type?: string; type?: string;
subtype?: string; subtype?: string;
event?: string; event?: string;
metadata?: { secondary: boolean };
} }
export interface DeviceAction extends DeviceAutomation { export interface DeviceAction extends DeviceAutomation {
@ -179,3 +180,16 @@ export const localizeDeviceAutomationTrigger = (
(trigger.subtype ? `"${trigger.subtype}" ${trigger.type}` : trigger.type!) (trigger.subtype ? `"${trigger.subtype}" ${trigger.type}` : trigger.type!)
); );
}; };
export const sortDeviceAutomations = (
automationA: DeviceAutomation,
automationB: DeviceAutomation
) => {
if (automationA.metadata?.secondary && !automationB.metadata?.secondary) {
return 1;
}
if (!automationA.metadata?.secondary && automationB.metadata?.secondary) {
return -1;
}
return 0;
};

View File

@ -1,5 +1,5 @@
import { css, CSSResultGroup, html, LitElement, TemplateResult } from "lit"; import { css, html, LitElement, TemplateResult } from "lit";
import { property } from "lit/decorators"; import { property, state } from "lit/decorators";
import { fireEvent } from "../../../../common/dom/fire_event"; import { fireEvent } from "../../../../common/dom/fire_event";
import "../../../../components/ha-card"; import "../../../../components/ha-card";
import "../../../../components/ha-chip"; import "../../../../components/ha-chip";
@ -10,6 +10,7 @@ import {
DeviceAutomation, DeviceAutomation,
} from "../../../../data/device_automation"; } from "../../../../data/device_automation";
import { showScriptEditor } from "../../../../data/script"; import { showScriptEditor } from "../../../../data/script";
import { buttonLinkStyle } from "../../../../resources/styles";
import { HomeAssistant } from "../../../../types"; import { HomeAssistant } from "../../../../types";
declare global { declare global {
@ -29,6 +30,8 @@ export abstract class HaDeviceAutomationCard<
@property() public automations: T[] = []; @property() public automations: T[] = [];
@state() public _showSecondary = false;
protected headerKey = ""; protected headerKey = "";
protected type = ""; protected type = "";
@ -60,28 +63,47 @@ export abstract class HaDeviceAutomationCard<
if (this.automations.length === 0) { if (this.automations.length === 0) {
return html``; return html``;
} }
const automations = this._showSecondary
? this.automations
: this.automations.filter(
(automation) => automation.metadata?.secondary === false
);
return html` return html`
<h3>${this.hass.localize(this.headerKey)}</h3> <h3>${this.hass.localize(this.headerKey)}</h3>
<div class="content"> <div class="content">
<ha-chip-set> <ha-chip-set>
${this.automations.map( ${automations.map(
(automation, idx) => (automation, idx) =>
html` html`
<ha-chip .index=${idx} @click=${this._handleAutomationClicked}> <ha-chip
.index=${idx}
@click=${this._handleAutomationClicked}
class=${automation.metadata?.secondary ? "secondary" : ""}
>
${this._localizeDeviceAutomation(this.hass, automation)} ${this._localizeDeviceAutomation(this.hass, automation)}
</ha-chip> </ha-chip>
` `
)} )}
</ha-chip-set> </ha-chip-set>
${!this._showSecondary && automations.length < this.automations.length
? html`<button class="link" @click=${this._toggleSecondary}>
Show ${this.automations.length - automations.length} more...
</button>`
: ""}
</div> </div>
`; `;
} }
private _toggleSecondary() {
this._showSecondary = !this._showSecondary;
}
private _handleAutomationClicked(ev: CustomEvent) { private _handleAutomationClicked(ev: CustomEvent) {
const automation = this.automations[(ev.currentTarget as any).index]; const automation = { ...this.automations[(ev.currentTarget as any).index] };
if (!automation) { if (!automation) {
return; return;
} }
delete automation.metadata;
if (this.script) { if (this.script) {
showScriptEditor({ sequence: [automation as DeviceAction] }); showScriptEditor({ sequence: [automation as DeviceAction] });
fireEvent(this, "entry-selected"); fireEvent(this, "entry-selected");
@ -93,11 +115,18 @@ export abstract class HaDeviceAutomationCard<
fireEvent(this, "entry-selected"); fireEvent(this, "entry-selected");
} }
static get styles(): CSSResultGroup { static styles = [
return css` buttonLinkStyle,
css`
h3 { h3 {
color: var(--primary-text-color); color: var(--primary-text-color);
} }
`; .secondary {
} --ha-chip-background-color: rgba(var(--rgb-primary-text-color), 0.07);
}
button.link {
color: var(--primary-color);
}
`,
];
} }

View File

@ -10,6 +10,7 @@ import {
fetchDeviceActions, fetchDeviceActions,
fetchDeviceConditions, fetchDeviceConditions,
fetchDeviceTriggers, fetchDeviceTriggers,
sortDeviceAutomations,
} from "../../../../data/device_automation"; } from "../../../../data/device_automation";
import { haStyleDialog } from "../../../../resources/styles"; import { haStyleDialog } from "../../../../resources/styles";
import { HomeAssistant } from "../../../../types"; import { HomeAssistant } from "../../../../types";
@ -63,16 +64,16 @@ export class DialogDeviceAutomation extends LitElement {
const { device, script } = this._params; const { device, script } = this._params;
fetchDeviceActions(this.hass, device.id).then((actions) => { fetchDeviceActions(this.hass, device.id).then((actions) => {
this._actions = actions; this._actions = actions.sort(sortDeviceAutomations);
}); });
if (script) { if (script) {
return; return;
} }
fetchDeviceTriggers(this.hass, device.id).then((triggers) => { fetchDeviceTriggers(this.hass, device.id).then((triggers) => {
this._triggers = triggers; this._triggers = triggers.sort(sortDeviceAutomations);
}); });
fetchDeviceConditions(this.hass, device.id).then((conditions) => { fetchDeviceConditions(this.hass, device.id).then((conditions) => {
this._conditions = conditions; this._conditions = conditions.sort(sortDeviceAutomations);
}); });
} }