Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/common/util/copy-clipboard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
export const copyToClipboard = (str) => {
export const copyToClipboard = async (str) => {
if (navigator.clipboard) {
try {
await navigator.clipboard.writeText(str);
return;
} catch {
// just continue with the fallback coding below
}
}

const el = document.createElement("textarea");
el.value = str;
document.body.appendChild(el);
Expand Down
21 changes: 20 additions & 1 deletion src/panels/developer-tools/state/developer-tools-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ import { EventsMixin } from "../../../mixins/events-mixin";
import LocalizeMixin from "../../../mixins/localize-mixin";
import "../../../styles/polymer-ha-style";
import { formatDateTimeWithSeconds } from "../../../common/datetime/format_date_time";
import { mdiInformationOutline } from "@mdi/js";
import {
mdiInformationOutline,
mdiClipboardTextMultipleOutline,
} from "@mdi/js";
import { computeRTL } from "../../../common/util/compute_rtl";
import { copyToClipboard } from "../../../common/util/copy-clipboard";

const ERROR_SENTINEL = {};
/*
Expand Down Expand Up @@ -205,6 +209,12 @@ class HaPanelDevState extends EventsMixin(LocalizeMixin(PolymerElement)) {
title="[[localize('ui.panel.developer-tools.tabs.states.more_info')]]"
path="[[informationOutlineIcon()]]"
></ha-svg-icon>
<ha-svg-icon
on-click="copyEntity"
alt="[[localize('ui.panel.developer-tools.tabs.states.copy_id')]]"
title="[[localize('ui.panel.developer-tools.tabs.states.copy_id')]]"
path="[[clipboardOutlineIcon()]]"
></ha-svg-icon>
<a href="#" on-click="entitySelected">[[entity.entity_id]]</a>
</td>
<td>
Expand Down Expand Up @@ -296,6 +306,11 @@ class HaPanelDevState extends EventsMixin(LocalizeMixin(PolymerElement)) {
};
}

copyEntity(ev) {
ev.preventDefault();
copyToClipboard(ev.model.entity.entity_id);
}

entitySelected(ev) {
const state = ev.model.entity;
this._entityId = state.entity_id;
Expand Down Expand Up @@ -345,6 +360,10 @@ class HaPanelDevState extends EventsMixin(LocalizeMixin(PolymerElement)) {
return mdiInformationOutline;
}

clipboardOutlineIcon() {
return mdiClipboardTextMultipleOutline;
}

computeEntities(hass, _entityFilter, _stateFilter, _attributeFilter) {
return Object.keys(hass.states)
.map(function (key) {
Expand Down
3 changes: 2 additions & 1 deletion src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -3063,7 +3063,8 @@
"more_info": "More Info",
"alert_entity_field": "Entity is a mandatory field",
"last_updated": "[%key:ui::dialogs::more_info_control::last_updated%]",
"last_changed": "[%key:ui::dialogs::more_info_control::last_changed%]"
"last_changed": "[%key:ui::dialogs::more_info_control::last_changed%]",
"copy_id": "Copy ID to clipboard"
},
"templates": {
"title": "Template",
Expand Down