Skip to content

Commit

Permalink
1.5.22
Browse files Browse the repository at this point in the history
Added app trigger for entity state changes.
  • Loading branch information
RonnyWinkler committed May 30, 2024
1 parent 3c2e935 commit 1045bf0
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .homeychangelog.json
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,10 @@
"1.5.21": {
"en": "Some technical corrections.",
"de": "Einige technische Korrekturen."
},
"1.5.22": {
"en": "Added app trigger for entity state changes.",
"de": "App-Trigger für Entitäts-Statusänderungen hinzugefügt."
}

}
2 changes: 1 addition & 1 deletion .homeycompose/app.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "io.home-assistant.community",
"version": "1.5.21",
"version": "1.5.22",
"compatibility": ">=8.1.0",
"sdk": 3,
"brandColor": "#0DA6EA",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"title": {
"en": "Entity state has changed",
"de": "Entity-Statue hat sich geändert",
"nl": "Entiteitsstatus is gewijzigd"
},
"titleFormatted":{
"en": "The state of entity [[entity]] has changed.",
"de": "Der Status der Entität [[entity]] hat sich geändert.",
"nl": "De status van entiteit [[entity]] is gewijzigd."
},
"hint": {
"en": "Triggers when the event 'state_changed' was triggered for the entity.",
"de": "Wird ausgelöst, wenn das Ereignis 'state_changed' für die Entität ausgelöst wurde.",
"nl": "Wordt geactiveerd wanneer het evenement 'state_changed' voor de entiteit is geactiveerd."
},
"tokens": [
{
"name": "entity",
"type": "string",
"title": {
"en": "Entity",
"de": "Entität",
"nl": "Entiteit"
}
},
{
"name": "state",
"type": "string",
"title": {
"en": "Entity state",
"de": "Entitätsstatus",
"nl": "Entiteitsstatus"
}
}
],
"args": [
{
"name": "entity",
"type": "text",
"required": false,
"title": {
"en": "Entity",
"de": "Entität",
"nl": "Entiteit"
},
"placeholder": {
"en": "Entity",
"de": "Eintität",
"nl": "Entiteit"
}
}
]
}
38 changes: 37 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,18 @@ class App extends Homey.App {
return false;
}
});
this._flowTriggerEventStateChangedTriggeredFilter = this.homey.flow.getTriggerCard("event_state_changed_triggered_filter");
this._flowTriggerEventStateChangedTriggeredFilter.registerRunListener(async (args, state) => {
// Check for entity id
if (
( !args.entity || args.entity === state.entity )
){
return true;
}
else{
return false;
}
});

// Flow Trigger: Devices
// Flow trigger for all capabilities (compound device)
Expand Down Expand Up @@ -1199,6 +1211,13 @@ class App extends Homey.App {
// args is [{ "my_arg": "user_value" }]
await this._registerFlowArgumentsEventTriggeredFilter(args);
});
await this._registerFlowArgumentsEventStateChangedTriggeredFilter(await this._flowTriggerEventStateChangedTriggeredFilter.getArgumentValues());
this._flowTriggerEventStateChangedTriggeredFilter.on("update", async () => {
this.log("Trigger argument updated for event_state_changed_triggered_filter.");
let args = await this._flowTriggerEventStateChangedTriggeredFilter.getArgumentValues();
// args is [{ "my_arg": "user_value" }]
await this._registerFlowArgumentsEventStateChangedTriggeredFilter(args);
});
}

async _registerFlowArgumentsEventTriggeredFilter(args){
Expand All @@ -1215,7 +1234,24 @@ class App extends Homey.App {
// }
// );
}
this.log(this.flowTriggerArguments);
this.log(this.flowTriggerArguments["event_triggered_filter"]);
}

async _registerFlowArgumentsEventStateChangedTriggeredFilter(args){
this.log("Trigger arguments read for event_state_changed_triggered_filter");
// this.log(args);
this.flowTriggerArguments["event_state_changed_triggered_filter"] = {};

for(let i=0; i<args.length; i++){
this.flowTriggerArguments["event_state_changed_triggered_filter"][args[i].entity] = true;
// this.flowTriggerArguments["event_triggered_filter"].push(
// {
// event: args[i].event,
// entity: args[i].entity
// }
// );
}
this.log(this.flowTriggerArguments["event_state_changed_triggered_filter"]);
}

// onLog(...args){
Expand Down
57 changes: 56 additions & 1 deletion app.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"_comment": "This file is generated. Please edit .homeycompose/app.json instead.",
"id": "io.home-assistant.community",
"version": "1.5.21",
"version": "1.5.22",
"compatibility": ">=8.1.0",
"sdk": 3,
"brandColor": "#0DA6EA",
Expand Down Expand Up @@ -596,6 +596,61 @@
],
"id": "climate_mode_swing_changed"
},
{
"title": {
"en": "Entity state has changed",
"de": "Entity-Statue hat sich geändert",
"nl": "Entiteitsstatus is gewijzigd"
},
"titleFormatted": {
"en": "The state of entity [[entity]] has changed.",
"de": "Der Status der Entität [[entity]] hat sich geändert.",
"nl": "De status van entiteit [[entity]] is gewijzigd."
},
"hint": {
"en": "Triggers when the event 'state_changed' was triggered for the entity.",
"de": "Wird ausgelöst, wenn das Ereignis 'state_changed' für die Entität ausgelöst wurde.",
"nl": "Wordt geactiveerd wanneer het evenement 'state_changed' voor de entiteit is geactiveerd."
},
"tokens": [
{
"name": "entity",
"type": "string",
"title": {
"en": "Entity",
"de": "Entität",
"nl": "Entiteit"
}
},
{
"name": "state",
"type": "string",
"title": {
"en": "Entity state",
"de": "Entitätsstatus",
"nl": "Entiteitsstatus"
}
}
],
"args": [
{
"name": "entity",
"type": "text",
"required": false,
"title": {
"en": "Entity",
"de": "Entität",
"nl": "Entiteit"
},
"placeholder": {
"en": "Entity",
"de": "Eintität",
"nl": "Entiteit"
}
}
],
"id": "event_state_changed_triggered_filter"
},
{
"title": {
"en": "Event was triggered",
Expand Down
35 changes: 35 additions & 0 deletions lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,14 @@ class Client extends Homey.SimpleClass {
// update entity in entity list
this._entities[entityId] = {...data.new_state};

// trigger event handler for state_changed events
try{
await this._onEventStateChanged(data);
}
catch(error){
this._app.log("onStateChanged error triggering _onEventStateChanged() :", error);
}

// Get simple device by entityId
let device = this._deviceRegistration[entityId];
if(device != null) {
Expand Down Expand Up @@ -641,6 +649,33 @@ class Client extends Homey.SimpleClass {
}
}

async _onEventStateChanged(event){
// this._app.log("HA event: ", event.event_type, " Entity: ",entityId);
let tokens = {
entity: event.entity_id,
state: event.new_state.state
};
let state = {
entity: event.entity_id,
state: event.new_state.state
};
// check registered flow arguments...to filter flow trigger for only reqested events
let args = this._app.getFlowTriggerArguments();
if ( args["event_state_changed_triggered_filter"] ){
if ( args["event_state_changed_triggered_filter"][""] ||
args["event_state_changed_triggered_filter"][event.entity_id] ){
// Trigger Flow
await this._app._flowTriggerEventStateChangedTriggeredFilter.trigger(tokens, state);
}

}
// Extended app log: log all entity updates
if (this._app.getLogSettings()['event']){
this._app.log("Event: "+ "state_changed" +" Entity: " + tokens.entity + " Data: ",tokens.state);
}

}

async _onEventScript(event){
if (event == undefined || event.data == undefined){
return;
Expand Down

0 comments on commit 1045bf0

Please sign in to comment.