Homey App, Permissions HomeyApi => Flow.triggerFlow

Hey Devs,

I’m running into an issue i can’t seem to tackle.
I’m exposing homey flows to a secondary system but I am unable to trigger the flows.

Using Api Playground I am able to execute this code:

Homey.flow.triggerFlow({ id: "c4330edf-8131-4a37-9729-c83872fed6ff" })

From within the homey App I’m developing I have set app.json

  "permissions": [
    "homey:manager:api"
  ]

This is a snippet of my code:

const { HomeyAPI } = require("athom-api");
let homeyApi = await HomeyAPI.forCurrentHomey(this.homey);

...

homeyApi.flow.triggerFlow({ id: "c4330edf-8131-4a37-9729-c83872fed6ff" }).catch((e) => {
  console.warn(e);
});

From the app it doesn’t work, seem to be a permission thing. I am however able to get all flows.
Error i receive:

missing_scopes: Je hebt geen toegang om dit te doen.
at /node_modules/athom-api/dist/index.js:1:1242141
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
code: 403,
cause: t {
__athom_api_type: ‘HomeyAPI.ManagerFlow.Error’,
code: 403,
error: ‘missing_scopes’,
error_description: ‘Je hebt geen toegang om dit te doen.’,
‘$stack’: undefined
}

Does anyone have a clue what I could do to fix this?

AFAIK you can’t, apps don’t have permission to trigger flows like this.

Thanks for the quick response.

at https://apps.developer.homey.app/the-basics/app/permissions it states:

The API permission ("homey:manager:api" ) allows an app to access the Homey Web API. This web api can be used to control all of Homey (devices, Flows, etc) even if they are not part of the App that requested the permission.

Should i create a support case?

Only thing i can try is using this API approach opposed to the one i’m currently using:

(homey-api VS athom-api)

You can try, but they will probably just change the documentation :wink:

athom-api is the predecessor of homey-api, which you should be using anyway :smiley:

Helaas:

'use strict';

const Homey = require('homey');
const { HomeyAPI } = require('homey-api');

class MyApp extends Homey.App {

  /**
   * onInit is called when the app is initialized.
   */
  async onInit() {
    this.homeyApi = await HomeyAPI.createAppAPI({
      homey: this.homey,
    });

    this.homeyApi.flow.triggerFlow({ id: "c4330edf-8131-4a37-9729-c83872fed6ff" }).catch((e) => {
      console.warn(e);
    });

    this.log('MyApp has been initialized');
  }

}

module.exports = MyApp;

Log:

─────────────── Logging stdout & stderr ───────────────
[log] 2023-05-22 09:18:50 [MyApp] MyApp has been initialized
[HomeyAPIError: Je hebt geen toegang om dit te doen.] {
  statusCode: 403,
  description: 'Je hebt geen toegang om dit te doen.'
}

No, Athom will not allow executing flows from apps.
Thats why H.O.O.P was released in the Community Store.

Can I asume that triggering moods has the same restriction?

(20.9s is caused by the debugger)

App code (not working):

  public async setMoodById(moodId: string): Promise<void> {
    let homeyAPI = await super.ensureHomeyAPI();
    await homeyAPI.flow.runFlowCardAction({
      uri: `homey:mood:${moodId}`,
      id: `homey:mood:${moodId}:set`,
      args: {}
    });
  }

Yet, it works from HomeyScript:

//...
  promises.push(Homey.flow.runFlowCardAction({
    uri: `homey:mood:${homeyMoodId}`,
    id: `homey:mood:${homeyMoodId}:set`,
    args: {}
  }));
//...

My app has Homey API permission:

  "permissions": [
    "homey:manager:api"
  ],

and other calls (like retrieving all devices) work just fine.

UPDATE

I thought I found the solution, be I got the same error (Missing Scopes)…

New app code:

  public async setMoodById(moodId: string): Promise<void> {
    let homeyAPI = await super.ensureHomeyAPI();
    await homeyAPI.moods.setMood({ id: moodId } );
  }

Homey API instance creation:

  protected async ensureHomeyAPI() {
    if(this._homeyAPI === null) {
      this._homeyAPI = await HomeyAPI.createAppAPI({ homey: this._app.homey });
    }
    return this._homeyAPI;
  }

Is there any other way to active a mood from a Homey app?

I don’t think Athom will allow this, but looks like you’ll have to ask Athom to add your app to the web api’s scope list of moods.

I don’t understand why the Homey Web API would block this. A mood exposes less functionality than devices, yet with "homey:manager:api" permission I can control all devices.

The funny thing is… I can read the properties (the configuration) of the mood and I can control the capabilities of the devices. That means I can actually set a mood by applying (for each device in the mood) each property in the mood to the capability in the device. So why don’t they save me the trouble and just allow setting moods when you have "homey:manager:api" permission? They could also add a new permission "homey:manager:moods" that explicitly allows setting moods.

For my setup at home, I can easily solve this by creating a HomeyScript “Set mood by ID”, but I would have loved to have this card in my app and offer it to the rest of the community too.

@Caseda

Thanks for responding… not the answer I was hoping for, but much appreciated.

you’ll have to ask Athom to add your app to the web api’s scope list of moods

How do I proceed? Can you point me in the right direction?
↳ Do I open a support ticket for this?
↳ Is there a topic for feature requests?
↳ Is there a mailbox I can write to?

Support ticket is your only way.
There are several things behind the scope wall, devices is one of the few that isn’t.
still waiting for dashboard scope support for HomeyScript (yes even HomeyScript doesn’t have all scopes)