# Zone and zoneName of device from driver context

**URL:** <https://community.homey.app/t/zone-and-zonename-of-device-from-driver-context/89327>\
**Category:** Developers\
**Created:** [September 13, 2023, 10:04am UTC](https://community.homey.app/t/zone-and-zonename-of-device-from-driver-context/89327 "2023-09-13T10:04:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Baldhor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/baldhor/32/27028_2.png) [@Baldhor](https://community.homey.app/u/Baldhor)\
**Post date:** [September 13, 2023, 10:04am UTC](https://community.homey.app/t/zone-and-zonename-of-device-from-driver-context/89327/1 "2023-09-13T10:04:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [September 13, 2023, 10:09am UTC](https://community.homey.app/t/zone-and-zonename-of-device-from-driver-context/89327/2 "2023-09-13T10:09:57Z")

</div>

From the SDK you can’t access the zone a device is in (why? nobody knows… 🤷🏼‍♂️), you need to use the Web API for that.

---

<div class="post-metadata">

**Author:** ![Baldhor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/baldhor/32/27028_2.png) [@Baldhor](https://community.homey.app/u/Baldhor)\
**Post date:** [September 14, 2023, 8:31am UTC](https://community.homey.app/t/zone-and-zonename-of-device-from-driver-context/89327/3 "2023-09-14T08:31:37Z")

</div>

Something like:

```auto
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?

---

<div class="post-metadata">

**Author:** ![Baldhor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/baldhor/32/27028_2.png) [@Baldhor](https://community.homey.app/u/Baldhor)\
**Post date:** [September 14, 2023, 8:47am UTC](https://community.homey.app/t/zone-and-zonename-of-device-from-driver-context/89327/4 "2023-09-14T08:47:39Z")

</div>

And how to retrieve the whole list of existing zone?

---

<div class="post-metadata">

**Author:** ![Baldhor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/baldhor/32/27028_2.png) [@Baldhor](https://community.homey.app/u/Baldhor)\
**Post date:** [September 14, 2023, 8:57am UTC](https://community.homey.app/t/zone-and-zonename-of-device-from-driver-context/89327/5 "2023-09-14T08:57:45Z")

</div>

Or maybe you meant to go through:

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

```

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

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [September 14, 2023, 9:27am UTC](https://community.homey.app/t/zone-and-zonename-of-device-from-driver-context/89327/6 "2023-09-14T09:27:14Z")

</div>

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

Documentation starts here: [homey-api - npm](https://www.npmjs.com/package/homey-api)

---

<div class="post-metadata">

**Author:** ![Baldhor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/baldhor/32/27028_2.png) [@Baldhor](https://community.homey.app/u/Baldhor)\
**Post date:** [September 30, 2023, 10:32am UTC](https://community.homey.app/t/zone-and-zonename-of-device-from-driver-context/89327/7 "2023-09-30T10:32:28Z")

</div>

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:

```auto
    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:

```auto
{
    "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:

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

```

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

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [September 30, 2023, 11:01am UTC](https://community.homey.app/t/zone-and-zonename-of-device-from-driver-context/89327/8 "2023-09-30T11:01:38Z")

</div>

> [@Baldhor](#):
>
> I am missing something?

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:

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

```

---

<div class="post-metadata">

**Author:** ![Baldhor](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/baldhor/32/27028_2.png) [@Baldhor](https://community.homey.app/u/Baldhor)\
**Post date:** [September 30, 2023, 8:35pm UTC](https://community.homey.app/t/zone-and-zonename-of-device-from-driver-context/89327/9 "2023-09-30T20:35:23Z")

</div>

> [@Baldhor](#):
>
> `createAppAPI`

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

```auto
            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
