From 0405adcd16e764f4b187c2a7640ca717321f3bff Mon Sep 17 00:00:00 2001 From: Spencer Williams Date: Thu, 8 Oct 2020 10:40:55 -0400 Subject: [PATCH] Edit Person button in the Person "more info" dialog (fixes #4706) (#7208) Co-authored-by: Zack Barett --- src/dialogs/more-info/ha-more-info-dialog.ts | 40 ++++++++++++++------ src/panels/config/person/ha-config-person.ts | 30 ++++++++++++++- src/translations/en.json | 2 + 3 files changed, 59 insertions(+), 13 deletions(-) diff --git a/src/dialogs/more-info/ha-more-info-dialog.ts b/src/dialogs/more-info/ha-more-info-dialog.ts index 9851cf2b5b..dc2eb77ba3 100644 --- a/src/dialogs/more-info/ha-more-info-dialog.ts +++ b/src/dialogs/more-info/ha-more-info-dialog.ts @@ -40,7 +40,14 @@ import "./ha-more-info-logbook"; import "./controls/more-info-default"; const DOMAINS_NO_INFO = ["camera", "configurator"]; +/** + * Entity domains that should be editable *if* they have an id present; + * {@see shouldShowEditIcon}. + * */ const EDITABLE_DOMAINS_WITH_ID = ["scene", "automation"]; +/** + * Entity Domains that should always be editable; {@see shouldShowEditIcon}. + * */ const EDITABLE_DOMAINS = ["script"]; export interface MoreInfoDialogParams { @@ -73,6 +80,20 @@ export class MoreInfoDialog extends LitElement { fireEvent(this, "dialog-closed", { dialog: this.localName }); } + protected shouldShowEditIcon(domain, stateObj): boolean { + if (EDITABLE_DOMAINS_WITH_ID.includes(domain) && stateObj.attributes.id) { + return true; + } + if (EDITABLE_DOMAINS.includes(domain)) { + return true; + } + if (domain === "person" && stateObj.attributes.editable !== "false") { + return true; + } + + return false; + } + protected updated(changedProperties) { if (!this.hass || !this._entityId || !changedProperties.has("_entityId")) { return; @@ -137,10 +158,7 @@ export class MoreInfoDialog extends LitElement { ` : ""} - ${this.hass.user!.is_admin && - ((EDITABLE_DOMAINS_WITH_ID.includes(domain) && - stateObj.attributes.id) || - EDITABLE_DOMAINS.includes(domain)) + ${this.shouldShowEditIcon(domain, stateObj) ? html` compare(ent1.name, ent2.name) ); + this._openDialogIfPersonSpecifiedInRoute(); + } + + private _openDialogIfPersonSpecifiedInRoute() { + if (!this.route.path.includes("/edit/")) { + return; + } + + const routeSegments = this.route.path.split("/edit/"); + const personId = routeSegments.length > 1 ? routeSegments[1] : null; + if (!personId) { + return; + } + + const personToEdit = this._storageItems!.find((p) => p.id === personId); + if (personToEdit) { + this._openDialog(personToEdit); + } else { + showAlertDialog(this, { + title: this.hass?.localize( + "ui.panel.config.person.person_not_found_title" + ), + text: this.hass?.localize("ui.panel.config.person.person_not_found"), + }); + } } private _createPerson() { diff --git a/src/translations/en.json b/src/translations/en.json index aee5a58d34..c86c81ea2e 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -1697,6 +1697,8 @@ "add_person": "Add Person", "confirm_delete": "Are you sure you want to delete this person?", "confirm_delete2": "All devices belonging to this person will become unassigned.", + "person_not_found_title": "Person Not Found", + "person_not_found": "We couldn't find the person you were trying to edit.", "detail": { "new_person": "New Person", "name": "Name",