This is the forum archive of Homey. For more information about Homey, visit the Official Homey website.

The Homey Community has been moved to https://community.athom.com.

This forum is now read-only for archive purposes.
Closed

Noob question around importing devices

Hi All,
I am new to coding with NodeJS and I have been working on my lighting driver, but have a question about importing the devices into Homey. I have followed the example in the developers library, but have gotten a bit lost when trying to import multi devices.
I have a command that lists all the lighting devices which I have feed into an array.
Is there a method that I can import all the devices in one go? or do I need to inject them one at a time?

Any help would be very appreciated

Comments

  • Stored devices are passed as an array in the init() method (check the driver documentation).. 

    Not exactly sure what you mean.. Do you have a code example? 
  • Thanks EvertorN for responding.
    I think I see now! I have been using the pairing method.
    I should be using the init method! (insert emoticon head slap) now I can register all 25 devices in one go.

    devices_data.forEach(function(device_data){
          initDevice( device_data );
    })

    but how do I structure the devices_data array? as now I am getting errors
    TypeError: devices_data.forEach is not a function
        at module.exports.init (/drivers/lights/driver.js:114:18)
        at /homey-app/manager/drivers.js:1:2241
        at Array.forEach (native)
        at eventListeners.clientDriverInit (/homey-app/manager/drivers.js:1:761)
        at /homey-app/helpers/client.js:1:868
        at Array.forEach (native)
        at process.<anonymous> (/homey-app/helpers/client.js:1:835)
        at emitTwo (events.js:87:13)
        at process.emit (events.js:172:7)
        at handleMessage (internal/child_process.js:695:10)


        var devices_data = {
            name: "Study",
            data: {
                id: '39bad840-176b-1034-9c8b-f43f905bba34',
                netname: "ANDY_XMS",
                network: "254",
                application: "56",
                unit: "18",
                level: "0"
            },
            name: "Kitchen",
            data: {
                id: '39bad840-176b-1034-ffff-f43f905bba34',
                netname: "ANDY_XMS",
                network: "254",
                application: "56",
                unit: "19",
                level: "0"
            }
        }

    114    devices_data.forEach(function (device_data) {
    115        initDevice(device_data);
    116    })
    117
    118    callback();
  • Thanks MacroF
    I can see how the init (); is being passed from homey the device and you testing to see if the device is on and the status of the device is being passed back to homey.
    What I am trying to do is add multiple new devices, I think I am correct in saying this is done via the pair(); function.
    I have a command that lists all the lighting devices, there light levels and state

    tree //ANDY_XMS/254/56

    320-Applications:
    320- Application 56 ($38) [lighting]
    320- Groups:
    320-  //ANDY_XMS/254/56/1 ($1) level=0 state=ok
    320-  //ANDY_XMS/254/56/2 ($2) level=0 state=ok
    320-  //ANDY_XMS/254/56/3 ($3) level=0 state=ok
    320-  //ANDY_XMS/254/56/4 ($4) level=0 state=ok
    320-  //ANDY_XMS/254/56/5 ($5) level=0 state=ok
    320-  //ANDY_XMS/254/56/6 ($6) level=0 state=ok
    320-  //ANDY_XMS/254/56/7 ($7) level=0 state=ok
    320-  //ANDY_XMS/254/56/8 ($8) level=0 state=ok
    320-  //ANDY_XMS/254/56/9 ($9) level=0 state=ok
    320-  //ANDY_XMS/254/56/10 ($a) level=0 state=ok
    320-  //ANDY_XMS/254/56/11 ($b) level=0 state=ok
    320-  //ANDY_XMS/254/56/12 ($c) level=0 state=ok
    320-  //ANDY_XMS/254/56/13 ($d) level=0 state=ok
    320-  //ANDY_XMS/254/56/14 ($e) level=0 state=ok
    320-  //ANDY_XMS/254/56/16 ($10) level=0 state=ok
    320-  //ANDY_XMS/254/56/17 ($11) level=0 state=ok
    320-  //ANDY_XMS/254/56/18 ($12) level=0 state=ok
    320-  //ANDY_XMS/254/56/19 ($13) level=0 state=ok
    320-  //ANDY_XMS/254/56/20 ($14) level=0 state=ok
    320-  //ANDY_XMS/254/56/21 ($15) level=0 state=ok
    320-  //ANDY_XMS/254/56/24 ($18) level=0 state=ok
    320-  //ANDY_XMS/254/56/25 ($19) level=0 state=ok

    Is there away to add all these devices to homey in one go, or do I need to  loop through each one, add them to honey one at a time?
  • swtttswttt Member
    On pair you should loop trough your devices and add them one by one.
    And for each device use Homey.addDevice (allmost at the bottom of the page here: https://developers.athom.com/library/drivers/pairing/)
  • So if I add the Homey.addDevice to the drivers\pair\start.hml
    and replace the 'list_devices' with 'add_devices'

        socket.on('add_devices', function( data, callback ){
            var devices_data = {
         {
                name: "Device 1",
                data: { id: "some-unique-device 1-id",  whatever_you_want: "can be placed here" },
                capabilities: [ "onoff", "dim" ]
         },
         {
                name: "Device 2",
                data: { id: "some-unique-device 2-id", whatever_you_want: "can be placed here" },
                capabilities: [ "onoff", "dim" ]
          }
    };
            // Loop through each device
           devices_data.forEach(device ){
            callback( null, [ device ] );
           }
        })
  • Hi All,
    Thanks for everyone help I have worked out the array syntax which was stopping me from adding multiple units, with this I can now import the 25 unit into the devices array and then into homey.

        socket.on('list_devices', function (data, callback) {
            var devices = [];
            devices[0] = {
                name: "39bad840-176b-1034-9c8b-f43f905bba34",
                data: {
                    id: '39bad840-176b-1034-9c8b-f43f905bba34',
                    cbusname: "ANDY_XMS",
                    network: "254",
                    application: "56",
                    unit: "18",
                    level: "0"
                },
                state: { onoff: false },
                capabilities: ["onoff"] 
            };

            devices[1] = {
                name: "bdaeaa50-1838-1034-9d23-f43f905bba34",
                data: {
                    id: 'bdaeaa50-1838-1034-9d23-f43f905bba34',
                    cbusname: "ANDY_XMS",
                    network: "254",
                    application: "56",
                    unit: "19",
                    level: "0"
                },
                state: { onoff: false },
                capabilities: ["onoff"] 
            };
            callback(null, devices);
  • thanks guys!
This discussion has been closed.