diff --git a/src/panels/config/js/script/call_service.tsx b/src/panels/config/js/script/call_service.tsx index 2f4ee670f9..a9a1f61872 100644 --- a/src/panels/config/js/script/call_service.tsx +++ b/src/panels/config/js/script/call_service.tsx @@ -1,14 +1,39 @@ 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 { + 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); } @@ -16,9 +41,26 @@ export default class CallServiceAction extends AutomationComponent { 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, }); } @@ -30,7 +72,9 @@ export default class CallServiceAction extends AutomationComponent { } 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 (
@@ -39,6 +83,16 @@ export default class CallServiceAction extends AutomationComponent { value={service} onChange={this.serviceChanged} /> + {entity && ( + + )}