Skip to content

Commit

Permalink
updates from review
Browse files Browse the repository at this point in the history
  • Loading branch information
karwosts committed Jul 3, 2024
1 parent 705fbea commit 3f37eff
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
3 changes: 1 addition & 2 deletions src/components/ha-menu-item.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { MdMenuItem } from "@material/web/menu/menu-item";
import type { CloseMenuEvent } from "@material/web/menu/menu";
import { css } from "lit";
import { customElement, property } from "lit/decorators";

@customElement("ha-menu-item")
export class HaMenuItem extends MdMenuItem {
@property() closeAction?: (ev: CloseMenuEvent) => void;
@property({ attribute: false }) clickAction?: (item?: HTMLElement) => void;

static override styles = [
...super.styles,
Expand Down
5 changes: 3 additions & 2 deletions src/components/ha-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ export class HaMenu extends MdMenu {
if (
ev.detail.reason.kind === CloseReason.KEYDOWN &&
ev.detail.reason.key === KeydownCloseKey.ESCAPE
)
) {
return;
(ev.detail.initiator as HaMenuItem).closeAction?.(ev);
}
(ev.detail.initiator as HaMenuItem).clickAction?.(ev.detail.initiator);
}

static override styles = [
Expand Down
57 changes: 33 additions & 24 deletions src/panels/config/automation/ha-automation-picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import "../../../components/ha-icon-overflow-menu";
import "../../../components/ha-menu";
import type { HaMenu } from "../../../components/ha-menu";
import "../../../components/ha-menu-item";
import type { HaMenuItem } from "../../../components/ha-menu-item";
import "../../../components/ha-sub-menu";
import "../../../components/ha-svg-icon";
import { createAreaRegistryEntry } from "../../../data/area_registry";
Expand Down Expand Up @@ -822,7 +823,7 @@ class HaAutomationPicker extends SubscribeMixin(LitElement) {
</ha-fab>
</hass-tabs-subpage-data-table>
<ha-menu id="overflow-menu" positioning="fixed">
<ha-menu-item .closeAction=${this._showInfo}>
<ha-menu-item .clickAction=${this._showInfo}>
<ha-svg-icon
.path=${mdiInformationOutline}
slot="start"
Expand All @@ -832,29 +833,29 @@ class HaAutomationPicker extends SubscribeMixin(LitElement) {
</div>
</ha-menu-item>
<ha-menu-item .closeAction=${this._showSettings}>
<ha-menu-item .clickAction=${this._showSettings}>
<ha-svg-icon .path=${mdiCog} slot="start"></ha-svg-icon>
<div slot="headline">
${this.hass.localize(
"ui.panel.config.automation.picker.show_settings"
)}
</div>
</ha-menu-item>
<ha-menu-item .closeAction=${this._editCategory}>
<ha-menu-item .clickAction=${this._editCategory}>
<ha-svg-icon .path=${mdiTag} slot="start"></ha-svg-icon>
<div slot="headline">
${this.hass.localize(
`ui.panel.config.automation.picker.${this._overflowAutomation?.category ? "edit_category" : "assign_category"}`
)}
</div>
</ha-menu-item>
<ha-menu-item .closeAction=${this._runActions}>
<ha-menu-item .clickAction=${this._runActions}>
<ha-svg-icon .path=${mdiPlay} slot="start"></ha-svg-icon>
<div slot="headline">
${this.hass.localize("ui.panel.config.automation.editor.run")}
</div>
</ha-menu-item>
<ha-menu-item .closeAction=${this._showTrace}>
<ha-menu-item .clickAction=${this._showTrace}>
<ha-svg-icon .path=${mdiTransitConnection} slot="start"></ha-svg-icon>
<div slot="headline">
${this.hass.localize(
Expand All @@ -863,13 +864,13 @@ class HaAutomationPicker extends SubscribeMixin(LitElement) {
</div>
</ha-menu-item>
<md-divider role="separator" tabindex="-1"></md-divider>
<ha-menu-item .closeAction=${this._duplicate}>
<ha-menu-item .clickAction=${this._duplicate}>
<ha-svg-icon .path=${mdiContentDuplicate} slot="start"></ha-svg-icon>
<div slot="headline">
${this.hass.localize("ui.panel.config.automation.picker.duplicate")}
</div>
</ha-menu-item>
<ha-menu-item .closeAction=${this._toggle}>
<ha-menu-item .clickAction=${this._toggle}>
<ha-svg-icon
.path=${
this._overflowAutomation?.state === "off"
Expand All @@ -888,7 +889,7 @@ class HaAutomationPicker extends SubscribeMixin(LitElement) {
}
</div>
</ha-menu-item>
<ha-menu-item .closeAction=${this._deleteConfirm} class="warning">
<ha-menu-item .clickAction=${this._deleteConfirm} class="warning">
<ha-svg-icon .path=${mdiDelete} slot="start"></ha-svg-icon>
<div slot="headline">
${this.hass.localize("ui.panel.config.automation.picker.delete")}
Expand Down Expand Up @@ -1051,28 +1052,32 @@ class HaAutomationPicker extends SubscribeMixin(LitElement) {
this._applyFilters();
}

private _showInfo = (ev) => {
const automation = ev.currentTarget.anchorElement.automation;
private _showInfo = (item: HaMenuItem) => {
const automation = ((item.parentElement as HaMenu)!.anchorElement as any)!
.automation;
fireEvent(this, "hass-more-info", { entityId: automation.entity_id });
};

private _showSettings = (ev) => {
const automation = ev.currentTarget.anchorElement.automation;
private _showSettings = (item: HaMenuItem) => {
const automation = ((item.parentElement as HaMenu)!.anchorElement as any)!
.automation;

fireEvent(this, "hass-more-info", {
entityId: automation.entity_id,
view: "settings",
});
};

private _runActions = (ev) => {
const automation = ev.currentTarget.anchorElement.automation;
private _runActions = (item: HaMenuItem) => {
const automation = ((item.parentElement as HaMenu)!.anchorElement as any)!
.automation;

triggerAutomationActions(this.hass, automation.entity_id);
};

private _editCategory = (ev) => {
const automation = ev.currentTarget.anchorElement.automation;
private _editCategory = (item: HaMenuItem) => {
const automation = ((item.parentElement as HaMenu)!.anchorElement as any)!
.automation;

const entityReg = this._entityReg.find(
(reg) => reg.entity_id === automation.entity_id
Expand All @@ -1094,8 +1099,9 @@ class HaAutomationPicker extends SubscribeMixin(LitElement) {
});
};

private _showTrace = (ev) => {
const automation = ev.currentTarget.anchorElement.automation;
private _showTrace = (item: HaMenuItem) => {
const automation = ((item.parentElement as HaMenu)!.anchorElement as any)!
.automation;

if (!automation.attributes.id) {
showAlertDialog(this, {
Expand All @@ -1110,17 +1116,19 @@ class HaAutomationPicker extends SubscribeMixin(LitElement) {
);
};

private _toggle = async (ev): Promise<void> => {
const automation = ev.currentTarget.anchorElement.automation;
private _toggle = async (item: HaMenuItem): Promise<void> => {
const automation = ((item.parentElement as HaMenu)!.anchorElement as any)!
.automation;

const service = automation.state === "off" ? "turn_on" : "turn_off";
await this.hass.callService("automation", service, {
entity_id: automation.entity_id,
});
};

private _deleteConfirm = async (ev) => {
const automation = ev.currentTarget.anchorElement.automation;
private _deleteConfirm = async (item: HaMenuItem) => {
const automation = ((item.parentElement as HaMenu)!.anchorElement as any)!
.automation;

showConfirmationDialog(this, {
title: this.hass.localize(
Expand Down Expand Up @@ -1155,8 +1163,9 @@ class HaAutomationPicker extends SubscribeMixin(LitElement) {
}
}

private _duplicate = async (ev) => {
const automation = ev.currentTarget.anchorElement.automation;
private _duplicate = async (item: HaMenuItem) => {
const automation = ((item.parentElement as HaMenu)!.anchorElement as any)!
.automation;

try {
const config = await fetchAutomationFileConfig(
Expand Down

0 comments on commit 3f37eff

Please sign in to comment.