Giter Club home page Giter Club logo

Comments (3)

aholstenson avatar aholstenson commented on August 12, 2024 1

Thanks! I'll update the README to be a bit clearer about subdevices not having tokens.

from miio.

aholstenson avatar aholstenson commented on August 12, 2024

Thanks for the report.

The proper way to do this via miio is either to connect to the gateway via miio.device or use discovery:

A call to miio.devices() would return a browser that should find your gateway and any devices connected to it. You can filter the results and add listeners to anything that is of type controller to listen for actions:

const devices = miio.devices({
  filter: reg => reg.type == 'gateway' || reg.type == 'controller'
});

devices.on('available', reg => {
  const device = reg.device;
  if(! device) {
    console.log(reg.id, 'could not be connected to');
    return;
  }

  console.log('Connected to', device);
  if(device.type === 'controller') {
     // This is a controller such as a switch, cube or wall mounted switch
     device.on('action', action => console.log(action.id));
  }
});

That should get you started! To help with debugging its also a good idea to run miio --discover to make sure that the library can see your gateway and the devices connected to it.

from miio.

Shuunen avatar Shuunen commented on August 12, 2024

This snippet works :)
But in my case, I was using the snippet examples on the main readme and it was not working because of this part :

 if (!reg.token) {
        console.log(reg.id, 'hides its token');
        return;
    }

Because all subdevices were hidding their tokens, it was always exiting before listening for action event...

Why exiting if no token ? Because finnaly even with no token, the library manage to detect devices events

Using a simpler snippet example like this would have been an easier way to start with the miio library :

const devices = miio.devices()
devices.on('available', reg => {
    const device = reg.device
    if (!device) {
        console.log(reg.id, 'could not be connected to')
        return
    }
    console.log('Connected to', device.type)
    device.on('propertyChanged', e => console.log(device.type + ' : property "' + e.property + '" changed from ' + e.oldValue + ' to ' + e.value))
    device.on('action', action => console.log(device.type + ' : action detected "' + action.id + '"'))
})

I'm not here to promote the previous library I mentioned before or any other library, it's just a feedback from a user that tryed to detect events on his chinese low cost home domotic kit πŸ˜„ and this token part was not easy to understand.

Even the miio --discover that I've tried was not clear in my mind how it could helped me, here is the output :

Device ID: 56xxx9
Model info: Unknown
Address: 192.168.31.137
Token: ???
Support: Unknown

Device ID: 54xxx6
Model info: Unknown
Address: 192.168.31.170
Token: 82exxxxx57e6 via auto-token
Support: Unknown

Device ID: 54xxx8
Model info: Unknown
Address: 192.168.31.54
Token: 51xxxxf9a1 via auto-token
Support: Unknown

(node:2340) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot set property 'device' of undefined
````

from miio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.