Skip to content
Merged
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
58 changes: 56 additions & 2 deletions src/panels/config/js/script/call_service.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,66 @@
import { h } from "preact";
import "../../../../components/ha-service-picker";
import "../../../../components/entity/ha-entity-picker";

import YAMLTextArea from "../yaml_textarea";
import { AutomationComponent } from "../automation-component";
import { computeDomain } from "../../../../common/entity/compute_domain";
import { computeObjectId } from "../../../../common/entity/compute_object_id";
import memoizeOne from "memoize-one";

export default class CallServiceAction extends AutomationComponent<any> {
private _getServiceData = memoizeOne((service: string) => {
if (!service) {
return [];
}
const domain = computeDomain(service);
const serviceName = computeObjectId(service);
const serviceDomains = this.props.hass.services;
if (!(domain in serviceDomains)) {
return [];
}
if (!(serviceName in serviceDomains[domain])) {
return [];
}

const fields = serviceDomains[domain][serviceName].fields;
return Object.keys(fields).map((field) => {
return { key: field, ...fields[field] };
});
});

constructor() {
super();

this.serviceChanged = this.serviceChanged.bind(this);
this.entityChanged = this.entityChanged.bind(this);
this.serviceDataChanged = this.serviceDataChanged.bind(this);
}

public serviceChanged(ev) {
if (!this.initialized) {
return;
}
this.props.onChange(this.props.index, {
const newAction = {
...this.props.action,
service: ev.target.value,
};
if (
computeDomain(this.props.action.service) !==
computeDomain(ev.target.value)
) {
delete newAction.entity_id;
}
this.props.onChange(this.props.index, newAction);
}

public entityChanged(ev) {
if (!this.initialized) {
return;
}
this.props.onChange(this.props.index, {
...this.props.action,
entity_id: ev.target.value,
});
}

Expand All @@ -30,7 +72,9 @@ export default class CallServiceAction extends AutomationComponent<any> {
}

public render({ action, hass, localize }) {
const { service, data } = action;
const { service, data, entity_id } = action;
const serviceData = this._getServiceData(service);
const entity = serviceData.find((attr) => attr.key === "entity_id");

return (
<div>
Expand All @@ -39,6 +83,16 @@ export default class CallServiceAction extends AutomationComponent<any> {
value={service}
onChange={this.serviceChanged}
/>
{entity && (
<ha-entity-picker
hass={hass}
value={entity_id}
label={entity.description}
onChange={this.entityChanged}
includeDomains={[computeDomain(service)]}
allow-custom-entity
/>
)}
<YAMLTextArea
label={localize(
"ui.panel.config.automation.editor.actions.type.service.service_data"
Expand Down