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

Call a service: Split remaining service_data's into data and target #21440

Merged
merged 2 commits into from
Jul 31, 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
18 changes: 14 additions & 4 deletions src/components/buttons/ha-call-service-button.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LitElement, TemplateResult, html } from "lit";
import { customElement, property } from "lit/decorators";
import { HassServiceTarget } from "home-assistant-js-websocket";
import { showConfirmationDialog } from "../../dialogs/generic/show-dialog-box";
import "./ha-progress-button";
import { HomeAssistant } from "../../types";
Expand All @@ -17,7 +18,9 @@ class HaCallServiceButton extends LitElement {

@property() public service!: string;

@property({ type: Object }) public serviceData = {};
@property({ type: Object }) public target!: HassServiceTarget;

@property({ type: Object }) public data = {};

@property() public confirmation?;

Expand All @@ -39,15 +42,21 @@ class HaCallServiceButton extends LitElement {
const eventData = {
domain: this.domain,
service: this.service,
serviceData: this.serviceData,
data: this.data,
target: this.target,
success: false,
};

const progressElement =
this.shadowRoot!.querySelector("ha-progress-button")!;

try {
await this.hass.callService(this.domain, this.service, this.serviceData);
await this.hass.callService(
this.domain,
this.service,
this.data,
this.target
);
this.progress = false;
progressElement.actionSuccess();
eventData.success = true;
Expand Down Expand Up @@ -85,7 +94,8 @@ declare global {
"hass-service-called": {
domain: string;
service: string;
serviceData: object;
target: HassServiceTarget;
data: object;
success: boolean;
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class ZHAClusterAttributes extends LitElement {
.hass=${this.hass}
domain="zha"
service="set_zigbee_cluster_attribute"
.serviceData=${this._setAttributeServiceData}
.data=${this._setAttributeServiceData}
>
${this.hass!.localize(
"ui.panel.config.zha.cluster_attributes.write_zigbee_attribute"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class ZHAClusterCommands extends LitElement {
.hass=${this.hass}
domain="zha"
service="issue_zigbee_cluster_command"
.serviceData=${this._issueClusterCommandServiceData}
.data=${this._issueClusterCommandServiceData}
.disabled=${!this._canIssueCommand}
>
${this.hass!.localize(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { fireEvent } from "../../../../../common/dom/fire_event";
import { computeStateName } from "../../../../../common/entity/compute_state_name";
import { stringCompare } from "../../../../../common/string/compare";
import { slugify } from "../../../../../common/string/slugify";
import "../../../../../components/buttons/ha-call-service-button";
import "../../../../../components/entity/state-badge";
import "../../../../../components/ha-area-picker";
import "../../../../../components/ha-card";
Expand Down
7 changes: 6 additions & 1 deletion src/panels/lovelace/elements/hui-service-button-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,17 @@ export class HuiServiceButtonElement
return nothing;
}

const updatedTarget = this._config.target ?? {
entity_id: this._config.service_data?.entity_id,
silamon marked this conversation as resolved.
Show resolved Hide resolved
};

return html`
<ha-call-service-button
.hass=${this.hass}
.domain=${this._domain}
.service=${this._service}
.serviceData=${this._config.service_data}
.data=${this._config.data ?? this._config.service_data}
.target=${updatedTarget}
>${this._config.title}</ha-call-service-button
>
`;
Expand Down
4 changes: 4 additions & 0 deletions src/panels/lovelace/elements/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { HassServiceTarget } from "home-assistant-js-websocket";
import { ActionConfig } from "../../../data/lovelace/config/action";
import { HomeAssistant } from "../../../types";
import { Condition } from "../common/validate-condition";
Expand Down Expand Up @@ -56,7 +57,10 @@ export interface ImageElementConfig extends LovelaceElementConfigBase {
export interface ServiceButtonElementConfig extends LovelaceElementConfigBase {
title?: string;
service?: string;
target?: HassServiceTarget;
// "service_data" is kept for backwards compatibility. Replaced by "data".
service_data?: Record<string, unknown>;
data?: Record<string, unknown>;
}

export interface StateBadgeElementConfig extends LovelaceElementConfigBase {
Expand Down
Loading