Zone and zoneName of device from driver context

Hello,

In the context of a driver, I try to retrieve the zone of each device.
I also want to be able to choose / modify the zone of a device from the driver.

I tried “zoneName”, but it is undefined, I have no ideas how to do this …

Any hint?

Thanks

From the SDK you can’t access the zone a device is in (why? nobody knows… :man_shrugging:t3:), you need to use the Web API for that.

Something like:

await fetch('http://127.0.0.1/api/manager/devices/device/' + device.id, {
                method: 'GET',
                headers: { 'Content-Type': 'application/json', 'Authorization' : "Bearer <token>"}

I suppose?

And how to retrieve the whole list of existing zone?

Or maybe you meant to go through:

const { HomeyAPI } = require('athom-api');
api= await HomeyAPI.forCurrentHomey();
await api.devices.getDevices();
....

I never used this athom-api, any documentation about it ?

athom-api is deprecated, you should use homey-api.

Documentation starts here: homey-api - npm

Hello Robert,

So I’m at the point, I want to create/update/delete a device through the homey-api.

I want to do it from the pairSession itself, so I use those methods:

    async initHomeyApi() {
        if (this.homeyApi === null) {
            this.homeyApi = await HomeyAPI.createAppAPI({
                homey: this.homey,
            });
        }
    }

    async updateDevice(virtualDeviceId, data) {
        this.log('updateDevice:', ...arguments);

        try {
            // Need to retrieve the homey id of the device
            let realDevice = this.getDevices().find(device => device.getData().id === virtualDeviceId);
            if (realDevice === undefined) {
                throw new Error('Could not find the device to delete:', virtualDeviceId);
            }

            await this.homeyApi.devices.updateDevice({
                id: realDevice.getId(),
                device: {
                    name: data.name,
                    zone: data.zoneId,
                    virtualClass: data.classId,
                    data: {
                        id: virtualDeviceId
                    }
                }
            });
        } catch (e) {
            throw e;
        }
    }

I get the error:

{
    "statusCode": 403,
    "description": "Vous n'avez pas la possibilité de faire ceci.",
    "name": "Error",
    "message": "Vous n'avez pas la possibilité de faire ceci."
}

As for the permission, I added to app.json:

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

I am missing something? Or I really really need to go through fetch?

I have no idea, I’ve never done this myself. It might be that the call to updateDevice() is throwing because it might think you’re trying to set data, which isn’t allowed.

So try this:

await this.homeyApi.devices.updateDevice({
  id: realDevice.getId(),
  device: {
    name: data.name,
    zone: data.zoneId,
    virtualClass : data.classId
  }
});

Ok, the solution I found is as follow (after spending a few hours on the subject):

            this.homeyApi = await HomeyAPI.createLocalAPI({
                address: "http://127.0.0.1",
                token: "<my super secret bearer token>"
            });

            let realDevice = this.getDevices().find(device => device.getData().id === virtualDeviceId);
            if (realDevice === undefined) {
                throw new Error('Could not find the device to delete:', virtualDeviceId);
            }

            if (data.name !== realDevice.getName() || data.zoneId !== realDevice.getZone()) {
                await this.homeyApi.devices.updateDevice({
                    id: realDevice.getId(),
                    device: {
                        name: data.name,
                        zone: data.zoneId
                    }
                });
            }

            if (data.classId !== realDevice.getClass()) {
                realDevice.setClass(data.classId);
            }

class cannot be modified through the wep API, and name/zone cannot be modified from the SDK.
Very nice consistency … good job Athom BV …

Remarks:

  • I tried fetch, but just cannot make it work out.
  • I tried createAppAPI instead of createLocalAPI, but it just doesn’t work, and I don’t understand why
  • Modification of class through the Web API returns a “not updatable” error