node-miio 0.0.0-dev-20250810-f1674d0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/.editorconfig +21 -0
  2. package/.eslintrc +32 -0
  3. package/.github/workflows/build.yml +50 -0
  4. package/.github/workflows/codecov.yml +35 -0
  5. package/.github/workflows/codeql.yml +24 -0
  6. package/.github/workflows/publish-dev-from-main.yml +92 -0
  7. package/.github/workflows/publish-release-to-npm.yml +48 -0
  8. package/CONTRIBUTING.md +104 -0
  9. package/LICENSE.md +20 -0
  10. package/README.md +216 -0
  11. package/cli/commands/configure.js +69 -0
  12. package/cli/commands/control.js +54 -0
  13. package/cli/commands/discover.js +36 -0
  14. package/cli/commands/inspect.js +197 -0
  15. package/cli/commands/protocol/call.js +66 -0
  16. package/cli/commands/protocol/json-dump.js +52 -0
  17. package/cli/commands/protocol/packet.js +29 -0
  18. package/cli/commands/protocol.js +8 -0
  19. package/cli/commands/tokens/update.js +58 -0
  20. package/cli/commands/tokens.js +8 -0
  21. package/cli/device-finder.js +66 -0
  22. package/cli/index.js +9 -0
  23. package/cli/log.js +89 -0
  24. package/docs/advanced-api.md +103 -0
  25. package/docs/devices/README.md +111 -0
  26. package/docs/devices/air-purifier.md +142 -0
  27. package/docs/devices/controller.md +89 -0
  28. package/docs/devices/gateway.md +41 -0
  29. package/docs/devices/humidifier.md +136 -0
  30. package/docs/devices/light.md +141 -0
  31. package/docs/devices/power-outlet.md +56 -0
  32. package/docs/devices/power-plug.md +69 -0
  33. package/docs/devices/power-strip.md +73 -0
  34. package/docs/devices/sensor.md +74 -0
  35. package/docs/devices/vacuum.md +80 -0
  36. package/docs/devices/wall-switch.md +62 -0
  37. package/docs/management.md +109 -0
  38. package/docs/missing-devices.md +48 -0
  39. package/docs/protocol.md +77 -0
  40. package/docs/reporting-issues.md +47 -0
  41. package/example.js +11 -0
  42. package/lib/checkResult.js +13 -0
  43. package/lib/connectToDevice.js +62 -0
  44. package/lib/device.js +290 -0
  45. package/lib/device_info.js +329 -0
  46. package/lib/device_info.test.js +337 -0
  47. package/lib/devices/air-monitor.js +53 -0
  48. package/lib/devices/air-purifier.js +162 -0
  49. package/lib/devices/capabilities/battery-level.js +16 -0
  50. package/lib/devices/capabilities/buzzer.js +78 -0
  51. package/lib/devices/capabilities/changeable-led-brightness.js +48 -0
  52. package/lib/devices/capabilities/colorable.js +14 -0
  53. package/lib/devices/capabilities/dimmable.js +14 -0
  54. package/lib/devices/capabilities/mode.js +13 -0
  55. package/lib/devices/capabilities/power.js +14 -0
  56. package/lib/devices/capabilities/sensor.js +50 -0
  57. package/lib/devices/capabilities/switchable-led.js +49 -0
  58. package/lib/devices/chuangmi.plug.v1.js +61 -0
  59. package/lib/devices/eyecare-lamp2.js +179 -0
  60. package/lib/devices/gateway/86sw1.js +28 -0
  61. package/lib/devices/gateway/86sw2.js +44 -0
  62. package/lib/devices/gateway/ctrl_ln1.js +44 -0
  63. package/lib/devices/gateway/ctrl_ln2.js +53 -0
  64. package/lib/devices/gateway/ctrl_neutral1.js +44 -0
  65. package/lib/devices/gateway/ctrl_neutral2.js +53 -0
  66. package/lib/devices/gateway/cube.js +55 -0
  67. package/lib/devices/gateway/developer-api.js +120 -0
  68. package/lib/devices/gateway/light-channel.js +32 -0
  69. package/lib/devices/gateway/light-mixin.js +71 -0
  70. package/lib/devices/gateway/magnet.js +29 -0
  71. package/lib/devices/gateway/magnet2.js +13 -0
  72. package/lib/devices/gateway/motion.js +27 -0
  73. package/lib/devices/gateway/motion2.js +31 -0
  74. package/lib/devices/gateway/plug.js +45 -0
  75. package/lib/devices/gateway/sensor_ht.js +17 -0
  76. package/lib/devices/gateway/subdevice.js +227 -0
  77. package/lib/devices/gateway/subdevices.js +37 -0
  78. package/lib/devices/gateway/switch.js +29 -0
  79. package/lib/devices/gateway/switch2.js +27 -0
  80. package/lib/devices/gateway/voltage.js +28 -0
  81. package/lib/devices/gateway/weather.js +22 -0
  82. package/lib/devices/gateway.js +244 -0
  83. package/lib/devices/humidifier.js +162 -0
  84. package/lib/devices/mijiavacuum.js +22 -0
  85. package/lib/devices/philips-light-bulb.js +73 -0
  86. package/lib/devices/power-plug.js +26 -0
  87. package/lib/devices/power-strip.js +38 -0
  88. package/lib/devices/vacuum.js +412 -0
  89. package/lib/devices/viomivacuum.js +250 -0
  90. package/lib/devices/yeelight.color.js +6 -0
  91. package/lib/devices/yeelight.js +286 -0
  92. package/lib/devices/yeelight.mono.js +9 -0
  93. package/lib/discovery.js +192 -0
  94. package/lib/index.js +40 -0
  95. package/lib/infoFromHostname.js +24 -0
  96. package/lib/management.js +102 -0
  97. package/lib/models.js +77 -0
  98. package/lib/network.js +234 -0
  99. package/lib/packet.js +191 -0
  100. package/lib/placeholder.js +12 -0
  101. package/lib/safeishJSON.js +14 -0
  102. package/lib/tokens.js +129 -0
  103. package/npm-shrinkwrap.json +853 -0
  104. package/package.json +40 -0
package/.editorconfig ADDED
@@ -0,0 +1,21 @@
1
+ root = true
2
+
3
+ [*]
4
+ indent_style = tab
5
+ end_of_line = lf
6
+ charset = utf-8
7
+ trim_trailing_whitespace = true
8
+ insert_final_newline = true
9
+
10
+ [*.md]
11
+ trim_trailing_whitespace = false
12
+ indent_style = space
13
+ indent_size = 2
14
+
15
+ [*.json]
16
+ indent_style = space
17
+ indent_size = 2
18
+
19
+ [package.json]
20
+ indent_style = space
21
+ indent_size = 2
package/.eslintrc ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "plugins": [ "node" ],
3
+ "extends": [ "eslint:recommended", "plugin:node/recommended" ],
4
+ "env": {},
5
+ "globals": {},
6
+ "rules": {
7
+ "strict": [ "error", "global" ],
8
+ "no-irregular-whitespace": 2,
9
+ "quotes": [
10
+ 2,
11
+ "single"
12
+ ],
13
+ "no-unused-vars": [
14
+ "error",
15
+ { "vars": "all", "args": "none", "ignoreRestSiblings": false }
16
+ ],
17
+ "eqeqeq": [ "error" ],
18
+ "no-throw-literal": [ "error" ],
19
+ "semi": [ "error", "always" ]
20
+ },
21
+ "overrides": [
22
+ {
23
+ "files": [
24
+ "**/*.test.js",
25
+ "**/*.test.ts"
26
+ ],
27
+ "env": {
28
+ "jest": true
29
+ }
30
+ }
31
+ ]
32
+ }
@@ -0,0 +1,50 @@
1
+ name: Node.js CI
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+ branches: [main]
8
+
9
+ jobs:
10
+ build:
11
+ runs-on: ${{ matrix.os }}
12
+
13
+ strategy:
14
+ fail-fast: false
15
+ matrix:
16
+ node-version: [20.x, 22.x, 24.x]
17
+ os:
18
+ - ubuntu-latest
19
+ - macos-latest
20
+ # - windows-latest
21
+
22
+ steps:
23
+ - name: Checkout repository
24
+ uses: actions/checkout@v4
25
+
26
+ - name: Use Node.js ${{ matrix.node-version }}
27
+ uses: actions/setup-node@v4
28
+ with:
29
+ node-version: ${{ matrix.node-version }}
30
+
31
+ - name: Clean cache
32
+ run: npm cache clean --force
33
+
34
+ - name: Verify Node.js version
35
+ run: node -v
36
+
37
+ - name: Verify Npm version
38
+ run: npm -v
39
+
40
+ - name: Install dependencies
41
+ run: npm ci
42
+
43
+ - name: Lint
44
+ run: npm run lint
45
+
46
+ - name: Test
47
+ run: npm t
48
+
49
+ # - name: Build
50
+ # run: npm run build
@@ -0,0 +1,35 @@
1
+ name: Codecov
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+ branches: [main]
8
+ workflow_dispatch:
9
+
10
+ jobs:
11
+ test:
12
+ name: Run tests and collect coverage
13
+ runs-on: ubuntu-latest
14
+ steps:
15
+ - name: Checkout
16
+ uses: actions/checkout@v4
17
+ with:
18
+ fetch-depth: 2
19
+
20
+ - name: Set up Node
21
+ uses: actions/setup-node@v4
22
+ with:
23
+ node-version: '22.x'
24
+ registry-url: 'https://registry.npmjs.org'
25
+
26
+ - name: Install dependencies
27
+ run: npm ci
28
+
29
+ - name: Run tests
30
+ run: npm run test:coverage
31
+
32
+ - name: Upload results to Codecov
33
+ uses: codecov/codecov-action@v5
34
+ with:
35
+ token: ${{ secrets.CODECOV_TOKEN }}
@@ -0,0 +1,24 @@
1
+ name: CodeQL
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+ branches: [main]
8
+ workflow_dispatch:
9
+
10
+ jobs:
11
+ analyze:
12
+ name: Analyze
13
+ runs-on: ubuntu-latest
14
+ permissions:
15
+ actions: read
16
+ contents: read
17
+ security-events: write
18
+
19
+ steps:
20
+ - uses: actions/checkout@v4
21
+ - uses: github/codeql-action/init@v3
22
+ with:
23
+ languages: javascript
24
+ - uses: github/codeql-action/analyze@v3
@@ -0,0 +1,92 @@
1
+ # Inspired by .github\workflows\publish-matterbridge-plugin-dev-daily-from-main.yml
2
+ name: Publish to npm from main branch to dev tag
3
+
4
+ on:
5
+ push:
6
+ branches: [ main ]
7
+ # # Daily at midnight UTC
8
+ # schedule:
9
+ # - cron: '0 0 * * *'
10
+ # Allow manual dispatch
11
+ workflow_dispatch:
12
+
13
+ jobs:
14
+ publish-dev:
15
+ runs-on: ubuntu-latest
16
+ steps:
17
+ # Always check out the main branch, even for scheduled runs
18
+ - name: Checkout main
19
+ uses: actions/checkout@v4
20
+ with:
21
+ ref: main
22
+ fetch-depth: 0
23
+
24
+ - name: Set up Node.js & registry
25
+ uses: actions/setup-node@v4
26
+ with:
27
+ node-version: '22.x'
28
+ registry-url: 'https://registry.npmjs.org'
29
+
30
+ - name: Clean npm cache
31
+ run: npm cache clean --force
32
+
33
+ - name: Lint & test & build
34
+ run: |
35
+ npm ci
36
+ npm run lint
37
+ npm t
38
+ npm pkg delete devDependencies
39
+ npx shx rm -rf ./node_modules
40
+ npm install --omit=dev
41
+ npm shrinkwrap
42
+
43
+ - name: Extract base version and date
44
+ id: vars
45
+ run: |
46
+ BASE=$(jq -r '.version' package.json)
47
+ DATE=$(date -u +'%Y%m%d')
48
+ SHA=$(git rev-parse --short=7 HEAD)
49
+ DEV_TAG="${BASE}-dev-${DATE}-${SHA}"
50
+ echo "DEV_TAG=$DEV_TAG" >> $GITHUB_ENV
51
+ echo "ORIG_SHA=$SHA" >> $GITHUB_ENV
52
+
53
+ - name: Bump to date-stamped dev version (no git tag)
54
+ run: npm version "${{ env.DEV_TAG }}" --no-git-tag-version
55
+
56
+ - name: Check if a new dev-publish is needed
57
+ id: check_new
58
+ env:
59
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
60
+ run: |
61
+ # ensure npm can read the private or public registry
62
+ npm config set //registry.npmjs.org/:_authToken=$NODE_AUTH_TOKEN
63
+
64
+ # 1) grab the package name
65
+ PKG=$(node -p "require('./package.json').name")
66
+
67
+ # 2) fetch the currently-published 'dev' dist-tag (e.g. "3.0.0-dev-20250430-1a2b3c4")
68
+ PUBLISHED=$(npm view "$PKG" dist-tags.dev 2>/dev/null || echo "")
69
+
70
+ # 3) extract the SHA suffix (after the last dash)
71
+ PUBLISHED_SHA=${PUBLISHED##*-}
72
+
73
+ # 4) get the current commit short-SHA
74
+ CURRENT_SHA=${ORIG_SHA}
75
+
76
+ echo "Published dev tag: $PUBLISHED"
77
+ echo "Published SHA: $PUBLISHED_SHA"
78
+ echo "Current SHA: $CURRENT_SHA"
79
+
80
+ if [ "$PUBLISHED_SHA" = "$CURRENT_SHA" ]; then
81
+ # nothing new → skip
82
+ echo "should_publish=false" >> $GITHUB_OUTPUT
83
+ else
84
+ # new commit → proceed
85
+ echo "should_publish=true" >> $GITHUB_OUTPUT
86
+ fi
87
+
88
+ - name: Publish to npm under ‘dev’ tag
89
+ if: steps.check_new.outputs.should_publish == 'true'
90
+ run: npm publish --tag dev
91
+ env:
92
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
@@ -0,0 +1,48 @@
1
+ name: Publish to npm on release
2
+
3
+ on:
4
+ release:
5
+ types: [created]
6
+
7
+ jobs:
8
+ publish:
9
+ runs-on: ubuntu-latest
10
+
11
+ steps:
12
+ - name: Checkout repository
13
+ uses: actions/checkout@v4
14
+
15
+ - name: Setup Node.js
16
+ uses: actions/setup-node@v4
17
+ with:
18
+ node-version: '22.x'
19
+ registry-url: 'https://registry.npmjs.org'
20
+
21
+ - name: Clean cache
22
+ run: npm cache clean --force
23
+
24
+ - name: Verify Node.js version
25
+ run: node -v
26
+
27
+ - name: Verify Npm version
28
+ run: npm -v
29
+
30
+ - name: Install dependencies
31
+ run: npm ci
32
+
33
+ - name: Link matterbridge in the project
34
+ run: npm link matterbridge
35
+
36
+ - name: Lint
37
+ run: npm run lint
38
+
39
+ - name: Test
40
+ run: npm t
41
+
42
+ # - name: Build
43
+ # run: npm run build
44
+
45
+ - name: Publish to npm
46
+ run: npm publish
47
+ env:
48
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
@@ -0,0 +1,104 @@
1
+ # Contributing
2
+
3
+ Contributions are welcomed in this repository, not only code changes but also
4
+ opening issues, helping with questions and otherwise giving feedback.
5
+
6
+ Please note that this project has a code of conduct, please read it before
7
+ jumping in and contributing to this project.
8
+
9
+ ## Working with the code
10
+
11
+ This project doesn't have a super-strict code-style, contributions are expected
12
+ to look about the same as existing code. Tabs are used for indentation except
13
+ for Markdown and a few JSON-files. Install an [Editorconfig](http://editorconfig.org/)
14
+ plugin in your editor to simplify white-space handling.
15
+
16
+ Before submitting a pull request, run `npm test` to run a few basic linting
17
+ tests. If you have an ESLint-plugin in your editor it should run automatically
18
+ while you work.
19
+
20
+ ## Issues and helping out with questions
21
+
22
+ This project is used both directly by developers and indirectly by end-users
23
+ via other projects. When opening an issue try to include as much details about
24
+ how the library is used, such as if its being used via a third-party project
25
+ and the version being used.
26
+
27
+ If you have know-how about the API and library please feel free to answer any
28
+ questions you can. If you have the same issue as someone else, try adding a few
29
+ more details if you can.
30
+
31
+ ## Code of Conduct
32
+
33
+ ### Our Pledge
34
+
35
+ In the interest of fostering an open and welcoming environment, we as
36
+ contributors and maintainers pledge to making participation in our project and
37
+ our community a harassment-free experience for everyone, regardless of age, body
38
+ size, disability, ethnicity, gender identity and expression, level of experience,
39
+ nationality, personal appearance, race, religion, or sexual identity and
40
+ orientation.
41
+
42
+ ### Our Standards
43
+
44
+ Examples of behavior that contributes to creating a positive environment
45
+ include:
46
+
47
+ * Using welcoming and inclusive language
48
+ * Being respectful of differing viewpoints and experiences
49
+ * Gracefully accepting constructive criticism
50
+ * Focusing on what is best for the community
51
+ * Showing empathy towards other community members
52
+
53
+ Examples of unacceptable behavior by participants include:
54
+
55
+ * The use of sexualized language or imagery and unwelcome sexual attention or
56
+ advances
57
+ * Trolling, insulting/derogatory comments, and personal or political attacks
58
+ * Public or private harassment
59
+ * Publishing others' private information, such as a physical or electronic
60
+ address, without explicit permission
61
+ * Other conduct which could reasonably be considered inappropriate in a
62
+ professional setting
63
+
64
+ ### Our Responsibilities
65
+
66
+ Project maintainers are responsible for clarifying the standards of acceptable
67
+ behavior and are expected to take appropriate and fair corrective action in
68
+ response to any instances of unacceptable behavior.
69
+
70
+ Project maintainers have the right and responsibility to remove, edit, or
71
+ reject comments, commits, code, wiki edits, issues, and other contributions
72
+ that are not aligned to this Code of Conduct, or to ban temporarily or
73
+ permanently any contributor for other behaviors that they deem inappropriate,
74
+ threatening, offensive, or harmful.
75
+
76
+ ### Scope
77
+
78
+ This Code of Conduct applies both within project spaces and in public spaces
79
+ when an individual is representing the project or its community. Examples of
80
+ representing a project or community include using an official project e-mail
81
+ address, posting via an official social media account, or acting as an appointed
82
+ representative at an online or offline event. Representation of a project may be
83
+ further defined and clarified by project maintainers.
84
+
85
+ ### Enforcement
86
+
87
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
88
+ reported by contacting the main developer at a@holstenson.se. All
89
+ complaints will be reviewed and investigated and will result in a response that
90
+ is deemed necessary and appropriate to the circumstances. The project team is
91
+ obligated to maintain confidentiality with regard to the reporter of an incident.
92
+ Further details of specific enforcement policies may be posted separately.
93
+
94
+ Project maintainers who do not follow or enforce the Code of Conduct in good
95
+ faith may face temporary or permanent repercussions as determined by other
96
+ members of the project's leadership.
97
+
98
+ ### Attribution
99
+
100
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
101
+ available at [http://contributor-covenant.org/version/1/4][version]
102
+
103
+ [homepage]: http://contributor-covenant.org
104
+ [version]: http://contributor-covenant.org/version/1/4/
package/LICENSE.md ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2025 Alejandro Fernandez Haro
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,216 @@
1
+ # miIO Device Library
2
+
3
+ [![npm version](https://badge.fury.io/js/node-miio.svg)](https://badge.fury.io/js/node-miio)
4
+
5
+ > The intention of this library is to update the [miio](https://github.com/aholstenson/miio). Since the original repo is no longer maintained, nor reviewing the existing PRs, I decided to create a new one.
6
+
7
+ Control Mi Home devices that implement the miIO protocol, such as the
8
+ Mi Air Purifier, Mi Robot Vacuum and Mi Smart Socket. These devices are commonly
9
+ part of what Xiaomi calls the Mi Ecosystem which is branded as MiJia.
10
+
11
+ `node-miio` is [MIT-licensed](LICENSE.md) and requires at least Node 22.0.0.
12
+
13
+ **Note:** Since 0.15.0 this library has been rewritten to use [abstract-things](https://github.com/tinkerhub/abstract-things)
14
+ as its base. The API of devices will have changed, and some bugs are to be
15
+ expected. Testing and feedback on the new API is welcome, please open issues
16
+ as needed.
17
+
18
+ ## Devices types
19
+
20
+ The intent of this library is to support all miIO-compatible devices and to
21
+ provide an easy to use API for them. The library maps specific device models to
22
+ generic device types with well defined capabilities to simplify interacting with
23
+ them.
24
+
25
+ Currently supported devices are:
26
+
27
+ * Air Purifiers (1, 2 and Pro)
28
+ * Mi Humidifier
29
+ * Mi Smart Socket Plug and Power Strips
30
+ * Mi Robot Vacuum (V1 and V2)
31
+ * Mi Smart Home Gateway (Aqara) and accessories - switches, sensors, etc
32
+ * Philips Light Bulb and Eyecare Lamp
33
+ * Yeelights (White Bulb, Color Bulb, Desk Lamp and Strip)
34
+
35
+ See [documentation for devices](docs/devices/README.md) for information about
36
+ the types, their API and supported device models. You can also check
37
+ [Missing devices](docs/missing-devices.md) if you want to know what you can do
38
+ to help this library with support for your device.
39
+
40
+ ## Installation
41
+
42
+ To install into your project:
43
+
44
+ ```
45
+ npm install node-miio
46
+ ```
47
+
48
+ To install globally for access to the command line tool:
49
+
50
+ ```
51
+ npm install -g node-miio
52
+ ```
53
+
54
+ ## Usage
55
+
56
+ ```javascript
57
+ const miio = require('node-miio');
58
+ ```
59
+
60
+ Resolve a handle to the device:
61
+
62
+ ```javascript
63
+ // Resolve a device, resolving the token automatically or from storage
64
+ miio.device({ address: '192.168.100.8' })
65
+ .then(device => console.log('Connected to', device))
66
+ .catch(err => handleErrorHere);
67
+
68
+ // Resolve a device, specifying the token (see below for how to get the token)
69
+ miio.device({ address: '192.168.100.8', token: 'token-as-hex' })
70
+ .then(device => console.log('Connected to', device))
71
+ .catch(err => handleErrorHere);
72
+ ```
73
+
74
+ Call methods to interact with the device:
75
+
76
+ ```javascript
77
+ // Switch the power of the device
78
+ device.togglePower()
79
+ .then(on => console.log('Power is now', on))
80
+ .catch(err => handleErrorHere);
81
+
82
+ // Using async/await
83
+ await device.togglePower();
84
+ ```
85
+
86
+ Listen to events such as property changes and actions:
87
+
88
+ ```javascript
89
+ // Listen for power changes
90
+ device.on('power', power => console.log('Power changed to', power));
91
+
92
+ // The device is available for event handlers
93
+ const handler = ({ action }, device) => console.log('Action', action, 'performed on', device);
94
+ device1.on('action', handler);
95
+ device2.on('action', handler);
96
+ ```
97
+
98
+ Capabilities and types are used to hint about what a device can do:
99
+
100
+ ```javascript
101
+ if(device.matches('cap:temperature')) {
102
+ console.log(await device.temperature());
103
+ }
104
+
105
+ if(device.matches('cap:switchable-power')) {
106
+ device.setPower(false)
107
+ .then(console.log)
108
+ .catch(console.error);
109
+ }
110
+ ```
111
+
112
+ If you are done with the device call `destroy` to stop all network traffic:
113
+
114
+ ```javascript
115
+ device.destroy();
116
+ ```
117
+
118
+ ## Tokens and device management
119
+
120
+ A few miIO devices send back their token during a handshake and can be used
121
+ without figuring out the token. Most devices hide their token, such as
122
+ Yeelights and the Mi Robot Vacuum.
123
+
124
+ There is a command line tool named `miio` that helps with finding and storing
125
+ tokens. See [Device management](docs/management.md) for details
126
+ and common use cases.
127
+
128
+ ## Discovering devices
129
+
130
+ Use `miio.devices()` to look for and connect to devices on the local network.
131
+ This method of discovery will tell you directly if a device reveals its token
132
+ and can be auto-connected to. If you do not want to automatically connect to
133
+ devices you can use `miio.browse()` instead.
134
+
135
+ Example using `miio.devices()`:
136
+
137
+ ```javascript
138
+ const devices = miio.devices({
139
+ cacheTime: 300 // 5 minutes. Default is 1800 seconds (30 minutes)
140
+ });
141
+
142
+ devices.on('available', device => {
143
+ if(device.matches('placeholder')) {
144
+ // This device is either missing a token or could not be connected to
145
+ } else {
146
+ // Do something useful with device
147
+ }
148
+ });
149
+
150
+ devices.on('unavailable', device => {
151
+ // Device is no longer available and is destroyed
152
+ });
153
+ ```
154
+
155
+ `miio.devices()` supports these options:
156
+
157
+ * `cacheTime`, the maximum amount of seconds a device can be unreachable before it becomes unavailable. Default: `1800`
158
+ * `filter`, function used to filter what devices are connected to. Default: `reg => true`
159
+ * `skipSubDevices`, if sub devices on Aqara gateways should be skipped. Default: `false`
160
+ * `useTokenStorage`, if tokens should be fetched from storage (see device management). Default: `true`
161
+ * `tokens`, object with manual mapping between ids and tokens (advanced, use [Device management](docs/management.md) if possible)
162
+
163
+ See [Advanced API](docs/advanced-api.md) for details about `miio.browse()`.
164
+
165
+ ## Device API
166
+
167
+ Check [documentation for devices](docs/devices/README.md) for details about
168
+ the API for supported devices. Detailed documentation of the core API is
169
+ available in the section [Using things in the abstract-things documentation](http://abstract-things.readthedocs.io/en/latest/using-things.html).
170
+
171
+ ## Library versioning and API stability
172
+
173
+ This library uses [semantic versioning](http://semver.org/) with an exception
174
+ being that the API for devices is based on their type and capabilities and not
175
+ their model.
176
+
177
+ This means that a device can have methods removed if its type or capabilities
178
+ change, which can happen if a better implementation is made available for the
179
+ model. When working with the library implement checks against type and
180
+ capabilities for future compatibility within the same major version of `miio`.
181
+
182
+ Capabilities can be considered stable across major versions, if a device
183
+ supports `power` no minor or patch version will introduce `power-mega` and
184
+ replace `power`. If new functionality is needed the new capability will be
185
+ added along side the older one.
186
+
187
+ ## Reporting issues
188
+
189
+ [Reporting issues](docs/reporting-issues.md) contains information that is
190
+ useful for making any issue you want to report easier to fix.
191
+
192
+ ## Debugging
193
+
194
+ The library uses [debug](https://github.com/visionmedia/debug) with two
195
+ namespaces, `miio` is used for packet details and network discovery and devices
196
+ use the `thing:miio` namespace. These are controlled via the `DEBUG`
197
+ environment flag. The flag can be set while running the miio command or any
198
+ NodeJS script:
199
+
200
+ Show debug info about devices during discovery:
201
+
202
+ ```
203
+ $ DEBUG=thing\* miio discover
204
+ ```
205
+
206
+ To activate both namespaces set `DEBUG` to both:
207
+
208
+ ```
209
+ $ DEBUG=miio\*,thing\* miio discover
210
+ ```
211
+
212
+ ## Protocol documentation
213
+
214
+ This library is based on the documentation provided by OpenMiHome. See https://github.com/OpenMiHome/mihome-binary-protocol for details. For details
215
+ about how to figure out the commands for new devices look at the
216
+ [documentation for protocol and commands](docs/protocol.md).