Device capabilities

I’m startng with my first Homey App. I want to build an app for my device that has a backlight. The backlight can be set with an available API to: on/off, brightness and color.

If I followed the developer introduction from Emile and I understand that I can use use the class ‘light’ to set on/off and dim values. But how about the color? How do you define that the color UI is also displayed in the App screen?

First try
in driver.compose.json:

 {
   "name": { "en": "Backlight" },
   "images": {
     "small": "/drivers/my_driver/assets/images/small.png",
     "large": "/drivers/my_driver/assets/images/large.png"
   },
   "class": "light",
   "capabilities": ["onoff", "dim"],
   "capabilitiesOptions": {
     "dim": { "preventInsights": true }
   }
 }

And when I register the capabilities:

    this.registerCapabilityListener('onoff', async (value) => {
      this.log('Backlight On/Off', value);
    });

    this.registerCapabilityListener('dim', async (value) => {
      this.log('Helderheid:  ', value);
    });

    this.registerCapabilityListener('light_hue', async (value) => {
     this.log('Licht Hue:  ', value);
    });

It worked only for onoff and dim values.

Second try
Then I found that I can add the capabilities to the device in the onInit() in device.js
In device.js:

    const lightCapabilities = [
      "onoff",
      "dim",
      "light_hue",
      "light_temperature",
      "light_saturation",
      "light_mode",
    ];
    this.registerMultipleCapabilityListener(
      lightCapabilities,
      async ({
        onoff,
        dim,
        light_hue,
        light_temperature,
        light_saturation,
        light_mode
      }) => {
        // handle the changed capabilities here all at once
        await DeviceApi.setOnOffAndDimAndColorAsync();
      }
    );

With in driver.compose.json:

{
  "name": {
    "en": "Backlight"
  },
  "class": "light",
  "capabilities": [
    "onoff",
    "dim",
    "light_hue",
    "light_temperature",
    "light_saturation",
    "light_mode"
  ],

Removed the app and installed it again now the color UI is available. (-:
But on every capabilitie change I get now:

image

Where is this ‘device by id’ available or where must it be set?

Hi Aren,

You can view the code of the example app, or any Homey app source @ github for example

Thanks Peter, I found that too but was so impressed by the amount of code for a lamp that I closed it :confused:

To big to understand at this moment for a beginner :man_shrugging:

My app id is in the generated code from the app so why is it complaining that it could not get the device id

No problem, Aren.
App ID = for ex. com.appname.my and is unrelated to a device ID

Ok noted but where is the device id?
How does this work then app builders…

Ok found it. The device id is in the driver.js.

Due an error in device.js the export off the Homey.Device class gave an error in the node.js console. But that was scolling out of the view, and started the app in Homey.
Now I fixed it and the color UI is also there.

The capabilities are now in the device.js like this:


    const lightCapabilities = [
      "onoff",
      "dim",
      "light_hue",
      "light_temperature",
      "light_saturation",
      "light_mode"      
    ];
    
    this.registerMultipleCapabilityListener(
      lightCapabilities,
      async ({
        onoff,
        dim,
        light_hue,
        light_temperature,
        light_saturation,
        light_mode
      }) => {
        // handle the changed capabilities here all at once
        await DeviceApi.setOnOffAndDimAndColorAsync();
      }
    );

And this is in driver.compose.json:

{
  "name": {
    "en": "Backlight"
  },
  "class": "light",
  "capabilities": [
    "onoff",
    "dim",
    "light_hue",
    "light_temperature",
    "light_saturation",
    "light_mode"
  ],

Now up to the next challange :grinning_face: