Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Button to copy service response as json for templates #21226

Merged
merged 4 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions src/components/ha-yaml-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export class HaYamlEditor extends LitElement {
"ui.components.yaml-editor.copy_to_clipboard"
)}
</mwc-button>
<slot name="extra-actions"></slot>
Copy link
Member

@bramkragten bramkragten Jul 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one should be outside of the copyClipboard ternary, it is not possible to add extra actions now without adding the copy clipboard action.

Copy link
Contributor Author

@karwosts karwosts Jul 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed, is another property the best way to solve? I don't want to create the card-actions div if there aren't any actions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah looks good! 👍

</div>`
: nothing}
`;
Expand Down
20 changes: 19 additions & 1 deletion src/panels/developer-tools/service/developer-tools-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ import { hasTemplate } from "../../../common/string/has-template";
import { extractSearchParam } from "../../../common/url/search-params";
import { HaProgressButton } from "../../../components/buttons/ha-progress-button";
import { LocalizeFunc } from "../../../common/translations/localize";
import { showToast } from "../../../util/toast";
import { copyToClipboard } from "../../../common/util/copy-clipboard";

import "../../../components/entity/ha-entity-picker";
import "../../../components/ha-card";
import "../../../components/ha-alert";
import "../../../components/ha-button";
import "../../../components/ha-expansion-panel";
import "../../../components/ha-icon-button";
import "../../../components/ha-service-control";
Expand Down Expand Up @@ -189,7 +192,13 @@ class HaPanelDevService extends LitElement {
readOnly
autoUpdate
.value=${this._response}
></ha-yaml-editor>
>
<ha-button slot="extra-actions" @click=${this._copyTemplate}
>${this.hass.localize(
"ui.panel.developer-tools.tabs.services.copy_clipboard_template"
)}</ha-button
>
</ha-yaml-editor>
</div>
</ha-card>
</div>`
Expand Down Expand Up @@ -291,6 +300,15 @@ class HaPanelDevService extends LitElement {
`;
}

private async _copyTemplate(): Promise<void> {
await copyToClipboard(
`{% set service_response = ${JSON.stringify(this._response)} %}`
);
showToast(this, {
message: this.hass.localize("ui.common.copied_clipboard"),
});
}

private _filterSelectorFields = memoizeOne((fields) =>
fields.filter((field) => !field.selector)
);
Expand Down
1 change: 1 addition & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -6689,6 +6689,7 @@
"all_parameters": "All available parameters",
"accepts_target": "This service accepts a target, for example: `entity_id: light.bed_light`",
"no_template_ui_support": "The UI does not support templates, you can still use the YAML editor.",
"copy_clipboard_template": "Copy to clipboard (template)",
"errors": {
"ui": {
"no_service": "No service selected, please select a service",
Expand Down
Loading