Skip to content

Commit

Permalink
fix: Move entity_id from target field to data field
Browse files Browse the repository at this point in the history
  • Loading branch information
zachowj committed Aug 17, 2024
1 parent 3b7a82a commit 1ae771d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
28 changes: 27 additions & 1 deletion src/nodes/action/ActionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default class ActionController extends InputOutputController<
> {
#queue: QueueItem[] = [];
#hasDeprecatedWarned = false;
#hasDeprecatedWarnedTargetEntityId = false;

protected async onInput({
message,
Expand Down Expand Up @@ -72,6 +73,31 @@ export default class ActionController extends InputOutputController<
render,
);

// TODO: Remove in version 1.0 - Check if entity_id should be in the data field not the target field
if (
parsedMessage.action?.value &&
typeof target.entity_id === 'string'
) {
const services = this.homeAssistant.websocket.getServices();
const [domain, service] = parsedMessage.action?.value
.toLowerCase()
.split('.');

if (
services[domain]?.[service]?.fields?.entity_id !== undefined &&
!mergedData.entity_id
) {
if (!this.#hasDeprecatedWarnedTargetEntityId) {
this.#hasDeprecatedWarnedTargetEntityId = true;
this.node.warn(
RED._('ha-action.error.entity_id_target_data'),
);
}
mergedData.entity_id = target.entity_id;
target.entity_id = undefined;
}
}

const queueItem: QueueItem = {
domain,
service,
Expand Down Expand Up @@ -301,7 +327,7 @@ export default class ActionController extends InputOutputController<
);

this.status.setSuccess([
'ha-action.status.service_called',
'ha-action.status.action_called',
{ domain, service },
]);

Expand Down
3 changes: 2 additions & 1 deletion src/nodes/action/locale.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"ha-action": {
"error": {
"domain_service_deprecated": "The domain and service input properties are deprecated and will be removed in version 1.0. Please use the action property instead.",
"entity_id_target_data": "Entity ID is required to be in the data property for this action. Please add the entity ID to the data property. This will stop working in version 1.0.",
"invalid_action_format": "Invalid action format: __action__",
"invalid_entity_id": "Invalid entity ID format",
"invalid_json": "Invalid JSON: __json__",
Expand All @@ -26,7 +27,7 @@
"targets": "Targets"
},
"status": {
"service_called": "__domain__.__service__ called"
"action_called": "__domain__.__service__ called"
}
}
}

0 comments on commit 1ae771d

Please sign in to comment.