Compare commits

...
431 Commits
Author SHA1 Message Date
Koen Kanters a14feef8c1 chore(dev): release 2.2.1 (#26934) 2025-04-04 22:12:32 +02:00
Koen Kanters d6a5128aa8 fix: Fix settings being overwriting when env var is set to a ref (#26988) 2025-04-04 21:57:11 +02:00
Koen Kanters 9d0ee03f38 fix(ignore): update zigbee-herdsman-converters to 23.20.1 (#26989) 2025-04-04 19:37:12 +00:00
Koen Kanters 37927e698a fix(ignore): update zigbee-herdsman-converters to 23.20.0 (#26978) 2025-04-03 20:27:14 +00:00
Koen Kantersandnurikk 89cdb1f180 fix(ignore): update zigbee2mqtt-frontend to 0.9.7 (#26976)
Co-authored-by: nurikk <1525421+nurikk@users.noreply.github.com>
2025-04-03 18:41:06 +00:00
Koen Kanters e90afb286a fix(ignore): update zigbee-herdsman-converters to 23.19.1 (#26961) 2025-04-02 20:07:16 +00:00
Koen Kantersandnurikk fb4d01ebce fix(ignore): update zigbee2mqtt-frontend to 0.9.6 (#26959)
Co-authored-by: nurikk <1525421+nurikk@users.noreply.github.com>
2025-04-02 21:29:10 +02:00
Nerivec 9e481fe5e5 fix: Fix issue with some settings when migrating from 1.x.x to 2.2.x (#26947) 2025-04-02 19:54:00 +02:00
Koen Kanters 0b7d0039f7 fix(ignore): update zigbee-herdsman-converters to 23.19.0 (#26938) 2025-04-01 20:13:05 +00:00
Koen Kanters 5257c7eae1 chore: prettier ignore release-notes.md 2025-04-01 21:06:04 +02:00
github-actions[bot] 952116d327 chore: promote to dev 2025-04-01 19:00:47 +00:00
Koen Kanters c5c07e7dfe chore(dev): release 2.2.0 (#26565) 2025-04-01 20:43:39 +02:00
Koen Kanters e02bbdcf0b fix(ignore): update dependencies (#26911) 2025-04-01 20:37:25 +02:00
Koen Kanters 69f0840feb fix(ignore): update zigbee-herdsman-converters to 23.18.0 (#26933) 2025-04-01 18:30:07 +00:00
Koen Kanters ea04f19709 fix(ignore): update zigbee-herdsman-converters to 23.17.0 (#26925) 2025-03-31 19:14:49 +00:00
Koen Kanters 9fe38b1677 fix(ignore): update zigbee-herdsman to 3.4.11 (#26924) 2025-03-31 18:50:39 +00:00
Koen Kanters 6e6a989159 fix(ignore): update zigbee-herdsman-converters to 23.16.0 (#26918) 2025-03-30 19:59:11 +00:00
Koen Kanters b52cfd87c3 fix(ignore): update zigbee-herdsman to 3.4.10 (#26908) 2025-03-29 17:23:23 +00:00
Koen Kanters 516425b44b fix(ignore): update zigbee-herdsman-converters to 23.15.0 (#26900) 2025-03-28 19:39:14 +00:00
Koen Kanters 6a65769060 fix(ignore): update zigbee-herdsman-converters to 23.14.0 (#26886) 2025-03-27 19:27:28 +00:00
Koen Kanters 0a41af5dd2 fix(ignore): update zigbee-herdsman to 3.4.9 (#26877) 2025-03-26 22:18:27 +00:00
clockbrain 267c399047 fix: Add device name to no converter available error message (#26867) 2025-03-26 21:45:33 +01:00
Koen Kanters f824295b7e fix(ignore): update zigbee-herdsman to 3.4.8 (#26866) 2025-03-25 22:50:21 +00:00
Koen Kanters 341c453657 fix(ignore): Fix HA addon onboarding (#26862) 2025-03-25 22:06:00 +01:00
Koen Kanters c5c7f70ad6 fix(ignore): update zigbee-herdsman-converters to 23.13.0 (#26861) 2025-03-25 19:41:18 +00:00
Koen Kanters 440e3a0094 fix(ignore): update zigbee-herdsman to 3.4.7 (#26859) 2025-03-25 18:50:46 +00:00
Koen Kanters 53e8226f0a fix(ignore): update zigbee-herdsman-converters to 23.12.0 (#26835) 2025-03-23 21:06:27 +00:00
Koen Kanters 7a1621a906 fix(ignore): fix EACCES: permission denied, mkdir '/app/dist/external_converters' 2025-03-23 22:02:17 +01:00
Koen Kanters 9e0c84beed fix(ignore): update zigbee-herdsman to 3.4.6 (#26834) 2025-03-23 20:59:26 +00:00
Koen Kanters 5f9b49d66f Merge remote-tracking branch 'origin/master' into dev 2025-03-23 21:54:59 +01:00
Koen Kanters 93d0ee44ba fix(ignore): update dependencies (#26833) 2025-03-23 21:46:52 +01:00
Koen Kanters fc35ac5737 chore: fix update-dependencies 2025-03-23 21:44:40 +01:00
Koen Kanters 2b90d7b3b9 fix(ignore): update zigbee-herdsman to 3.4.5 (#26826) 2025-03-22 19:24:20 +00:00
Nerivec f2a1c9a2be fix(gnore): Onboarding fixes (#26824) 2025-03-22 16:11:06 +01:00
Koen Kanters 0714582eb9 fix(ignore): update zigbee-herdsman to 3.4.4 (#26821) 2025-03-21 21:46:26 +00:00
Koen Kanters 5288549744 fix(ignore): update zigbee-herdsman-converters to 23.11.0 (#26820) 2025-03-21 20:36:17 +00:00
Nerivec 05d6f08a0d feat: Onboarding (#26608) 2025-03-21 21:30:19 +01:00
Koen Kanters 6f1266fd0e chore: remove docker-compose.yml 2025-03-21 21:10:30 +01:00
Koen Kantersandnurikk 9792466aac fix(ignore): update zigbee2mqtt-frontend to 0.9.5 (#26814)
Co-authored-by: nurikk <1525421+nurikk@users.noreply.github.com>
2025-03-21 10:52:52 +00:00
Koen Kanters 52c255bcc2 fix(ignore): update zigbee-herdsman-converters to 23.10.0 (#26792) 2025-03-20 20:35:53 +00:00
Nerivec badf107991 feat: Use dynamic import for external JS (#26522) 2025-03-20 21:08:32 +01:00
Simon f872a5180c feat: Frontend: support filtering notifications (#26805) 2025-03-20 20:59:58 +01:00
Koen Kanters 4f2b052016 fix(ignore): update zigbee-herdsman to 3.4.3 (#26802) 2025-03-19 20:30:37 +00:00
Koen Kanters ef5b3de5e2 chore: enable provenance for release Docker image 2025-03-18 21:20:07 +01:00
Koen Kanters ff7b09eb99 chore: fix provenance 2025-03-18 21:16:36 +01:00
Koen Kanters 1baafbb6cf chore: enable provenance for NPM package 2025-03-18 20:54:49 +01:00
Koen Kanters 17ce9f4c52 chore: enable provenance for NPM package 2025-03-18 20:52:05 +01:00
Koen Kanters e6ce7ecdc1 chore: disable automatic pnpm updates 2025-03-18 20:50:24 +01:00
Koen Kanters 36ba1350a9 chore: remove codeql (enable basic) 2025-03-18 20:45:50 +01:00
Koen Kanters 582bf7d2c6 fix(ignore): update zigbee-herdsman to 3.4.2 (#26789) 2025-03-18 19:18:19 +00:00
Koen Kanters 46fd04364d fix(ignore): update zigbee-herdsman to 3.4.1 (#26779) 2025-03-17 20:22:38 +00:00
Alexander D. Kanevskiy 747f6ac476 fix: Home Assistant: fix electrical measurements sensors category (#26769) 2025-03-17 21:03:05 +01:00
Ömer Güven 6e781eafae fix(ignore): Make update check less verbose (#26774) 2025-03-17 20:43:56 +01:00
Koen Kanters 325199d445 fix(ignore): update zigbee-herdsman-converters to 23.8.0 (#26761) 2025-03-16 22:16:34 +01:00
Koen Kanters e602dc849a fix(ignore): update dependencies (#26751) 2025-03-16 22:09:37 +01:00
Koen Kanters ad0a222df1 Merge branch 'master' into dev 2025-03-16 21:58:34 +01:00
Koen Kanters 6d6372ccf0 chore: Skip updating dependencies on forks 2025-03-16 21:27:40 +01:00
NerivecandKoen Kanters 92955c02a4 feat: Support ZigBee on Host adapter (#26742)
Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
2025-03-15 08:36:52 +01:00
Koen Kanters e5a80e3b1b fix(ignore): update zigbee-herdsman to 3.4.0 (#26744) 2025-03-15 07:26:13 +00:00
Ömer Güven fb48bd0cb9 fix(ignore): Fix update check (#26723) 2025-03-15 08:09:55 +01:00
Nerivec db578d9d11 fix: Use dynamic import for optional extensions (#26735) 2025-03-15 08:07:56 +01:00
Koen Kanters 8d2ef6df06 fix(ignore): update zigbee-herdsman-converters to 23.7.0 (#26733) 2025-03-13 19:13:26 +00:00
Koen Kanters 5db115057e fix(ignore): Module NodeNext (#26719) 2025-03-12 21:37:32 +01:00
Koen Kanters be722a0500 fix(ignore): Move to Node16 module (#26717) 2025-03-12 20:01:11 +00:00
BlueMöhre e3985002c2 fix: Fix bridge offline message format for frontend (#26707) 2025-03-12 20:23:40 +01:00
Koen Kanters fcbb355f47 Revert "fix(ignore): Move to Node16 module (#26704)"
This reverts commit ea5e0d47a5.
2025-03-12 07:25:37 +01:00
Koen Kanters 06ba62b833 fix(ignore): update zigbee-herdsman-converters to 23.6.0 (#26691) 2025-03-11 21:55:32 +01:00
Koen Kanters ea5e0d47a5 fix(ignore): Move to Node16 module (#26704) 2025-03-11 21:51:04 +01:00
Koen Kanters 8378949e1d fix(ignore): update zigbee-herdsman to 3.3.2 (#26702) 2025-03-11 19:52:35 +00:00
Koen Kanters 78cde99469 chore: Small improvements to CI pipelines (#26690) 2025-03-10 21:09:20 +01:00
Koen Kanters a5195a6eee fix(ignore): update zigbee-herdsman to 3.3.1 (#26674) 2025-03-09 19:32:28 +00:00
Koen Kanters fb9816659b fix(ignore): update dependencies (#26662) 2025-03-09 20:22:16 +01:00
Nerivec f6f7ba68c9 fix: Write ZIGBEE2MQTT_CONFIG_* envs to yaml as overrides (#26653) 2025-03-08 14:32:52 +01:00
Ömer Güven 1b6ad46462 fix: Check if new commits are available in update.sh (#26631) 2025-03-08 14:14:25 +01:00
Koen Kanters ad32fec1d0 fix(ignore): Fix tests failing when zhc is linked via pnpm link (#26630) 2025-03-06 20:27:17 +00:00
Koen Kanters c603f42c88 fix(ignore): update zigbee-herdsman-converters to 23.4.0 (#26629) 2025-03-06 20:20:30 +00:00
Koen Kanters ed7e7c7e53 fix(ignore): update zigbee-herdsman to 3.3.0 (#26628) 2025-03-06 19:20:29 +00:00
Koen Kanters 3941692665 fix(ignore): update zigbee-herdsman-converters to 23.3.0 (#26598) 2025-03-03 20:39:41 +00:00
Nerivec 8775ceb0c1 fix: General cleanup (#26595) 2025-03-03 21:12:57 +01:00
11fb858ff2 fix(ignore): update dependencies (#26554)
Co-authored-by: Simon Rettberg <srett@users.noreply.github.com>
Co-authored-by: Nerivec <62446222+Nerivec@users.noreply.github.com>
2025-03-02 22:21:46 +01:00
Koen Kanters 4fa794a639 fix(ignore): Reduce chaining in homeassistant.ts (#26582) 2025-03-02 20:47:23 +00:00
Koen Kanters 1fe2d8dae6 fix(ignore): Remove uncessary extensions spread (#26581) 2025-03-02 20:30:36 +00:00
bfeac07c4c fix: Implement systemd-notify directly (#26456)
Co-authored-by: Nerivec <62446222+Nerivec@users.noreply.github.com>
Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
2025-03-02 08:59:28 +01:00
github-actions[bot] b6c0f4e5db chore: promote to dev 2025-03-02 07:58:15 +00:00
Koen Kanters ba337bd329 chore(dev): release 2.1.3 (#26550) 2025-03-02 08:44:41 +01:00
Koen Kanters 7e7057eccd fix: Home Assistant: fix brightness only lights (#26560) 2025-03-02 08:43:21 +01:00
github-actions[bot] cbcd32064c chore: promote to dev 2025-03-01 20:51:01 +00:00
Koen Kanters d2d7ebeed9 chore(dev): release 2.1.2 (#26217) 2025-03-01 21:37:52 +01:00
Koen Kanters 9f72c5c455 fix(ignore): update zigbee-herdsman-converters to 23.2.0 (#26549) 2025-03-01 20:19:30 +00:00
Koen Kanters e050e209b0 fix: Fix Docker build hanging (#26547) 2025-03-01 21:13:30 +01:00
Koen Kanters 29d25aed66 fix: Make compatible with pnpm 10 (#26546) 2025-03-01 20:56:46 +01:00
Koen Kanters 063aba1b6c fix: Home Assistant: add brightness to supported_color_modes (#26545) 2025-03-01 20:11:41 +01:00
Tom Davis e8031efd2d fix: Add Home Assistant device class to external_temperature_input entity (#26535) 2025-03-01 17:43:30 +01:00
Koen Kanters 102857a7fe fix(ignore): update zigbee-herdsman-converters to 23.1.1 (#26510) 2025-02-26 06:57:43 +00:00
Koen Kanters db7b51ac80 fix(ignore): update zigbee-herdsman-converters to 23.1.0 (#26509) 2025-02-25 21:32:49 +00:00
Koen Kanters d2166f2f4f fix(ignore): update dependencies (#26478) 2025-02-24 21:31:56 +00:00
ghozandKoen Kanters 07145accba fix(ignore): Add publish to meta of toZigbee converters (#26490)
Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
2025-02-24 22:29:00 +01:00
Koenkk 44f9fe7a14 fix(ignore): update zigbee-herdsman-converters to 23.0.0 2025-02-24 22:29:00 +01:00
Koen Kanters d522d2cd18 fix(ignore): remove obsolete @types/eslint__js 2025-02-24 22:21:55 +01:00
Koen Kanters 4e650b72cd fix(ignore): update zigbee-herdsman to 3.2.7 (#26498) 2025-02-24 19:45:59 +00:00
ghoz a6072adcd0 chore: exclude cache, editor tmp files (#26497) 2025-02-24 20:40:25 +01:00
Koen Kanters 0d193ebac2 Revert "chore: bump pnpm"
This reverts commit 24779e3476.
2025-02-23 10:05:39 +01:00
Koen Kanters ede1a470dd chore: bump node version in workflows 2025-02-22 21:09:34 +01:00
Koen Kanters 24779e3476 chore: bump pnpm 2025-02-22 21:07:31 +01:00
Koen Kanters 67e1a02525 fix(ignore): update zigbee-herdsman-converters to 22.2.0 (#26469) 2025-02-22 13:08:17 +00:00
Lorenz Brun 7ce5b581aa fix: Home Assistant: support speed-controlled fans (#24483) 2025-02-21 20:24:08 +01:00
Koen Kanters 40f41477e7 fix(ignore): update zigbee-herdsman-converters to 22.1.0 (#26445) 2025-02-20 12:42:13 +00:00
Koen Kanters 382c89c49e chore: fix changelog generator 2025-02-18 16:15:46 +01:00
Koen KantersandNerivec 7d63a9a0f2 fix(ignore): update zigbee-herdsman-converters to 22.0.1 (#26418)
Co-authored-by: Nerivec <62446222+Nerivec@users.noreply.github.com>
2025-02-18 14:50:01 +00:00
Koen Kanters 93af062a9b fix(ignore): update zigbee-herdsman-converters to 21.38.0 (#26400) 2025-02-17 13:36:53 +00:00
Koen Kanters 758c56647d fix(ignore): update dependencies (#26383) 2025-02-17 14:30:20 +01:00
FilipandFilip Vilicic 845e03e48a fix: Allow configuring number of folders of old logs to keep (#26398)
Co-authored-by: Filip Vilicic <kusefin@hotmail.com>
2025-02-17 14:04:39 +01:00
Koen Kanters dec5ca8b3a fix(ignore): update zigbee-herdsman-converters to 21.37.0 (#26367) 2025-02-14 20:09:39 +00:00
XS400DOHC 1e656c2114 fix: Home Assistant: add missing device_class for some current and power entities (#26324) 2025-02-13 17:52:27 +01:00
Koen Kanters ba76533c2f fix(ignore): update zigbee-herdsman-converters to 21.36.0 (#26333) 2025-02-12 20:41:22 +00:00
Koen Kanters 30d13d6e5e fix(ignore): update zigbee-herdsman-converters to 21.35.0 (#26305) 2025-02-10 20:46:28 +00:00
Koen Kanters 49fe147921 fix(ignore): update zigbee-herdsman to 3.2.6 (#26303) 2025-02-10 19:57:41 +00:00
Koen Kanters 7a0964ebc9 fix(ignore): update zigbee-herdsman-converters to 21.34.0 (#26285) 2025-02-09 20:18:19 +00:00
Rob Coleman b35cdc3474 fix: Fix HA MQTT dict entry indicating that an update is progress (#26267) 2025-02-09 14:59:08 +01:00
Evaldas Auryla 5e55ef0454 fix: Support FreeBSD for update.sh (#26261) 2025-02-09 14:50:36 +01:00
Koen Kanters 3e3cefff1b fix(ignore): update dependencies (#26266) 2025-02-09 14:48:11 +01:00
Koen Kanters 97c74852c9 fix(ignore): update zigbee-herdsman-converters to 21.33.0 (#26258) 2025-02-08 14:06:29 +00:00
Koen Kanters 8542b35706 fix(ignore): update zigbee-herdsman-converters to 21.32.0 (#26245) 2025-02-07 19:32:33 +00:00
ramon18 1eda69a50a fix: Home Assistant: add is_updating attribute (#26231) 2025-02-07 20:06:28 +01:00
github-actions[bot] 16cecd917e chore: promote to dev 2025-02-06 19:43:25 +00:00
Koen Kanters 1accb8b66b chore(dev): release 2.1.1 (#26135) 2025-02-06 20:33:58 +01:00
Koen Kanters a3682b5fc7 chore: fix update_dep 2025-02-06 20:31:47 +01:00
Koen Kanters cfea8b76e2 Update zigbee-herdsman-converters to 21.31.0 (#26216) 2025-02-06 19:23:29 +00:00
Koen Kanters 9e28818c53 Update zigbee-herdsman-converters to 21.30.0 (#26203) 2025-02-05 20:38:05 +00:00
Koen Kanters bfbbfcda37 Update zigbee-herdsman-converters to 21.29.0 (#26183) 2025-02-04 20:25:50 +00:00
Rohan KapoorandKoen Kanters e4e4f16ec8 fix: Rename internal_temperature to internalTemperature to correct typo (#26114)
Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
2025-02-03 20:09:24 +01:00
Koen Kanters 75c3c5e57f chore: Update release_please.yml 2025-02-02 21:11:51 +01:00
Koen Kanters 6a15328001 Update zigbee-herdsman-converters to 21.28.0 (#26134) 2025-02-02 20:08:59 +00:00
Koen Kanters b960775160 fix(ignore): update dependencies (#26126) 2025-02-02 15:06:40 +01:00
Koen Kanters 41ba32db49 chore: merge master to dev 2025-02-02 15:03:46 +01:00
Koen Kanters 83ff4a7684 chore: Add packageManager to package.json (#26106) 2025-02-02 15:03:26 +01:00
Koen Kanters 98b5a60886 chore: Add packageManager to package.json (#26106) 2025-02-01 22:14:03 +01:00
github-actions[bot] b34723db36 chore: promote to dev 2025-02-01 20:34:39 +00:00
Koen Kanters 5e9d53f0aa chore(dev): release 2.1.0 (#25439) 2025-02-01 21:23:35 +01:00
Koen Kanters 8c7961de1f Update zigbee-herdsman-converters to 21.27.1 (#26102) 2025-02-01 20:01:44 +00:00
Koen Kanters ff466f817f chore: merge master to dev 2025-02-01 20:57:03 +01:00
Koen Kanters c7a7c3d8c2 chore: lint codeql.yaml 2025-02-01 20:56:47 +01:00
Koen Kanters 7540a0d02f chore: merge master to dev 2025-02-01 20:54:57 +01:00
Koen Kanters 33d5f0595d chore: Create codeql.yaml 2025-02-01 20:54:42 +01:00
Koen Kanters c4510d7382 chore: merge master to dev 2025-02-01 20:52:04 +01:00
Koen Kanters e670524241 chore: remove codeql.yaml 2025-02-01 20:51:44 +01:00
Koen Kanters 75f8ed07ed Update zigbee-herdsman to 3.2.5 (#26101) 2025-02-01 19:47:49 +00:00
Koen Kanters 0706a5e039 Update zigbee-herdsman-converters to 21.27.0 (#26071) 2025-01-30 20:15:53 +00:00
Koen Kanters 32329695d0 Update zigbee-herdsman-converters to 21.26.0 (#26043) 2025-01-28 18:37:44 +00:00
Tatham Oddie cdbd55320a fix: Add Home Assistant device class for flow rates (e.g., Sonoff SWV) (#26035) 2025-01-28 19:12:00 +01:00
Koen Kanters 7b199f898e Update zigbee-herdsman-converters to 21.25.0 (#26032) 2025-01-27 20:00:24 +00:00
Rob Coleman 0462b94f5a fix: Only use endpoint-specific states if the device definition uses them (#26019) 2025-01-27 20:54:01 +01:00
Koen Kanters 85460cd5ce fix(ignore): update dependencies (#26005) 2025-01-27 19:44:50 +01:00
Koen Kanters 420a9f2ff6 Update zigbee-herdsman-converters to 21.24.0 (#26000) 2025-01-25 19:04:47 +00:00
gracianodias3 5cf4547fe0 fix: Allow to set throttle via frontend (#25997) 2025-01-25 20:00:09 +01:00
Koen Kanters 2af65cf28e fix: Fix duplicate action publish when advanced.output has attribute (#25963) 2025-01-23 21:01:56 +01:00
Koen Kanters e9f74f0958 Update zigbee-herdsman-converters to 21.23.0 (#25951) 2025-01-22 19:28:32 +00:00
Koen Kanters 2567b4c39e Update zigbee-herdsman to 3.2.4 (#25919) 2025-01-20 21:01:53 +00:00
Koen Kanters ac1671609c Update zigbee-herdsman-converters to 21.22.0 (#25918) 2025-01-20 20:58:50 +00:00
Koen Kanters 8da5fa663d fix(ignore): update dependencies (#25890) 2025-01-19 21:23:36 +01:00
Koen Kanters 3d3c68b54a Update zigbee-herdsman-converters to 21.21.1 (#25872) 2025-01-19 13:04:23 +00:00
Koen Kanters 5e60f1dc8d fix(ignore): Remove adapter auto from schema (#25896) 2025-01-19 12:58:35 +00:00
Koen Kanters a1f74a312a Update zigbee-herdsman to 3.2.3 (#25895) 2025-01-19 12:32:15 +00:00
Koen Kanters b21167b326 Update zigbee-herdsman-converters to 21.20.0 (#25837) 2025-01-15 19:54:49 +00:00
Koen Kanters be17e698a1 Update zigbee-herdsman-converters to 21.19.0 (#25797) 2025-01-13 20:07:23 +00:00
Koen Kanters 7392557af2 Update zigbee-herdsman-converters to 21.18.0 (#25779) 2025-01-12 21:18:15 +00:00
Koen Kanters 360be57079 Update zigbee-herdsman-converters to 21.17.0 (#25770) 2025-01-12 13:43:05 +00:00
Koen Kanters 361f9eef8b Update zigbee-herdsman to 3.2.2 (#25769) 2025-01-12 13:17:20 +00:00
Koen Kanters 5b82d7e4df fix(ignore): update dependencies (#25760) 2025-01-12 14:12:38 +01:00
Koen Kanters 9daeba1f0a fix: Fix Failed to call 'Frontend' 'stop' (TypeError: Cannot read properties of undefined (reading 'close') https://github.com/Koenkk/zigbee2mqtt/issues/25715 2025-01-11 14:38:21 +01:00
Koen Kanters 2d5e7bf9ff fix: Fix Error: write after end error (#25737) 2025-01-11 14:11:26 +01:00
Koen Kanters b5bef45cb2 Update zigbee-herdsman-converters to 21.16.0 (#25735) 2025-01-10 20:15:20 +00:00
NerivecandKoen Kanters f1f73bd464 fix: Long startup time when OnEvent 'start' times out (repeatedly) (#25693)
Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
2025-01-09 22:22:19 +01:00
Nerivec 23b709cee2 fix(ignore): Ensure config example is never out of sync with settings (#25707) 2025-01-09 20:37:01 +01:00
Koen Kanters 31bdf02a72 Update zigbee-herdsman-converters to 21.15.0 (#25690) 2025-01-08 21:27:05 +00:00
Jens-Uwe MagerandNerivec 26ef565c8a feat: Add a settings option to log to console in json format (#25649)
Co-authored-by: Nerivec <62446222+Nerivec@users.noreply.github.com>
2025-01-08 19:22:26 +00:00
Koen Kanters 6f3524b251 fix(ignore): update dependencies (#25535) 2025-01-07 19:08:30 +00:00
Jan BouwhuisandKoen Kanters 318dc04132 feat: Allow Home Assistant OTA Update entity to show progress while updating (#25632)
Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
2025-01-07 20:04:45 +01:00
Koen Kanters bf354f2121 Update zigbee-herdsman-converters to 21.14.0 (#25635) 2025-01-06 21:15:58 +00:00
Koen Kanters 1667ba64ac fix: Remove invalid warn log_level https://github.com/nurikk/zigbee2mqtt-frontend/issues/2369 (#25634) 2025-01-06 21:45:55 +01:00
SteffenBeermann a28a5baf17 fix: Enable use of MQTT username without password (#25611) 2025-01-06 20:19:18 +01:00
Koen Kanters c33946ffc3 Update zigbee-herdsman-converters to 21.13.0 (#25563) 2025-01-05 13:59:54 +00:00
RoboMagus 0d30eb553b fix: Add semver tags to Docker releases (#25530) 2025-01-05 13:03:38 +00:00
Devin Buhl 43a5b24dcc fix: Container: remove modifying capabilities of /usr/bin/node (#25456) 2025-01-05 13:36:57 +01:00
Koen Kanters 3c46cfb2e9 chore: promote to dev 2025-01-03 22:00:11 +01:00
Koen Kanters 7972950c84 chore: fix ci.yaml 2025-01-03 21:59:40 +01:00
Koen Kanters 060ae99cff chore(dev): release 2.0.0 (#24988) 2025-01-03 20:45:37 +00:00
Koen Kanters b7847d8203 Update zigbee-herdsman-converters to 21.12.0 (#25437) 2025-01-03 20:06:03 +00:00
Zitrium31andKoen Kanters c586df8242 feat: Home Assistant: support endpoints for locks (#25359)
Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
2025-01-03 19:58:39 +00:00
Ric Klaren 7cf01e9882 fix(ignore): add cap_net_bind_service=+ep to /usr/bin/node (#25385) (#25424) 2025-01-03 20:55:34 +01:00
Koen Kanters 2d6b00bc69 Update zigbee-herdsman to 3.2.1 (#25416) 2025-01-02 13:22:14 +00:00
Koen Kanters 9055eccae9 Revert "fix: add cap_net_bind_service=+ep to /usr/bin/node (#25385)" (#25415) 2025-01-02 14:15:39 +01:00
Koen Kanters 2046c05fd4 chore: merge master to dev 2025-01-02 13:55:12 +01:00
Koen Kanters 902db5ea5b chore: update stale comment 2025-01-02 13:54:56 +01:00
Kenny Levinsen 60c37c11d0 fix: Home Assistant: use extended device identifiers (#25389) 2025-01-02 12:48:26 +00:00
Ric Klaren 61864f0fb2 fix: Add cap_net_bind_service=+ep to /usr/bin/node in Docker container (#25385) (#25387) 2025-01-02 13:39:38 +01:00
Koen Kanters 240ab43f2d Update zigbee-herdsman-converters to 21.11.0 (#25386) 2024-12-30 20:49:56 +00:00
Koen Kanters 2713125768 fix(ignore): remove illuminance_lux from cached payload https://github.com/Koenkk/zigbee2mqtt/issues/25379 2024-12-30 21:34:42 +01:00
Koen Kanters ed75553603 fix(ignore): Fix availabilty, frontend and homeassistant cannot be disabled via the frontend https://github.com/nurikk/zigbee2mqtt-frontend/issues/2339 2024-12-30 21:23:27 +01:00
Koen Kantersandnurikk e48cd22ab7 Update zigbee2mqtt-frontend to 0.9.4 (#25381)
Co-authored-by: nurikk <1525421+nurikk@users.noreply.github.com>
2024-12-30 16:21:27 +00:00
Koen Kanters 1e7e00e8dd fix(ignore): Improvements for #25299 (#25364) 2024-12-30 14:30:54 +01:00
Koen Kanters 02666e9139 fix(ignore): Fix 2 to 3 migration enabling frontend/availabilty/homeassistant when not set (#25349) 2024-12-30 14:22:18 +01:00
Koen Kanters 47d73eb23a fix(ignore): update dependencies (#25360) 2024-12-29 15:07:18 +01:00
NerivecandKoen Kanters 120a5b43e9 fix(ignore): Use new exports from ZH (#25350)
Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
2024-12-28 14:34:12 +00:00
Koen Kanters e8a5521a4c chore: remove onlythis 2024-12-28 15:25:12 +01:00
Koen Kanters 329b8c9c27 fix: Improve device icon serving (#25299) 2024-12-28 15:07:02 +01:00
Koen Kanters 0e19c227fc Update zigbee-herdsman-converters to 21.10.0 (#25348) 2024-12-28 13:35:19 +00:00
Koen Kantersandnurikk e868a38c09 Update zigbee2mqtt-frontend to 0.9.3 (#25347)
Co-authored-by: nurikk <1525421+nurikk@users.noreply.github.com>
2024-12-28 13:30:31 +00:00
Koen Kanters 301cb331d5 Update zigbee-herdsman to 3.2.0 (#25346) 2024-12-28 13:19:26 +00:00
Ricc68andRicc68 bcec40f84a fix: Fix URL in startup failed bailout message (#25339)
Co-authored-by: Ricc68 <Ricc68@local>
2024-12-28 13:59:50 +01:00
Koen Kanters 62e18a65f3 Update zigbee-herdsman-converters to 21.9.2 (#25316) 2024-12-25 09:38:01 +00:00
Koen Kanters b8e6385e05 chore: add prepack 2024-12-24 22:10:04 +01:00
Koen Kantersandnurikk b65cc0ee74 Update zigbee2mqtt-frontend to 0.9.2 (#25313)
Co-authored-by: nurikk <1525421+nurikk@users.noreply.github.com>
2024-12-24 13:41:24 +00:00
Koen Kanters eaafc7c28a Update zigbee-herdsman-converters to 21.8.0 (#25296) 2024-12-22 20:14:21 +00:00
Koen Kanters 2128e8323c fix(ignore): update dependencies (#25288) 2024-12-22 20:24:36 +01:00
NerivecandKoen Kanters 06f44533af feat: Use Vitest for testing (#25284)
Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
2024-12-22 20:19:45 +01:00
Rohan Kapoor 5d059b7bcb fix: Home Assistant: expose the internal_temperature sensor with the correct device class (#25289) 2024-12-22 19:15:34 +00:00
Koen Kanters 6eae15fb5e Update zigbee-herdsman to 3.1.1 (#25287) 2024-12-22 09:55:36 +00:00
Koen Kanters 2abf55ccf8 fix!: Change default homeassistant.status_topic to homeassistant/status (#25286) 2024-12-21 20:33:44 +00:00
Koen Kanters 803e9060ea Update zigbee-herdsman-converters to 21.7.0 (#25285) 2024-12-21 20:27:27 +00:00
Koen Kanters 7170215e96 fix: Fix log directories not being deleted (#25282) 2024-12-21 16:54:19 +01:00
Nerivec 758ab34747 fix(ignore): import using node: prefix for built-in (#25248) 2024-12-18 22:19:33 +01:00
Koen KantersandNerivec c3b4628a1d fix: Remove oneOf bool/obj from settings schema (#25195)
Co-authored-by: Nerivec <62446222+Nerivec@users.noreply.github.com>
2024-12-17 22:27:55 +01:00
Koen Kanters e7f8de1e94 Update zigbee-herdsman-converters to 21.6.0 (#25253) 2024-12-17 20:57:56 +00:00
NerivecandKoen Kanters 1c75eef16d fix(ignore): remove dep in favor of built-in (#25250)
Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
2024-12-17 21:31:21 +01:00
NerivecandKoen Kanters c88f630299 fix(ignore): overrides (#25246)
Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
2024-12-17 21:30:00 +01:00
Tobias G ef6524b93f fix: Fix dual cover Home Assistant discovery (#25240) 2024-12-17 21:25:30 +01:00
Koen Kanters 9822c75497 Update zigbee-herdsman-converters to 21.5.0 (#25221) 2024-12-15 20:22:53 +00:00
Koen Kanters 56746ded19 fix(ignore): update dependencies (#25210) 2024-12-15 19:44:39 +00:00
Nerivec 0b62aefbb2 fix(ignore): Force rebuild of @serialport/bindings-cpp in Docker (#25202) 2024-12-15 20:38:41 +01:00
Koen Kanters e234f42e1f Update zigbee-herdsman to 3.0.4 (#25196) 2024-12-14 13:36:56 +00:00
Koen Kanters 72428897a7 fix: Fix group with only multi endpoints devices not being controllable (#25156) 2024-12-14 14:24:34 +01:00
Koen Kanters 39c007d211 fix(ignore): Add back Home Assistant legacy action sensor (#25192) 2024-12-14 13:30:33 +01:00
Koen Kanters 5e71cf2cc8 fix(ignore): Update zigbee-herdsman-converters to 21.4.0 (#25191) 2024-12-13 21:28:39 +01:00
Koen Kanters 146b6c3860 fix(ignore): Rebuild serialport, attempt to fix #25092 2024-12-13 21:25:50 +01:00
NerivecandKoen Kanters 2b08c688d3 fix: Proper extended PAN ID from getNetworkParameters (#25174)
Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
2024-12-13 20:58:21 +01:00
Koenkk 0a20e4a455 fix(ignore): update zigbee-herdsman to 3.0.3 2024-12-13 20:58:21 +01:00
Cody Cutrer 08ac795bb3 fix: OpenHAB: Fix rejectattr argument in event value_template (#25179) 2024-12-13 19:41:47 +00:00
Koen Kanters a2027d79e8 fix(ignore): Add back linux/386 (#25187) 2024-12-13 20:35:11 +01:00
Koen Kanters 1dde41f7ea chore: Lint Dockerfile 2024-12-11 22:45:38 +01:00
e23d300fd9 feat: Docker: RISC-V support (#22824)
Co-authored-by: Koenkk <2892853+Koenkk@users.noreply.github.com>
Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
2024-12-11 22:42:37 +01:00
Koen Kanters 32c341c7f5 fix!: Docker: update Alpine to 3.21, drop x86 support (#25171) 2024-12-11 22:31:24 +01:00
Koen Kanters 9eea58eba4 fix: Fix Cannot read properties of null (reading 'toString') when Home Assistant event entities are enabled https://github.com/Koenkk/zigbee2mqtt/issues/25133 2024-12-11 22:25:54 +01:00
Nerivec ec13ebd3ad fix(ignore): Minor cleanup (#25166) 2024-12-11 21:09:41 +01:00
Koen Kanters 9fad883ae7 chore: merge master to dev 2024-12-10 21:28:20 +01:00
Koen Kanters bc6eccf615 chore: update stale 2024-12-10 21:28:01 +01:00
Koen Kanters 6905c10a05 Update zigbee-herdsman-converters to 21.3.0 (#25143) 2024-12-09 21:36:21 +00:00
Koen Kantersandnurikk 1d176f4358 Update zigbee2mqtt-frontend to 0.9.1 (#25142)
Co-authored-by: nurikk <1525421+nurikk@users.noreply.github.com>
2024-12-09 21:12:11 +00:00
Cody Cutrer 226945cae0 fix: Fix Home Assistant event entities for openHAB (#25140) 2024-12-09 20:34:19 +01:00
7d63a87aad fix!: Remove permit_join_timeout (#25082)
Co-authored-by: Nerivec <62446222+Nerivec@users.noreply.github.com>
Co-authored-by: IIIEII <al@iiieii.ru>
Co-authored-by: Artem Draft <Drafteed@users.noreply.github.com>
2024-12-08 15:49:42 +01:00
Koen Kantersandnurikk f44ea32bca Update zigbee2mqtt-frontend to 0.9.0 (#25128)
Co-authored-by: nurikk <1525421+nurikk@users.noreply.github.com>
2024-12-08 14:46:00 +00:00
Koen Kanters 7803a166d2 fix(ignore): update dependencies (#25120) 2024-12-08 15:02:27 +01:00
Artem DraftandKoen Kanters ba45445544 fix!: Home Assistant: remove duplicate sensor/select for select/number/button entities (#25026)
Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
2024-12-07 21:43:47 +01:00
Nerivec 1a9c79bb2f feat: Typing for MQTT API (#25098) 2024-12-07 20:21:30 +00:00
IIIEII d891320314 fix(ignore): Fixed additional case of calculation of group state when including no… (#25104) 2024-12-07 21:05:55 +01:00
Koen Kanters 639e3bb6fd chore: merge master to dev 2024-12-05 21:11:58 +01:00
Koen Kanters 7dc92d4b6c chore: fix update_dep.yml 2024-12-05 21:11:31 +01:00
Koen Kanters 0687195aca Update zigbee-herdsman to 3.0.1 (#25081) 2024-12-05 20:11:14 +00:00
Koen Kanters 0affca4fe4 chore: merge master to dev 2024-12-05 21:09:05 +01:00
Koen Kanters f8ff89d442 Revert "chore: fix update_dep.yml"
This reverts commit d943b31372.
2024-12-05 21:07:30 +01:00
Koen Kanters 66404cc9f4 chore: merge master to dev 2024-12-05 21:07:00 +01:00
Koen Kanters d943b31372 chore: fix update_dep.yml 2024-12-05 21:06:37 +01:00
alorenteandKoen Kanters b370b80dfb fix: Add more OCI Image Labels (#25074)
Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
2024-12-05 20:12:23 +01:00
Koen Kanters 51f364a1d5 fix(ignore): Fix external converter loading (#25063) 2024-12-05 19:58:55 +01:00
Koen Kanters 53208ae06f Merge remote-tracking branch 'origin/master' into dev 2024-12-04 21:25:27 +01:00
IIIEII 9227e139de fix: Fixed calculation of group state when including non-default endpoints (#25059) 2024-12-04 21:20:47 +01:00
Koen Kanters 71e163fcc3 Update zigbee-herdsman-converters to 21.2.1 (#25054) 2024-12-03 21:18:23 +00:00
Koen Kanters 5b907f2f77 chore: fix update_dep(s) 2024-12-03 22:05:55 +01:00
Koen Kanters acba533397 chore: fix ci commit 2024-12-01 21:34:34 +01:00
Koen Kanters 9bd20afb9c fix(ignore): Update usb discovery discussion link 2024-12-01 20:54:46 +01:00
Koen Kanters 32e10d89f4 fix(ignore): update dependencies (#24999) 2024-12-01 20:48:04 +01:00
Koen Kanters 80b00b6254 chore: fix ci.yml 2024-12-01 20:47:39 +01:00
Koen Kanters c2c6b1991c chore: fix release_please 2024-12-01 20:44:34 +01:00
Koen Kanters 1f56496869 Merge pull request #24492 from Koenkk/feat/2.0.0
Zigbee2MQTT 2.0.0
2024-12-01 20:39:46 +01:00
Koen Kanters b581dc6994 fix(ignore): fix tests 2024-12-01 20:37:13 +01:00
Koen Kanters 1ce45c8c41 fix(ignore): update deps 2024-12-01 20:12:46 +01:00
Koen Kanters a6b36051f0 fix!: Ensure data in response is always empty on error (#24971) 2024-12-01 20:11:34 +01:00
Nerivec d78c6facab fix: Cleanup leftover legacy settings logic (#24947) 2024-12-01 20:11:34 +01:00
Koen Kanters 8d58048f34 chore: improve ci 2024-12-01 20:11:34 +01:00
Koen Kanters f54719fb89 chore: fix provenance 2024-12-01 20:11:34 +01:00
NerivecandKoen Kanters 9907005256 feat: Live loading of external JS (converters/extensions) (#24764)
* feat: Live loading of external JS (converters/extensions)

* Fix imports

* Improve error message on MQTT save

* Handle non-existing base path

* Throw on bad converter

* Add tests

* Fix use of ext conv in network map tests.

* More coverage.

* Dont mock zhc for basics, tests actual live loading

* Update

* feat: Live loading of external JS (converters/extensions)

* Fix imports

* Improve error message on MQTT save

* Handle non-existing base path

* Throw on bad converter

* Add tests

* Fix use of ext conv in network map tests.

* More coverage.

* Dont mock zhc for basics, tests actual live loading

* Update

* Fix rebase

* Fix

* Bump zhc

* pretty

* fix typing

* Cleanup `external_converters` setting remnants.

---------

Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
2024-12-01 20:11:34 +01:00
Koen Kanters cc61f67215 fix(ignore): some fixes 2024-12-01 20:11:34 +01:00
Koen Kanters 23d8cea4e2 fix: Cleanup Dockerfile 2024-12-01 20:11:34 +01:00
Koen Kanters 8aef77e57e fix(ignore): Remove external definition type 2024-12-01 20:11:34 +01:00
Nerivec c6958c4991 feat: Automatic settings migration (#24871)
* feat: Automatic settings migration

* Handle failing on unsupported version.

* Handle change not needed.

* Test change not wanted.

* Cleanup.

* Add `external_converters` removal.

* Fix schema export.
2024-12-01 20:11:34 +01:00
Nerivec e052988663 fix: Use built-in async for mqtt (#24786)
* fix: Use async for mqtt.

* Update mocks, fix expects

* Prettier.

* Use appropriate error for publish

* Set max listeners immediately after connect

* Add mqtt v5+ `disconnect` event handler

* Set maximum packet size in `CONNECT` packet

* Fix tests.

* Add setting for maximum packet size.

* Add `reasonString` to disconnect logs

* Fix tests

* Fix

* Prettier
2024-12-01 20:11:34 +01:00
NerivecandKoen Kanters 563514c446 fix!: Rework OTA (#24634)
* fix(ignore): Update zh and zhc

* update

* Update

* fix

* fix!: Rework OTA

* Import only required from zhc.

* Remove uri-js

* Update settings.schema.json

* fix save

---------

Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
2024-12-01 20:11:33 +01:00
Koen Kanters 95279b02af fix(ignore): Small cleanup (#24696) 2024-12-01 20:11:08 +01:00
Koen Kanters e290057e21 fix!: Remove deprecated Home Assistant integration features (#24494)
* Remove `legacy_trigger`

* Remove legacy_entity_attributes

* Remove `LEGACY_MAPPING`

* Finish cleanup

* Updates

* revert some entities removal

* Update

* Updates
2024-12-01 20:11:06 +01:00
Koen Kanters 61cf5094c7 fix 236b084396 2024-12-01 20:10:35 +01:00
Nerivec 9537ef05d5 fix!: Improve bind/bridge/groups parsing and resolving consistency (#24432)
* Improve bind/bridge/groups parsing and resolving consistency

* Fix mqtt payload casing. Improve typing.

* Cleanup tests

* Add test for bind endpoint by ID.

* Match behavior for group response 'default' endpoint.
2024-12-01 20:09:28 +01:00
Koen Kanters 3f76586a50 fix!: Remove configuring group members through configuration.yaml (#24338)
* fix!: Remove configuring group members through `configuration.yaml`

* Updates

* Updates

* fix

* Updates

* Feedback
2024-12-01 20:09:28 +01:00
Nerivec 6f6e7c3ee0 fix: Refactor tests to TS (#24357) 2024-12-01 20:09:27 +01:00
NerivecandKoen Kanters 2124d3450c fix!: Improve permit join (#24257)
* fix: Improve permit join

* Update Home Assistant permit join switch

* Remove `permit_join` from `settings.schema.json`

* Update zigbee-herdsman version to pre-release.

* Fix pnpm overrides

* Update test/homeassistant.test.js

---------

Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
2024-12-01 20:07:06 +01:00
Koen KantersandNerivec b6186623f0 feat!: The big cleanup (#24200)
* Remove `deviceGroupMembership`

* Fix controller.ts

* Remove `legacy_api` logic from non-legacy extensions.

* Fix network map coverage.

* Remove all `legacy` extensions.

* Remove `legacy_availability_payload`, `legacy_api`. Remove legacy from configure.

* Fix prettier.

* Remove some leftovers

* Renamed `updateAvailable` to `update_available`

* Remove emitPublishAvailability

* Remove `configuration.yaml`

* Switch to pnpm

* Remove direct mqtt-packet dependency.

* fix pretty

* fix `getDependencyVersion`

* fix pnpm publish

* fix(ignore): fix pnpm publish

* Remove deprecated bridge `config/*`

* Improve update script

* Improve update.sh

---------

Co-authored-by: Nerivec <62446222+Nerivec@users.noreply.github.com>
2024-12-01 20:06:49 +01:00
Koen Kanters 3220b27c8a fix!: HA permit join switch: disable automatically after 254 seconds (#24245) 2024-12-01 20:06:37 +01:00
Koen Kanters 98b977030e fix(ignore): Changes for improved adapter discovery (#24147)
* fix: Comment serial config in example configuration

* Link to issue

* Update link

* Update example config

* Feedback
2024-12-01 20:06:37 +01:00
Koen Kanters 1fdc427358 chore: update update_deps.yml 2024-12-01 20:04:54 +01:00
github-actions[bot] 9a74b015dc chore: promote to dev 2024-12-01 13:44:52 +00:00
Koen Kanters 861cba63b3 chore(dev): release 1.42.0 (#24569) 2024-12-01 14:33:44 +01:00
Koen Kanters 59d0eb4534 Update zigbee-herdsman-converters to 20.58.0 (#24987) 2024-12-01 13:30:48 +00:00
Koen Kanters 57ee303cda Update zigbee-herdsman-converters to 20.57.0 (#24977) 2024-11-30 20:21:19 +00:00
Der Mundschenk & Compagnie 4690fe1a55 fix(ignore): Ignore None actions for HA events (#24972) 2024-11-30 21:09:09 +01:00
Der Mundschenk & CompagnieandKoen Kanters 826547a8e1 fix(ignore): Prevent empty events from being published to HA (#24969)
Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
2024-11-30 13:46:31 +01:00
Koen Kanters 7047aef7f7 fix(ignore): Fix Cannot read properties of undefined (reading 'clients') https://github.com/Koenkk/zigbee2mqtt/issues/24957 2024-11-29 20:43:30 +01:00
Der Mundschenk & Compagnie d82d782bac fix(ignore): Prevent incorrect event_type due to duplicate keys (#24954) 2024-11-29 20:26:33 +01:00
Koen Kantersandnurikk 6351f4618e Update zigbee2mqtt-frontend to 0.7.6 (#24955)
Co-authored-by: nurikk <1525421+nurikk@users.noreply.github.com>
2024-11-28 20:44:30 +00:00
Koen Kanters 2c52c9884c Update zigbee-herdsman-converters to 20.56.0 (#24952) 2024-11-28 19:56:10 +00:00
Koen Kanters 5e47a21673 Update zigbee-herdsman-converters to 20.55.0 (#24939) 2024-11-27 19:41:38 +00:00
Der Mundschenk & Compagnie 5ca4cf4acd fix: Home Assistant: template errors when action key is missing or empty in state payload (#24931) 2024-11-27 08:11:07 +01:00
Koen Kanters 267d800122 Update zigbee-herdsman-converters to 20.54.0 (#24920) 2024-11-25 19:26:00 +00:00
Koen KantersandIlya Kirov 790373b896 fix(ignore): Update zigbee-herdsman-converters to 20.53.0 (#24906)
Co-authored-by: Ilya Kirov <kirovilya@gmail.com>
2024-11-24 21:29:37 +00:00
Koen Kanters ca6493528d fix(ignore): Update dependencies (#24894) 2024-11-24 22:01:37 +01:00
Koen Kanters 1e77893f75 fix(ignore): update zigbee-herdsman-converters to 20.52.0 (#24874) 2024-11-22 20:55:41 +00:00
Koen Kanters 82a026e02e fix(ignore): update zigbee-herdsman-converters to 20.51.0 (#24856) 2024-11-21 20:58:55 +00:00
Koen Kanters 988984a1ae fix(ignore): update zigbee-herdsman-converters to 20.50.0 (#24843) 2024-11-20 20:27:28 +00:00
Koen Kanters 5859947207 fix(ignore): update zigbee-herdsman-converters to 20.49.1 (#24830) 2024-11-19 19:43:54 +00:00
Koen Kanters b1508ae52f Update zigbee-herdsman-converters to 20.48.0 (#24820)
* fix(ignore): update zigbee-herdsman-converters to 20.48.0

* u
2024-11-18 21:06:11 +00:00
Koen Kanters 152ad12508 fix(ignore): update zigbee-herdsman to 2.1.9 (#24819) 2024-11-18 20:05:50 +00:00
Koen Kanters 36e452b0f3 fix(ignore): update zigbee-herdsman-converters to 20.47.0 (#24772) 2024-11-14 20:36:53 +00:00
Koen Kanters 2a9bbb21ba fix(ignore): update zigbee-herdsman to 2.1.8 (#24763) 2024-11-13 20:56:59 +00:00
Koen Kanters f7e7115f79 chore: attempt to fix codeql.yaml 2024-11-13 21:53:01 +01:00
Koen Kanters 6e04f7b03e fix(ignore): update zigbee-herdsman-converters to 20.46.0 (#24762) 2024-11-13 20:49:13 +00:00
Nerivec 2a1ab78f7a chore: Update codeql config (#24751)
* Update codeql config

* Fix prettier
2024-11-13 21:01:37 +01:00
Koen Kanters e5d02c4b95 chore: merge master to dev 2024-11-12 21:30:24 +01:00
Koen Kanters 69f7282952 chore: fix README.md badge 2024-11-12 21:30:05 +01:00
Koen Kanters b8c3104aa1 fix(ignore): update zigbee-herdsman-converters to 20.45.0 (#24733) 2024-11-12 20:23:45 +00:00
Der Mundschenk & Compagnie 22e13c52b9 fix: Home Assistant event entities, part 2 (#24717)
* Add Philips Hue Tap dial rotation

* Add support for Hue Tap buttons

* Refactor action parsing
2024-11-11 21:10:20 +01:00
Koen Kanters 32e2637594 fix(ignore): update zigbee-herdsman-converters to 20.44.0 (#24715) 2024-11-10 19:39:18 +00:00
Koen Kanters 928635f1da fix(ignore): update dependencies (#24702) 2024-11-10 15:11:17 +01:00
Koen Kanters a353acc8e3 fix(ignore): update zigbee-herdsman-converters to 20.43.0 (#24680) 2024-11-07 19:53:05 +00:00
Koen Kanters b5d4a253b4 fix(ignore): update zigbee-herdsman-converters to 20.42.0 (#24666) 2024-11-06 20:47:04 +00:00
Boris Dolgov 7267d27779 fix: Temporarily disable serving compressed index file. (#24657)
express-static-gzip does not handle redirects with custom base URLs
properly. As index.html file is only 5KB, compression is not critical
for it.
2024-11-06 21:11:02 +01:00
Koen Kanters f2e5d59c35 fix(ignore): update zigbee-herdsman-converters to 20.41.0 (#24656) 2024-11-05 21:16:36 +00:00
Koen Kanters 1558a5d8ac fix(ignore): update zigbee-herdsman-converters to 20.40.0 (#24644) 2024-11-04 21:07:13 +00:00
Boris Dolgov 87cee1aea6 fix: Replace connect-gzip-static with express-static-gzip to become compatible with Node 23 (#24619)
Old versions of connect-gzip-static are not compatible with node 23,
new versions of connect-gzip-static are not compatible with node 18,
but we want to support both. express-static-gzip is compatible with
all versions.
2024-11-04 20:57:25 +01:00
Koen Kantersandnurikk 0c2cc10ad8 fix(ignore): update zigbee2mqtt-frontend to 0.7.5 (#24631)
Co-authored-by: nurikk <1525421+nurikk@users.noreply.github.com>
2024-11-04 10:56:25 +00:00
Koen Kanters 84cc92f91a fix(ignore): update dependencies (#24614) 2024-11-03 16:00:30 +01:00
Koen Kanters a6bfab7934 Update zigbee-herdsman-converters to 20.39.2 (#24609)
* fix(ignore): update zigbee-herdsman-converters to 20.39.2

* u
2024-11-03 14:34:04 +00:00
Der Mundschenk & CompagnieandKoen Kanters 848f250dbb feat: Add experimental support for Home Assistant event entities (#24233)
* Expose new event entity for actions

* Unify exposed actions for HA

* Fix event tests

* Only add event entities wen homeassistant/experimental_event_entities is true

---------

Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
2024-11-03 14:54:42 +01:00
Magnus Ihse BursieandKoen Kanters c078ccb136 fix: Home Assistant: remove the diagnostic category classification for power (#24575)
Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
2024-11-02 18:56:57 +00:00
github-actions[bot] 67d3f05fd7 chore: promote to dev 2024-11-01 07:57:31 +00:00
Koen Kanters cc31ceabd1 chore(dev): release 1.41.0 (#24173) 2024-11-01 08:46:52 +01:00
Koen Kanters c5d30b585b fix(ignore): update zigbee-herdsman-converters to 20.38.0 (#24567) 2024-10-31 20:23:53 +00:00
Koen Kanters f1615a255c fix(ignore): update zigbee-herdsman to 2.1.7 (#24566) 2024-10-31 20:17:43 +00:00
Magnus Ihse Bursie a2ce3230ac fix: Add additional discovery lookups for Home Assistant (#24555) 2024-10-31 20:36:52 +01:00
Koen Kanters bd1f6d24a1 fix(ignore): update zigbee-herdsman-converters to 20.37.0 (#24553) 2024-10-30 19:47:11 +00:00
Koen Kanters 9837488f44 fix(ignore): update zigbee-herdsman to 2.1.6 (#24545) 2024-10-29 20:08:04 +00:00
Koen Kanters b620a21d46 fix(ignore): update zigbee-herdsman-converters to 20.36.0 (#24542) 2024-10-29 19:31:53 +00:00
Koen Kanters 2139e7a794 fix(ignore): update dependencies (#24498) 2024-10-29 20:26:52 +01:00
Bacchus777 e0a0f868c0 fix: Improve power_phase_b and power_phase_c Home Assistant discovery (#24538)
Added power_phase_b and power_phase_c
2024-10-29 20:07:07 +01:00
Cody Cutrer ab726061b7 fix: Remove extraneous whitespace in template for binary attributes (#24520)
I assume Home Assistant strips the whitespace at some point before
comparing it to payload_on/payload_off, but I haven't quite found where yet.
I have confirmed Python preserves the whitespace when simply evaluating
the Jinja.

For openHAB, it does not strip the whitespace, causing errors because
it doesn't match the payload_on or payload_off values. At some point I'd
like to bring openHAB inline with Home Assistant, but I need to do so
carefully without breaking something unexpectedly (especially with non-
Home Assistant MQTT integrations). In the meantime, this should fix the
issue in openHAB, without causing issues for Home Assistant or any other
software that follows Home Assistant's MQTT discovery process.
2024-10-28 21:16:40 +01:00
Koen Kanters 074b2e20e0 fix(ignore): update zigbee-herdsman-converters to 20.35.0 (#24514) 2024-10-27 20:54:05 +00:00
Koen Kanters 2b460d3a5f fix(ignore): update zigbee-herdsman-converters to 20.34.0 (#24493) 2024-10-26 13:20:20 +00:00
Koen Kanters c09aeecd95 fix(ignore): update zigbee-herdsman-converters to 20.33.0 (#24457) 2024-10-23 20:27:22 +00:00
Koen Kanters 9ad7c70f24 fix(ignore): update zigbee-herdsman-converters to 20.32.0 (#24434) 2024-10-21 20:24:51 +00:00
Koen Kanters 64778650d3 fix(ignore): update zigbee-herdsman to 2.1.5 (#24433) 2024-10-21 19:51:21 +00:00
Koen Kanters eb606a5b98 fix(ignore): update dependencies (#24411) 2024-10-20 20:22:33 +02:00
Koen Kanters 669a16f13a fix(ignore): update zigbee-herdsman-converters to 20.31.0 (#24408) 2024-10-19 18:48:46 +00:00
Koen Kanters b2d3bbce3c Update zigbee-herdsman-converters to 20.30.0 (#24398)
* fix(ignore): update zigbee-herdsman-converters to 20.30.0

* u
2024-10-19 12:00:50 +00:00
Koen Kanters 12cb6e135d fix(ignore): update zigbee-herdsman to 2.1.4 (#24397) 2024-10-18 18:11:34 +00:00
Koen Kanters d79e71b13e fix(ignore): update zigbee-herdsman-converters to 20.29.0 (#24363) 2024-10-16 20:07:32 +00:00
Koen Kanters dbda1481ec chore: improve testExternalConverter.js 2024-10-16 21:59:24 +02:00
Koen Kanters d12b117b43 fix(ignore): update zigbee-herdsman-converters to 20.28.0 (#24337) 2024-10-14 20:10:31 +00:00
Koen KantersandNerivec e2f19f19b4 fix: Frontend code cleanup @Nerivec (#24322)
Co-authored-by: Nerivec <62446222+Nerivec@users.noreply.github.com>
2024-10-14 19:56:00 +02:00
Koen Kanters 618b318214 fix(ignore): update zigbee-herdsman-converters to 20.27.2 (#24321) 2024-10-13 20:32:13 +00:00
Koen Kanters 9f12ebb54e fix(ignore): update dependencies (#24309) 2024-10-13 20:10:19 +02:00
Koen Kanters db00759a03 fix(ignore): update zigbee-herdsman-converters to 20.26.0 (#24307) 2024-10-12 20:44:31 +00:00
Koen Kanters 5f601873fe fix(ignore): update zigbee-herdsman-converters to 20.25.0 (#24285) 2024-10-10 20:35:52 +00:00
Vladimir Kotikov 1fdf0a0a47 feat: Allow serving frontend under subpath (#24244)
* Allow app running in the server subdirectory

* Use path.posix to manage urls

* Add base_utl to config schema
2024-10-10 14:22:48 +02:00
Koen Kanters 3bb4af253a fix(ignore): update zigbee-herdsman-converters to 20.24.0 (#24246) 2024-10-06 21:07:58 +00:00
Massimiliano 076ada2325 fix: Fix group state change for open/close devices (#24226)
* Fix group state change if OPEN

* Runned Prettier
2024-10-06 21:35:46 +02:00
Koen Kanters 5c3f71292d fix(ignore): update dependencies (#24236) 2024-10-06 21:33:07 +02:00
Koen Kanters 02292590ee fix(ignore): update zigbee-herdsman-converters to 20.23.1 (#24225) 2024-10-05 13:40:41 +00:00
Koen Kanters b09ae10588 fix(ignore): update zigbee-herdsman-converters to 20.23.0 (#24201) 2024-10-03 19:34:26 +00:00
Koen Kanters e5fac76060 fix(ignore): update zigbee-herdsman-converters to 20.22.0 (#24186) 2024-10-02 19:48:01 +00:00
Ivan F. MartinezandKoen Kanters 17ac9cf8e7 feat: Add throttle option for devices (#24122)
* basic spam control

* used npm run pretty:write

* add test for SPAMMER description to comply with 100% coverage test

* define friendly name to spammer test devices

* Update README.md

Co-authored-by: Koen Kanters <koenkanters94@gmail.com>

* trying now with throttleit library

* lint corrections

* last lint request

* correct await / async definiction

* remove description support

* change first command to be executed

---------

Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
2024-10-02 21:15:15 +02:00
github-actions[bot] 063d4620d2 chore: promote to dev 2024-10-01 19:41:45 +00:00
Koen Kanters e06848db8c chore(dev): release 1.40.2 (#23885) 2024-10-01 21:30:57 +02:00
Koen Kanters c82f93230d fix(ignore): update zigbee-herdsman-converters to 20.21.0 (#24172)
* fix(ignore): update zigbee-herdsman-converters to 20.21.0

* fix
2024-10-01 21:27:13 +02:00
Koen Kanters 13e4937db2 fix(ignore): update zigbee-herdsman to 2.1.3 (#24171) 2024-10-01 18:45:21 +00:00
Koen Kanters 58c856bbd0 fix(ignore): update zigbee-herdsman-converters to 20.20.0 (#24144) 2024-09-29 18:25:56 +00:00
Koen Kanters df1913bc56 fix(ignore): update zigbee-herdsman to 2.1.2 (#24138) 2024-09-29 11:47:51 +00:00
Koen Kanters 37f80dfb69 fix(ignore): update dependencies (#24132) 2024-09-29 13:20:17 +02:00
Koen Kanters 8fe76e090f fix(ignore): update zigbee-herdsman-converters to 20.19.0 (#24130)
* fix(ignore): update zigbee-herdsman-converters to 20.19.0

* fix tests
2024-09-28 18:47:32 +00:00
Koen Kanters 15d845bf92 fix(ignore): update zigbee-herdsman-converters to 20.18.0 (#24105) 2024-09-25 19:33:27 +00:00
Koen Kanters 22e1480f74 Update zigbee-herdsman-converters to 20.17.1 (#24092)
* fix(ignore): update zigbee-herdsman-converters to 20.17.1

* Update
2024-09-24 20:29:25 +00:00
Koen Kanters 5dd1bfb8a6 fix(ignore): update zigbee-herdsman to 2.1.1 (#24093) 2024-09-24 20:01:58 +00:00
Koen Kanters b1c7fd0033 fix(ignore): update zigbee-herdsman-converters to 20.17.0 (#24077) 2024-09-23 18:46:59 +00:00
Koen Kanters 2314147d0e fix(ignore): update zigbee-herdsman to 2.1.0 (#24076) 2024-09-23 18:35:57 +00:00
Koen Kanters ee4becb64e fix(ignore): update zigbee-herdsman to 2.0.0 (#24064) 2024-09-22 19:36:21 +00:00
Koen Kanters a5bb49aa71 fix(ignore): update dependencies (#24057)
* fix(ignore): update dependencies

* u
2024-09-22 20:08:50 +02:00
Koen Kanters d289194a71 fix(ignore): update zigbee-herdsman to 1.1.0 (#24054) 2024-09-21 19:06:54 +00:00
Koen Kanters 7ad51ce662 fix: Fix Home Assistant Invalid state message error when state is too long (#24045) 2024-09-21 00:06:44 +02:00
Koen Kanters 951bbd3576 fix(ignore): update zigbee-herdsman-converters to 20.16.3 (#24033) 2024-09-19 20:17:21 +00:00
Koen Kanters 8a54a01583 fix(ignore): update zigbee-herdsman to 1.0.1 (#24032) 2024-09-19 19:58:47 +00:00
Koen Kanters e7a3f0da0a fix(ignore): update zigbee-herdsman-converters to 20.16.2 (#24022) 2024-09-18 20:09:35 +00:00
Koen Kanters 2ae19a7c42 fix(ignore): update zigbee-herdsman-converters to 20.16.1 (#24009) 2024-09-17 20:30:55 +00:00
Koen Kanters da2a6974e0 fix(ignore): update zigbee-herdsman to 1.0.0 (#24008) 2024-09-17 20:24:12 +00:00
Koen Kanters d3de77da13 fix(ignore): update zigbee-herdsman-converters to 20.16.0 (#23990) 2024-09-16 20:49:03 +00:00
Koen Kanters 28650daf83 chore: fix cedc0623f7 2024-09-16 22:46:37 +02:00
cedc0623f7 chore: Fix CI - trigger helm update (#23977)
* Fix CI - trigger helm update

* Update .github/workflows/ci.yml

Co-authored-by: Koen Kanters <koenkanters94@gmail.com>

---------

Co-authored-by: jose <jose.pedrosa@reddit.com>
Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
2024-09-16 20:23:13 +02:00
Koen Kanters 3efc2b237b fix(ignore): update dependencies (#23973) 2024-09-15 20:46:51 +02:00
Koen Kanters 03b5b0043a fix(ignore): update zigbee-herdsman-converters to 20.15.0 (#23969) 2024-09-14 19:01:07 +00:00
Koen Kanters 70a65c7dcf fix(ignore): update zigbee-herdsman to 0.57.4 (#23968) 2024-09-14 18:46:56 +00:00
Koen Kanters eb8685682f fix(ignore): update zigbee-herdsman-converters to 20.14.1 (#23952) 2024-09-12 21:02:39 +00:00
Koen Kanters e642f7c86a fix(ignore): Sync eslint settings from zhc (#23951)
* fix(ignore): Sync eslint settings from zhc

* u
2024-09-12 19:36:57 +00:00
Tomer c6ca472ca1 fix: Improve performance when logging is disabled (#23893)
* Improve log performance

* Bug fix

* Revert sample configuration

* Align package versions

* Revert dependecies version to allow testing

* Update package-lock.json

* Fix prettier issue

* Coverage to 100%

* Fix CR comments

* remove uneeded changes

* Make the same perf optimization to the MQTT messages

* Fix MQTT tests

* prettier fix

* Allow easy mocking of stub logger callTransports function to receive the actual message

* cleanup leftover

* Fix prettier issues

* Finalize logger mock structure and add few lambda log calls

* fix prettier issue

* Run prettier

* Revert "fix prettier issue"

This reverts commit b409d538e3.

* Update zigbee.ts

Fix prettier issue

* Fix test issues

* Revert await removal
2024-09-12 20:33:05 +02:00
Koen Kanters 0540e4fbb4 fix(ignore): update zigbee-herdsman-converters to 20.13.0 (#23927) 2024-09-09 20:13:08 +00:00
Koen Kanters 9c72a768f2 fix(ignore): Improvement for afd80449b3 2024-09-08 21:33:56 +02:00
Koen Kanters 8c0476612f fix(ignore): update dependencies (#23904) 2024-09-08 14:28:03 +02:00
Koen Kanters afd80449b3 fix(ignore): Migrate to eslint 9 (#23800)
* Eslint 9

* Automatic changes

* Manual changes

* Process feedback

* u
2024-09-08 14:26:18 +02:00
Koen Kanters d989061122 fix(ignore): update dependencies (#23898) 2024-09-08 13:08:03 +02:00
NerivecandKoen Kanters 48d77e4b5e fix: Enforce TS strict type checking (#23601)
* Enforce TS `strict` type checking.

* updates

* updates

* updates

* Updates

* Updates

* pretty

* u

* u

* u

* Updates

* updates

* Updates

* Updates

* `ReadonlyArray`

* scenesChanged

* objectID

* Improve coverage

* u

* u

* process feedback

---------

Co-authored-by: Koen Kanters <koenkanters94@gmail.com>
2024-09-07 11:40:50 +00:00
github-actions[bot] 63c3bb4166 chore: promote to dev 2024-09-07 11:33:51 +00:00
Koen Kanters 403d3c090d chore(dev): release 1.40.1 (#23797) 2024-09-07 13:25:22 +02:00
Koen Kanters 9283c4b497 fix(ignore): update zigbee-herdsman-converters to 20.12.1 (#23884) 2024-09-07 11:17:24 +00:00
Koen Kanters 0d46354c48 fix(ignore): update zigbee-herdsman to 0.57.3 (#23877) 2024-09-06 20:26:15 +00:00
Koen Kanters 13a108ddbc fix: Log stack trace when update check fails https://github.com/Koenkk/zigbee2mqtt/discussions/23798 2024-09-06 14:37:20 +02:00
Koen Kanters 5524e881e6 fix(ignore): update zigbee-herdsman to 0.57.2 (#23872) 2024-09-06 12:19:21 +00:00
Koen Kanters a33c29f10a fix(ignore): update zigbee-herdsman-converters to 20.11.0 (#23837) 2024-09-05 12:39:50 +00:00
Koen Kanters fc02071c8f chore: fix e2da2fa8d3 2024-09-05 14:37:30 +02:00
Koen Kanters 06556d44f3 chore: merge master to dev 2024-09-05 13:35:31 +02:00
Koen Kanters 13084d22a7 chore: Update new device support template 2024-09-05 13:35:15 +02:00
dependabot[bot]anddependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> 14787d3a6a chore(deps): bump peter-evans/create-pull-request from 6 to 7 (#23832)
Bumps [peter-evans/create-pull-request](https://github.com/peter-evans/create-pull-request) from 6 to 7.
- [Release notes](https://github.com/peter-evans/create-pull-request/releases)
- [Commits](https://github.com/peter-evans/create-pull-request/compare/v6...v7)

---
updated-dependencies:
- dependency-name: peter-evans/create-pull-request
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2024-09-03 20:21:00 +02:00
Jose Luis Pedrosaandjose e2da2fa8d3 chore: Invoke chart CI (#23819)
The workflow name on the chart repository is OnZigbee2MQTTRelease, this event means that a new version of zigbe2mqtt is has been released, it should probably trigger too the release, but that is a chart repo concern

Co-authored-by: jose <jose.pedrosa@reddit.com>
2024-09-03 20:20:40 +02:00
Koen Kanters 6dade8b11f fix(ignore): update zigbee-herdsman-converters to 20.9.0 (#23822) 2024-09-02 20:48:09 +00:00
Koen Kanters 557b17ad5b fix: Fix Cannot read properties of undefined (reading 'some') https://github.com/Koenkk/zigbee2mqtt/issues/23821 2024-09-02 22:22:19 +02:00
Koen Kanters 64222d231f Update zigbee-herdsman-converters to 20.8.5 (#23799)
* fix(ignore): update zigbee-herdsman-converters to 20.8.5

* Update utils.ts
2024-09-01 19:23:14 +00:00
Koen Kanters b7fa722109 fix(ignore): update dependencies (#23790) 2024-09-01 20:23:49 +02:00
github-actions[bot] 4a1d983168 chore: promote to dev 2024-09-01 18:22:59 +00:00
139 changed files with 21928 additions and 25759 deletions
-2
View File
@@ -1,2 +0,0 @@
node_modules/*
test/
-63
View File
@@ -1,63 +0,0 @@
module.exports = {
env: {
'jest/globals': true,
es6: true,
node: true,
},
extends: ['eslint:recommended', 'plugin:jest/recommended', 'plugin:jest/style', 'prettier'],
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module',
},
rules: {
'require-jsdoc': 'off',
'no-prototype-builtins': 'off',
'@typescript-eslint/no-floating-promises': 'error',
},
plugins: ['jest', 'perfectionist'],
overrides: [
{
files: ['*.ts'],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
extends: ['plugin:@typescript-eslint/recommended'],
parserOptions: {
project: './tsconfig.json',
},
rules: {
'@typescript-eslint/await-thenable': 'error',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/explicit-function-return-type': 'error',
'@typescript-eslint/no-empty-function': 'off',
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/no-floating-promises': 'error',
'@typescript-eslint/no-unused-vars': 'error',
'no-return-await': 'error',
'perfectionist/sort-imports': [
'error',
{
groups: [
'type',
['builtin', 'external'],
'internal-type',
'internal',
['parent-type', 'sibling-type', 'index-type'],
['parent', 'sibling', 'index'],
'object',
'unknown',
],
customGroups: {
value: {},
type: {},
},
newlinesBetween: 'always',
internalPattern: ['~/**'],
type: 'natural',
order: 'asc',
ignoreCase: false,
},
],
},
},
],
};
@@ -27,6 +27,14 @@ body:
placeholder: '{"id":53,"type":"Router","ieeeAddr":"0x10458d00024284f69","nwkAddr":10148,"manufId":4151,"manufName":"LUMI","powerSource":"DC Source","modelId":"lumi.relay.c2acn01","epList":[1,2],"endpoints":{"1":{"profId":260,"epId":1,"devId":257,"inClusterList":[0,3,4,5,1,2,10,6,16,2820,12],"outClusterList":[25,10],"clusters":{"genBasic":{"attributes":{"modelId":"lumi.relay.c2acn01","appVersion":1,"manufacturerName":"LUMI","powerSource":4,"zclVersion":0,"stackVersion":2,"hwVersion":18,"dateCode":"8-6-2020"}},"genAnalogInput":{"attributes":{"presentValue":129.04425048828125}},"genOnOff":{"attributes":{"61440":117440715,"onOff":1}}},"binds":[],"configuredReportings":[],"meta":{}},"2":{"profId":260,"epId":2,"devId":257,"inClusterList":[6,16,4,5],"outClusterList":[],"clusters":{"genOnOff":{"attributes":{"61440":237478966,"onOff":0}}},"binds":[],"configuredReportings":[],"meta":{}}},"appVersion":1,"stackVersion":2,"hwVersion":18,"dateCode":"8-6-2020","zclVersion":0,"interviewCompleted":true,"meta":{},"lastSeen":1640285631405}'
validations:
required: true
- type: input
id: z2m_version
attributes:
label: Zigbee2MQTT version
description: Can be found in the frontend -> settings -> about -> Zigbee2MQTT version. Are you running Zigbee2MQTT 1.18.1? Then read [this](https://github.com/Koenkk/zigbee2mqtt/releases/tag/1.19.0).
placeholder: '1.22.1'
validations:
required: true
- type: textarea
id: notes
attributes:
+98 -52
View File
@@ -1,4 +1,4 @@
name: ci
name: CI
on: [pull_request, push]
@@ -9,74 +9,114 @@ permissions:
jobs:
ci:
runs-on: ubuntu-latest
permissions:
id-token: write
steps:
- uses: actions/checkout@v4
if: (github.ref == 'refs/heads/dev' || startsWith(github.ref, 'refs/tags/')) && github.event_name == 'push'
with:
# Required for `release: merge dev -> master and promote dev`
token: ${{secrets.GH_TOKEN}}
token: ${{ secrets.GH_TOKEN }}
- uses: actions/checkout@v4
if: ((github.ref == 'refs/heads/dev' || startsWith(github.ref, 'refs/tags/')) && github.event_name == 'push') == false
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: 20
node-version: 22
registry-url: https://registry.npmjs.org/
cache: npm
cache: pnpm
- name: Install dependencies
run: npm ci
run: pnpm install --frozen-lockfile
- name: Build
run: npm run build
- name: Lint
run: |
npm run pretty:check
npm run eslint
run: pnpm run build
- name: Check
run: pnpm run check
- name: Test
run: npm run test-with-coverage
- name: Docker login
run: pnpm run test:coverage
- name: Log in to the Docker container registry
if: (github.ref == 'refs/heads/dev' || startsWith(github.ref, 'refs/tags/')) && github.event_name == 'push'
run: echo ${{ secrets.DOCKER_KEY }} | docker login -u koenkk --password-stdin
- name: Docker login ghcr.io
uses: docker/login-action@v3
with:
username: koenkk
password: ${{ secrets.DOCKER_KEY }}
- name: Log in to the GitHub container registry
if: (github.ref == 'refs/heads/dev' || startsWith(github.ref, 'refs/tags/')) && github.event_name == 'push'
run: echo ${{ secrets.GH_TOKEN }} | docker login ghcr.io -u koenkk --password-stdin
uses: docker/login-action@v3
with:
registry: ghcr.io
username: koenkk
password: ${{ secrets.GH_TOKEN }}
- name: Docker setup - QEMU
if: (github.ref == 'refs/heads/dev' || startsWith(github.ref, 'refs/tags/')) && github.event_name == 'push'
uses: docker/setup-qemu-action@v3
with:
platforms: all
- name: Docker setup - Buildx
if: (github.ref == 'refs/heads/dev' || startsWith(github.ref, 'refs/tags/')) && github.event_name == 'push'
id: buildx
uses: docker/setup-buildx-action@v3
with:
version: latest
- name: 'dev: Docker build'
- name: dev - Docker build and push
if: github.ref == 'refs/heads/dev' && github.event_name == 'push'
run: |
docker buildx build \
--build-arg COMMIT=$(git rev-parse --short HEAD) \
--platform linux/arm64/v8,linux/386,linux/amd64,linux/arm/v6,linux/arm/v7 \
-f docker/Dockerfile \
--provenance=false \
--push \
-t koenkk/zigbee2mqtt:latest-dev -t ghcr.io/koenkk/zigbee2mqtt:latest-dev \
.
- name: 'release: Docker build'
uses: docker/build-push-action@v6
with:
context: .
file: docker/Dockerfile
platforms: linux/arm64/v8,linux/amd64,linux/arm/v6,linux/arm/v7,linux/riscv64,linux/386
tags: koenkk/zigbee2mqtt:latest-dev,ghcr.io/koenkk/zigbee2mqtt:latest-dev
push: true
build-args: |
COMMIT=${{ github.sha }}
VERSION=dev
DATE=${{ github.event.repository.updated_at }}
- name: release - Docker meta
if: startsWith(github.ref, 'refs/tags/') && github.event_name == 'push'
run: |
TAG="$(git describe --tags)"
docker buildx build \
--build-arg COMMIT=$(git rev-parse --short HEAD) \
--platform linux/arm64/v8,linux/386,linux/amd64,linux/arm/v6,linux/arm/v7 \
-f docker/Dockerfile \
--provenance=false \
--push \
-t koenkk/zigbee2mqtt:latest -t "koenkk/zigbee2mqtt:$TAG" -t ghcr.io/koenkk/zigbee2mqtt:latest -t "ghcr.io/koenkk/zigbee2mqtt:$TAG" \
.
uses: docker/metadata-action@v5
id: meta
with:
images: |
koenkk/zigbee2mqtt
ghcr.io/koenkk/zigbee2mqtt
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
- name: release - Docker build and push
if: startsWith(github.ref, 'refs/tags/') && github.event_name == 'push'
uses: docker/build-push-action@v6
with:
context: .
file: docker/Dockerfile
platforms: linux/arm64/v8,linux/amd64,linux/arm/v6,linux/arm/v7,linux/riscv64,linux/386
tags: ${{ steps.meta.outputs.tags }}
push: true
build-args: |
COMMIT=${{ github.sha }}
VERSION=${{ github.ref_name }}
DATE=${{ github.event.repository.updated_at }}
- name: 'release: Publish to npm'
if: startsWith(github.ref, 'refs/tags/') && github.event_name == 'push'
run: npm publish
run: pnpm publish --no-git-checks
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN }}
NPM_CONFIG_PROVENANCE: true
- name: 'dev: Trigger zigbee2mqtt/hassio-zigbee2mqtt build'
if: github.ref == 'refs/heads/dev' && github.event_name == 'push'
run: |
@@ -86,6 +126,7 @@ jobs:
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/zigbee2mqtt/hassio-zigbee2mqtt/actions/workflows/ci.yml/dispatches \
-d '{"ref":"master","inputs":{}}'
- name: 'release: Trigger zigbee2mqtt/hassio-zigbee2mqtt build'
if: startsWith(github.ref, 'refs/tags/') && github.event_name == 'push'
run: |
@@ -98,18 +139,20 @@ jobs:
-H "Content-Type: application/json" \
https://api.github.com/repos/zigbee2mqtt/hassio-zigbee2mqtt/dispatches \
--data "{\"event_type\": \"release\", \"client_payload\": { \"version\": \"$TAG-1\"}}"
- name: 'release: Trigger zigbee2mqtt-chart release'
- name: 'release: Trigger zigbee2mqtt-chart image update'
if: startsWith(github.ref, 'refs/tags/') && github.event_name == 'push'
run: |
TAG=${GITHUB_REF#refs/*/}
echo "Triggering with tag '$TAG'"
curl \
curl -L \
-X POST \
-H "Authorization: token ${{ secrets.GH_TOKEN }}" \
-H "Accept: application/vnd.github.everest-preview+json" \
-H "Content-Type: application/json" \
https://api.github.com/repos/Koenkk/zigbee2mqtt-chart/dispatches \
--data "{\"event_type\": \"release\", \"client_payload\": { \"version\": \"$TAG\"}}"
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer ${{ secrets.GH_TOKEN }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/Koenkk/zigbee2mqtt-chart/actions/workflows/on_zigbee2mqtt_release.yaml/dispatches \
--data "{\"ref\": \"main\", \"inputs\": { \"zigbee2mqtt_version\": \"$TAG\"}}"
- name: 'release: merge dev -> master and promote dev'
if: startsWith(github.ref, 'refs/tags/') && github.event_name == 'push'
run: |
@@ -123,9 +166,7 @@ jobs:
git push origin master
git checkout dev
jq --indent 4 ".version = \"$TAG-dev\"" package.json > package.json.tmp
jq --indent 4 ".version = \"$TAG-dev\"" package-lock.json > package-lock.json.tmp
mv package.json.tmp package.json
mv package-lock.json.tmp package-lock.json
git add -A
git commit -m "chore: promote to dev"
git push origin dev
@@ -139,15 +180,20 @@ jobs:
continue-on-error: true
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
registry-url: https://registry.npmjs.org/
cache: 'npm'
cache: pnpm
- name: Install dependencies
# --ignore-scripts prevents the serialport build which often fails on Windows
run: npm ci --ignore-scripts
# --ignore-scripts prevents build on Windows (only for unix-dgram, so doesn't matter, others have pre-builds)
run: pnpm i --frozen-lockfile ${{ matrix.os == 'windows-latest' && '--ignore-scripts' || '' }}
- name: Build
run: npm run build
run: pnpm run build
- name: Test
run: npm run test-with-coverage
run: pnpm run test:coverage
-23
View File
@@ -1,23 +0,0 @@
name: codeql
on:
push:
branches:
- dev
jobs:
CodeQL-Build:
runs-on: ubuntu-latest
permissions:
security-events: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Initialize CodeQL
uses: github/codeql-action/init@v3
- name: Autobuild
uses: github/codeql-action/autobuild@v3
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
@@ -1,4 +1,5 @@
name: deps-review
name: Dependency review
on: [pull_request]
permissions:
@@ -6,7 +6,7 @@ on:
- master
jobs:
fail_pr:
fail-pr-to-master:
runs-on: ubuntu-latest
steps:
- name: Fail PR to master
@@ -1,7 +1,7 @@
on:
workflow_dispatch:
name: ghcr-cleanup
name: GHCR cleanup
permissions: {}
jobs:
@@ -1,4 +1,4 @@
name: merge-master-to-dev
name: Merge master to dev
on:
push:
@@ -6,7 +6,7 @@ on:
- master
jobs:
merge_master_to_dev:
merge-master-to-dev:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
@@ -7,7 +7,7 @@ permissions:
contents: write
pull-requests: write
name: release-please
name: Release Please
jobs:
release-please:
@@ -16,9 +16,13 @@ jobs:
release_created: ${{ steps.release.outputs.release_created }}
version: '${{steps.release.outputs.major}}.${{steps.release.outputs.minor}}.${{steps.release.outputs.patch}}'
steps:
- uses: pnpm/action-setup@v4
with:
version: 9
- uses: actions/setup-node@v4
with:
node-version: 20
node-version: 22
- uses: googleapis/release-please-action@v4
id: release
@@ -52,7 +56,7 @@ jobs:
MASTER_FRONTEND_VERSION=$(cat z2m-master/package.json | jq -r '.dependencies."zigbee2mqtt-frontend"')
wget -q -O - https://raw.githubusercontent.com/Koenkk/zigbee2mqtt/release-please--branches--dev--components--zigbee2mqtt/CHANGELOG.md > z2m/CHANGELOG.md
cd z2m
npm ci
pnpm i --frozen-lockfile
node scripts/generateChangelog.js $MASTER_Z2M_VERSION $MASTER_ZHC_VERSION $MASTER_ZH_VERSION $MASTER_FRONTEND_VERSION >> ../changelog.md
env:
GH_TOKEN: ${{secrets.GH_TOKEN}}
+5 -5
View File
@@ -1,4 +1,4 @@
name: stale
name: Stale
on:
schedule:
@@ -12,9 +12,9 @@ jobs:
- uses: actions/stale@v9
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
stale-issue-message: 'This issue is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 30 days'
stale-pr-message: 'This pull request is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 30 days'
days-before-stale: 180
days-before-close: 30
stale-issue-message: 'This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days'
stale-pr-message: 'This pull request is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days'
days-before-stale: 60
days-before-close: 7
exempt-issue-labels: dont-stale
operations-per-run: 500
+41
View File
@@ -0,0 +1,41 @@
name: Update dependencies
on:
workflow_dispatch:
schedule:
- cron: '0 0 * * 0'
permissions:
contents: write
pull-requests: write
jobs:
update-dependencies:
if: github.repository == 'Koenkk/zigbee2mqtt'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.GH_TOKEN }}
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: pnpm
- run: |
# Disable for now as pnpm updates through corepack
# don't work well yet for bare-metal installations.
# pnpm self-update
pnpm up --latest
- uses: peter-evans/create-pull-request@v7
id: cpr
with:
commit-message: 'fix(ignore): update dependencies'
branch: 'deps/all'
title: 'fix(ignore): update dependencies'
token: ${{ secrets.GH_TOKEN }}
- uses: peter-evans/create-or-update-comment@v4
if: ${{ steps.cpr.outputs.pull-request-number }}
with:
issue-number: ${{ steps.cpr.outputs.pull-request-number }}
body: 'CC: @Koenkk'
@@ -2,11 +2,11 @@ on:
repository_dispatch:
types: update_dep
name: update-dep
name: Update dependency
permissions: {}
jobs:
update_dep:
update-dependency:
permissions:
contents: write
pull-requests: write
@@ -17,17 +17,20 @@ jobs:
with:
ref: dev
token: ${{ secrets.GH_TOKEN }}
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm install ${{ github.event.client_payload.package }}@${{ github.event.client_payload.version }} --save-exact
- uses: peter-evans/create-pull-request@v6
node-version: 22
cache: pnpm
- run: |
pnpm install ${{ github.event.client_payload.package }}@${{ github.event.client_payload.version }} --save-exact
pnpm install --no-frozen-lockfile
- uses: peter-evans/create-pull-request@v7
id: cpr
with:
commit-message: 'fix(ignore): update ${{ github.event.client_payload.package }} to ${{ github.event.client_payload.version }}'
branch: 'deps/${{ github.event.client_payload.package }}'
title: Update ${{ github.event.client_payload.package }} to ${{ github.event.client_payload.version }}
title: 'fix(ignore): update ${{ github.event.client_payload.package }} to ${{ github.event.client_payload.version }}'
token: ${{ secrets.GH_TOKEN }}
- run: sleep 5 # Otherwise pull request may not exist yet causing automerge to fail
- run: gh pr merge --squash --auto "${{ steps.cpr.outputs.pull-request-number }}"
-35
View File
@@ -1,35 +0,0 @@
name: Update dependencies
on:
workflow_dispatch:
schedule:
- cron: '0 0 * * 0'
permissions:
contents: read
jobs:
update_deps:
permissions:
contents: write # for peter-evans/create-pull-request to create branch
pull-requests: write # for peter-evans/create-pull-request to create a PR
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: dev
token: ${{ secrets.GH_TOKEN }}
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
# connect-gzip-static@4.0.0 requires Node 20 >=
# eslint: https://github.com/typescript-eslint/typescript-eslint/issues/8211
- run: npx npm-check-updates -u -x connect-gzip-static -x eslint
- run: rm -f package-lock.json
- run: npm install
- uses: peter-evans/create-pull-request@v6
with:
commit-message: 'fix(ignore): update dependencies'
branch: 'deps/all'
title: Update dependencies
token: ${{ secrets.GH_TOKEN }}
+4
View File
@@ -45,6 +45,9 @@ typings/
# Optional npm cache directory
.npm
# Optional pnpm cache directory
.pnpm-store
# Optional eslint cache
.eslintcache
@@ -68,6 +71,7 @@ tsconfig.tsbuildinfo
# IDE specific folders
.idea
.*.swp
# Ignore config
data/*
+3 -2
View File
@@ -1,2 +1,3 @@
package-lock.json
CHANGELOG.md
pnpm-lock.yaml
CHANGELOG.md
release-notes.md
+20 -1
View File
@@ -5,5 +5,24 @@
"printWidth": 150,
"bracketSpacing": false,
"endOfLine": "lf",
"tabWidth": 4
"tabWidth": 4,
"importOrder": [
"^[./]*/mocks",
"",
"<TYPES>^(node:)",
"",
"<TYPES>",
"",
"<TYPES>^[.]",
"",
"<BUILTIN_MODULES>",
"",
"<THIRD_PARTY_MODULES>",
"",
"^zigbee",
"",
"^[.]"
],
"importOrderParserPlugins": ["typescript", "decorators"],
"plugins": ["@ianvs/prettier-plugin-sort-imports"]
}
+1 -1
View File
@@ -1,3 +1,3 @@
{
".": "1.40.0"
".": "2.2.1"
}
+388
View File
@@ -1,5 +1,393 @@
# Changelog
## [2.2.1](https://github.com/Koenkk/zigbee2mqtt/compare/2.2.0...2.2.1) (2025-04-04)
### Bug Fixes
* Fix issue with some settings when migrating from 1.x.x to 2.2.x ([#26947](https://github.com/Koenkk/zigbee2mqtt/issues/26947)) ([9e481fe](https://github.com/Koenkk/zigbee2mqtt/commit/9e481fe5e57a8e664dd58cff7765b5365630c7c1))
* Fix settings being overwriting when env var is set to a ref ([#26988](https://github.com/Koenkk/zigbee2mqtt/issues/26988)) ([d6a5128](https://github.com/Koenkk/zigbee2mqtt/commit/d6a5128aa819a85e2a06e57307329d2b35938397))
* **ignore:** update zigbee-herdsman-converters to 23.19.0 ([#26938](https://github.com/Koenkk/zigbee2mqtt/issues/26938)) ([0b7d003](https://github.com/Koenkk/zigbee2mqtt/commit/0b7d0039f709215ac1a4895f15f06db41b4d4998))
* **ignore:** update zigbee-herdsman-converters to 23.19.1 ([#26961](https://github.com/Koenkk/zigbee2mqtt/issues/26961)) ([e90afb2](https://github.com/Koenkk/zigbee2mqtt/commit/e90afb286a15c5416fd1126864acdfc4c03c3a64))
* **ignore:** update zigbee-herdsman-converters to 23.20.0 ([#26978](https://github.com/Koenkk/zigbee2mqtt/issues/26978)) ([37927e6](https://github.com/Koenkk/zigbee2mqtt/commit/37927e698abcb242965423e9ee37623255feadab))
* **ignore:** update zigbee-herdsman-converters to 23.20.1 ([#26989](https://github.com/Koenkk/zigbee2mqtt/issues/26989)) ([9d0ee03](https://github.com/Koenkk/zigbee2mqtt/commit/9d0ee03f38ee6881dce3b17c219a6580303607ef))
* **ignore:** update zigbee2mqtt-frontend to 0.9.6 ([#26959](https://github.com/Koenkk/zigbee2mqtt/issues/26959)) ([fb4d01e](https://github.com/Koenkk/zigbee2mqtt/commit/fb4d01ebced7046919d6657c7609a196d39a1a6c))
* **ignore:** update zigbee2mqtt-frontend to 0.9.7 ([#26976](https://github.com/Koenkk/zigbee2mqtt/issues/26976)) ([89cdb1f](https://github.com/Koenkk/zigbee2mqtt/commit/89cdb1f180bb343b56a50c94dc6a23dda1345582))
## [2.2.0](https://github.com/Koenkk/zigbee2mqtt/compare/2.1.3...2.2.0) (2025-04-01)
### Features
* Frontend: support filtering notifications ([#26805](https://github.com/Koenkk/zigbee2mqtt/issues/26805)) ([f872a51](https://github.com/Koenkk/zigbee2mqtt/commit/f872a5180c5618b7d3e7c0d11f1283a454dcbc6b))
* Onboarding ([#26608](https://github.com/Koenkk/zigbee2mqtt/issues/26608)) ([05d6f08](https://github.com/Koenkk/zigbee2mqtt/commit/05d6f08a0dd6a5097b7d19b8de3b39e37e9a8533))
* Support ZigBee on Host adapter ([#26742](https://github.com/Koenkk/zigbee2mqtt/issues/26742)) ([92955c0](https://github.com/Koenkk/zigbee2mqtt/commit/92955c02a4de09cbed79249db8bf0e6529423b93))
* Use dynamic import for external JS ([#26522](https://github.com/Koenkk/zigbee2mqtt/issues/26522)) ([badf107](https://github.com/Koenkk/zigbee2mqtt/commit/badf1079919baa06f160a42c34e9f742a9b584c9))
### Bug Fixes
* Add device name to no converter available error message ([#26867](https://github.com/Koenkk/zigbee2mqtt/issues/26867)) ([267c399](https://github.com/Koenkk/zigbee2mqtt/commit/267c3990474dcc646e6987a108867a54b8b940db))
* Check if new commits are available in update.sh ([#26631](https://github.com/Koenkk/zigbee2mqtt/issues/26631)) ([1b6ad46](https://github.com/Koenkk/zigbee2mqtt/commit/1b6ad46462681b8f223a6de54c5996d0e8f66f64))
* Fix bridge offline message format for frontend ([#26707](https://github.com/Koenkk/zigbee2mqtt/issues/26707)) ([e398500](https://github.com/Koenkk/zigbee2mqtt/commit/e3985002c2bf374c2edd82e95aaa2da7c920b464))
* General cleanup ([#26595](https://github.com/Koenkk/zigbee2mqtt/issues/26595)) ([8775ceb](https://github.com/Koenkk/zigbee2mqtt/commit/8775ceb0c1d2bac552a345751935d0191410bfa3))
* **gnore:** Onboarding fixes ([#26824](https://github.com/Koenkk/zigbee2mqtt/issues/26824)) ([f2a1c9a](https://github.com/Koenkk/zigbee2mqtt/commit/f2a1c9a2be052ec46d845299d73ff1495d4ea4e1))
* Home Assistant: fix electrical measurements sensors category ([#26769](https://github.com/Koenkk/zigbee2mqtt/issues/26769)) ([747f6ac](https://github.com/Koenkk/zigbee2mqtt/commit/747f6ac4767522a51fb5e5cc0b796b3a92243476))
* **ignore:** fix `EACCES: permission denied, mkdir '/app/dist/external_converters'` ([7a1621a](https://github.com/Koenkk/zigbee2mqtt/commit/7a1621a9060cd0109643655df2b2833e046158fa))
* **ignore:** Fix HA addon onboarding ([#26862](https://github.com/Koenkk/zigbee2mqtt/issues/26862)) ([341c453](https://github.com/Koenkk/zigbee2mqtt/commit/341c453657d74aef61e4a44b24d081fab9671d39))
* **ignore:** Fix tests failing when zhc is linked via `pnpm link` ([#26630](https://github.com/Koenkk/zigbee2mqtt/issues/26630)) ([ad32fec](https://github.com/Koenkk/zigbee2mqtt/commit/ad32fec1d02f2d43b7dc18bf38bbd14f7c97ffde))
* **ignore:** Fix update check ([#26723](https://github.com/Koenkk/zigbee2mqtt/issues/26723)) ([fb48bd0](https://github.com/Koenkk/zigbee2mqtt/commit/fb48bd0cb9a0311302d82a7f1b9cec21a585799e))
* **ignore:** Make update check less verbose ([#26774](https://github.com/Koenkk/zigbee2mqtt/issues/26774)) ([6e781ea](https://github.com/Koenkk/zigbee2mqtt/commit/6e781eafaefa54fad24ed857e1f96261ff3bebb7))
* **ignore:** Module `NodeNext` ([#26719](https://github.com/Koenkk/zigbee2mqtt/issues/26719)) ([5db1150](https://github.com/Koenkk/zigbee2mqtt/commit/5db115057ef733a33575c889e981696d2b9e74b9))
* **ignore:** Move to `Node16` `module` ([#26704](https://github.com/Koenkk/zigbee2mqtt/issues/26704)) ([ea5e0d4](https://github.com/Koenkk/zigbee2mqtt/commit/ea5e0d47a599ac66dcf4671e7a8b0687d8a08b85))
* **ignore:** Move to Node16 module ([#26717](https://github.com/Koenkk/zigbee2mqtt/issues/26717)) ([be722a0](https://github.com/Koenkk/zigbee2mqtt/commit/be722a0500892e6787d669ceb10c7d0bfc1c7e4c))
* **ignore:** Reduce chaining in `homeassistant.ts` ([#26582](https://github.com/Koenkk/zigbee2mqtt/issues/26582)) ([4fa794a](https://github.com/Koenkk/zigbee2mqtt/commit/4fa794a6392917d0140b94257bb7cd155fc25936))
* **ignore:** Remove uncessary extensions spread ([#26581](https://github.com/Koenkk/zigbee2mqtt/issues/26581)) ([1fe2d8d](https://github.com/Koenkk/zigbee2mqtt/commit/1fe2d8dae6ace27c4b297b7943ac5501c096e8ab))
* **ignore:** update dependencies ([#26554](https://github.com/Koenkk/zigbee2mqtt/issues/26554)) ([11fb858](https://github.com/Koenkk/zigbee2mqtt/commit/11fb858ff2819319f8aa4bd8a6b2f25944619a8e))
* **ignore:** update dependencies ([#26662](https://github.com/Koenkk/zigbee2mqtt/issues/26662)) ([fb98166](https://github.com/Koenkk/zigbee2mqtt/commit/fb9816659bec15d0a9a39f73d4900fc7586a378e))
* **ignore:** update dependencies ([#26751](https://github.com/Koenkk/zigbee2mqtt/issues/26751)) ([e602dc8](https://github.com/Koenkk/zigbee2mqtt/commit/e602dc849a1d17b9097ce06015ba587c59ba5006))
* **ignore:** update dependencies ([#26833](https://github.com/Koenkk/zigbee2mqtt/issues/26833)) ([93d0ee4](https://github.com/Koenkk/zigbee2mqtt/commit/93d0ee44ba4445711b7e43ab4bfa06e8ac266d68))
* **ignore:** update dependencies ([#26911](https://github.com/Koenkk/zigbee2mqtt/issues/26911)) ([e02bbdc](https://github.com/Koenkk/zigbee2mqtt/commit/e02bbdcf0bbf8577ab6e138c915f7e2ef1a32482))
* **ignore:** update zigbee-herdsman to 3.3.0 ([#26628](https://github.com/Koenkk/zigbee2mqtt/issues/26628)) ([ed7e7c7](https://github.com/Koenkk/zigbee2mqtt/commit/ed7e7c7e530010aeb44eb71e50f31145c7285c26))
* **ignore:** update zigbee-herdsman to 3.3.1 ([#26674](https://github.com/Koenkk/zigbee2mqtt/issues/26674)) ([a5195a6](https://github.com/Koenkk/zigbee2mqtt/commit/a5195a6eee9a3e852709c4b979c965dee33f2b54))
* **ignore:** update zigbee-herdsman to 3.3.2 ([#26702](https://github.com/Koenkk/zigbee2mqtt/issues/26702)) ([8378949](https://github.com/Koenkk/zigbee2mqtt/commit/8378949e1d9cdfb3d33bc771dc61594ca7ef3c05))
* **ignore:** update zigbee-herdsman to 3.4.0 ([#26744](https://github.com/Koenkk/zigbee2mqtt/issues/26744)) ([e5a80e3](https://github.com/Koenkk/zigbee2mqtt/commit/e5a80e3b1bf8ab5f693d248f2b6a90481bdb10fc))
* **ignore:** update zigbee-herdsman to 3.4.1 ([#26779](https://github.com/Koenkk/zigbee2mqtt/issues/26779)) ([46fd043](https://github.com/Koenkk/zigbee2mqtt/commit/46fd04364ded0b6f7c9fcea6f33a2b523971b389))
* **ignore:** update zigbee-herdsman to 3.4.10 ([#26908](https://github.com/Koenkk/zigbee2mqtt/issues/26908)) ([b52cfd8](https://github.com/Koenkk/zigbee2mqtt/commit/b52cfd87c3deedfa0d1d391ce4f6f64083389559))
* **ignore:** update zigbee-herdsman to 3.4.11 ([#26924](https://github.com/Koenkk/zigbee2mqtt/issues/26924)) ([9fe38b1](https://github.com/Koenkk/zigbee2mqtt/commit/9fe38b1677681b1f3b47ac6d9876fa7305572244))
* **ignore:** update zigbee-herdsman to 3.4.2 ([#26789](https://github.com/Koenkk/zigbee2mqtt/issues/26789)) ([582bf7d](https://github.com/Koenkk/zigbee2mqtt/commit/582bf7d2c6a21003e09892b569ef5fb115f5d1ab))
* **ignore:** update zigbee-herdsman to 3.4.3 ([#26802](https://github.com/Koenkk/zigbee2mqtt/issues/26802)) ([4f2b052](https://github.com/Koenkk/zigbee2mqtt/commit/4f2b052016f4d313dfc43d35aca986b682c173b0))
* **ignore:** update zigbee-herdsman to 3.4.4 ([#26821](https://github.com/Koenkk/zigbee2mqtt/issues/26821)) ([0714582](https://github.com/Koenkk/zigbee2mqtt/commit/0714582eb975c39ef0fb5205d7f2c76ce7de2a05))
* **ignore:** update zigbee-herdsman to 3.4.5 ([#26826](https://github.com/Koenkk/zigbee2mqtt/issues/26826)) ([2b90d7b](https://github.com/Koenkk/zigbee2mqtt/commit/2b90d7b3b974353c312dc00c26b6223be74e1272))
* **ignore:** update zigbee-herdsman to 3.4.6 ([#26834](https://github.com/Koenkk/zigbee2mqtt/issues/26834)) ([9e0c84b](https://github.com/Koenkk/zigbee2mqtt/commit/9e0c84beed18d13ec310d91b89e35eba46fe3817))
* **ignore:** update zigbee-herdsman to 3.4.7 ([#26859](https://github.com/Koenkk/zigbee2mqtt/issues/26859)) ([440e3a0](https://github.com/Koenkk/zigbee2mqtt/commit/440e3a00942dc8bb7a7b9b66efd825e15dccad6c))
* **ignore:** update zigbee-herdsman to 3.4.8 ([#26866](https://github.com/Koenkk/zigbee2mqtt/issues/26866)) ([f824295](https://github.com/Koenkk/zigbee2mqtt/commit/f824295b7efefb78d6f803a4291fb2a485694c96))
* **ignore:** update zigbee-herdsman to 3.4.9 ([#26877](https://github.com/Koenkk/zigbee2mqtt/issues/26877)) ([0a41af5](https://github.com/Koenkk/zigbee2mqtt/commit/0a41af5dd23d1806d703b878feaba25d90c2d016))
* **ignore:** update zigbee-herdsman-converters to 23.10.0 ([#26792](https://github.com/Koenkk/zigbee2mqtt/issues/26792)) ([52c255b](https://github.com/Koenkk/zigbee2mqtt/commit/52c255bcc2a37a7238afe675f7c521c0834e2d9d))
* **ignore:** update zigbee-herdsman-converters to 23.11.0 ([#26820](https://github.com/Koenkk/zigbee2mqtt/issues/26820)) ([5288549](https://github.com/Koenkk/zigbee2mqtt/commit/5288549744795296a9919828b1a85b1223edd755))
* **ignore:** update zigbee-herdsman-converters to 23.12.0 ([#26835](https://github.com/Koenkk/zigbee2mqtt/issues/26835)) ([53e8226](https://github.com/Koenkk/zigbee2mqtt/commit/53e8226f0aff0cdfba76f6f30672c0bd3d72d52d))
* **ignore:** update zigbee-herdsman-converters to 23.13.0 ([#26861](https://github.com/Koenkk/zigbee2mqtt/issues/26861)) ([c5c7f70](https://github.com/Koenkk/zigbee2mqtt/commit/c5c7f70ad6aaca083c4edb4dba783e60f8ec17a5))
* **ignore:** update zigbee-herdsman-converters to 23.14.0 ([#26886](https://github.com/Koenkk/zigbee2mqtt/issues/26886)) ([6a65769](https://github.com/Koenkk/zigbee2mqtt/commit/6a657690604daacc4a2cc1afa8407fd0d4dc9eaf))
* **ignore:** update zigbee-herdsman-converters to 23.15.0 ([#26900](https://github.com/Koenkk/zigbee2mqtt/issues/26900)) ([516425b](https://github.com/Koenkk/zigbee2mqtt/commit/516425b44b7f395db7bbed93699b989d6a8276be))
* **ignore:** update zigbee-herdsman-converters to 23.16.0 ([#26918](https://github.com/Koenkk/zigbee2mqtt/issues/26918)) ([6e6a989](https://github.com/Koenkk/zigbee2mqtt/commit/6e6a989159383a3534ce6d12ea8c9bdc9092d2bc))
* **ignore:** update zigbee-herdsman-converters to 23.17.0 ([#26925](https://github.com/Koenkk/zigbee2mqtt/issues/26925)) ([ea04f19](https://github.com/Koenkk/zigbee2mqtt/commit/ea04f197090e3a3bc52dc4eb4ecde65146484bfb))
* **ignore:** update zigbee-herdsman-converters to 23.18.0 ([#26933](https://github.com/Koenkk/zigbee2mqtt/issues/26933)) ([69f0840](https://github.com/Koenkk/zigbee2mqtt/commit/69f0840feb2f8a5332148cfe271d16e413a02ebf))
* **ignore:** update zigbee-herdsman-converters to 23.3.0 ([#26598](https://github.com/Koenkk/zigbee2mqtt/issues/26598)) ([3941692](https://github.com/Koenkk/zigbee2mqtt/commit/3941692665d96fee575995bb6a372bfb92e0a466))
* **ignore:** update zigbee-herdsman-converters to 23.4.0 ([#26629](https://github.com/Koenkk/zigbee2mqtt/issues/26629)) ([c603f42](https://github.com/Koenkk/zigbee2mqtt/commit/c603f42c889b5508a56f0f43df7d09dba909b0cd))
* **ignore:** update zigbee-herdsman-converters to 23.6.0 ([#26691](https://github.com/Koenkk/zigbee2mqtt/issues/26691)) ([06ba62b](https://github.com/Koenkk/zigbee2mqtt/commit/06ba62b8331d8d5db1696c47bd3b643449584b16))
* **ignore:** update zigbee-herdsman-converters to 23.7.0 ([#26733](https://github.com/Koenkk/zigbee2mqtt/issues/26733)) ([8d2ef6d](https://github.com/Koenkk/zigbee2mqtt/commit/8d2ef6df06bbfcc856a91ef592e8ac54f46a705c))
* **ignore:** update zigbee-herdsman-converters to 23.8.0 ([#26761](https://github.com/Koenkk/zigbee2mqtt/issues/26761)) ([325199d](https://github.com/Koenkk/zigbee2mqtt/commit/325199d4455adb32f912687827ed01d89e009bc1))
* **ignore:** update zigbee2mqtt-frontend to 0.9.5 ([#26814](https://github.com/Koenkk/zigbee2mqtt/issues/26814)) ([9792466](https://github.com/Koenkk/zigbee2mqtt/commit/9792466aac8b94747fe41fe47db766b7a7e93367))
* Implement systemd-notify directly ([#26456](https://github.com/Koenkk/zigbee2mqtt/issues/26456)) ([bfeac07](https://github.com/Koenkk/zigbee2mqtt/commit/bfeac07c4c38fb55db99d5f8204e85ec54552564))
* Use dynamic import for optional extensions ([#26735](https://github.com/Koenkk/zigbee2mqtt/issues/26735)) ([db578d9](https://github.com/Koenkk/zigbee2mqtt/commit/db578d9d1180c01c14f1b4707e779c28961eb5b8))
* Write `ZIGBEE2MQTT_CONFIG_*` envs to yaml as overrides ([#26653](https://github.com/Koenkk/zigbee2mqtt/issues/26653)) ([f6f7ba6](https://github.com/Koenkk/zigbee2mqtt/commit/f6f7ba68c9dd84ccdd82df0769b197756e42bbf7))
## [2.1.3](https://github.com/Koenkk/zigbee2mqtt/compare/2.1.2...2.1.3) (2025-03-02)
### Bug Fixes
* Home Assistant: fix brightness only lights ([#26560](https://github.com/Koenkk/zigbee2mqtt/issues/26560)) ([7e7057e](https://github.com/Koenkk/zigbee2mqtt/commit/7e7057eccd1fe638dd73b4a4f303c6612b73c89d))
## [2.1.2](https://github.com/Koenkk/zigbee2mqtt/compare/2.1.1...2.1.2) (2025-03-01)
### Bug Fixes
* Add Home Assistant device class to external_temperature_input entity ([#26535](https://github.com/Koenkk/zigbee2mqtt/issues/26535)) ([e8031ef](https://github.com/Koenkk/zigbee2mqtt/commit/e8031efd2d95658c5f3fe72ee6e2f95dd584b5e8))
* Allow configuring number of folders of old logs to keep ([#26398](https://github.com/Koenkk/zigbee2mqtt/issues/26398)) ([845e03e](https://github.com/Koenkk/zigbee2mqtt/commit/845e03e48acf1367feadeaae986f3c987bbc164d))
* Fix Docker build hanging ([#26547](https://github.com/Koenkk/zigbee2mqtt/issues/26547)) ([e050e20](https://github.com/Koenkk/zigbee2mqtt/commit/e050e209b0d273cad6ad0efcf20761f33963da7b))
* Fix HA MQTT dict entry indicating that an update is progress ([#26267](https://github.com/Koenkk/zigbee2mqtt/issues/26267)) ([b35cdc3](https://github.com/Koenkk/zigbee2mqtt/commit/b35cdc347423b219722c9f9dd0d2a82ded8eb24f))
* Home Assistant: add `brightness` to `supported_color_modes` ([#26545](https://github.com/Koenkk/zigbee2mqtt/issues/26545)) ([063aba1](https://github.com/Koenkk/zigbee2mqtt/commit/063aba1b6c72901ed2d51a367485092691c358da))
* Home Assistant: add `is_updating` attribute ([#26231](https://github.com/Koenkk/zigbee2mqtt/issues/26231)) ([1eda69a](https://github.com/Koenkk/zigbee2mqtt/commit/1eda69a50a8f92c3471acdbdc82b41055d5992f2))
* Home Assistant: add missing `device_class` for some `current` and `power` entities ([#26324](https://github.com/Koenkk/zigbee2mqtt/issues/26324)) ([1e656c2](https://github.com/Koenkk/zigbee2mqtt/commit/1e656c2114d58b96fd7b7750e2e7d255a9175653))
* Home Assistant: support speed-controlled fans ([#24483](https://github.com/Koenkk/zigbee2mqtt/issues/24483)) ([7ce5b58](https://github.com/Koenkk/zigbee2mqtt/commit/7ce5b581aa57d828043a1ca75ef7efe7b7bf1619))
* **ignore:** Add `publish` to `meta` of toZigbee converters ([#26490](https://github.com/Koenkk/zigbee2mqtt/issues/26490)) ([07145ac](https://github.com/Koenkk/zigbee2mqtt/commit/07145accbae6c481f6d6740e66e27ab1baf5eb6d))
* **ignore:** remove obsolete @types/eslint__js ([d522d2c](https://github.com/Koenkk/zigbee2mqtt/commit/d522d2cd18b8cb77fa50cd457823cfa573622cb7))
* **ignore:** update dependencies ([#26266](https://github.com/Koenkk/zigbee2mqtt/issues/26266)) ([3e3ceff](https://github.com/Koenkk/zigbee2mqtt/commit/3e3cefff1b7f85f59b6bf529f6593339498561a1))
* **ignore:** update dependencies ([#26383](https://github.com/Koenkk/zigbee2mqtt/issues/26383)) ([758c566](https://github.com/Koenkk/zigbee2mqtt/commit/758c56647d353e81ea22a34c2fa2665ed76dc149))
* **ignore:** update dependencies ([#26478](https://github.com/Koenkk/zigbee2mqtt/issues/26478)) ([d2166f2](https://github.com/Koenkk/zigbee2mqtt/commit/d2166f2f4fa1e1e286face994ec4825a70144908))
* **ignore:** update zigbee-herdsman to 3.2.6 ([#26303](https://github.com/Koenkk/zigbee2mqtt/issues/26303)) ([49fe147](https://github.com/Koenkk/zigbee2mqtt/commit/49fe1479213c928786c64adeb3031ca6fe91250d))
* **ignore:** update zigbee-herdsman to 3.2.7 ([#26498](https://github.com/Koenkk/zigbee2mqtt/issues/26498)) ([4e650b7](https://github.com/Koenkk/zigbee2mqtt/commit/4e650b72cd187488893213018b619e933835a58e))
* **ignore:** update zigbee-herdsman-converters to 21.32.0 ([#26245](https://github.com/Koenkk/zigbee2mqtt/issues/26245)) ([8542b35](https://github.com/Koenkk/zigbee2mqtt/commit/8542b35706cefc87c9ec9dccb2980302ea25f100))
* **ignore:** update zigbee-herdsman-converters to 21.33.0 ([#26258](https://github.com/Koenkk/zigbee2mqtt/issues/26258)) ([97c7485](https://github.com/Koenkk/zigbee2mqtt/commit/97c74852c95536945e33d0388dc5fcd0b132cd7e))
* **ignore:** update zigbee-herdsman-converters to 21.34.0 ([#26285](https://github.com/Koenkk/zigbee2mqtt/issues/26285)) ([7a0964e](https://github.com/Koenkk/zigbee2mqtt/commit/7a0964ebc905903b4ca5b168d9bd1041b004b4cf))
* **ignore:** update zigbee-herdsman-converters to 21.35.0 ([#26305](https://github.com/Koenkk/zigbee2mqtt/issues/26305)) ([30d13d6](https://github.com/Koenkk/zigbee2mqtt/commit/30d13d6e5ee9678f7da812bd287128824a535d48))
* **ignore:** update zigbee-herdsman-converters to 21.36.0 ([#26333](https://github.com/Koenkk/zigbee2mqtt/issues/26333)) ([ba76533](https://github.com/Koenkk/zigbee2mqtt/commit/ba76533c2f241c8362f43ad07d10b2fe084479a0))
* **ignore:** update zigbee-herdsman-converters to 21.37.0 ([#26367](https://github.com/Koenkk/zigbee2mqtt/issues/26367)) ([dec5ca8](https://github.com/Koenkk/zigbee2mqtt/commit/dec5ca8b3a3703e6289652192657af9a035fe026))
* **ignore:** update zigbee-herdsman-converters to 21.38.0 ([#26400](https://github.com/Koenkk/zigbee2mqtt/issues/26400)) ([93af062](https://github.com/Koenkk/zigbee2mqtt/commit/93af062a9b84358c866a5a1191ae5895fc6e866f))
* **ignore:** update zigbee-herdsman-converters to 22.0.1 ([#26418](https://github.com/Koenkk/zigbee2mqtt/issues/26418)) ([7d63a9a](https://github.com/Koenkk/zigbee2mqtt/commit/7d63a9a0f29445044bcfe0f3a4b629266db37654))
* **ignore:** update zigbee-herdsman-converters to 22.1.0 ([#26445](https://github.com/Koenkk/zigbee2mqtt/issues/26445)) ([40f4147](https://github.com/Koenkk/zigbee2mqtt/commit/40f41477e776b4e75434251d03bf2844fceb7b13))
* **ignore:** update zigbee-herdsman-converters to 22.2.0 ([#26469](https://github.com/Koenkk/zigbee2mqtt/issues/26469)) ([67e1a02](https://github.com/Koenkk/zigbee2mqtt/commit/67e1a0252517da28629a4c97b4dd610624d451a1))
* **ignore:** update zigbee-herdsman-converters to 23.0.0 ([44f9fe7](https://github.com/Koenkk/zigbee2mqtt/commit/44f9fe7a1494a9450e247ad353ce788c621f901c))
* **ignore:** update zigbee-herdsman-converters to 23.1.0 ([#26509](https://github.com/Koenkk/zigbee2mqtt/issues/26509)) ([db7b51a](https://github.com/Koenkk/zigbee2mqtt/commit/db7b51ac801829801656d978aff2605d8bd7b56a))
* **ignore:** update zigbee-herdsman-converters to 23.1.1 ([#26510](https://github.com/Koenkk/zigbee2mqtt/issues/26510)) ([102857a](https://github.com/Koenkk/zigbee2mqtt/commit/102857a7fec60e3f65887d92b6fe7a2d81add110))
* **ignore:** update zigbee-herdsman-converters to 23.2.0 ([#26549](https://github.com/Koenkk/zigbee2mqtt/issues/26549)) ([9f72c5c](https://github.com/Koenkk/zigbee2mqtt/commit/9f72c5c4555bda5b347a6a5406f619b9668813ec))
* Make compatible with pnpm 10 ([#26546](https://github.com/Koenkk/zigbee2mqtt/issues/26546)) ([29d25ae](https://github.com/Koenkk/zigbee2mqtt/commit/29d25aed66a66912fb55ede8751ba8056efb673f))
* Support FreeBSD for `update.sh` ([#26261](https://github.com/Koenkk/zigbee2mqtt/issues/26261)) ([5e55ef0](https://github.com/Koenkk/zigbee2mqtt/commit/5e55ef045427f78b410e3d292387c3592366f698))
## [2.1.1](https://github.com/Koenkk/zigbee2mqtt/compare/2.1.0...2.1.1) (2025-02-06)
### Bug Fixes
* **ignore:** update dependencies ([#26126](https://github.com/Koenkk/zigbee2mqtt/issues/26126)) ([b960775](https://github.com/Koenkk/zigbee2mqtt/commit/b960775160b33dcd276376efaa7dc1ca3f791a53))
* Rename `internal_temperature` to `internalTemperature` to correct typo ([#26114](https://github.com/Koenkk/zigbee2mqtt/issues/26114)) ([e4e4f16](https://github.com/Koenkk/zigbee2mqtt/commit/e4e4f16ec8994a63ba148b36d7e18a09f49ac298))
## [2.1.0](https://github.com/Koenkk/zigbee2mqtt/compare/2.0.0...2.1.0) (2025-02-01)
### Features
* Add a settings option to log to console in json format ([#25649](https://github.com/Koenkk/zigbee2mqtt/issues/25649)) ([26ef565](https://github.com/Koenkk/zigbee2mqtt/commit/26ef565c8a20d1b6c2e09c679eb96d3f56811a87))
* Allow Home Assistant OTA Update entity to show progress while updating ([#25632](https://github.com/Koenkk/zigbee2mqtt/issues/25632)) ([318dc04](https://github.com/Koenkk/zigbee2mqtt/commit/318dc0413284d204d13a99afd400a5f00f5ef338))
### Bug Fixes
* Add Home Assistant device class for flow rates (e.g., Sonoff SWV) ([#26035](https://github.com/Koenkk/zigbee2mqtt/issues/26035)) ([cdbd553](https://github.com/Koenkk/zigbee2mqtt/commit/cdbd55320acbf7d857c518e8aac8ab9e495eb6b5))
* Add semver tags to Docker releases ([#25530](https://github.com/Koenkk/zigbee2mqtt/issues/25530)) ([0d30eb5](https://github.com/Koenkk/zigbee2mqtt/commit/0d30eb553b9cb027f8ae5424b9e740136742f74c))
* Allow to set throttle via frontend ([#25997](https://github.com/Koenkk/zigbee2mqtt/issues/25997)) ([5cf4547](https://github.com/Koenkk/zigbee2mqtt/commit/5cf4547fe04e9a5627826225c3b26586aea3edc1))
* Container: remove modifying capabilities of `/usr/bin/node` ([#25456](https://github.com/Koenkk/zigbee2mqtt/issues/25456)) ([43a5b24](https://github.com/Koenkk/zigbee2mqtt/commit/43a5b24dcc22af3ec4c0bb76b45a2706f898421f))
* Enable use of MQTT username without password ([#25611](https://github.com/Koenkk/zigbee2mqtt/issues/25611)) ([a28a5ba](https://github.com/Koenkk/zigbee2mqtt/commit/a28a5baf17b8dd3fe4703cdb38d80796157521af))
* Fix `Error: write after end` error ([#25737](https://github.com/Koenkk/zigbee2mqtt/issues/25737)) ([2d5e7bf](https://github.com/Koenkk/zigbee2mqtt/commit/2d5e7bf9ff611387f2334a55caab6991d65ad3fa))
* Fix `Failed to call 'Frontend' 'stop' (TypeError: Cannot read properties of undefined (reading 'close')` https://github.com/Koenkk/zigbee2mqtt/issues/25715 ([9daeba1](https://github.com/Koenkk/zigbee2mqtt/commit/9daeba1f0ab350d9609bcd4c1e8bc291b5eaab8b))
* Fix duplicate `action` publish when `advanced.output` has `attribute` ([#25963](https://github.com/Koenkk/zigbee2mqtt/issues/25963)) ([2af65cf](https://github.com/Koenkk/zigbee2mqtt/commit/2af65cf28eea397013a0f629761536339d6ace63))
* **ignore:** Ensure config example is never out of sync with settings ([#25707](https://github.com/Koenkk/zigbee2mqtt/issues/25707)) ([23b709c](https://github.com/Koenkk/zigbee2mqtt/commit/23b709cee28a5926f1608e70e30703ba52011005))
* **ignore:** Remove `adapter` `auto` from schema ([#25896](https://github.com/Koenkk/zigbee2mqtt/issues/25896)) ([5e60f1d](https://github.com/Koenkk/zigbee2mqtt/commit/5e60f1dc8dae96a04d1a76a677f89f426da64a94))
* **ignore:** update dependencies ([#25535](https://github.com/Koenkk/zigbee2mqtt/issues/25535)) ([6f3524b](https://github.com/Koenkk/zigbee2mqtt/commit/6f3524b251ed8218ffed132df262eb0850687e61))
* **ignore:** update dependencies ([#25760](https://github.com/Koenkk/zigbee2mqtt/issues/25760)) ([5b82d7e](https://github.com/Koenkk/zigbee2mqtt/commit/5b82d7e4df9fdc868689f8a745e502b5beea837a))
* **ignore:** update dependencies ([#25890](https://github.com/Koenkk/zigbee2mqtt/issues/25890)) ([8da5fa6](https://github.com/Koenkk/zigbee2mqtt/commit/8da5fa663d2fbf4a68e797cff9938b57dd620935))
* **ignore:** update dependencies ([#26005](https://github.com/Koenkk/zigbee2mqtt/issues/26005)) ([85460cd](https://github.com/Koenkk/zigbee2mqtt/commit/85460cd5ce2a17a2036a54d3984a5f59b75be41b))
* Long startup time when `OnEvent` 'start' times out (repeatedly) ([#25693](https://github.com/Koenkk/zigbee2mqtt/issues/25693)) ([f1f73bd](https://github.com/Koenkk/zigbee2mqtt/commit/f1f73bd46425262fc1ff7e897d879faf16e3d8a9))
* Only use endpoint-specific states if the device definition uses them ([#26019](https://github.com/Koenkk/zigbee2mqtt/issues/26019)) ([0462b94](https://github.com/Koenkk/zigbee2mqtt/commit/0462b94f5a346e64a85a90cb6c7e33a36ac98311))
* Remove invalid `warn` `log_level` https://github.com/nurikk/zigbee2mqtt-frontend/issues/2369 ([#25634](https://github.com/Koenkk/zigbee2mqtt/issues/25634)) ([1667ba6](https://github.com/Koenkk/zigbee2mqtt/commit/1667ba64ac2b8272cb0dea7ce30e459450e1c847))
## [2.0.0](https://github.com/Koenkk/zigbee2mqtt/compare/1.42.0...2.0.0) (2025-01-03)
### ⚠ BREAKING CHANGES
* Change default `homeassistant.status_topic` to `homeassistant/status` ([#25286](https://github.com/Koenkk/zigbee2mqtt/issues/25286))
* Docker: update Alpine to 3.21, drop x86 support ([#25171](https://github.com/Koenkk/zigbee2mqtt/issues/25171))
* Remove `permit_join_timeout` ([#25082](https://github.com/Koenkk/zigbee2mqtt/issues/25082))
* Home Assistant: remove duplicate `sensor`/`select` for `select`/`number`/`button` entities ([#25026](https://github.com/Koenkk/zigbee2mqtt/issues/25026))
* Ensure `data` in response is always empty on `error` ([#24971](https://github.com/Koenkk/zigbee2mqtt/issues/24971))
* Rework OTA ([#24634](https://github.com/Koenkk/zigbee2mqtt/issues/24634))
* Remove deprecated Home Assistant integration features ([#24494](https://github.com/Koenkk/zigbee2mqtt/issues/24494))
* Improve bind/bridge/groups parsing and resolving consistency ([#24432](https://github.com/Koenkk/zigbee2mqtt/issues/24432))
* Remove configuring group members through `configuration.yaml` ([#24338](https://github.com/Koenkk/zigbee2mqtt/issues/24338))
* Improve permit join ([#24257](https://github.com/Koenkk/zigbee2mqtt/issues/24257))
* The big cleanup ([#24200](https://github.com/Koenkk/zigbee2mqtt/issues/24200))
* HA permit join switch: disable automatically after 254 seconds ([#24245](https://github.com/Koenkk/zigbee2mqtt/issues/24245))
### Features
* Automatic settings migration ([#24871](https://github.com/Koenkk/zigbee2mqtt/issues/24871)) ([c6958c4](https://github.com/Koenkk/zigbee2mqtt/commit/c6958c4991c2cfbac87907c78e75bc634401a894))
* Docker: RISC-V support ([#22824](https://github.com/Koenkk/zigbee2mqtt/issues/22824)) ([e23d300](https://github.com/Koenkk/zigbee2mqtt/commit/e23d300fd9f5c2380560b2d07071cb332ea93339))
* Home Assistant: support endpoints for locks ([#25359](https://github.com/Koenkk/zigbee2mqtt/issues/25359)) ([c586df8](https://github.com/Koenkk/zigbee2mqtt/commit/c586df82429546fba56ab10b579bf225bb33ebb2))
* Live loading of external JS (converters/extensions) ([#24764](https://github.com/Koenkk/zigbee2mqtt/issues/24764)) ([9907005](https://github.com/Koenkk/zigbee2mqtt/commit/9907005256c65902cbb9475e9410b795b262b41a))
* The big cleanup ([#24200](https://github.com/Koenkk/zigbee2mqtt/issues/24200)) ([b618662](https://github.com/Koenkk/zigbee2mqtt/commit/b6186623f0bcf9d88de1772aebd8a9eaead21a4c))
* Typing for MQTT API ([#25098](https://github.com/Koenkk/zigbee2mqtt/issues/25098)) ([1a9c79b](https://github.com/Koenkk/zigbee2mqtt/commit/1a9c79bb2f9b5e123b8cd017e82498ab534596fd))
* Use Vitest for testing ([#25284](https://github.com/Koenkk/zigbee2mqtt/issues/25284)) ([06f4453](https://github.com/Koenkk/zigbee2mqtt/commit/06f44533af488cd632d4eec78e2c629d8d6ef278))
### Bug Fixes
* Add `cap_net_bind_service=+ep` to `/usr/bin/node` in Docker container ([#25385](https://github.com/Koenkk/zigbee2mqtt/issues/25385)) ([#25387](https://github.com/Koenkk/zigbee2mqtt/issues/25387)) ([61864f0](https://github.com/Koenkk/zigbee2mqtt/commit/61864f0fb2c7ffc90bbb85c969ae5b589dbbb92b))
* Add more OCI Image Labels ([#25074](https://github.com/Koenkk/zigbee2mqtt/issues/25074)) ([b370b80](https://github.com/Koenkk/zigbee2mqtt/commit/b370b80dfb6701b8601c32f44102aec60a5d5ac8))
* Change default `homeassistant.status_topic` to `homeassistant/status` ([#25286](https://github.com/Koenkk/zigbee2mqtt/issues/25286)) ([2abf55c](https://github.com/Koenkk/zigbee2mqtt/commit/2abf55ccf8de35c2ec3463e29c31a80a35d7392b))
* Cleanup Dockerfile ([23d8cea](https://github.com/Koenkk/zigbee2mqtt/commit/23d8cea4e243429bf5a376db94d09f535776de49))
* Cleanup leftover legacy settings logic ([#24947](https://github.com/Koenkk/zigbee2mqtt/issues/24947)) ([d78c6fa](https://github.com/Koenkk/zigbee2mqtt/commit/d78c6facab57e0ec5676eb76ec099f5dce0f801a))
* Docker: update Alpine to 3.21, drop x86 support ([#25171](https://github.com/Koenkk/zigbee2mqtt/issues/25171)) ([32c341c](https://github.com/Koenkk/zigbee2mqtt/commit/32c341c7f58ef920e99940f0bf4b2218301dc377))
* Ensure `data` in response is always empty on `error` ([#24971](https://github.com/Koenkk/zigbee2mqtt/issues/24971)) ([a6b3605](https://github.com/Koenkk/zigbee2mqtt/commit/a6b36051f032a2acebefe0232a20abebfb9641be))
* Fix `Cannot read properties of null (reading 'toString')` when Home Assistant event entities are enabled https://github.com/Koenkk/zigbee2mqtt/issues/25133 ([9eea58e](https://github.com/Koenkk/zigbee2mqtt/commit/9eea58eba471b9e670dff327a8666e9c78b223e0))
* Fix dual cover Home Assistant discovery ([#25240](https://github.com/Koenkk/zigbee2mqtt/issues/25240)) ([ef6524b](https://github.com/Koenkk/zigbee2mqtt/commit/ef6524b93fb8e77ab2944715c2a2d3f3e3bd2923))
* Fix group with only multi endpoints devices not being controllable ([#25156](https://github.com/Koenkk/zigbee2mqtt/issues/25156)) ([7242889](https://github.com/Koenkk/zigbee2mqtt/commit/72428897a7ab78057aa3aafa5d2a0e112e6a2a73))
* Fix Home Assistant event entities for openHAB ([#25140](https://github.com/Koenkk/zigbee2mqtt/issues/25140)) ([226945c](https://github.com/Koenkk/zigbee2mqtt/commit/226945cae031007a86865dbbe22a42e951999f03))
* Fix log directories not being deleted ([#25282](https://github.com/Koenkk/zigbee2mqtt/issues/25282)) ([7170215](https://github.com/Koenkk/zigbee2mqtt/commit/7170215e96d30259d7673cb9ae77c0d6d741431d))
* Fix URL in startup failed bailout message ([#25339](https://github.com/Koenkk/zigbee2mqtt/issues/25339)) ([bcec40f](https://github.com/Koenkk/zigbee2mqtt/commit/bcec40f84a73142efef41d239fea0c77ce4bdfa3))
* Fixed calculation of group state when including non-default endpoints ([#25059](https://github.com/Koenkk/zigbee2mqtt/issues/25059)) ([9227e13](https://github.com/Koenkk/zigbee2mqtt/commit/9227e139de86299eb3ec4b1585bdd21448bfa94a))
* HA permit join switch: disable automatically after 254 seconds ([#24245](https://github.com/Koenkk/zigbee2mqtt/issues/24245)) ([3220b27](https://github.com/Koenkk/zigbee2mqtt/commit/3220b27c8a91fc65912d299db22b8ac489b929b2))
* Home Assistant: expose the internal_temperature sensor with the correct device class ([#25289](https://github.com/Koenkk/zigbee2mqtt/issues/25289)) ([5d059b7](https://github.com/Koenkk/zigbee2mqtt/commit/5d059b7bcb7f710df93e34b2bb56a1e880757c05))
* Home Assistant: remove duplicate `sensor`/`select` for `select`/`number`/`button` entities ([#25026](https://github.com/Koenkk/zigbee2mqtt/issues/25026)) ([ba45445](https://github.com/Koenkk/zigbee2mqtt/commit/ba45445544948ad74e3db67ffee1d23996a37fb5))
* Home Assistant: use extended device identifiers ([#25389](https://github.com/Koenkk/zigbee2mqtt/issues/25389)) ([60c37c1](https://github.com/Koenkk/zigbee2mqtt/commit/60c37c11d0a2e22e0bec0b7c50d5725f3b062b05))
* **ignore:** Add back `linux/386` ([#25187](https://github.com/Koenkk/zigbee2mqtt/issues/25187)) ([a2027d7](https://github.com/Koenkk/zigbee2mqtt/commit/a2027d79e899e8db22d3ca090d28d1fa4b9e3bc6))
* **ignore:** Add back Home Assistant legacy action sensor ([#25192](https://github.com/Koenkk/zigbee2mqtt/issues/25192)) ([39c007d](https://github.com/Koenkk/zigbee2mqtt/commit/39c007d211453b3dc13335edba2396c13163ae96))
* **ignore:** add cap_net_bind_service=+ep to /usr/bin/node ([#25385](https://github.com/Koenkk/zigbee2mqtt/issues/25385)) ([#25424](https://github.com/Koenkk/zigbee2mqtt/issues/25424)) ([7cf01e9](https://github.com/Koenkk/zigbee2mqtt/commit/7cf01e988209b78660ae1cb6fef609906db1df0b))
* **ignore:** Changes for improved adapter discovery ([#24147](https://github.com/Koenkk/zigbee2mqtt/issues/24147)) ([98b9770](https://github.com/Koenkk/zigbee2mqtt/commit/98b977030ec73e5a75c28f4f779fce9189b12d13))
* **ignore:** Fix 2 to 3 migration enabling frontend/availabilty/homeassistant when not set ([#25349](https://github.com/Koenkk/zigbee2mqtt/issues/25349)) ([02666e9](https://github.com/Koenkk/zigbee2mqtt/commit/02666e91393ec304e1098c01cd468889cccbd9d6))
* **ignore:** Fix availabilty, frontend and homeassistant cannot be disabled via the frontend https://github.com/nurikk/zigbee2mqtt-frontend/issues/2339 ([ed75553](https://github.com/Koenkk/zigbee2mqtt/commit/ed7555360392e07b0ae22167dc28e4996e2148cc))
* **ignore:** Fix external converter loading ([#25063](https://github.com/Koenkk/zigbee2mqtt/issues/25063)) ([51f364a](https://github.com/Koenkk/zigbee2mqtt/commit/51f364a1d5dfd46f50830c6412ae78fb74994929))
* **ignore:** fix tests ([b581dc6](https://github.com/Koenkk/zigbee2mqtt/commit/b581dc6994c92e125b9a83a69ae74f23f4dc20b9))
* **ignore:** Fixed additional case of calculation of group state when including no… ([#25104](https://github.com/Koenkk/zigbee2mqtt/issues/25104)) ([d891320](https://github.com/Koenkk/zigbee2mqtt/commit/d891320314b48d463d2926da8fb1f674913efcc9))
* **ignore:** Force rebuild of @serialport/bindings-cpp in Docker ([#25202](https://github.com/Koenkk/zigbee2mqtt/issues/25202)) ([0b62aef](https://github.com/Koenkk/zigbee2mqtt/commit/0b62aefbb28d0376f89920f962a88ec5728e0b43))
* **ignore:** import using `node:` prefix for built-in ([#25248](https://github.com/Koenkk/zigbee2mqtt/issues/25248)) ([758ab34](https://github.com/Koenkk/zigbee2mqtt/commit/758ab34747baeaf62e7a1552cd452c4445279ff1))
* **ignore:** Improvements for [#25299](https://github.com/Koenkk/zigbee2mqtt/issues/25299) ([#25364](https://github.com/Koenkk/zigbee2mqtt/issues/25364)) ([1e7e00e](https://github.com/Koenkk/zigbee2mqtt/commit/1e7e00e8ddd2e491e6ba7d8b8f31121f15f66d31))
* **ignore:** Minor cleanup ([#25166](https://github.com/Koenkk/zigbee2mqtt/issues/25166)) ([ec13ebd](https://github.com/Koenkk/zigbee2mqtt/commit/ec13ebd3ad461c3dc06bafda23d1bb87a94f3768))
* **ignore:** overrides ([#25246](https://github.com/Koenkk/zigbee2mqtt/issues/25246)) ([c88f630](https://github.com/Koenkk/zigbee2mqtt/commit/c88f630299714d7496165fa1701d2906689e5180))
* **ignore:** Rebuild serialport, attempt to fix [#25092](https://github.com/Koenkk/zigbee2mqtt/issues/25092) ([146b6c3](https://github.com/Koenkk/zigbee2mqtt/commit/146b6c386041b5139e3a1dff0688c7bbaaa88ea1))
* **ignore:** remove `illuminance_lux` from cached payload https://github.com/Koenkk/zigbee2mqtt/issues/25379 ([2713125](https://github.com/Koenkk/zigbee2mqtt/commit/271312576824ea9315373be72d1a6480ab1e8db3))
* **ignore:** remove dep in favor of built-in ([#25250](https://github.com/Koenkk/zigbee2mqtt/issues/25250)) ([1c75eef](https://github.com/Koenkk/zigbee2mqtt/commit/1c75eef16d1ed0c1748b23fcc9f9b39091b7b9b6))
* **ignore:** Remove external definition type ([8aef77e](https://github.com/Koenkk/zigbee2mqtt/commit/8aef77e57e76aeb23ba9fd5f848ea5ee50c14a56))
* **ignore:** Small cleanup ([#24696](https://github.com/Koenkk/zigbee2mqtt/issues/24696)) ([95279b0](https://github.com/Koenkk/zigbee2mqtt/commit/95279b02afe87788c2080ad7b9b2df2c5cbb5f7b))
* **ignore:** some fixes ([cc61f67](https://github.com/Koenkk/zigbee2mqtt/commit/cc61f672155918c11ec7ac6c48096b9f0629695e))
* **ignore:** update dependencies ([#24999](https://github.com/Koenkk/zigbee2mqtt/issues/24999)) ([32e10d8](https://github.com/Koenkk/zigbee2mqtt/commit/32e10d89f48871d8646f31772569fddab6d6824a))
* **ignore:** update dependencies ([#25120](https://github.com/Koenkk/zigbee2mqtt/issues/25120)) ([7803a16](https://github.com/Koenkk/zigbee2mqtt/commit/7803a166d233ba0067e05ddf2afd35569f8d07e7))
* **ignore:** update dependencies ([#25210](https://github.com/Koenkk/zigbee2mqtt/issues/25210)) ([56746de](https://github.com/Koenkk/zigbee2mqtt/commit/56746ded19013d47c681da142444b3c69bfd8992))
* **ignore:** update dependencies ([#25288](https://github.com/Koenkk/zigbee2mqtt/issues/25288)) ([2128e83](https://github.com/Koenkk/zigbee2mqtt/commit/2128e8323cae6a79774d570ba36f0077586d09e4))
* **ignore:** update dependencies ([#25360](https://github.com/Koenkk/zigbee2mqtt/issues/25360)) ([47d73eb](https://github.com/Koenkk/zigbee2mqtt/commit/47d73eb23a3c452f1757587184ba5a066f0e5edc))
* **ignore:** update deps ([1ce45c8](https://github.com/Koenkk/zigbee2mqtt/commit/1ce45c8c4176a17d7aa03117fc6f39510b06f4a4))
* **ignore:** Update usb discovery discussion link ([9bd20af](https://github.com/Koenkk/zigbee2mqtt/commit/9bd20afb9c7172a67c2d36f373ab151f20027cb9))
* **ignore:** update zigbee-herdsman to 3.0.3 ([0a20e4a](https://github.com/Koenkk/zigbee2mqtt/commit/0a20e4a455ef1e25ab7134d6901512cb4980fe93))
* **ignore:** Update zigbee-herdsman-converters to 21.4.0 ([#25191](https://github.com/Koenkk/zigbee2mqtt/issues/25191)) ([5e71cf2](https://github.com/Koenkk/zigbee2mqtt/commit/5e71cf2cc84cd6c5c1c24d801a1f2dadee4e7068))
* **ignore:** Use new exports from ZH ([#25350](https://github.com/Koenkk/zigbee2mqtt/issues/25350)) ([120a5b4](https://github.com/Koenkk/zigbee2mqtt/commit/120a5b43e90a8fb206263e937c0031c3c1163da7))
* Improve bind/bridge/groups parsing and resolving consistency ([#24432](https://github.com/Koenkk/zigbee2mqtt/issues/24432)) ([9537ef0](https://github.com/Koenkk/zigbee2mqtt/commit/9537ef05d563de815b24e45176a1bec68eb811b8))
* Improve device icon serving ([#25299](https://github.com/Koenkk/zigbee2mqtt/issues/25299)) ([329b8c9](https://github.com/Koenkk/zigbee2mqtt/commit/329b8c9c27c2ba2537b59ef16241a6e84167fa7b))
* Improve permit join ([#24257](https://github.com/Koenkk/zigbee2mqtt/issues/24257)) ([2124d34](https://github.com/Koenkk/zigbee2mqtt/commit/2124d3450cbc5ad5901a7fa2d657b881152a6db6))
* OpenHAB: Fix rejectattr argument in `event` value_template ([#25179](https://github.com/Koenkk/zigbee2mqtt/issues/25179)) ([08ac795](https://github.com/Koenkk/zigbee2mqtt/commit/08ac795bb39854d29388e7ff1e12002cd89b87f8))
* Proper extended PAN ID from `getNetworkParameters` ([#25174](https://github.com/Koenkk/zigbee2mqtt/issues/25174)) ([2b08c68](https://github.com/Koenkk/zigbee2mqtt/commit/2b08c688d3fab77fc1becb6646eb038040e5cf0a))
* Refactor tests to TS ([#24357](https://github.com/Koenkk/zigbee2mqtt/issues/24357)) ([6f6e7c3](https://github.com/Koenkk/zigbee2mqtt/commit/6f6e7c3ee040504231e1aecda004edbd16301578))
* Remove `permit_join_timeout` ([#25082](https://github.com/Koenkk/zigbee2mqtt/issues/25082)) ([7d63a87](https://github.com/Koenkk/zigbee2mqtt/commit/7d63a87aad954225c77156d22576939b1ffe7d6c))
* Remove configuring group members through `configuration.yaml` ([#24338](https://github.com/Koenkk/zigbee2mqtt/issues/24338)) ([3f76586](https://github.com/Koenkk/zigbee2mqtt/commit/3f76586a506a3674333d17cff93d32810c76687e))
* Remove deprecated Home Assistant integration features ([#24494](https://github.com/Koenkk/zigbee2mqtt/issues/24494)) ([e290057](https://github.com/Koenkk/zigbee2mqtt/commit/e290057e21ce5551563068420ea2942979206509))
* Remove oneOf bool/obj from settings schema ([#25195](https://github.com/Koenkk/zigbee2mqtt/issues/25195)) ([c3b4628](https://github.com/Koenkk/zigbee2mqtt/commit/c3b4628a1d6891d186da01d9f4a7a3b089617fd4))
* Rework OTA ([#24634](https://github.com/Koenkk/zigbee2mqtt/issues/24634)) ([563514c](https://github.com/Koenkk/zigbee2mqtt/commit/563514c446e39134ccd18829b10f6d2e02bef09b))
* Use built-in async for mqtt ([#24786](https://github.com/Koenkk/zigbee2mqtt/issues/24786)) ([e052988](https://github.com/Koenkk/zigbee2mqtt/commit/e052988663d967ed8c41ecb5ea0e52e6f85905b4))
## [1.42.0](https://github.com/Koenkk/zigbee2mqtt/compare/1.41.0...1.42.0) (2024-12-01)
### Features
* Add experimental support for Home Assistant `event` entities ([#24233](https://github.com/Koenkk/zigbee2mqtt/issues/24233)) ([848f250](https://github.com/Koenkk/zigbee2mqtt/commit/848f250dbb7e0142c7e702dab97536b8be749f5b))
### Bug Fixes
* Home Assistant `event` entities, part 2 ([#24717](https://github.com/Koenkk/zigbee2mqtt/issues/24717)) ([22e13c5](https://github.com/Koenkk/zigbee2mqtt/commit/22e13c52b96152a24d735791c313b8c6c2cf68e7))
* Home Assistant: remove the diagnostic category classification for power ([#24575](https://github.com/Koenkk/zigbee2mqtt/issues/24575)) ([c078ccb](https://github.com/Koenkk/zigbee2mqtt/commit/c078ccb13606a0893a19f25378197be70c0127f6))
* Home Assistant: template errors when `action` key is missing or empty in state payload ([#24931](https://github.com/Koenkk/zigbee2mqtt/issues/24931)) ([5ca4cf4](https://github.com/Koenkk/zigbee2mqtt/commit/5ca4cf4acd758b9c8163c3f8d07cb9903d91e118))
* **ignore:** Fix `Cannot read properties of undefined (reading 'clients')` https://github.com/Koenkk/zigbee2mqtt/issues/24957 ([7047aef](https://github.com/Koenkk/zigbee2mqtt/commit/7047aef7f7810225e80d923e6c45d5922562e12b))
* **ignore:** Ignore `None` actions for HA events ([#24972](https://github.com/Koenkk/zigbee2mqtt/issues/24972)) ([4690fe1](https://github.com/Koenkk/zigbee2mqtt/commit/4690fe1a55b4d6a2a8ad39c21c157b67a41d5f62))
* **ignore:** Prevent empty events from being published to HA ([#24969](https://github.com/Koenkk/zigbee2mqtt/issues/24969)) ([826547a](https://github.com/Koenkk/zigbee2mqtt/commit/826547a8e14beabfced2d9da408fa1d7a8e8a015))
* **ignore:** Prevent incorrect `event_type` due to duplicate keys ([#24954](https://github.com/Koenkk/zigbee2mqtt/issues/24954)) ([d82d782](https://github.com/Koenkk/zigbee2mqtt/commit/d82d782bac0deda328e7025f9a2bcbd50fecf857))
* **ignore:** update dependencies ([#24614](https://github.com/Koenkk/zigbee2mqtt/issues/24614)) ([84cc92f](https://github.com/Koenkk/zigbee2mqtt/commit/84cc92f91a020c86d5ae3938c635512cd106cd67))
* **ignore:** update dependencies ([#24702](https://github.com/Koenkk/zigbee2mqtt/issues/24702)) ([928635f](https://github.com/Koenkk/zigbee2mqtt/commit/928635f1daea6e3e80afc30ff1eca3bc83de7f97))
* **ignore:** Update dependencies ([#24894](https://github.com/Koenkk/zigbee2mqtt/issues/24894)) ([ca64935](https://github.com/Koenkk/zigbee2mqtt/commit/ca6493528db3ffb374068e837ec0151f079a6ac8))
* **ignore:** update zigbee-herdsman to 2.1.8 ([#24763](https://github.com/Koenkk/zigbee2mqtt/issues/24763)) ([2a9bbb2](https://github.com/Koenkk/zigbee2mqtt/commit/2a9bbb21ba5c22bf32d702f02b60084c4e12f140))
* **ignore:** update zigbee-herdsman to 2.1.9 ([#24819](https://github.com/Koenkk/zigbee2mqtt/issues/24819)) ([152ad12](https://github.com/Koenkk/zigbee2mqtt/commit/152ad12508724be23f1792238e4bc37f48413ec0))
* **ignore:** update zigbee-herdsman-converters to 20.40.0 ([#24644](https://github.com/Koenkk/zigbee2mqtt/issues/24644)) ([1558a5d](https://github.com/Koenkk/zigbee2mqtt/commit/1558a5d8ace136ac1469f34502901d368603cbd1))
* **ignore:** update zigbee-herdsman-converters to 20.41.0 ([#24656](https://github.com/Koenkk/zigbee2mqtt/issues/24656)) ([f2e5d59](https://github.com/Koenkk/zigbee2mqtt/commit/f2e5d59c3582ec12e1717c9c28520d54c593dd0e))
* **ignore:** update zigbee-herdsman-converters to 20.42.0 ([#24666](https://github.com/Koenkk/zigbee2mqtt/issues/24666)) ([b5d4a25](https://github.com/Koenkk/zigbee2mqtt/commit/b5d4a253b4ef5b4930f8d84e878e2343cc0b38df))
* **ignore:** update zigbee-herdsman-converters to 20.43.0 ([#24680](https://github.com/Koenkk/zigbee2mqtt/issues/24680)) ([a353acc](https://github.com/Koenkk/zigbee2mqtt/commit/a353acc8e3e98bf9ea3a935c1ee179aed335ff58))
* **ignore:** update zigbee-herdsman-converters to 20.44.0 ([#24715](https://github.com/Koenkk/zigbee2mqtt/issues/24715)) ([32e2637](https://github.com/Koenkk/zigbee2mqtt/commit/32e2637594add5c6fa8f8bfccc28b0f759a5d873))
* **ignore:** update zigbee-herdsman-converters to 20.45.0 ([#24733](https://github.com/Koenkk/zigbee2mqtt/issues/24733)) ([b8c3104](https://github.com/Koenkk/zigbee2mqtt/commit/b8c3104aa1baaf03c3db7954bc92b5ac71d985c4))
* **ignore:** update zigbee-herdsman-converters to 20.46.0 ([#24762](https://github.com/Koenkk/zigbee2mqtt/issues/24762)) ([6e04f7b](https://github.com/Koenkk/zigbee2mqtt/commit/6e04f7b03e8c7f0493007ce1d9d359f819088557))
* **ignore:** update zigbee-herdsman-converters to 20.47.0 ([#24772](https://github.com/Koenkk/zigbee2mqtt/issues/24772)) ([36e452b](https://github.com/Koenkk/zigbee2mqtt/commit/36e452b0f3284eb5ab872fb2d0aab04bbdfe4cee))
* **ignore:** update zigbee-herdsman-converters to 20.49.1 ([#24830](https://github.com/Koenkk/zigbee2mqtt/issues/24830)) ([5859947](https://github.com/Koenkk/zigbee2mqtt/commit/5859947207adf8597436971706cdff101c1effbe))
* **ignore:** update zigbee-herdsman-converters to 20.50.0 ([#24843](https://github.com/Koenkk/zigbee2mqtt/issues/24843)) ([988984a](https://github.com/Koenkk/zigbee2mqtt/commit/988984a1ae069d90ce1f67c165dd76c4edafa656))
* **ignore:** update zigbee-herdsman-converters to 20.51.0 ([#24856](https://github.com/Koenkk/zigbee2mqtt/issues/24856)) ([82a026e](https://github.com/Koenkk/zigbee2mqtt/commit/82a026e02e0ecc662cf8302ff3005793cb66a23f))
* **ignore:** update zigbee-herdsman-converters to 20.52.0 ([#24874](https://github.com/Koenkk/zigbee2mqtt/issues/24874)) ([1e77893](https://github.com/Koenkk/zigbee2mqtt/commit/1e77893f759908bafc373b047b6409d227516759))
* **ignore:** Update zigbee-herdsman-converters to 20.53.0 ([#24906](https://github.com/Koenkk/zigbee2mqtt/issues/24906)) ([790373b](https://github.com/Koenkk/zigbee2mqtt/commit/790373b896efdb6d9ccb5c29c97812db7445c6a3))
* **ignore:** update zigbee2mqtt-frontend to 0.7.5 ([#24631](https://github.com/Koenkk/zigbee2mqtt/issues/24631)) ([0c2cc10](https://github.com/Koenkk/zigbee2mqtt/commit/0c2cc10ad8fd8de1ac88566f8ca1a0d131b1055b))
* Replace connect-gzip-static with express-static-gzip to become compatible with Node 23 ([#24619](https://github.com/Koenkk/zigbee2mqtt/issues/24619)) ([87cee1a](https://github.com/Koenkk/zigbee2mqtt/commit/87cee1aea6eace2efb31a2d30551b5ea32187e95))
* Temporarily disable serving compressed index file. ([#24657](https://github.com/Koenkk/zigbee2mqtt/issues/24657)) ([7267d27](https://github.com/Koenkk/zigbee2mqtt/commit/7267d27779c854a216279b28b27551310b9cee97))
## [1.41.0](https://github.com/Koenkk/zigbee2mqtt/compare/1.40.2...1.41.0) (2024-10-31)
### Features
* Add `throttle` option for devices ([#24122](https://github.com/Koenkk/zigbee2mqtt/issues/24122)) ([17ac9cf](https://github.com/Koenkk/zigbee2mqtt/commit/17ac9cf8e777dfa4b86066650f3c6b2712b011b6))
* Allow serving frontend under subpath ([#24244](https://github.com/Koenkk/zigbee2mqtt/issues/24244)) ([1fdf0a0](https://github.com/Koenkk/zigbee2mqtt/commit/1fdf0a0a47d2b11962862ff433089d7f17bb7421))
### Bug Fixes
* Add additional discovery lookups for Home Assistant ([#24555](https://github.com/Koenkk/zigbee2mqtt/issues/24555)) ([a2ce323](https://github.com/Koenkk/zigbee2mqtt/commit/a2ce3230acff41ed52b684385130b67dbee3ad17))
* Fix group state change for open/close devices ([#24226](https://github.com/Koenkk/zigbee2mqtt/issues/24226)) ([076ada2](https://github.com/Koenkk/zigbee2mqtt/commit/076ada23257dccbe206ae9de9e70be850e756262))
* Frontend code cleanup @Nerivec ([#24322](https://github.com/Koenkk/zigbee2mqtt/issues/24322)) ([e2f19f1](https://github.com/Koenkk/zigbee2mqtt/commit/e2f19f19b4413fc4cdad6f64879840497bb2ca81))
* **ignore:** update dependencies ([#24236](https://github.com/Koenkk/zigbee2mqtt/issues/24236)) ([5c3f712](https://github.com/Koenkk/zigbee2mqtt/commit/5c3f71292d330210474e381d18e73840ffd5f5df))
* **ignore:** update dependencies ([#24309](https://github.com/Koenkk/zigbee2mqtt/issues/24309)) ([9f12ebb](https://github.com/Koenkk/zigbee2mqtt/commit/9f12ebb54e207ffbde7769f55ad494d689142d9c))
* **ignore:** update dependencies ([#24411](https://github.com/Koenkk/zigbee2mqtt/issues/24411)) ([eb606a5](https://github.com/Koenkk/zigbee2mqtt/commit/eb606a5b9832866b494d24dc6ebfe4e776069780))
* **ignore:** update dependencies ([#24498](https://github.com/Koenkk/zigbee2mqtt/issues/24498)) ([2139e7a](https://github.com/Koenkk/zigbee2mqtt/commit/2139e7a794c6bb1f32889eb1a951b039da124231))
* **ignore:** update zigbee-herdsman to 2.1.4 ([#24397](https://github.com/Koenkk/zigbee2mqtt/issues/24397)) ([12cb6e1](https://github.com/Koenkk/zigbee2mqtt/commit/12cb6e135d20e1631562d5b8e7d2bdf26cf22aa0))
* **ignore:** update zigbee-herdsman to 2.1.5 ([#24433](https://github.com/Koenkk/zigbee2mqtt/issues/24433)) ([6477865](https://github.com/Koenkk/zigbee2mqtt/commit/64778650d34cfa712c60bf662d25ea3e954bb3eb))
* **ignore:** update zigbee-herdsman to 2.1.6 ([#24545](https://github.com/Koenkk/zigbee2mqtt/issues/24545)) ([9837488](https://github.com/Koenkk/zigbee2mqtt/commit/9837488f44f24f0d5e7631fe79c68ce82ed50093))
* **ignore:** update zigbee-herdsman to 2.1.7 ([#24566](https://github.com/Koenkk/zigbee2mqtt/issues/24566)) ([f1615a2](https://github.com/Koenkk/zigbee2mqtt/commit/f1615a255c085c263519779be2444742765ee509))
* **ignore:** update zigbee-herdsman-converters to 20.22.0 ([#24186](https://github.com/Koenkk/zigbee2mqtt/issues/24186)) ([e5fac76](https://github.com/Koenkk/zigbee2mqtt/commit/e5fac76060b6cb9e762584b204fff69c8f68f3cf))
* **ignore:** update zigbee-herdsman-converters to 20.23.0 ([#24201](https://github.com/Koenkk/zigbee2mqtt/issues/24201)) ([b09ae10](https://github.com/Koenkk/zigbee2mqtt/commit/b09ae105882af2008dbb6a5f0f445ee0f03d1b3c))
* **ignore:** update zigbee-herdsman-converters to 20.23.1 ([#24225](https://github.com/Koenkk/zigbee2mqtt/issues/24225)) ([0229259](https://github.com/Koenkk/zigbee2mqtt/commit/02292590eee8e5ab0f655da39bba26681b814bec))
* **ignore:** update zigbee-herdsman-converters to 20.24.0 ([#24246](https://github.com/Koenkk/zigbee2mqtt/issues/24246)) ([3bb4af2](https://github.com/Koenkk/zigbee2mqtt/commit/3bb4af253ad8439b1354ce7fda2cc12ad25c934f))
* **ignore:** update zigbee-herdsman-converters to 20.25.0 ([#24285](https://github.com/Koenkk/zigbee2mqtt/issues/24285)) ([5f60187](https://github.com/Koenkk/zigbee2mqtt/commit/5f601873feca094ae9c6c73fc3ff25e4fcd01907))
* **ignore:** update zigbee-herdsman-converters to 20.26.0 ([#24307](https://github.com/Koenkk/zigbee2mqtt/issues/24307)) ([db00759](https://github.com/Koenkk/zigbee2mqtt/commit/db00759a030957ad6f80b8e1c230f959efb563ed))
* **ignore:** update zigbee-herdsman-converters to 20.27.2 ([#24321](https://github.com/Koenkk/zigbee2mqtt/issues/24321)) ([618b318](https://github.com/Koenkk/zigbee2mqtt/commit/618b318214ca58b94a1068e0320d9a163a1cd22f))
* **ignore:** update zigbee-herdsman-converters to 20.28.0 ([#24337](https://github.com/Koenkk/zigbee2mqtt/issues/24337)) ([d12b117](https://github.com/Koenkk/zigbee2mqtt/commit/d12b117b433f937266fa65e5e79af3973b376ab7))
* **ignore:** update zigbee-herdsman-converters to 20.29.0 ([#24363](https://github.com/Koenkk/zigbee2mqtt/issues/24363)) ([d79e71b](https://github.com/Koenkk/zigbee2mqtt/commit/d79e71b13e24002bf9dca469d558c7d70f39a487))
* **ignore:** update zigbee-herdsman-converters to 20.31.0 ([#24408](https://github.com/Koenkk/zigbee2mqtt/issues/24408)) ([669a16f](https://github.com/Koenkk/zigbee2mqtt/commit/669a16f13a4f88825ae343fa7140615ba2913004))
* **ignore:** update zigbee-herdsman-converters to 20.32.0 ([#24434](https://github.com/Koenkk/zigbee2mqtt/issues/24434)) ([9ad7c70](https://github.com/Koenkk/zigbee2mqtt/commit/9ad7c70f242e3b0bf68aef8568aff70ab28a9636))
* **ignore:** update zigbee-herdsman-converters to 20.33.0 ([#24457](https://github.com/Koenkk/zigbee2mqtt/issues/24457)) ([c09aeec](https://github.com/Koenkk/zigbee2mqtt/commit/c09aeecd95e2656548df297e509e2fd6bda7896e))
* **ignore:** update zigbee-herdsman-converters to 20.34.0 ([#24493](https://github.com/Koenkk/zigbee2mqtt/issues/24493)) ([2b460d3](https://github.com/Koenkk/zigbee2mqtt/commit/2b460d3a5fef83e732160890153db9941f5ba000))
* **ignore:** update zigbee-herdsman-converters to 20.35.0 ([#24514](https://github.com/Koenkk/zigbee2mqtt/issues/24514)) ([074b2e2](https://github.com/Koenkk/zigbee2mqtt/commit/074b2e20e03fe2a47636e9b072d21429681f9475))
* **ignore:** update zigbee-herdsman-converters to 20.36.0 ([#24542](https://github.com/Koenkk/zigbee2mqtt/issues/24542)) ([b620a21](https://github.com/Koenkk/zigbee2mqtt/commit/b620a21d469d1f69df411e459ca4539221bd4b55))
* **ignore:** update zigbee-herdsman-converters to 20.37.0 ([#24553](https://github.com/Koenkk/zigbee2mqtt/issues/24553)) ([bd1f6d2](https://github.com/Koenkk/zigbee2mqtt/commit/bd1f6d24a1febcbea6092716e503d280f2ba7916))
* **ignore:** update zigbee-herdsman-converters to 20.38.0 ([#24567](https://github.com/Koenkk/zigbee2mqtt/issues/24567)) ([c5d30b5](https://github.com/Koenkk/zigbee2mqtt/commit/c5d30b585b263b5de4d97dc2d2d02772ef01f34c))
* Improve `power_phase_b` and `power_phase_c` Home Assistant discovery ([#24538](https://github.com/Koenkk/zigbee2mqtt/issues/24538)) ([e0a0f86](https://github.com/Koenkk/zigbee2mqtt/commit/e0a0f868c05fd02cf21c802256c25a33ff8e89a0))
* Remove extraneous whitespace in template for binary attributes ([#24520](https://github.com/Koenkk/zigbee2mqtt/issues/24520)) ([ab72606](https://github.com/Koenkk/zigbee2mqtt/commit/ab726061b71935d91c14517689fafeb238ab0228))
## [1.40.2](https://github.com/Koenkk/zigbee2mqtt/compare/1.40.1...1.40.2) (2024-10-01)
### Bug Fixes
* Enforce TS `strict` type checking ([#23601](https://github.com/Koenkk/zigbee2mqtt/issues/23601)) ([48d77e4](https://github.com/Koenkk/zigbee2mqtt/commit/48d77e4b5ef88e985a363fecf45b8c23038b2a11))
* Fix Home Assistant `Invalid state message` error when state is too long ([#24045](https://github.com/Koenkk/zigbee2mqtt/issues/24045)) ([7ad51ce](https://github.com/Koenkk/zigbee2mqtt/commit/7ad51ce662e6e80aaaede7c7e5d1ecb37d89dff0))
* **ignore:** Improvement for afd80449b35a10de40cc5393ccb6daa515f26e9b ([9c72a76](https://github.com/Koenkk/zigbee2mqtt/commit/9c72a768f255898141818bce5987547473982dbe))
* **ignore:** Migrate to eslint 9 ([#23800](https://github.com/Koenkk/zigbee2mqtt/issues/23800)) ([afd8044](https://github.com/Koenkk/zigbee2mqtt/commit/afd80449b35a10de40cc5393ccb6daa515f26e9b))
* **ignore:** Sync eslint settings from zhc ([#23951](https://github.com/Koenkk/zigbee2mqtt/issues/23951)) ([e642f7c](https://github.com/Koenkk/zigbee2mqtt/commit/e642f7c86a25e329c3f367f2f40c6634c207caee))
* **ignore:** update dependencies ([#23898](https://github.com/Koenkk/zigbee2mqtt/issues/23898)) ([d989061](https://github.com/Koenkk/zigbee2mqtt/commit/d989061122334e6c5a7cde5a65bfee680b14db2d))
* **ignore:** update dependencies ([#23904](https://github.com/Koenkk/zigbee2mqtt/issues/23904)) ([8c04766](https://github.com/Koenkk/zigbee2mqtt/commit/8c0476612fe88ca0eeab28c193d29963e128f5a7))
* **ignore:** update dependencies ([#23973](https://github.com/Koenkk/zigbee2mqtt/issues/23973)) ([3efc2b2](https://github.com/Koenkk/zigbee2mqtt/commit/3efc2b237b51db932a758b2be90bd18dd6e25311))
* **ignore:** update dependencies ([#24057](https://github.com/Koenkk/zigbee2mqtt/issues/24057)) ([a5bb49a](https://github.com/Koenkk/zigbee2mqtt/commit/a5bb49aa7118ea2d9822b62f1109b2f6f183ab78))
* **ignore:** update dependencies ([#24132](https://github.com/Koenkk/zigbee2mqtt/issues/24132)) ([37f80df](https://github.com/Koenkk/zigbee2mqtt/commit/37f80dfb69fccd54db65de72024f36b7f9619cae))
* **ignore:** update zigbee-herdsman to 0.57.4 ([#23968](https://github.com/Koenkk/zigbee2mqtt/issues/23968)) ([70a65c7](https://github.com/Koenkk/zigbee2mqtt/commit/70a65c7dcf35a0fe88662edf257bf8117203420f))
* **ignore:** update zigbee-herdsman to 1.0.0 ([#24008](https://github.com/Koenkk/zigbee2mqtt/issues/24008)) ([da2a697](https://github.com/Koenkk/zigbee2mqtt/commit/da2a6974e078af2413db74b0c8eaa88c6a179126))
* **ignore:** update zigbee-herdsman to 1.0.1 ([#24032](https://github.com/Koenkk/zigbee2mqtt/issues/24032)) ([8a54a01](https://github.com/Koenkk/zigbee2mqtt/commit/8a54a0158351c71ef5da9fb80ef8e35c15d770ca))
* **ignore:** update zigbee-herdsman to 1.1.0 ([#24054](https://github.com/Koenkk/zigbee2mqtt/issues/24054)) ([d289194](https://github.com/Koenkk/zigbee2mqtt/commit/d289194a71f67dd821335d467532f74b3d47f03a))
* **ignore:** update zigbee-herdsman to 2.0.0 ([#24064](https://github.com/Koenkk/zigbee2mqtt/issues/24064)) ([ee4becb](https://github.com/Koenkk/zigbee2mqtt/commit/ee4becb64ecd35ad2cfdf4cc78688967700e8719))
* **ignore:** update zigbee-herdsman to 2.1.0 ([#24076](https://github.com/Koenkk/zigbee2mqtt/issues/24076)) ([2314147](https://github.com/Koenkk/zigbee2mqtt/commit/2314147d0eb4d513ca84405245c4ff460134700a))
* **ignore:** update zigbee-herdsman to 2.1.1 ([#24093](https://github.com/Koenkk/zigbee2mqtt/issues/24093)) ([5dd1bfb](https://github.com/Koenkk/zigbee2mqtt/commit/5dd1bfb8a62d8e176800b2cfb66874e0ab73f125))
* **ignore:** update zigbee-herdsman to 2.1.2 ([#24138](https://github.com/Koenkk/zigbee2mqtt/issues/24138)) ([df1913b](https://github.com/Koenkk/zigbee2mqtt/commit/df1913bc56f9ac60beb8ae4d648faf3ce57f349c))
* **ignore:** update zigbee-herdsman to 2.1.3 ([#24171](https://github.com/Koenkk/zigbee2mqtt/issues/24171)) ([13e4937](https://github.com/Koenkk/zigbee2mqtt/commit/13e4937db2d2cb916013479fc9bd9c6650c1bb8f))
* **ignore:** update zigbee-herdsman-converters to 20.13.0 ([#23927](https://github.com/Koenkk/zigbee2mqtt/issues/23927)) ([0540e4f](https://github.com/Koenkk/zigbee2mqtt/commit/0540e4fbb4c8e81322f1fb1d5de1ba3d2c0d0cfc))
* **ignore:** update zigbee-herdsman-converters to 20.14.1 ([#23952](https://github.com/Koenkk/zigbee2mqtt/issues/23952)) ([eb86856](https://github.com/Koenkk/zigbee2mqtt/commit/eb8685682f64c7a2dd09e22d04922b4d47053aa2))
* **ignore:** update zigbee-herdsman-converters to 20.15.0 ([#23969](https://github.com/Koenkk/zigbee2mqtt/issues/23969)) ([03b5b00](https://github.com/Koenkk/zigbee2mqtt/commit/03b5b0043a314c9fa14832ed0111b8582deb1857))
* **ignore:** update zigbee-herdsman-converters to 20.16.0 ([#23990](https://github.com/Koenkk/zigbee2mqtt/issues/23990)) ([d3de77d](https://github.com/Koenkk/zigbee2mqtt/commit/d3de77da13a391d717a773e33197c0b851227ac5))
* **ignore:** update zigbee-herdsman-converters to 20.16.1 ([#24009](https://github.com/Koenkk/zigbee2mqtt/issues/24009)) ([2ae19a7](https://github.com/Koenkk/zigbee2mqtt/commit/2ae19a7c4247fa511ef16b3db449c6ce6eb10eb8))
* **ignore:** update zigbee-herdsman-converters to 20.16.2 ([#24022](https://github.com/Koenkk/zigbee2mqtt/issues/24022)) ([e7a3f0d](https://github.com/Koenkk/zigbee2mqtt/commit/e7a3f0da0aadfc9b7c12fed7534acc18f05ffa9d))
* **ignore:** update zigbee-herdsman-converters to 20.16.3 ([#24033](https://github.com/Koenkk/zigbee2mqtt/issues/24033)) ([951bbd3](https://github.com/Koenkk/zigbee2mqtt/commit/951bbd357690d849790a7342d352f32e2e0b1f76))
* **ignore:** update zigbee-herdsman-converters to 20.17.0 ([#24077](https://github.com/Koenkk/zigbee2mqtt/issues/24077)) ([b1c7fd0](https://github.com/Koenkk/zigbee2mqtt/commit/b1c7fd0033e57f9119f78b6c94d0b9b053013ead))
* **ignore:** update zigbee-herdsman-converters to 20.18.0 ([#24105](https://github.com/Koenkk/zigbee2mqtt/issues/24105)) ([15d845b](https://github.com/Koenkk/zigbee2mqtt/commit/15d845bf923f3a845a88a1f8bf506207c27a8aee))
* **ignore:** update zigbee-herdsman-converters to 20.19.0 ([#24130](https://github.com/Koenkk/zigbee2mqtt/issues/24130)) ([8fe76e0](https://github.com/Koenkk/zigbee2mqtt/commit/8fe76e090f84d91c74e88555ee83716510b19caa))
* **ignore:** update zigbee-herdsman-converters to 20.20.0 ([#24144](https://github.com/Koenkk/zigbee2mqtt/issues/24144)) ([58c856b](https://github.com/Koenkk/zigbee2mqtt/commit/58c856bbd0595c8270ee225685a09bfb34334d02))
* **ignore:** update zigbee-herdsman-converters to 20.21.0 ([#24172](https://github.com/Koenkk/zigbee2mqtt/issues/24172)) ([c82f932](https://github.com/Koenkk/zigbee2mqtt/commit/c82f93230da8d430cbbdb4aa3a35c1a3d7659b1c))
* Improve performance when logging is disabled ([#23893](https://github.com/Koenkk/zigbee2mqtt/issues/23893)) ([c6ca472](https://github.com/Koenkk/zigbee2mqtt/commit/c6ca472ca1374c1b217063816359315931bbcdb0))
## [1.40.1](https://github.com/Koenkk/zigbee2mqtt/compare/1.40.0...1.40.1) (2024-09-07)
### Bug Fixes
* Fix `Cannot read properties of undefined (reading 'some')` https://github.com/Koenkk/zigbee2mqtt/issues/23821 ([557b17a](https://github.com/Koenkk/zigbee2mqtt/commit/557b17ad5b6dc375e361fb3ec5a84134be761a00))
* **ignore:** update dependencies ([#23790](https://github.com/Koenkk/zigbee2mqtt/issues/23790)) ([b7fa722](https://github.com/Koenkk/zigbee2mqtt/commit/b7fa7221093656cee3c908aa1c0e45e350aa43c9))
* **ignore:** update zigbee-herdsman to 0.57.2 ([#23872](https://github.com/Koenkk/zigbee2mqtt/issues/23872)) ([5524e88](https://github.com/Koenkk/zigbee2mqtt/commit/5524e881e6458afac2c5ae712bc6d0a1090e7115))
* **ignore:** update zigbee-herdsman to 0.57.3 ([#23877](https://github.com/Koenkk/zigbee2mqtt/issues/23877)) ([0d46354](https://github.com/Koenkk/zigbee2mqtt/commit/0d46354c485ba5c644af8d933b1689468b3a058c))
* **ignore:** update zigbee-herdsman-converters to 20.11.0 ([#23837](https://github.com/Koenkk/zigbee2mqtt/issues/23837)) ([a33c29f](https://github.com/Koenkk/zigbee2mqtt/commit/a33c29f10af5fc423ff9668af59826a37d9784bf))
* **ignore:** update zigbee-herdsman-converters to 20.12.1 ([#23884](https://github.com/Koenkk/zigbee2mqtt/issues/23884)) ([9283c4b](https://github.com/Koenkk/zigbee2mqtt/commit/9283c4b4973e9e7309081a9444427c87f9309f91))
* **ignore:** update zigbee-herdsman-converters to 20.9.0 ([#23822](https://github.com/Koenkk/zigbee2mqtt/issues/23822)) ([6dade8b](https://github.com/Koenkk/zigbee2mqtt/commit/6dade8b11faa13096191f9774a7a1dd320af6bde))
* Log stack trace when update check fails https://github.com/Koenkk/zigbee2mqtt/discussions/23798 ([13a108d](https://github.com/Koenkk/zigbee2mqtt/commit/13a108ddbc8f33d1cb23b92e2c67854fbd2dc338))
## [1.40.0](https://github.com/Koenkk/zigbee2mqtt/compare/1.39.1...1.40.0) (2024-09-01)
+15 -15
View File
@@ -17,24 +17,24 @@ diverse, inclusive, and healthy community.
Examples of behavior that contributes to a positive environment for our
community include:
- Demonstrating empathy and kindness toward other people
- Being respectful of differing opinions, viewpoints, and experiences
- Giving and gracefully accepting constructive feedback
- Accepting responsibility and apologizing to those affected by our mistakes,
and learning from the experience
- Focusing on what is best not just for us as individuals, but for the
overall community
- Demonstrating empathy and kindness toward other people
- Being respectful of differing opinions, viewpoints, and experiences
- Giving and gracefully accepting constructive feedback
- Accepting responsibility and apologizing to those affected by our mistakes,
and learning from the experience
- Focusing on what is best not just for us as individuals, but for the
overall community
Examples of unacceptable behavior include:
- The use of sexualized language or imagery, and sexual attention or
advances of any kind
- Trolling, insulting or derogatory comments, and personal or political attacks
- Public or private harassment
- Publishing others' private information, such as a physical or email
address, without their explicit permission
- Other conduct which could reasonably be considered inappropriate in a
professional setting
- The use of sexualized language or imagery, and sexual attention or
advances of any kind
- Trolling, insulting or derogatory comments, and personal or political attacks
- Public or private harassment
- Publishing others' private information, such as a physical or email
address, without their explicit permission
- Other conduct which could reasonably be considered inappropriate in a
professional setting
## Enforcement Responsibilities
+4 -4
View File
@@ -2,7 +2,7 @@
Everybody is invited and welcome to contribute to Zigbee2MQTT. Zigbee2MQTT is written in JavaScript and is based upon [zigbee-herdsman](https://github.com/koenkk/zigbee-herdsman) and [zigbee-herdsman-converters](https://github.com/koenkk/zigbee-herdsman-converters). Zigbee-herdsman-converters contains all device definitions, zigbee-herdsman is responsible for handling all communication with the adapter.
- Pull requests are always created against the [**dev**](https://github.com/Koenkk/zigbee2mqtt/tree/dev) branch.
- Easiest way to start developing Zigbee2MQTT is by setting up a development environment (aka bare-metal installation). You can follow this [guide](https://www.zigbee2mqtt.io/guide/installation/01_linux.html) to do this.
- You can run the tests locally by executing `npm test`. Zigbee2MQTT enforces 100% code coverage, in case you add new code check if your code is covered by running `npm run test-with-coverage`. The coverage report can be found under `coverage/lcov-report/index.html`. Linting is also enforced and can be run with `npm run eslint`.
- When you want to add support for a new device no changes to Zigbee2MQTT have to be made, only to zigbee-herdsman-converters. You can find a guide for it [here](https://www.zigbee2mqtt.io/advanced/support-new-devices/01_support_new_devices.html).
- Pull requests are always created against the [**dev**](https://github.com/Koenkk/zigbee2mqtt/tree/dev) branch.
- Easiest way to start developing Zigbee2MQTT is by setting up a development environment (aka bare-metal installation). You can follow this [guide](https://www.zigbee2mqtt.io/guide/installation/01_linux.html) to do this.
- You can run the tests locally by executing `pnpm test`. Zigbee2MQTT enforces 100% code coverage, in case you add new code check if your code is covered by running `pnpm run test-with-coverage`. The coverage report can be found under `coverage/lcov-report/index.html`. Linting is also enforced and can be run with `pnpm run eslint`.
- When you want to add support for a new device no changes to Zigbee2MQTT have to be made, only to zigbee-herdsman-converters. You can find a guide for it [here](https://www.zigbee2mqtt.io/advanced/support-new-devices/01_support_new_devices.html).
+13 -10
View File
@@ -5,8 +5,8 @@
<br>
<br>
<div style="display: flex;">
<a href="https://github.com/Koenkk/zigbee2mqtt/actions?query=workflow%3ACI">
<img src="https://github.com/koenkk/zigbee2mqtt/workflows/CI/badge.svg">
<a href="https://github.com/Koenkk/zigbee2mqtt/actions?query=workflow%3Aci">
<img src="https://github.com/koenkk/zigbee2mqtt/workflows/ci/badge.svg">
</a>
<a href="https://github.com/Koenkk/zigbee2mqtt/releases">
<img src="https://img.shields.io/github/release/koenkk/zigbee2mqtt.svg">
@@ -56,8 +56,8 @@ Zigbee2MQTT integrates well with (almost) every home automation solution because
### [Home Assistant](https://www.home-assistant.io/)
- [Home Assistant OS](https://www.home-assistant.io/installation/): Using [the official addon](https://github.com/zigbee2mqtt/hassio-zigbee2mqtt)
- Other installation: using instructions [here](https://www.zigbee2mqtt.io/guide/usage/integrations/home_assistant.html)
- [Home Assistant OS](https://www.home-assistant.io/installation/): Using [the official addon](https://github.com/zigbee2mqtt/hassio-zigbee2mqtt)
- Other installation: using instructions [here](https://www.zigbee2mqtt.io/guide/usage/integrations/home_assistant.html)
<br>
@@ -65,8 +65,8 @@ Zigbee2MQTT integrates well with (almost) every home automation solution because
### [Homey](https://homey.app/)
- Integration implemented in the [Homey App](https://homey.app/nl-nl/app/com.gruijter.zigbee2mqtt/)
- Documentation and support in the [Homey Forum](https://community.homey.app/t/83214)
- Integration implemented in the [Homey App](https://homey.app/nl-nl/app/com.gruijter.zigbee2mqtt/)
- Documentation and support in the [Homey Forum](https://community.homey.app/t/83214)
<br>
@@ -74,7 +74,7 @@ Zigbee2MQTT integrates well with (almost) every home automation solution because
### [Domoticz](https://www.domoticz.com/)
- Integration implemented in Domoticz ([documentation](https://www.domoticz.com/wiki/Zigbee2MQTT)).
- Integration implemented in Domoticz ([documentation](https://www.domoticz.com/wiki/Zigbee2MQTT)).
<br>
@@ -82,7 +82,7 @@ Zigbee2MQTT integrates well with (almost) every home automation solution because
### [Gladys Assistant](https://gladysassistant.com/)
- Integration implemented natively in Gladys Assistant ([documentation](https://gladysassistant.com/docs/integrations/zigbee2mqtt/)).
- Integration implemented natively in Gladys Assistant ([documentation](https://gladysassistant.com/docs/integrations/zigbee2mqtt/)).
<br>
@@ -90,7 +90,7 @@ Zigbee2MQTT integrates well with (almost) every home automation solution because
### [IoBroker](https://www.iobroker.net/)
- Integration implemented in IoBroker ([documentation](https://github.com/o0shojo0o/ioBroker.zigbee2mqtt)).
- Integration implemented in IoBroker ([documentation](https://github.com/o0shojo0o/ioBroker.zigbee2mqtt)).
<br>
@@ -104,7 +104,10 @@ Zigbee2MQTT is made up of three modules, each developed in its own Github projec
### Developing
Zigbee2MQTT uses TypeScript (partially for now). Therefore after making changes to files in the `lib/` directory you need to recompile Zigbee2MQTT. This can be done by executing `npm run build`. For faster development instead of running `npm run build` you can run `npm run build-watch` in another terminal session, this will recompile as you change files.
Zigbee2MQTT uses TypeScript (partially for now). Therefore after making changes to files in the `lib/` directory you need to recompile Zigbee2MQTT. This can be done by executing `pnpm run build`. For faster development instead of running `pnpm run build` you can run `pnpm run build-watch` in another terminal session, this will recompile as you change files.
Before running any of the commands, you'll first need to run `pnpm install --include=dev`.
Before submitting changes run `pnpm run test:coverage`, `pnpm run pretty:check` and `pnpm run eslint`
## Supported devices
-5
View File
@@ -1,5 +0,0 @@
module.exports = {
presets: [['@babel/preset-env', {targets: {node: 'current'}}], '@babel/preset-typescript'],
assumptions: {setPublicClassFields: true},
plugins: [['@babel/plugin-proposal-decorators', {legacy: true}]],
};
+23 -6
View File
@@ -1,8 +1,14 @@
# Indicates the configuration version (used by configuration migrations)
version: 4
# Home Assistant integration (MQTT discovery)
homeassistant: false
homeassistant:
enabled: false
# Enable the frontend, runs on port 8080 by default
frontend: true
frontend:
enabled: true
# port: 8080
# MQTT settings
mqtt:
@@ -14,13 +20,24 @@ mqtt:
# user: my_user
# password: my_password
# Serial settings
serial:
# Location of CC2531 USB sniffer
port: /dev/ttyACM0
# Serial settings, only required when Zigbee2MQTT fails to start with:
# USB adapter discovery error (No valid USB adapter found).
# Specify valid 'adapter' and 'port' in your configuration.
# serial:
# # Location of the adapter
# # USB adapters - use format "port: /dev/serial/by-id/XXX"
# # Ethernet adapters - use format "port: tcp://192.168.1.12:6638"
# port: /dev/serial/by-id/usb-Texas_Instruments_TI_CC2531_USB_CDC___0X00124B0018ED3DDF-if00
# # Adapter type, allowed values: `zstack`, `ember`, `deconz`, `zigate` or `zboss`
# adapter: zstack
# Periodically check whether devices are online/offline
# availability:
# enabled: false
# Advanced settings
advanced:
# channel: 11
# Let Zigbee2MQTT generate a network key on first start
network_key: GENERATE
# Let Zigbee2MQTT generate a pan_id on first start
-20
View File
@@ -1,20 +0,0 @@
# Home Assistant integration (MQTT discovery)
homeassistant: false
# allow new devices to join
permit_join: true
# MQTT settings
mqtt:
# MQTT base topic for zigbee2mqtt MQTT messages
base_topic: zigbee2mqtt
# MQTT server URL
server: 'mqtt://localhost'
# MQTT server authentication, uncomment if required:
# user: my_user
# password: my_password
# Serial settings
serial:
# Location of CC2531 USB sniffer
port: /dev/ttyACM0
+33 -16
View File
@@ -1,33 +1,52 @@
FROM alpine:3.18.4 as base
ARG TARGETPLATFORM
# Need to use Alpine 3.18.4 which uses Node 18 for arm/v6 and arm/v7, otherwise the build hangs.
# See https://github.com/nodejs/docker-node/issues/2077
FROM alpine:3.18.4 AS linux-arm-alpine
FROM alpine:3.21 AS linux-arm64-alpine
FROM alpine:3.21 AS linux-amd64-alpine
FROM alpine:3.21 AS linux-riscv64-alpine
FROM alpine:3.21 AS linux-386-alpine
FROM linux-${TARGETARCH}-alpine AS base
ENV NODE_ENV=production
WORKDIR /app
RUN apk add --no-cache tzdata eudev tini nodejs
# Dependencies and build
FROM base as dependencies_and_build
FROM base AS deps
COPY package*.json tsconfig.json index.js ./
COPY lib ./lib
RUN apk add --no-cache --virtual .buildtools make gcc g++ python3 linux-headers git npm && \
npm ci --production --no-audit --no-optional --no-update-notifier && \
# Serialport needs to be rebuild for Alpine https://serialport.io/docs/9.x.x/guide-installation#alpine-linux
npm rebuild --build-from-source && \
apk del .buildtools
COPY package.json pnpm-lock.yaml ./
# Make and such are needed to compile serialport for riscv64
RUN apk add make gcc g++ python3 linux-headers npm && \
npm install -g pnpm@10.4.1 && \
pnpm install --frozen-lockfile --no-optional && \
# serialport has outdated prebuilds that appear to fail on some archs, force build on target platform
rm -rf `find ./node_modules/.pnpm/ -wholename "*/@serialport/bindings-cpp/prebuilds" -type d` && \
pnpm rebuild @serialport/bindings-cpp
# Release
FROM base as release
FROM base AS release
ARG DATE
ARG VERSION
LABEL org.opencontainers.image.authors="Koen Kanters"
LABEL org.opencontainers.image.title="zigbee2mqtt"
LABEL org.opencontainers.image.title="Zigbee2MQTT"
LABEL org.opencontainers.image.description="Zigbee to MQTT bridge using Zigbee-herdsman"
LABEL org.opencontainers.image.url="https://github.com/Koenkk/zigbee2mqtt"
LABEL org.opencontainers.image.documentation="https://www.zigbee2mqtt.io/"
LABEL org.opencontainers.image.source="https://github.com/Koenkk/zigbee2mqtt"
LABEL org.opencontainers.image.licenses="GPL-3.0"
LABEL org.opencontainers.image.created=${DATE}
LABEL org.opencontainers.image.version=${VERSION}
COPY --from=dependencies_and_build /app/node_modules ./node_modules
COPY --from=deps /app/node_modules ./node_modules
COPY dist ./dist
COPY package.json LICENSE index.js data/configuration.yaml data/configuration.example.yaml ./
# To prevent `Error: EACCES: permission denied, mkdir '/app/dist/external_converters'`
# when running rootless.
RUN chmod -R 777 ./dist
COPY package.json LICENSE index.js data/configuration.example.yaml ./
COPY docker/docker-entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
@@ -37,7 +56,5 @@ RUN mkdir /app/data
ARG COMMIT
RUN echo "$COMMIT" > dist/.hash
ENV NODE_ENV production
ENTRYPOINT ["docker-entrypoint.sh"]
CMD [ "/sbin/tini", "--", "node", "index.js"]
-14
View File
@@ -1,14 +0,0 @@
version: '3'
services:
zigbee2mqtt:
container_name: zigbee2mqtt
image: koenkk/zigbee2mqtt
volumes:
- ./data:/app/data
devices:
# CC251
#- /dev/ttyUSB_cc2531:/dev/ttyACM0
# CC2530 / GBAN GB2530S
#- /dev/ttyUSB_cc2530:/dev/ttyACM0
restart: always
network_mode: host
-5
View File
@@ -9,9 +9,4 @@ fi
echo "Using '$DATA' as data directory"
if [ ! -f "$DATA/configuration.yaml" ]; then
echo "Creating configuration file..."
cp /app/configuration.example.yaml "$DATA/configuration.yaml"
fi
exec "$@"
+32
View File
@@ -0,0 +1,32 @@
// @ts-check
import eslint from '@eslint/js';
import eslintConfigPrettier from 'eslint-config-prettier';
import tseslint from 'typescript-eslint';
export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.recommended,
{
languageOptions: {
parserOptions: {
project: true,
},
},
rules: {
'@typescript-eslint/await-thenable': 'error',
'@typescript-eslint/ban-ts-comment': 'error',
'@typescript-eslint/explicit-function-return-type': 'error',
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/no-unused-vars': 'error',
'array-bracket-spacing': ['error', 'never'],
'@typescript-eslint/return-await': ['error', 'always'],
'object-curly-spacing': ['error', 'never'],
'@typescript-eslint/no-floating-promises': 'error',
},
},
{
ignores: ['dist/', '**/*.js', '**/*.mjs'],
},
eslintConfigPrettier,
);
+30 -35
View File
@@ -4,7 +4,6 @@ const fs = require('fs');
const os = require('os');
const path = require('path');
const {exec} = require('child_process');
const {rimrafSync} = require('rimraf');
require('source-map-support').install();
let controller;
@@ -16,7 +15,7 @@ let unsolicitedStop = false;
let watchdogDelays = [2000, 60000, 300000, 900000, 1800000, 3600000];
if (watchdog && process.env.Z2M_WATCHDOG !== 'default') {
if (/^(?:(?:[0-9]*[.])?[0-9]+)+(?:,?(?:[0-9]*[.])?[0-9]+)*$/.test(process.env.Z2M_WATCHDOG)) {
if (/^\d+(.\d+)?(,\d+(.\d+)?)*$/.test(process.env.Z2M_WATCHDOG)) {
watchdogDelays = process.env.Z2M_WATCHDOG.split(',').map((v) => parseFloat(v) * 60000);
} else {
console.log(`Invalid watchdog delays (must use number-only CSV format representing minutes, example: 'Z2M_WATCHDOG=1,5,15,30,60'.`);
@@ -58,10 +57,16 @@ async function exit(code, restart = false) {
}
async function currentHash() {
const git = require('git-last-commit');
return await new Promise((resolve) => {
exec('git rev-parse --short=8 HEAD', (error, stdout) => {
const commitHash = stdout.trim();
return new Promise((resolve) => {
git.getLastCommit((err, commit) => (err ? resolve('unknown') : resolve(commit.shortHash)));
if (error || commitHash === '') {
resolve('unknown');
} else {
resolve(commitHash);
}
});
});
}
@@ -72,10 +77,9 @@ async function writeHash() {
}
async function build(reason) {
return new Promise((resolve, reject) => {
process.stdout.write(`Building Zigbee2MQTT... (${reason})`);
rimrafSync('dist');
process.stdout.write(`Building Zigbee2MQTT... (${reason})`);
return await new Promise((resolve, reject) => {
const env = {...process.env};
const _600mb = 629145600;
@@ -85,7 +89,8 @@ async function build(reason) {
env.NODE_OPTIONS = '--max_old_space_size=256';
}
exec('npm run build', {env, cwd: __dirname}, async (err, stdout, stderr) => {
// clean build, prevent failures due to tsc incremental building
exec('pnpm run prepack', {env, cwd: __dirname}, async (err, stdout, stderr) => {
if (err) {
process.stdout.write(', failed\n');
@@ -107,8 +112,9 @@ async function checkDist() {
await build('initial build');
}
const distHash = fs.readFileSync(hashFile, 'utf-8');
const distHash = fs.readFileSync(hashFile, 'utf8');
const hash = await currentHash();
if (hash !== 'unknown' && distHash !== hash) {
await build('hash changed');
}
@@ -118,37 +124,26 @@ async function start() {
console.log(`Starting Zigbee2MQTT ${watchdog ? `with watchdog (${watchdogDelays})` : `without watchdog`}.`);
await checkDist();
const version = engines.node;
// gc
{
const version = engines.node;
if (!semver.satisfies(process.version, version)) {
console.log(`\t\tZigbee2MQTT requires node version ${version}, you are running ${process.version}!\n`);
}
// Validate settings
const settings = require('./dist/util/settings');
settings.reRead();
const errors = settings.validate();
if (errors.length > 0) {
unsolicitedStop = false;
console.log(`\n\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!`);
console.log(' READ THIS CAREFULLY\n');
console.log(`Refusing to start because configuration is not valid, found the following errors:`);
for (const error of errors) {
console.log(`- ${error}`);
if (!semver.satisfies(process.version, version)) {
console.log(`\t\tZigbee2MQTT requires node version ${version}, you are running ${process.version}!\n`);
}
console.log(`\nIf you don't know how to solve this, read https://www.zigbee2mqtt.io/guide/configuration`);
console.log(`\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n`);
const {onboard} = require('./dist/util/onboarding');
return exit(1);
const success = await onboard();
if (!success) {
unsolicitedStop = false;
return await exit(1);
}
}
const Controller = require('./dist/controller');
const {Controller} = require('./dist/controller');
controller = new Controller(restart, exit);
await controller.start();
+173 -140
View File
@@ -1,24 +1,23 @@
import assert from 'assert';
import type {IClientPublishOptions} from 'mqtt';
import type Extension from './extension/extension';
import type {Zigbee2MQTTAPI} from './types/api';
import bind from 'bind-decorator';
import stringify from 'json-stable-stringify-without-jsonify';
import {setLogger as zhSetLogger} from 'zigbee-herdsman';
import {setLogger as zhcSetLogger} from 'zigbee-herdsman-converters';
import EventBus from './eventBus';
// Extensions
import ExtensionAvailability from './extension/availability';
import ExtensionBind from './extension/bind';
import ExtensionBridge from './extension/bridge';
import ExtensionConfigure from './extension/configure';
import ExtensionExternalConverters from './extension/externalConverters';
import ExtensionExternalExtension from './extension/externalExtension';
// Extensions
import ExtensionFrontend from './extension/frontend';
import ExtensionExternalExtensions from './extension/externalExtensions';
import ExtensionGroups from './extension/groups';
import ExtensionHomeAssistant from './extension/homeassistant';
import ExtensionBridgeLegacy from './extension/legacy/bridgeLegacy';
import ExtensionDeviceGroupMembership from './extension/legacy/deviceGroupMembership';
import ExtensionReport from './extension/legacy/report';
import ExtensionSoftReset from './extension/legacy/softReset';
import ExtensionNetworkMap from './extension/networkMap';
import ExtensionOnEvent from './extension/onEvent';
import ExtensionOTAUpdate from './extension/otaUpdate';
@@ -27,51 +26,11 @@ import ExtensionReceive from './extension/receive';
import MQTT from './mqtt';
import State from './state';
import logger from './util/logger';
import {initSdNotify} from './util/sd-notify';
import * as settings from './util/settings';
import utils from './util/utils';
import Zigbee from './zigbee';
const AllExtensions = [
ExtensionPublish,
ExtensionReceive,
ExtensionNetworkMap,
ExtensionSoftReset,
ExtensionHomeAssistant,
ExtensionConfigure,
ExtensionDeviceGroupMembership,
ExtensionBridgeLegacy,
ExtensionBridge,
ExtensionGroups,
ExtensionBind,
ExtensionReport,
ExtensionOnEvent,
ExtensionOTAUpdate,
ExtensionExternalConverters,
ExtensionFrontend,
ExtensionExternalExtension,
ExtensionAvailability,
];
type ExtensionArgs = [
Zigbee,
MQTT,
State,
PublishEntityState,
EventBus,
enableDisableExtension: (enable: boolean, name: string) => Promise<void>,
restartCallback: () => Promise<void>,
addExtension: (extension: Extension) => Promise<void>,
];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let sdNotify: any = null;
try {
// eslint-disable-next-line @typescript-eslint/no-require-imports
sdNotify = process.env.NOTIFY_SOCKET ? require('sd-notify') : null;
} catch {
// sd-notify is optional
}
export class Controller {
private eventBus: EventBus;
private zigbee: Zigbee;
@@ -79,8 +38,9 @@ export class Controller {
private mqtt: MQTT;
private restartCallback: () => Promise<void>;
private exitCallback: (code: number, restart: boolean) => Promise<void>;
private extensions: Extension[];
private extensionArgs: ExtensionArgs;
public readonly extensions: Set<Extension>;
public readonly extensionArgs: ConstructorParameters<typeof Extension>;
private sdNotify: Awaited<ReturnType<typeof initSdNotify>>;
constructor(restartCallback: () => Promise<void>, exitCallback: (code: number, restart: boolean) => Promise<void>) {
logger.init();
@@ -105,63 +65,68 @@ export class Controller {
this.addExtension,
];
this.extensions = [
this.extensions = new Set([
new ExtensionExternalConverters(...this.extensionArgs),
new ExtensionOnEvent(...this.extensionArgs),
new ExtensionBridge(...this.extensionArgs),
new ExtensionPublish(...this.extensionArgs),
new ExtensionReceive(...this.extensionArgs),
new ExtensionDeviceGroupMembership(...this.extensionArgs),
new ExtensionConfigure(...this.extensionArgs),
new ExtensionNetworkMap(...this.extensionArgs),
new ExtensionGroups(...this.extensionArgs),
new ExtensionBind(...this.extensionArgs),
new ExtensionOTAUpdate(...this.extensionArgs),
new ExtensionReport(...this.extensionArgs),
new ExtensionExternalExtension(...this.extensionArgs),
new ExtensionExternalExtensions(...this.extensionArgs),
new ExtensionAvailability(...this.extensionArgs),
settings.get().frontend && new ExtensionFrontend(...this.extensionArgs),
settings.get().advanced.legacy_api && new ExtensionBridgeLegacy(...this.extensionArgs),
settings.get().external_converters.length && new ExtensionExternalConverters(...this.extensionArgs),
settings.get().homeassistant && new ExtensionHomeAssistant(...this.extensionArgs),
/* istanbul ignore next */
settings.get().advanced.soft_reset_timeout !== 0 && new ExtensionSoftReset(...this.extensionArgs),
].filter((n) => n);
]);
}
async start(): Promise<void> {
if (settings.get().frontend.enabled) {
const {Frontend} = await import('./extension/frontend.js');
this.extensions.add(new Frontend(...this.extensionArgs));
}
if (settings.get().homeassistant.enabled) {
const {HomeAssistant} = await import('./extension/homeassistant.js');
this.extensions.add(new HomeAssistant(...this.extensionArgs));
}
this.state.start();
const info = await utils.getZigbee2MQTTVersion();
logger.info(`Starting Zigbee2MQTT version ${info.version} (commit #${info.commitHash})`);
// Start zigbee
let startResult;
try {
startResult = await this.zigbee.start();
await this.zigbee.start();
this.eventBus.onAdapterDisconnected(this, this.onZigbeeAdapterDisconnected);
} catch (error) {
logger.error('Failed to start zigbee');
logger.error('Check https://www.zigbee2mqtt.io/guide/installation/20_zigbee2mqtt-fails-to-start.html for possible solutions');
logger.error('Failed to start zigbee-herdsman');
logger.error(
'Check https://www.zigbee2mqtt.io/guide/installation/20_zigbee2mqtt-fails-to-start_crashes-runtime.html for possible solutions',
);
logger.error('Exiting...');
logger.error(error.stack);
return this.exit(1);
}
logger.error((error as Error).stack!);
// Disable some legacy options on new network creation
if (startResult === 'reset') {
settings.set(['advanced', 'homeassistant_legacy_entity_attributes'], false);
settings.set(['advanced', 'legacy_api'], false);
settings.set(['advanced', 'legacy_availability_payload'], false);
settings.set(['device_options', 'legacy'], false);
await this.enableDisableExtension(false, 'BridgeLegacy');
/* v8 ignore start */
if ((error as Error).message.includes('USB adapter discovery error (No valid USB adapter found)')) {
logger.error('If this happens after updating to Zigbee2MQTT 2.0.0, see https://github.com/Koenkk/zigbee2mqtt/discussions/24364');
}
/* v8 ignore stop */
return await this.exit(1);
}
// Log zigbee clients on startup
let deviceCount = 0;
for (const device of this.zigbee.devicesIterator(utils.deviceNotCoordinator)) {
// `definition` validated by `isSupported`
const model = device.isSupported
? `${device.definition.model} - ${device.definition.vendor} ${device.definition.description}`
? `${device.definition!.model} - ${device.definition!.vendor} ${device.definition!.description}`
: 'Not supported';
logger.info(`${device.name} (${device.ieeeAddr}): ${model} (${device.zh.type})`);
@@ -170,30 +135,18 @@ export class Controller {
logger.info(`Currently ${deviceCount} devices are joined.`);
// Enable zigbee join
try {
if (settings.get().permit_join) {
logger.warning('`permit_join` set to `true` in configuration.yaml.');
logger.warning('Allowing new devices to join.');
logger.warning('Set `permit_join` to `false` once you joined all devices.');
}
await this.zigbee.permitJoin(settings.get().permit_join);
} catch (error) {
logger.error(`Failed to set permit join to ${settings.get().permit_join} (${error.message})`);
}
// MQTT
try {
await this.mqtt.connect();
} catch (error) {
logger.error(`MQTT failed to connect, exiting... (${error.message})`);
logger.error(`MQTT failed to connect, exiting... (${(error as Error).message})`);
await this.zigbee.stop();
return this.exit(1);
return await this.exit(1);
}
// Call extensions
await this.callExtensions('start', [...this.extensions]);
for (const extension of this.extensions) {
await this.startExtension(extension);
}
// Send all cached states.
if (settings.get().advanced.cache_state_send_on_startup && settings.get().advanced.cache_state) {
@@ -208,39 +161,131 @@ export class Controller {
logger.info(`Zigbee2MQTT started!`);
const watchdogInterval = sdNotify?.watchdogInterval() || 0;
if (watchdogInterval > 0) {
sdNotify.startWatchdogMode(Math.floor(watchdogInterval / 2));
}
sdNotify?.ready();
this.sdNotify = await initSdNotify();
}
@bind async enableDisableExtension(enable: boolean, name: string): Promise<void> {
if (!enable) {
const extension = this.extensions.find((e) => e.constructor.name === name);
if (extension) {
await this.callExtensions('stop', [extension]);
this.extensions.splice(this.extensions.indexOf(extension), 1);
if (enable) {
switch (name) {
case 'Frontend': {
if (!settings.get().frontend.enabled) {
throw new Error('Tried to enable Frontend extension disabled in settings');
}
// this is not actually used, not tested either
/* v8 ignore start */
const {Frontend} = await import('./extension/frontend.js');
await this.addExtension(new Frontend(...this.extensionArgs));
break;
/* v8 ignore stop */
}
case 'HomeAssistant': {
if (!settings.get().homeassistant.enabled) {
throw new Error('Tried to enable HomeAssistant extension disabled in settings');
}
const {HomeAssistant} = await import('./extension/homeassistant.js');
await this.addExtension(new HomeAssistant(...this.extensionArgs));
break;
}
default: {
throw new Error(
`Extension ${name} does not exist (should be added with 'addExtension') or is built-in that cannot be enabled at runtime`,
);
}
}
} else {
const Extension = AllExtensions.find((e) => e.name === name);
assert(Extension, `Extension '${name}' does not exist`);
const extension = new Extension(...this.extensionArgs);
this.extensions.push(extension);
await this.callExtensions('start', [extension]);
switch (name) {
case 'Frontend': {
if (settings.get().frontend.enabled) {
throw new Error('Tried to disable Frontend extension enabled in settings');
}
break;
}
case 'HomeAssistant': {
if (settings.get().homeassistant.enabled) {
throw new Error('Tried to disable HomeAssistant extension enabled in settings');
}
break;
}
case 'Availability':
case 'Bind':
case 'Bridge':
case 'Configure':
case 'ExternalConverters':
case 'ExternalExtensions':
case 'Groups':
case 'NetworkMap':
case 'OnEvent':
case 'OTAUpdate':
case 'Publish':
case 'Receive': {
throw new Error(`Built-in extension ${name} cannot be disabled at runtime`);
}
}
const extension = this.getExtension(name);
if (extension) {
await this.removeExtension(extension);
}
}
}
public getExtension(name: string): Extension | undefined {
for (const extension of this.extensions) {
if (extension.constructor.name === name) {
return extension;
}
}
}
@bind async addExtension(extension: Extension): Promise<void> {
this.extensions.push(extension);
await this.callExtensions('start', [extension]);
for (const ext of this.extensions) {
if (ext.constructor.name === extension.constructor.name) {
throw new Error(`Extension with name ${ext.constructor.name} already present`);
}
}
this.extensions.add(extension);
await this.startExtension(extension);
}
async removeExtension(extension: Extension): Promise<void> {
if (this.extensions.delete(extension)) {
await this.stopExtension(extension);
}
}
private async startExtension(extension: Extension): Promise<void> {
try {
await extension.start();
} catch (error) {
logger.error(`Failed to start '${extension.constructor.name}' (${(error as Error).stack})`);
}
}
private async stopExtension(extension: Extension): Promise<void> {
try {
await extension.stop();
} catch (error) {
logger.error(`Failed to stop '${extension.constructor.name}' (${(error as Error).stack})`);
}
}
async stop(restart = false): Promise<void> {
sdNotify?.stopping();
this.sdNotify?.notifyStopping();
for (const extension of this.extensions) {
await this.stopExtension(extension);
}
// Call extensions
await this.callExtensions('stop', this.extensions);
this.eventBus.removeListeners(this);
// Wrap-up
@@ -252,17 +297,17 @@ export class Controller {
await this.zigbee.stop();
logger.info('Stopped Zigbee2MQTT');
} catch (error) {
logger.error(`Failed to stop Zigbee2MQTT (${error.message})`);
logger.error(`Failed to stop Zigbee2MQTT (${(error as Error).message})`);
code = 1;
}
sdNotify?.stopWatchdogMode();
return this.exit(code, restart);
this.sdNotify?.stop();
return await this.exit(code, restart);
}
async exit(code: number, restart = false): Promise<void> {
await logger.end();
return this.exitCallback(code, restart);
return await this.exitCallback(code, restart);
}
@bind async onZigbeeAdapterDisconnected(): Promise<void> {
@@ -271,7 +316,7 @@ export class Controller {
}
@bind async publishEntityState(entity: Group | Device, payload: KeyValue, stateChangeReason?: StateChangeReason): Promise<void> {
let message = {...payload};
let message: Zigbee2MQTTAPI['{friendlyName}'] = {...payload};
// Update state cache with new state.
const newState = this.state.set(entity, payload, stateChangeReason);
@@ -281,14 +326,14 @@ export class Controller {
message = newState;
}
const options: MQTTOptions = {
retain: utils.getObjectProperty(entity.options, 'retain', false) as boolean,
qos: utils.getObjectProperty(entity.options, 'qos', 0) as 0 | 1 | 2,
const options: IClientPublishOptions = {
retain: utils.getObjectProperty(entity.options, 'retain', false),
qos: utils.getObjectProperty(entity.options, 'qos', 0),
};
const retention = utils.getObjectProperty<number | false>(entity.options, 'retention', false);
const retention = utils.getObjectProperty(entity.options, 'retention', false);
if (retention !== false) {
options.properties = {messageExpiryInterval: retention as number};
options.properties = {messageExpiryInterval: retention};
}
if (entity.isDevice() && settings.get().mqtt.include_device_information) {
@@ -308,6 +353,7 @@ export class Controller {
softwareBuildID: entity.zh.softwareBuildID,
// Manufacturer name can contain \u0000, remove this.
// https://github.com/home-assistant/core/issues/85691
/* v8 ignore next */
manufacturerName: entity.zh.manufacturerName?.split('\u0000')[0],
};
}
@@ -330,7 +376,7 @@ export class Controller {
// Filter mqtt message attributes
utils.filterProperties(entity.options.filtered_attributes, message);
if (Object.entries(message).length) {
if (!utils.objectIsEmpty(message)) {
const output = settings.get().advanced.output;
if (output === 'attribute_and_json' || output === 'json') {
await this.mqtt.publish(entity.name, stringify(message), options);
@@ -344,7 +390,7 @@ export class Controller {
this.eventBus.emitPublishEntityState({entity, message, stateChangeReason, payload});
}
async iteratePayloadAttributeOutput(topicRoot: string, payload: KeyValue, options: MQTTOptions): Promise<void> {
async iteratePayloadAttributeOutput(topicRoot: string, payload: KeyValue, options: IClientPublishOptions): Promise<void> {
for (const [key, value] of Object.entries(payload)) {
let subPayload = value;
let message = null;
@@ -370,17 +416,4 @@ export class Controller {
}
}
}
private async callExtensions(method: 'start' | 'stop', extensions: Extension[]): Promise<void> {
for (const extension of extensions) {
try {
await extension[method]?.();
} catch (error) {
/* istanbul ignore next */
logger.error(`Failed to call '${extension.constructor.name}' '${method}' (${error.stack})`);
}
}
}
}
module.exports = Controller;
+48 -17
View File
@@ -1,13 +1,42 @@
import events from 'events';
import events from 'node:events';
import logger from './util/logger';
// eslint-disable-next-line
type ListenerKey = object;
interface EventBusMap {
adapterDisconnected: [];
permitJoinChanged: [data: eventdata.PermitJoinChanged];
publishAvailability: [];
deviceRenamed: [data: eventdata.EntityRenamed];
deviceRemoved: [data: eventdata.EntityRemoved];
lastSeenChanged: [data: eventdata.LastSeenChanged];
deviceNetworkAddressChanged: [data: eventdata.DeviceNetworkAddressChanged];
deviceAnnounce: [data: eventdata.DeviceAnnounce];
deviceInterview: [data: eventdata.DeviceInterview];
deviceJoined: [data: eventdata.DeviceJoined];
entityOptionsChanged: [data: eventdata.EntityOptionsChanged];
exposesChanged: [data: eventdata.ExposesChanged];
deviceLeave: [data: eventdata.DeviceLeave];
deviceMessage: [data: eventdata.DeviceMessage];
mqttMessage: [data: eventdata.MQTTMessage];
mqttMessagePublished: [data: eventdata.MQTTMessagePublished];
publishEntityState: [data: eventdata.PublishEntityState];
groupMembersChanged: [data: eventdata.GroupMembersChanged];
devicesChanged: [];
scenesChanged: [data: eventdata.ScenesChanged];
reconfigure: [data: eventdata.Reconfigure];
stateChange: [data: eventdata.StateChange];
}
type EventBusListener<K> = K extends keyof EventBusMap
? EventBusMap[K] extends unknown[]
? (...args: EventBusMap[K]) => Promise<void> | void
: never
: never;
export default class EventBus {
private callbacksByExtension: {[s: string]: {event: string; callback: (...args: unknown[]) => void}[]} = {};
private emitter = new events.EventEmitter();
private callbacksByExtension: {[s: string]: {event: keyof EventBusMap; callback: EventBusListener<keyof EventBusMap>}[]} = {};
private emitter = new events.EventEmitter<EventBusMap>();
constructor() {
this.emitter.setMaxListeners(100);
@@ -27,13 +56,6 @@ export default class EventBus {
this.on('permitJoinChanged', callback, key);
}
public emitPublishAvailability(): void {
this.emitter.emit('publishAvailability');
}
public onPublishAvailability(key: ListenerKey, callback: () => void): void {
this.on('publishAvailability', callback, key);
}
public emitEntityRenamed(data: eventdata.EntityRenamed): void {
this.emitter.emit('deviceRenamed', data);
}
@@ -167,18 +189,27 @@ export default class EventBus {
this.on('stateChange', callback, key);
}
private on(event: string, callback: (...args: unknown[]) => Promise<void> | void, key: ListenerKey): void {
if (!this.callbacksByExtension[key.constructor.name]) this.callbacksByExtension[key.constructor.name] = [];
const wrappedCallback = async (...args: unknown[]): Promise<void> => {
public emitExposesAndDevicesChanged(device: Device): void {
this.emitDevicesChanged();
this.emitExposesChanged({device});
}
private on<K extends keyof EventBusMap>(event: K, callback: EventBusListener<K>, key: ListenerKey): void {
if (!this.callbacksByExtension[key.constructor.name]) {
this.callbacksByExtension[key.constructor.name] = [];
}
const wrappedCallback = async (...args: never[]): Promise<void> => {
try {
await callback(...args);
} catch (error) {
logger.error(`EventBus error '${key.constructor.name}/${event}': ${error.message}`);
logger.debug(error.stack);
logger.error(`EventBus error '${key.constructor.name}/${event}': ${(error as Error).message}`);
logger.debug((error as Error).stack!);
}
};
this.callbacksByExtension[key.constructor.name].push({event, callback: wrappedCallback});
this.emitter.on(event, wrappedCallback);
this.emitter.on(event, wrappedCallback as EventBusListener<K>);
}
public removeListeners(key: ListenerKey): void {
+28 -21
View File
@@ -1,5 +1,10 @@
import type {Zigbee2MQTTAPI} from '../types/api';
import assert from 'node:assert';
import bind from 'bind-decorator';
import debounce from 'debounce';
import * as zhc from 'zigbee-herdsman-converters';
import logger from '../util/logger';
@@ -26,14 +31,9 @@ export default class Availability extends Extension {
return utils.minutes(device.options.availability.timeout);
}
const key = this.isActiveDevice(device) ? 'active' : 'passive';
let value = settings.get().availability?.[key]?.timeout;
const type = this.isActiveDevice(device) ? 'active' : 'passive';
if (value == null) {
value = key == 'active' ? 10 : 1500;
}
return utils.minutes(value);
return utils.minutes(settings.get().availability[type].timeout);
}
private isActiveDevice(device: Device): boolean {
@@ -41,9 +41,12 @@ export default class Availability extends Extension {
}
private isAvailable(entity: Device | Group): boolean {
return entity.isDevice()
? Date.now() - entity.zh.lastSeen < this.getTimeout(entity)
: entity.membersDevices().length === 0 || entity.membersDevices().some((d) => this.availabilityCache[d.ieeeAddr]);
if (entity.isDevice()) {
return Date.now() - (entity.zh.lastSeen ?? /* v8 ignore next */ 0) < this.getTimeout(entity);
} else {
const membersDevices = entity.membersDevices();
return membersDevices.length === 0 || membersDevices.some((d) => this.availabilityCache[d.ieeeAddr]);
}
}
private resetTimer(device: Device): void {
@@ -92,7 +95,7 @@ export default class Availability extends Extension {
logger.debug(`Successfully pinged '${device.name}' (attempt ${i}/${attempts})`);
break;
} catch (error) {
logger.warning(`Failed to ping '${device.name}' (attempt ${i}/${attempts}, ${error.message})`);
logger.warning(`Failed to ping '${device.name}' (attempt ${i}/${attempts}, ${(error as Error).message})`);
// Try again in 3 seconds.
if (i !== attempts) {
@@ -125,7 +128,7 @@ export default class Availability extends Extension {
this.eventBus.onEntityRenamed(this, async (data) => {
if (utils.isAvailabilityEnabledForEntity(data.entity, settings.get())) {
await this.mqtt.publish(`${data.from}/availability`, null, {retain: true, qos: 1});
await this.mqtt.publish(`${data.from}/availability`, '', {retain: true, qos: 1});
await this.publishAvailability(data.entity, false, true);
}
});
@@ -134,7 +137,6 @@ export default class Availability extends Extension {
this.eventBus.onDeviceLeave(this, (data) => clearTimeout(this.timers[data.ieeeAddr]));
this.eventBus.onDeviceAnnounce(this, (data) => this.retrieveState(data.device));
this.eventBus.onLastSeenChanged(this, this.onLastSeenChanged);
this.eventBus.onPublishAvailability(this, this.publishAvailabilityForAllEntities);
this.eventBus.onGroupMembersChanged(this, (data) => this.publishAvailability(data.group, false));
// Publish initial availability
await this.publishAvailabilityForAllEntities();
@@ -162,7 +164,8 @@ export default class Availability extends Extension {
private async publishAvailability(entity: Device | Group, logLastSeen: boolean, forcePublish = false, skipGroups = false): Promise<void> {
if (logLastSeen && entity.isDevice()) {
const ago = Date.now() - entity.zh.lastSeen;
const ago = Date.now() - (entity.zh.lastSeen ?? /* v8 ignore next */ 0);
if (this.isActiveDevice(entity)) {
logger.debug(`Active device '${entity.name}' was last seen '${(ago / utils.minutes(1)).toFixed(2)}' minutes ago.`);
} else {
@@ -182,9 +185,9 @@ export default class Availability extends Extension {
}
const topic = `${entity.name}/availability`;
const payload = utils.availabilityPayload(available ? 'online' : 'offline', settings.get());
const payload: Zigbee2MQTTAPI['{friendlyName}/availability'] = {state: available ? 'online' : 'offline'};
this.availabilityCache[entity.ID] = available;
await this.mqtt.publish(topic, payload, {retain: true, qos: 1});
await this.mqtt.publish(topic, JSON.stringify(payload), {retain: true, qos: 1});
if (!skipGroups && entity.isDevice()) {
for (const group of this.zigbee.groupsIterator()) {
@@ -230,22 +233,26 @@ export default class Availability extends Extension {
continue;
}
const converter = device.definition.toZigbee.find((c) => !c.key || c.key.find((k) => item.keys.includes(k)));
const converter = device.definition!.toZigbee.find((c) => !c.key || c.key.find((k) => item.keys.includes(k)));
const options: KeyValue = device.options;
const state = this.state.get(device);
const meta: zhc.Tz.Meta = {
message: this.state.get(device),
mapped: device.definition,
endpoint_name: null,
mapped: device.definition!,
endpoint_name: undefined,
options,
state,
device: device.zh,
/* v8 ignore next */
publish: (payload: KeyValue) => this.publishEntityState(device, payload),
};
try {
await converter?.convertGet?.(device.endpoint(), item.keys[0], meta);
const endpoint = device.endpoint();
assert(endpoint);
await converter?.convertGet?.(endpoint, item.keys[0], meta);
} catch (error) {
logger.error(`Failed to read state of '${device.name}' after reconnect (${error.message})`);
logger.error(`Failed to read state of '${device.name}' after reconnect (${(error as Error).message})`);
}
await utils.sleep(500);
+216 -162
View File
@@ -1,6 +1,11 @@
import type {Zigbee2MQTTAPI, Zigbee2MQTTResponseEndpoints} from '../types/api';
import assert from 'node:assert';
import bind from 'bind-decorator';
import debounce from 'debounce';
import stringify from 'json-stable-stringify-without-jsonify';
import {Zcl} from 'zigbee-herdsman';
import {ClusterName} from 'zigbee-herdsman/dist/zspec/zcl/definition/tstype';
@@ -11,8 +16,6 @@ import * as settings from '../util/settings';
import utils from '../util/utils';
import Extension from './extension';
const LEGACY_API = settings.get().advanced.legacy_api;
const LEGACY_TOPIC_REGEX = new RegExp(`^${settings.get().mqtt.base_topic}/bridge/(bind|unbind)/.+$`);
const TOPIC_REGEX = new RegExp(`^${settings.get().mqtt.base_topic}/bridge/request/device/(bind|unbind)`);
const ALL_CLUSTER_CANDIDATES: readonly ClusterName[] = [
'genScenes',
@@ -87,7 +90,7 @@ const REPORT_CLUSTERS: Readonly<
type PollOnMessage = {
cluster: Readonly<Partial<Record<ClusterName, {type: string; data: KeyValue}[]>>>;
read: Readonly<{cluster: string; attributes: string[]; attributesForEndpoint?: (endpoint: zh.Endpoint) => Promise<string[]>}>;
manufacturerIDs: readonly number[];
manufacturerIDs: readonly Zcl.ManufacturerCode[];
manufacturerNames: readonly string[];
}[];
@@ -167,12 +170,10 @@ const POLL_ON_MESSAGE: Readonly<PollOnMessage> = [
const supportedAttrs = await getColorCapabilities(endpoint);
const readAttrs: string[] = [];
/* istanbul ignore else */
if (supportedAttrs.colorXY) {
readAttrs.push('currentX', 'currentY');
}
/* istanbul ignore else */
if (supportedAttrs.colorTemperature) {
readAttrs.push('colorTemperature');
}
@@ -194,10 +195,16 @@ const POLL_ON_MESSAGE: Readonly<PollOnMessage> = [
interface ParsedMQTTMessage {
type: 'bind' | 'unbind';
sourceKey: string;
targetKey: string;
clusters: string[];
sourceKey?: string;
sourceEndpointKey?: string | number;
targetKey?: string;
targetEndpointKey?: string | number;
clusters?: string[];
skipDisableReporting: boolean;
resolvedSource?: Device;
resolvedTarget?: Device | Group | typeof DEFAULT_BIND_GROUP;
resolvedSourceEndpoint?: zh.Endpoint;
resolvedBindTarget?: number | zh.Endpoint | zh.Group;
}
export default class Bind extends Extension {
@@ -209,153 +216,203 @@ export default class Bind extends Extension {
this.eventBus.onGroupMembersChanged(this, this.onGroupMembersChanged);
}
private parseMQTTMessage(data: eventdata.MQTTMessage): ParsedMQTTMessage {
let type: ParsedMQTTMessage['type'] = null;
let sourceKey: ParsedMQTTMessage['sourceKey'] = null;
let targetKey: ParsedMQTTMessage['targetKey'] = null;
let clusters: ParsedMQTTMessage['clusters'] = null;
let skipDisableReporting: ParsedMQTTMessage['skipDisableReporting'] = false;
private parseMQTTMessage(
data: eventdata.MQTTMessage,
): [raw: KeyValue | undefined, parsed: ParsedMQTTMessage | undefined, error: string | undefined] {
if (data.topic.match(TOPIC_REGEX)) {
const type = data.topic.endsWith('unbind') ? 'unbind' : 'bind';
let skipDisableReporting = false;
const message = JSON.parse(data.message) as Zigbee2MQTTAPI['bridge/request/device/bind'];
if (LEGACY_API && data.topic.match(LEGACY_TOPIC_REGEX)) {
const topic = data.topic.replace(`${settings.get().mqtt.base_topic}/bridge/`, '');
type = topic.split('/')[0] as ParsedMQTTMessage['type'];
sourceKey = topic.replace(`${type}/`, '');
targetKey = data.message;
} else if (data.topic.match(TOPIC_REGEX)) {
type = data.topic.endsWith('unbind') ? 'unbind' : 'bind';
const message = JSON.parse(data.message);
sourceKey = message.from;
targetKey = message.to;
clusters = message.clusters;
skipDisableReporting = 'skip_disable_reporting' in message ? message.skip_disable_reporting : false;
if (typeof message !== 'object' || message.from == undefined || message.to == undefined) {
return [message, {type, skipDisableReporting}, `Invalid payload`];
}
const sourceKey = message.from;
const sourceEndpointKey = message.from_endpoint ?? 'default';
const targetKey = message.to;
const targetEndpointKey = message.to_endpoint;
const clusters = message.clusters;
skipDisableReporting = message.skip_disable_reporting != undefined ? message.skip_disable_reporting : false;
const resolvedSource = this.zigbee.resolveEntity(message.from) as Device;
if (!resolvedSource || !(resolvedSource instanceof Device)) {
return [message, {type, skipDisableReporting}, `Source device '${message.from}' does not exist`];
}
const resolvedTarget = message.to === DEFAULT_BIND_GROUP.name ? DEFAULT_BIND_GROUP : this.zigbee.resolveEntity(message.to);
if (!resolvedTarget) {
return [message, {type, skipDisableReporting}, `Target device or group '${message.to}' does not exist`];
}
const resolvedSourceEndpoint = resolvedSource.endpoint(sourceEndpointKey);
if (!resolvedSourceEndpoint) {
return [
message,
{type, skipDisableReporting},
`Source device '${resolvedSource.name}' does not have endpoint '${sourceEndpointKey}'`,
];
}
// resolves to 'default' endpoint if targetEndpointKey is invalid (used by frontend for 'Coordinator')
const resolvedBindTarget =
resolvedTarget instanceof Device
? resolvedTarget.endpoint(targetEndpointKey)
: resolvedTarget instanceof Group
? resolvedTarget.zh
: Number(resolvedTarget.ID);
if (resolvedTarget instanceof Device && !resolvedBindTarget) {
return [
message,
{type, skipDisableReporting},
`Target device '${resolvedTarget.name}' does not have endpoint '${targetEndpointKey}'`,
];
}
return [
message,
{
type,
sourceKey,
sourceEndpointKey,
targetKey,
targetEndpointKey,
clusters,
skipDisableReporting,
resolvedSource,
resolvedTarget,
resolvedSourceEndpoint,
resolvedBindTarget,
},
undefined,
];
} else {
return [undefined, undefined, undefined];
}
return {type, sourceKey, targetKey, clusters, skipDisableReporting};
}
@bind private async onMQTTMessage(data: eventdata.MQTTMessage): Promise<void> {
const {type, sourceKey, targetKey, clusters, skipDisableReporting} = this.parseMQTTMessage(data);
const [raw, parsed, error] = this.parseMQTTMessage(data);
if (!type) {
return null;
}
const message = utils.parseJSON(data.message, data.message);
let error = null;
const parsedSource = this.zigbee.resolveEntityAndEndpoint(sourceKey);
const parsedTarget = this.zigbee.resolveEntityAndEndpoint(targetKey);
const source = parsedSource.entity;
const target = targetKey === DEFAULT_BIND_GROUP.name ? DEFAULT_BIND_GROUP : parsedTarget.entity;
const responseData: KeyValue = {from: sourceKey, to: targetKey};
if (!source || !(source instanceof Device)) {
error = `Source device '${sourceKey}' does not exist`;
} else if (parsedSource.endpointID && !parsedSource.endpoint) {
error = `Source device '${parsedSource.ID}' does not have endpoint '${parsedSource.endpointID}'`;
} else if (!target) {
error = `Target device or group '${targetKey}' does not exist`;
} else if (target instanceof Device && parsedTarget.endpointID && !parsedTarget.endpoint) {
error = `Target device '${parsedTarget.ID}' does not have endpoint '${parsedTarget.endpointID}'`;
} else {
const successfulClusters: string[] = [];
const failedClusters = [];
const attemptedClusters = [];
const bindSource: zh.Endpoint = parsedSource.endpoint;
const bindTarget: number | zh.Group | zh.Endpoint =
target instanceof Device ? parsedTarget.endpoint : target instanceof Group ? target.zh : Number(target.ID);
// Find which clusters are supported by both the source and target.
// Groups are assumed to support all clusters.
const clusterCandidates = clusters ?? ALL_CLUSTER_CANDIDATES;
for (const cluster of clusterCandidates) {
let matchingClusters = false;
const anyClusterValid = utils.isZHGroup(bindTarget) || typeof bindTarget === 'number' || (target as Device).zh.type === 'Coordinator';
if (!anyClusterValid && utils.isEndpoint(bindTarget)) {
matchingClusters =
(bindTarget.supportsInputCluster(cluster) && bindSource.supportsOutputCluster(cluster)) ||
(bindSource.supportsInputCluster(cluster) && bindTarget.supportsOutputCluster(cluster));
}
const sourceValid = bindSource.supportsInputCluster(cluster) || bindSource.supportsOutputCluster(cluster);
if (sourceValid && (anyClusterValid || matchingClusters)) {
logger.debug(`${type}ing cluster '${cluster}' from '${source.name}' to '${target.name}'`);
attemptedClusters.push(cluster);
try {
if (type === 'bind') {
await bindSource.bind(cluster, bindTarget);
} else {
await bindSource.unbind(cluster, bindTarget);
}
successfulClusters.push(cluster);
logger.info(
`Successfully ${type === 'bind' ? 'bound' : 'unbound'} cluster '${cluster}' from '${source.name}' to '${target.name}'`,
);
/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
await this.mqtt.publish(
'bridge/log',
stringify({type: `device_${type}`, message: {from: source.name, to: target.name, cluster}}),
);
}
} catch (error) {
failedClusters.push(cluster);
logger.error(`Failed to ${type} cluster '${cluster}' from '${source.name}' to '${target.name}' (${error})`);
/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
await this.mqtt.publish(
'bridge/log',
stringify({type: `device_${type}_failed`, message: {from: source.name, to: target.name, cluster}}),
);
}
}
}
}
if (attemptedClusters.length === 0) {
logger.error(`Nothing to ${type} from '${source.name}' to '${target.name}'`);
error = `Nothing to ${type}`;
/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
await this.mqtt.publish('bridge/log', stringify({type: `device_${type}_failed`, message: {from: source.name, to: target.name}}));
}
} else if (failedClusters.length === attemptedClusters.length) {
error = `Failed to ${type}`;
}
responseData[`clusters`] = successfulClusters;
responseData[`failed`] = failedClusters;
if (successfulClusters.length !== 0) {
if (type === 'bind') {
await this.setupReporting(bindSource.binds.filter((b) => successfulClusters.includes(b.cluster.name) && b.target === bindTarget));
} else if (typeof bindTarget !== 'number' && !skipDisableReporting) {
await this.disableUnnecessaryReportings(bindTarget);
}
}
}
const triggeredViaLegacyApi = data.topic.match(LEGACY_TOPIC_REGEX);
if (!triggeredViaLegacyApi) {
const response = utils.getResponse(message, responseData, error);
await this.mqtt.publish(`bridge/response/device/${type}`, stringify(response));
if (!raw || !parsed) {
return;
}
if (error) {
await this.publishResponse(parsed.type, raw, {}, error);
return;
}
const {
type,
sourceKey,
sourceEndpointKey,
targetKey,
targetEndpointKey,
clusters,
skipDisableReporting,
resolvedSource,
resolvedTarget,
resolvedSourceEndpoint,
resolvedBindTarget,
} = parsed;
assert(resolvedSource, '`resolvedSource` is missing');
assert(resolvedTarget, '`resolvedTarget` is missing');
assert(resolvedSourceEndpoint, '`resolvedSourceEndpoint` is missing');
assert(resolvedBindTarget != undefined, '`resolvedBindTarget` is missing');
const successfulClusters: string[] = [];
const failedClusters = [];
const attemptedClusters = [];
// Find which clusters are supported by both the source and target.
// Groups are assumed to support all clusters.
const clusterCandidates = clusters ?? ALL_CLUSTER_CANDIDATES;
for (const cluster of clusterCandidates) {
let matchingClusters = false;
const anyClusterValid =
utils.isZHGroup(resolvedBindTarget) ||
typeof resolvedBindTarget === 'number' ||
(resolvedTarget instanceof Device && resolvedTarget.zh.type === 'Coordinator');
if (!anyClusterValid && utils.isZHEndpoint(resolvedBindTarget)) {
matchingClusters =
(resolvedBindTarget.supportsInputCluster(cluster) && resolvedSourceEndpoint.supportsOutputCluster(cluster)) ||
(resolvedSourceEndpoint.supportsInputCluster(cluster) && resolvedBindTarget.supportsOutputCluster(cluster));
}
const sourceValid = resolvedSourceEndpoint.supportsInputCluster(cluster) || resolvedSourceEndpoint.supportsOutputCluster(cluster);
if (sourceValid && (anyClusterValid || matchingClusters)) {
logger.debug(`${type}ing cluster '${cluster}' from '${resolvedSource.name}' to '${resolvedTarget.name}'`);
attemptedClusters.push(cluster);
try {
if (type === 'bind') {
await resolvedSourceEndpoint.bind(cluster, resolvedBindTarget);
} else {
await resolvedSourceEndpoint.unbind(cluster, resolvedBindTarget);
}
successfulClusters.push(cluster);
logger.info(
`Successfully ${type === 'bind' ? 'bound' : 'unbound'} cluster '${cluster}' from '${resolvedSource.name}' to '${resolvedTarget.name}'`,
);
} catch (error) {
failedClusters.push(cluster);
logger.error(`Failed to ${type} cluster '${cluster}' from '${resolvedSource.name}' to '${resolvedTarget.name}' (${error})`);
}
}
}
if (attemptedClusters.length === 0) {
logger.error(`Nothing to ${type} from '${resolvedSource.name}' to '${resolvedTarget.name}'`);
await this.publishResponse(parsed.type, raw, {}, `Nothing to ${type}`);
return;
} else if (failedClusters.length === attemptedClusters.length) {
await this.publishResponse(parsed.type, raw, {}, `Failed to ${type}`);
return;
}
const responseData: Zigbee2MQTTAPI['bridge/response/device/bind'] | Zigbee2MQTTAPI['bridge/response/device/unbind'] = {
from: sourceKey!, // valid with assert above on `resolvedSource`
from_endpoint: sourceEndpointKey!, // valid with assert above on `resolvedSourceEndpoint`
to: targetKey!, // valid with assert above on `resolvedTarget`
to_endpoint: targetEndpointKey,
clusters: successfulClusters,
failed: failedClusters,
};
if (successfulClusters.length !== 0) {
if (type === 'bind') {
await this.setupReporting(
resolvedSourceEndpoint.binds.filter((b) => successfulClusters.includes(b.cluster.name) && b.target === resolvedBindTarget),
);
} else if (typeof resolvedBindTarget !== 'number' && !skipDisableReporting) {
await this.disableUnnecessaryReportings(resolvedBindTarget);
}
}
await this.publishResponse(parsed.type, raw, responseData);
this.eventBus.emitDevicesChanged();
}
private async publishResponse<T extends Zigbee2MQTTResponseEndpoints>(
type: ParsedMQTTMessage['type'],
request: KeyValue,
data: Zigbee2MQTTAPI[T],
error?: string,
): Promise<void> {
const response = utils.getResponse(request, data, error);
await this.mqtt.publish(`bridge/response/device/${type}`, stringify(response));
if (error) {
logger.error(error);
} else {
this.eventBus.emitDevicesChanged();
}
}
@@ -383,7 +440,7 @@ export default class Bind extends Extension {
}
getSetupReportingEndpoints(bind: zh.Bind, coordinatorEp: zh.Endpoint): zh.Endpoint[] {
const endpoints = utils.isEndpoint(bind.target) ? [bind.target] : bind.target.members;
const endpoints = utils.isZHEndpoint(bind.target) ? [bind.target] : bind.target.members;
return endpoints.filter((e) => {
if (!e.supportsInputCluster(bind.cluster.name)) {
@@ -406,18 +463,16 @@ export default class Bind extends Extension {
const coordinatorEndpoint = this.zigbee.firstCoordinatorEndpoint();
for (const bind of binds) {
/* istanbul ignore else */
if (bind.cluster.name in REPORT_CLUSTERS) {
for (const endpoint of this.getSetupReportingEndpoints(bind, coordinatorEndpoint)) {
const entity = `${this.zigbee.resolveEntity(endpoint.getDevice()).name}/${endpoint.ID}`;
const entity = `${this.zigbee.resolveEntity(endpoint.getDevice())!.name}/${endpoint.ID}`;
try {
await endpoint.bind(bind.cluster.name, coordinatorEndpoint);
const items = [];
for (const c of REPORT_CLUSTERS[bind.cluster.name as ClusterName]) {
/* istanbul ignore else */
for (const c of REPORT_CLUSTERS[bind.cluster.name as ClusterName]!) {
if (!c.condition || (await c.condition(endpoint))) {
const i = {...c};
delete i.condition;
@@ -429,7 +484,7 @@ export default class Bind extends Extension {
await endpoint.configureReporting(bind.cluster.name, items);
logger.info(`Successfully setup reporting for '${entity}' cluster '${bind.cluster.name}'`);
} catch (error) {
logger.warning(`Failed to setup reporting for '${entity}' cluster '${bind.cluster.name}' (${error.message})`);
logger.warning(`Failed to setup reporting for '${entity}' cluster '${bind.cluster.name}' (${(error as Error).message})`);
}
}
}
@@ -440,7 +495,7 @@ export default class Bind extends Extension {
async disableUnnecessaryReportings(target: zh.Group | zh.Endpoint): Promise<void> {
const coordinator = this.zigbee.firstCoordinatorEndpoint();
const endpoints = utils.isEndpoint(target) ? [target] : target.members;
const endpoints = utils.isZHEndpoint(target) ? [target] : target.members;
const allBinds: zh.Bind[] = [];
for (const device of this.zigbee.devicesIterator(utils.deviceNotCoordinator)) {
@@ -458,13 +513,12 @@ export default class Bind extends Extension {
const boundClusters: string[] = [];
for (const bind of allBinds) {
if (utils.isEndpoint(bind.target) ? bind.target === endpoint : bind.target.members.includes(endpoint)) {
if (utils.isZHEndpoint(bind.target) ? bind.target === endpoint : bind.target.members.includes(endpoint)) {
requiredClusters.push(bind.cluster.name);
}
}
for (const b of endpoint.binds) {
/* istanbul ignore else */
if (b.target === coordinator && !requiredClusters.includes(b.cluster.name) && b.cluster.name in REPORT_CLUSTERS) {
boundClusters.push(b.cluster.name);
}
@@ -476,8 +530,7 @@ export default class Bind extends Extension {
const items = [];
for (const item of REPORT_CLUSTERS[cluster as ClusterName]) {
/* istanbul ignore else */
for (const item of REPORT_CLUSTERS[cluster as ClusterName]!) {
if (!item.condition || (await item.condition(endpoint))) {
const i = {...item};
delete i.condition;
@@ -489,7 +542,7 @@ export default class Bind extends Extension {
await endpoint.configureReporting(cluster, items);
logger.info(`Successfully disabled reporting for '${entity}' cluster '${cluster}'`);
} catch (error) {
logger.warning(`Failed to disable reporting for '${entity}' cluster '${cluster}' (${error.message})`);
logger.warning(`Failed to disable reporting for '${entity}' cluster '${cluster}' (${(error as Error).message})`);
}
}
@@ -516,7 +569,7 @@ export default class Bind extends Extension {
// Add bound devices
for (const endpoint of data.device.zh.endpoints) {
for (const bind of endpoint.binds) {
if (utils.isEndpoint(bind.target) && bind.target.getDevice().type !== 'Coordinator') {
if (utils.isZHEndpoint(bind.target) && bind.target.getDevice().type !== 'Coordinator') {
toPoll.add(bind.target);
}
}
@@ -532,10 +585,11 @@ export default class Bind extends Extension {
}
for (const endpoint of toPoll) {
const device = endpoint.getDevice();
for (const poll of polls) {
// XXX: manufacturerID/manufacturerName can be undefined and won't match `includes`, but TS enforces same-type
if (
(!poll.manufacturerIDs.includes(endpoint.getDevice().manufacturerID) &&
!poll.manufacturerNames.includes(endpoint.getDevice().manufacturerName)) ||
(!poll.manufacturerIDs.includes(device.manufacturerID!) && !poll.manufacturerNames.includes(device.manufacturerName!)) ||
!endpoint.supportsInputCluster(poll.read.cluster)
) {
continue;
@@ -548,7 +602,7 @@ export default class Bind extends Extension {
readAttrs = [...poll.read.attributes, ...attrsForEndpoint];
}
const key = `${endpoint.getDevice().ieeeAddr}_${endpoint.ID}_${POLL_ON_MESSAGE.indexOf(poll)}`;
const key = `${device.ieeeAddr}_${endpoint.ID}_${POLL_ON_MESSAGE.indexOf(poll)}`;
if (!this.pollDebouncers[key]) {
this.pollDebouncers[key] = debounce(async () => {
@@ -556,7 +610,7 @@ export default class Bind extends Extension {
await endpoint.read(poll.read.cluster, readAttrs);
} catch (error) {
logger.error(
`Failed to poll ${readAttrs} from ${this.zigbee.resolveEntity(endpoint.getDevice()).name} (${error.message})`,
`Failed to poll ${readAttrs} from ${this.zigbee.resolveEntity(device)!.name} (${(error as Error).message})`,
);
}
}, 1000);
+274 -296
View File
@@ -1,13 +1,15 @@
/* eslint-disable camelcase */
import type {Zigbee2MQTTAPI, Zigbee2MQTTDevice, Zigbee2MQTTResponse, Zigbee2MQTTResponseEndpoints} from '../types/api';
import fs from 'node:fs';
import bind from 'bind-decorator';
import fs from 'fs';
import stringify from 'json-stable-stringify-without-jsonify';
import JSZip from 'jszip';
import objectAssignDeep from 'object-assign-deep';
import winston from 'winston';
import Transport from 'winston-transport';
import {Clusters} from 'zigbee-herdsman/dist/zspec/zcl/definition/cluster';
import {CustomClusters, ClusterDefinition, ClusterName} from 'zigbee-herdsman/dist/zspec/zcl/definition/tstype';
import {Zcl} from 'zigbee-herdsman';
import * as zhc from 'zigbee-herdsman-converters';
import Device from '../model/device';
@@ -18,58 +20,41 @@ import * as settings from '../util/settings';
import utils from '../util/utils';
import Extension from './extension';
const requestRegex = new RegExp(`${settings.get().mqtt.base_topic}/bridge/request/(.*)`);
type DefinitionPayload = {
model: string;
vendor: string;
description: string;
exposes: zhc.Expose[];
supports_ota: boolean;
icon: string;
options: zhc.Expose[];
};
const REQUEST_REGEX = new RegExp(`${settings.get().mqtt.base_topic}/bridge/request/(.*)`);
export default class Bridge extends Extension {
private zigbee2mqttVersion: {commitHash: string; version: string};
private zigbeeHerdsmanVersion: {version: string};
private zigbeeHerdsmanConvertersVersion: {version: string};
private coordinatorVersion: zh.CoordinatorVersion;
private zigbee2mqttVersion!: {commitHash?: string; version: string};
private zigbeeHerdsmanVersion!: {version: string};
private zigbeeHerdsmanConvertersVersion!: {version: string};
private coordinatorVersion!: zh.CoordinatorVersion;
private restartRequired = false;
private lastJoinedDeviceIeeeAddr: string;
private lastBridgeLoggingPayload: string;
private logTransport: winston.transport;
private requestLookup: {[key: string]: (message: KeyValue | string) => Promise<MQTTResponse>};
private lastJoinedDeviceIeeeAddr?: string;
private lastBridgeLoggingPayload?: string;
private logTransport!: winston.transport;
private requestLookup: {[key: string]: (message: KeyValue | string) => Promise<Zigbee2MQTTResponse<Zigbee2MQTTResponseEndpoints>>} = {
'device/options': this.deviceOptions,
'device/configure_reporting': this.deviceConfigureReporting,
'device/remove': this.deviceRemove,
'device/interview': this.deviceInterview,
'device/generate_external_definition': this.deviceGenerateExternalDefinition,
'device/rename': this.deviceRename,
'group/add': this.groupAdd,
'group/options': this.groupOptions,
'group/remove': this.groupRemove,
'group/rename': this.groupRename,
permit_join: this.permitJoin,
restart: this.restart,
backup: this.backup,
'touchlink/factory_reset': this.touchlinkFactoryReset,
'touchlink/identify': this.touchlinkIdentify,
'install_code/add': this.installCodeAdd,
'touchlink/scan': this.touchlinkScan,
health_check: this.healthCheck,
coordinator_check: this.coordinatorCheck,
options: this.bridgeOptions,
};
override async start(): Promise<void> {
this.requestLookup = {
'device/options': this.deviceOptions,
'device/configure_reporting': this.deviceConfigureReporting,
'device/remove': this.deviceRemove,
'device/interview': this.deviceInterview,
'device/generate_external_definition': this.deviceGenerateExternalDefinition,
'device/rename': this.deviceRename,
'group/add': this.groupAdd,
'group/options': this.groupOptions,
'group/remove': this.groupRemove,
'group/rename': this.groupRename,
permit_join: this.permitJoin,
restart: this.restart,
backup: this.backup,
'touchlink/factory_reset': this.touchlinkFactoryReset,
'touchlink/identify': this.touchlinkIdentify,
'install_code/add': this.installCodeAdd,
'touchlink/scan': this.touchlinkScan,
health_check: this.healthCheck,
coordinator_check: this.coordinatorCheck,
options: this.bridgeOptions,
// Below are deprecated
'config/last_seen': this.configLastSeen,
'config/homeassistant': this.configHomeAssistant,
'config/elapsed': this.configElapsed,
'config/log_level': this.configLogLevel,
};
const debugToMQTTFrontend = settings.get().advanced.log_debug_to_mqtt_frontend;
const baseTopic = settings.get().mqtt.base_topic;
@@ -84,7 +69,7 @@ export default class Bridge extends Extension {
if (debugToMQTTFrontend) {
class DebugEventTransport extends Transport {
log(info: {message: string; level: string; namespace: string}, next: () => void): void {
override log(info: {message: string; level: string; namespace: string}, next: () => void): void {
bridgeLogging(info.message, info.level, info.namespace);
next();
}
@@ -93,7 +78,7 @@ export default class Bridge extends Extension {
this.logTransport = new DebugEventTransport();
} else {
class EventTransport extends Transport {
log(info: {message: string; level: string; namespace: string}, next: () => void): void {
override log(info: {message: string; level: string; namespace: string}, next: () => void): void {
if (info.level !== 'debug') {
bridgeLogging(info.message, info.level, info.namespace);
}
@@ -111,41 +96,84 @@ export default class Bridge extends Extension {
this.zigbeeHerdsmanConvertersVersion = await utils.getDependencyVersion('zigbee-herdsman-converters');
this.coordinatorVersion = await this.zigbee.getCoordinatorVersion();
this.eventBus.onEntityRenamed(this, () => this.publishInfo());
this.eventBus.onGroupMembersChanged(this, () => this.publishGroups());
this.eventBus.onDevicesChanged(this, () => this.publishDevices() && this.publishInfo() && this.publishDefinitions());
this.eventBus.onPermitJoinChanged(this, () => !this.zigbee.isStopping() && this.publishInfo());
this.eventBus.onEntityRenamed(this, async () => {
await this.publishInfo();
});
this.eventBus.onGroupMembersChanged(this, async () => {
await this.publishGroups();
});
this.eventBus.onDevicesChanged(this, async () => {
await this.publishDevices();
await this.publishInfo();
await this.publishDefinitions();
});
this.eventBus.onPermitJoinChanged(this, async () => {
if (!this.zigbee.isStopping()) {
await this.publishInfo();
}
});
this.eventBus.onScenesChanged(this, async () => {
await this.publishDevices();
await this.publishGroups();
});
// Zigbee events
const publishEvent = async (type: string, data: KeyValue): Promise<void> =>
this.mqtt.publish('bridge/event', stringify({type, data}), {retain: false, qos: 0});
this.eventBus.onDeviceJoined(this, async (data) => {
this.lastJoinedDeviceIeeeAddr = data.device.ieeeAddr;
await this.publishDevices();
await publishEvent('device_joined', {friendly_name: data.device.name, ieee_address: data.device.ieeeAddr});
const payload: Zigbee2MQTTAPI['bridge/event'] = {
type: 'device_joined',
data: {friendly_name: data.device.name, ieee_address: data.device.ieeeAddr},
};
await this.mqtt.publish('bridge/event', stringify(payload), {retain: false, qos: 0});
});
this.eventBus.onDeviceLeave(this, async (data) => {
await this.publishDevices();
await this.publishDefinitions();
await publishEvent('device_leave', {ieee_address: data.ieeeAddr, friendly_name: data.name});
const payload: Zigbee2MQTTAPI['bridge/event'] = {type: 'device_leave', data: {ieee_address: data.ieeeAddr, friendly_name: data.name}};
await this.mqtt.publish('bridge/event', stringify(payload), {retain: false, qos: 0});
});
this.eventBus.onDeviceNetworkAddressChanged(this, async () => {
await this.publishDevices();
});
this.eventBus.onDeviceNetworkAddressChanged(this, () => this.publishDevices());
this.eventBus.onDeviceInterview(this, async (data) => {
await this.publishDevices();
const payload: KeyValue = {friendly_name: data.device.name, status: data.status, ieee_address: data.device.ieeeAddr};
let payload: Zigbee2MQTTAPI['bridge/event'];
if (data.status === 'successful') {
payload.supported = data.device.isSupported;
payload.definition = this.getDefinitionPayload(data.device);
payload = {
type: 'device_interview',
data: {
friendly_name: data.device.name,
status: data.status,
ieee_address: data.device.ieeeAddr,
supported: data.device.isSupported,
definition: this.getDefinitionPayload(data.device),
},
};
} else {
payload = {
type: 'device_interview',
data: {friendly_name: data.device.name, status: data.status, ieee_address: data.device.ieeeAddr},
};
}
await publishEvent('device_interview', payload);
await this.mqtt.publish('bridge/event', stringify(payload), {retain: false, qos: 0});
});
this.eventBus.onDeviceAnnounce(this, async (data) => {
await this.publishDevices();
await publishEvent('device_announce', {friendly_name: data.device.name, ieee_address: data.device.ieeeAddr});
const payload: Zigbee2MQTTAPI['bridge/event'] = {
type: 'device_announce',
data: {friendly_name: data.device.name, ieee_address: data.device.ieeeAddr},
};
await this.mqtt.publish('bridge/event', stringify(payload), {retain: false, qos: 0});
});
await this.publishInfo();
@@ -162,8 +190,14 @@ export default class Bridge extends Extension {
}
@bind async onMQTTMessage(data: eventdata.MQTTMessage): Promise<void> {
const match = data.topic.match(requestRegex);
const key = match?.[1]?.toLowerCase();
const match = data.topic.match(REQUEST_REGEX);
if (!match) {
return;
}
const key = match[1].toLowerCase();
if (key in this.requestLookup) {
const message = utils.parseJSON(data.message, data.message);
@@ -171,9 +205,9 @@ export default class Bridge extends Extension {
const response = await this.requestLookup[key](message);
await this.mqtt.publish(`bridge/response/${match[1]}`, stringify(response));
} catch (error) {
logger.error(`Request '${data.topic}' failed with error: '${error.message}'`);
logger.debug(error.stack);
const response = utils.getResponse(message, {}, error.message);
logger.error(`Request '${data.topic}' failed with error: '${(error as Error).message}'`);
logger.debug((error as Error).stack!);
const response = utils.getResponse(message, {}, (error as Error).message);
await this.mqtt.publish(`bridge/response/${match[1]}`, stringify(response));
}
}
@@ -183,30 +217,25 @@ export default class Bridge extends Extension {
* Requests
*/
@bind async deviceOptions(message: KeyValue | string): Promise<MQTTResponse> {
return this.changeEntityOptions('device', message);
@bind async deviceOptions(message: KeyValue | string): Promise<Zigbee2MQTTResponse<'bridge/response/device/options'>> {
return await this.changeEntityOptions('device', message);
}
@bind async groupOptions(message: KeyValue | string): Promise<MQTTResponse> {
return this.changeEntityOptions('group', message);
@bind async groupOptions(message: KeyValue | string): Promise<Zigbee2MQTTResponse<'bridge/response/group/options'>> {
return await this.changeEntityOptions('group', message);
}
@bind async bridgeOptions(message: KeyValue | string): Promise<MQTTResponse> {
@bind async bridgeOptions(message: KeyValue | string): Promise<Zigbee2MQTTResponse<'bridge/response/options'>> {
if (typeof message !== 'object' || typeof message.options !== 'object') {
throw new Error(`Invalid payload`);
}
const newSettings = message.options;
const restartRequired = settings.apply(newSettings);
if (restartRequired) this.restartRequired = true;
const newSettings = message.options as Partial<Settings>;
this.restartRequired = settings.apply(newSettings);
// Apply some settings on-the-fly.
if (newSettings.permit_join != undefined) {
await this.zigbee.permitJoin(settings.get().permit_join);
}
if (newSettings.homeassistant != undefined) {
await this.enableDisableExtension(!!settings.get().homeassistant, 'HomeAssistant');
if (newSettings.homeassistant) {
await this.enableDisableExtension(settings.get().homeassistant.enabled, 'HomeAssistant');
}
if (newSettings.advanced?.log_level != undefined) {
@@ -223,58 +252,58 @@ export default class Bridge extends Extension {
logger.info('Successfully changed options');
await this.publishInfo();
return utils.getResponse(message, {restart_required: this.restartRequired}, null);
return utils.getResponse(message, {restart_required: this.restartRequired});
}
@bind async deviceRemove(message: string | KeyValue): Promise<MQTTResponse> {
return this.removeEntity('device', message);
@bind async deviceRemove(message: string | KeyValue): Promise<Zigbee2MQTTResponse<'bridge/response/device/remove'>> {
return await this.removeEntity('device', message);
}
@bind async groupRemove(message: string | KeyValue): Promise<MQTTResponse> {
return this.removeEntity('group', message);
@bind async groupRemove(message: string | KeyValue): Promise<Zigbee2MQTTResponse<'bridge/response/group/remove'>> {
return await this.removeEntity('group', message);
}
@bind async healthCheck(message: string | KeyValue): Promise<MQTTResponse> {
return utils.getResponse(message, {healthy: true}, null);
@bind async healthCheck(message: string | KeyValue): Promise<Zigbee2MQTTResponse<'bridge/response/health_check'>> {
return utils.getResponse(message, {healthy: true});
}
@bind async coordinatorCheck(message: string | KeyValue): Promise<MQTTResponse> {
@bind async coordinatorCheck(message: string | KeyValue): Promise<Zigbee2MQTTResponse<'bridge/response/coordinator_check'>> {
const result = await this.zigbee.coordinatorCheck();
const missingRouters = result.missingRouters.map((d) => {
return {ieee_address: d.ieeeAddr, friendly_name: d.name};
});
return utils.getResponse(message, {missing_routers: missingRouters}, null);
return utils.getResponse(message, {missing_routers: missingRouters});
}
@bind async groupAdd(message: string | KeyValue): Promise<MQTTResponse> {
if (typeof message === 'object' && !message.hasOwnProperty('friendly_name')) {
@bind async groupAdd(message: string | KeyValue): Promise<Zigbee2MQTTResponse<'bridge/response/group/add'>> {
if (typeof message === 'object' && message.friendly_name === undefined) {
throw new Error(`Invalid payload`);
}
const friendlyName = typeof message === 'object' ? message.friendly_name : message;
const ID = typeof message === 'object' && message.hasOwnProperty('id') ? message.id : null;
const ID = typeof message === 'object' && message.id !== undefined ? message.id : null;
const group = settings.addGroup(friendlyName, ID);
this.zigbee.createGroup(group.ID);
await this.publishGroups();
return utils.getResponse(message, {friendly_name: group.friendly_name, id: group.ID}, null);
return utils.getResponse(message, {friendly_name: group.friendly_name, id: group.ID});
}
@bind async deviceRename(message: string | KeyValue): Promise<MQTTResponse> {
return this.renameEntity('device', message);
@bind async deviceRename(message: string | KeyValue): Promise<Zigbee2MQTTResponse<'bridge/response/device/rename'>> {
return await this.renameEntity('device', message);
}
@bind async groupRename(message: string | KeyValue): Promise<MQTTResponse> {
return this.renameEntity('group', message);
@bind async groupRename(message: string | KeyValue): Promise<Zigbee2MQTTResponse<'bridge/response/group/rename'>> {
return await this.renameEntity('group', message);
}
@bind async restart(message: string | KeyValue): Promise<MQTTResponse> {
@bind async restart(message: string | KeyValue): Promise<Zigbee2MQTTResponse<'bridge/response/restart'>> {
// Wait 500 ms before restarting so response can be send.
setTimeout(this.restartCallback, 500);
logger.info('Restarting Zigbee2MQTT');
return utils.getResponse(message, {}, null);
return utils.getResponse(message, {});
}
@bind async backup(message: string | KeyValue): Promise<MQTTResponse> {
@bind async backup(message: string | KeyValue): Promise<Zigbee2MQTTResponse<'bridge/response/backup'>> {
await this.zigbee.backup();
const dataPath = data.getPath();
const files = utils
@@ -284,33 +313,34 @@ export default class Bridge extends Extension {
const zip = new JSZip();
files.forEach((f) => zip.file(f[1], fs.readFileSync(f[0])));
const base64Zip = await zip.generateAsync({type: 'base64'});
return utils.getResponse(message, {zip: base64Zip}, null);
return utils.getResponse(message, {zip: base64Zip});
}
@bind async installCodeAdd(message: KeyValue | string): Promise<MQTTResponse> {
if (typeof message === 'object' && !message.hasOwnProperty('value')) {
@bind async installCodeAdd(message: KeyValue | string): Promise<Zigbee2MQTTResponse<'bridge/response/install_code/add'>> {
if (typeof message === 'object' && message.value === undefined) {
throw new Error('Invalid payload');
}
const value = typeof message === 'object' ? message.value : message;
await this.zigbee.addInstallCode(value);
logger.info('Successfully added new install code');
return utils.getResponse(message, {value}, null);
return utils.getResponse(message, {value});
}
@bind async permitJoin(message: KeyValue | string): Promise<MQTTResponse> {
if (typeof message === 'object' && !message.hasOwnProperty('value')) {
throw new Error('Invalid payload');
}
@bind async permitJoin(message: KeyValue | string): Promise<Zigbee2MQTTResponse<'bridge/response/permit_join'>> {
let time: number | undefined;
let device: Device | undefined;
let value: boolean | string;
let time: number;
let device: Device = null;
if (typeof message === 'object') {
value = message.value;
time = message.time;
if (message.time === undefined) {
throw new Error('Invalid payload');
}
time = Number.parseInt(message.time, 10);
if (message.device) {
const resolved = this.zigbee.resolveEntity(message.device);
if (resolved instanceof Device) {
device = resolved;
} else {
@@ -318,90 +348,42 @@ export default class Bridge extends Extension {
}
}
} else {
value = message;
time = Number.parseInt(message, 10);
}
if (typeof value === 'string') {
value = value.toLowerCase() === 'true';
await this.zigbee.permitJoin(time, device);
const response: {time: number; device?: string} = {time};
if (device) {
response.device = device.name;
}
await this.zigbee.permitJoin(value, device, time);
const response: {value: boolean; device?: string; time?: number} = {value};
if (device && typeof message === 'object') response.device = message.device;
if (time && typeof message === 'object') response.time = message.time;
return utils.getResponse(message, response, null);
return utils.getResponse(message, response);
}
// Deprecated
@bind async configLastSeen(message: KeyValue | string): Promise<MQTTResponse> {
const allowed = ['disable', 'ISO_8601', 'epoch', 'ISO_8601_local'];
const value = this.getValue(message);
if (typeof value !== 'string' || !allowed.includes(value)) {
throw new Error(`'${value}' is not an allowed value, allowed: ${allowed}`);
}
settings.set(['advanced', 'last_seen'], value);
await this.publishInfo();
return utils.getResponse(message, {value}, null);
}
// Deprecated
@bind async configHomeAssistant(message: string | KeyValue): Promise<MQTTResponse> {
const allowed = [true, false];
const value = this.getValue(message);
if (typeof value !== 'boolean' || !allowed.includes(value)) {
throw new Error(`'${value}' is not an allowed value, allowed: ${allowed}`);
}
await this.enableDisableExtension(value, 'HomeAssistant');
settings.set(['homeassistant'], value);
await this.publishInfo();
return utils.getResponse(message, {value}, null);
}
// Deprecated
@bind async configElapsed(message: KeyValue | string): Promise<MQTTResponse> {
const allowed = [true, false];
const value = this.getValue(message);
if (typeof value !== 'boolean' || !allowed.includes(value)) {
throw new Error(`'${value}' is not an allowed value, allowed: ${allowed}`);
}
settings.set(['advanced', 'elapsed'], value);
await this.publishInfo();
return utils.getResponse(message, {value}, null);
}
// Deprecated
@bind async configLogLevel(message: KeyValue | string): Promise<MQTTResponse> {
const value = this.getValue(message) as settings.LogLevel;
if (typeof value !== 'string' || !settings.LOG_LEVELS.includes(value)) {
throw new Error(`'${value}' is not an allowed value, allowed: ${settings.LOG_LEVELS}`);
}
logger.setLevel(value);
await this.publishInfo();
return utils.getResponse(message, {value}, null);
}
@bind async touchlinkIdentify(message: KeyValue | string): Promise<MQTTResponse> {
if (typeof message !== 'object' || !message.hasOwnProperty('ieee_address') || !message.hasOwnProperty('channel')) {
@bind async touchlinkIdentify(message: KeyValue | string): Promise<Zigbee2MQTTResponse<'bridge/response/touchlink/identify'>> {
if (typeof message !== 'object' || message.ieee_address === undefined || message.channel === undefined) {
throw new Error('Invalid payload');
}
logger.info(`Start Touchlink identify of '${message.ieee_address}' on channel ${message.channel}`);
await this.zigbee.touchlinkIdentify(message.ieee_address, message.channel);
return utils.getResponse(message, {ieee_address: message.ieee_address, channel: message.channel}, null);
return utils.getResponse(message, {ieee_address: message.ieee_address, channel: message.channel});
}
@bind async touchlinkFactoryReset(message: KeyValue | string): Promise<MQTTResponse> {
@bind async touchlinkFactoryReset(message: KeyValue | string): Promise<Zigbee2MQTTResponse<'bridge/response/touchlink/factory_reset'>> {
let result = false;
const payload: {ieee_address?: string; channel?: number} = {};
if (typeof message === 'object' && message.hasOwnProperty('ieee_address') && message.hasOwnProperty('channel')) {
let payload: Zigbee2MQTTAPI['bridge/response/touchlink/factory_reset'] = {};
if (typeof message === 'object' && message.ieee_address !== undefined && message.channel !== undefined) {
logger.info(`Start Touchlink factory reset of '${message.ieee_address}' on channel ${message.channel}`);
result = await this.zigbee.touchlinkFactoryReset(message.ieee_address, message.channel);
payload.ieee_address = message.ieee_address;
payload.channel = message.channel;
payload = {
ieee_address: message.ieee_address,
channel: message.channel,
};
} else {
logger.info('Start Touchlink factory reset of first found device');
result = await this.zigbee.touchlinkFactoryResetFirst();
@@ -409,41 +391,32 @@ export default class Bridge extends Extension {
if (result) {
logger.info('Successfully factory reset device through Touchlink');
return utils.getResponse(message, payload, null);
return utils.getResponse(message, payload);
} else {
logger.error('Failed to factory reset device through Touchlink');
throw new Error('Failed to factory reset device through Touchlink');
}
}
@bind async touchlinkScan(message: KeyValue | string): Promise<MQTTResponse> {
@bind async touchlinkScan(message: KeyValue | string): Promise<Zigbee2MQTTResponse<'bridge/response/touchlink/scan'>> {
logger.info('Start Touchlink scan');
const result = await this.zigbee.touchlinkScan();
const found = result.map((r) => {
return {ieee_address: r.ieeeAddr, channel: r.channel};
});
logger.info('Finished Touchlink scan');
return utils.getResponse(message, {found}, null);
return utils.getResponse(message, {found});
}
/**
* Utils
*/
getValue(message: KeyValue | string): string | boolean | number {
if (typeof message === 'object') {
if (!message.hasOwnProperty('value')) {
throw new Error('No value given');
}
return message.value;
} else {
return message;
}
}
async changeEntityOptions(entityType: 'device' | 'group', message: KeyValue | string): Promise<MQTTResponse> {
if (typeof message !== 'object' || !message.hasOwnProperty('id') || !message.hasOwnProperty('options')) {
async changeEntityOptions<T extends 'device' | 'group'>(
entityType: T,
message: KeyValue | string,
): Promise<Zigbee2MQTTResponse<T extends 'device' ? 'bridge/response/device/options' : 'bridge/response/group/options'>> {
if (typeof message !== 'object' || message.id === undefined || message.options === undefined) {
throw new Error(`Invalid payload`);
}
@@ -459,6 +432,16 @@ export default class Bridge extends Extension {
const ID = message.id;
const entity = this.getEntity(entityType, ID);
const oldOptions = objectAssignDeep({}, cleanup(entity.options));
if (message.options.icon) {
const base64Match = utils.matchBase64File(message.options.icon);
if (base64Match) {
const fileSettings = utils.saveBase64DeviceIcon(base64Match);
message.options.icon = fileSettings;
logger.debug(`Saved base64 image as file to '${fileSettings}'`);
}
}
const restartRequired = settings.changeEntityOptions(ID, message.options);
if (restartRequired) this.restartRequired = true;
const newOptions = cleanup(entity.options);
@@ -467,28 +450,28 @@ export default class Bridge extends Extension {
logger.info(`Changed config for ${entityType} ${ID}`);
this.eventBus.emitEntityOptionsChanged({from: oldOptions, to: newOptions, entity});
return utils.getResponse(message, {from: oldOptions, to: newOptions, id: ID, restart_required: this.restartRequired}, null);
return utils.getResponse(message, {from: oldOptions, to: newOptions, id: ID, restart_required: this.restartRequired});
}
@bind async deviceConfigureReporting(message: string | KeyValue): Promise<MQTTResponse> {
@bind async deviceConfigureReporting(message: string | KeyValue): Promise<Zigbee2MQTTResponse<'bridge/response/device/configure_reporting'>> {
if (
typeof message !== 'object' ||
!message.hasOwnProperty('id') ||
!message.hasOwnProperty('cluster') ||
!message.hasOwnProperty('maximum_report_interval') ||
!message.hasOwnProperty('minimum_report_interval') ||
!message.hasOwnProperty('reportable_change') ||
!message.hasOwnProperty('attribute')
message.id === undefined ||
message.endpoint === undefined ||
message.cluster === undefined ||
message.maximum_report_interval === undefined ||
message.minimum_report_interval === undefined ||
message.reportable_change === undefined ||
message.attribute === undefined
) {
throw new Error(`Invalid payload`);
}
const device = this.zigbee.resolveEntityAndEndpoint(message.id);
if (!device.entity) throw new Error(`Device '${message.id}' does not exist`);
const device = this.getEntity('device', message.id);
const endpoint = device.endpoint(message.endpoint);
const endpoint = device.endpoint;
if (device.endpointID && !endpoint) {
throw new Error(`Device '${device.ID}' does not have endpoint '${device.endpointID}'`);
if (!endpoint) {
throw new Error(`Device '${device.ID}' does not have endpoint '${message.endpoint}'`);
}
const coordinatorEndpoint = this.zigbee.firstCoordinatorEndpoint();
@@ -511,26 +494,23 @@ export default class Bridge extends Extension {
logger.info(`Configured reporting for '${message.id}', '${message.cluster}.${message.attribute}'`);
return utils.getResponse(
message,
{
id: message.id,
cluster: message.cluster,
maximum_report_interval: message.maximum_report_interval,
minimum_report_interval: message.minimum_report_interval,
reportable_change: message.reportable_change,
attribute: message.attribute,
},
null,
);
return utils.getResponse(message, {
id: message.id,
endpoint: message.endpoint,
cluster: message.cluster,
maximum_report_interval: message.maximum_report_interval,
minimum_report_interval: message.minimum_report_interval,
reportable_change: message.reportable_change,
attribute: message.attribute,
});
}
@bind async deviceInterview(message: string | KeyValue): Promise<MQTTResponse> {
if (typeof message !== 'object' || !message.hasOwnProperty('id')) {
@bind async deviceInterview(message: string | KeyValue): Promise<Zigbee2MQTTResponse<'bridge/response/device/interview'>> {
if (typeof message !== 'object' || message.id === undefined) {
throw new Error(`Invalid payload`);
}
const device = this.getEntity('device', message.id) as Device;
const device = this.getEntity('device', message.id);
logger.info(`Interviewing '${device.name}'`);
try {
@@ -545,25 +525,29 @@ export default class Bridge extends Extension {
this.eventBus.emitDevicesChanged();
this.eventBus.emitExposesChanged({device});
return utils.getResponse(message, {id: message.id}, null);
return utils.getResponse(message, {id: message.id});
}
@bind async deviceGenerateExternalDefinition(message: string | KeyValue): Promise<MQTTResponse> {
if (typeof message !== 'object' || !message.hasOwnProperty('id')) {
@bind async deviceGenerateExternalDefinition(
message: string | KeyValue,
): Promise<Zigbee2MQTTResponse<'bridge/response/device/generate_external_definition'>> {
if (typeof message !== 'object' || message.id === undefined) {
throw new Error(`Invalid payload`);
}
const device = this.zigbee.resolveEntityAndEndpoint(message.id).entity as Device;
if (!device) throw new Error(`Device '${message.id}' does not exist`);
const device = this.getEntity('device', message.id);
const source = await zhc.generateExternalDefinitionSource(device.zh);
return utils.getResponse(message, {id: message.id, source}, null);
return utils.getResponse(message, {id: message.id, source});
}
async renameEntity(entityType: 'group' | 'device', message: string | KeyValue): Promise<MQTTResponse> {
async renameEntity<T extends 'device' | 'group'>(
entityType: T,
message: string | KeyValue,
): Promise<Zigbee2MQTTResponse<T extends 'device' ? 'bridge/response/device/rename' : 'bridge/response/group/rename'>> {
const deviceAndHasLast = entityType === 'device' && typeof message === 'object' && message.last === true;
if (typeof message !== 'object' || (!message.hasOwnProperty('from') && !deviceAndHasLast) || !message.hasOwnProperty('to')) {
if (typeof message !== 'object' || (message.from === undefined && !deviceAndHasLast) || message.to === undefined) {
throw new Error(`Invalid payload`);
}
@@ -573,7 +557,7 @@ export default class Bridge extends Extension {
const from = deviceAndHasLast ? this.lastJoinedDeviceIeeeAddr : message.from;
const to = message.to;
const homeAssisantRename = message.hasOwnProperty('homeassistant_rename') ? message.homeassistant_rename : false;
const homeAssisantRename = message.homeassistant_rename !== undefined ? message.homeassistant_rename : false;
const entity = this.getEntity(entityType, from);
const oldFriendlyName = entity.options.friendly_name;
@@ -594,10 +578,13 @@ export default class Bridge extends Extension {
// Republish entity state
await this.publishEntityState(entity, {});
return utils.getResponse(message, {from: oldFriendlyName, to, homeassistant_rename: homeAssisantRename}, null);
return utils.getResponse(message, {from: oldFriendlyName, to, homeassistant_rename: homeAssisantRename});
}
async removeEntity(entityType: 'group' | 'device', message: string | KeyValue): Promise<MQTTResponse> {
async removeEntity<T extends 'device' | 'group'>(
entityType: T,
message: string | KeyValue,
): Promise<Zigbee2MQTTResponse<T extends 'device' ? 'bridge/response/device/remove' : 'bridge/response/group/remove'>> {
const ID = typeof message === 'object' ? message.id : message.trim();
const entity = this.getEntity(entityType, ID);
const friendlyName = entity.name;
@@ -630,25 +617,17 @@ export default class Bridge extends Extension {
} else {
await entity.zh.removeFromNetwork();
}
this.eventBus.emitEntityRemoved({id: entityID, name, type: 'device'});
settings.removeDevice(entityID as string);
} else {
if (force) {
entity.zh.removeFromDatabase();
} else {
await entity.zh.removeFromNetwork();
}
}
// Fire event
if (entity instanceof Device) {
this.eventBus.emitEntityRemoved({id: entityID, name, type: 'device'});
} else {
this.eventBus.emitEntityRemoved({id: entityID, name, type: 'group'});
}
// Remove from configuration.yaml
if (entity instanceof Device) {
settings.removeDevice(entityID as string);
} else {
settings.removeGroup(entityID);
}
@@ -665,16 +644,29 @@ export default class Bridge extends Extension {
await this.publishDevices();
// Refresh Cluster definition
await this.publishDefinitions();
return utils.getResponse(message, {id: ID, block, force}, null);
const responseData: Zigbee2MQTTAPI['bridge/response/device/remove'] = {id: ID, block, force};
return utils.getResponse(message, responseData);
} else {
await this.publishGroups();
return utils.getResponse(message, {id: ID, force: force}, null);
const responseData: Zigbee2MQTTAPI['bridge/response/group/remove'] = {id: ID, force};
return utils.getResponse(
message,
// @ts-expect-error typing infer does not work here
responseData,
);
}
} catch (error) {
throw new Error(`Failed to remove ${entityType} '${friendlyName}'${blockForceLog} (${error})`);
}
}
getEntity(type: 'group', ID: string): Group;
getEntity(type: 'device', ID: string): Device;
getEntity(type: 'group' | 'device', ID: string): Device | Group;
getEntity(type: 'group' | 'device', ID: string): Device | Group {
const entity = this.zigbee.resolveEntity(ID);
if (!entity || entity.constructor.name.toLowerCase() !== type) {
@@ -685,12 +677,13 @@ export default class Bridge extends Extension {
async publishInfo(): Promise<void> {
const config = objectAssignDeep({}, settings.get());
// @ts-expect-error hidden from publish
delete config.advanced.network_key;
delete config.mqtt.password;
if (config.frontend) {
delete config.frontend.auth_token;
}
const payload = {
delete config.frontend.auth_token;
const networkParams = await this.zigbee.getNetworkParameters();
const payload: Zigbee2MQTTAPI['bridge/info'] = {
version: this.zigbee2mqttVersion.version,
commit: this.zigbee2mqttVersion.commitHash,
zigbee_herdsman_converters: this.zigbeeHerdsmanConvertersVersion,
@@ -699,40 +692,30 @@ export default class Bridge extends Extension {
ieee_address: this.zigbee.firstCoordinatorEndpoint().getDevice().ieeeAddr,
...this.coordinatorVersion,
},
network: utils.toSnakeCase(await this.zigbee.getNetworkParameters()),
network: {
pan_id: networkParams.panID,
extended_pan_id: networkParams.extendedPanID,
channel: networkParams.channel,
},
log_level: logger.getLevel(),
permit_join: this.zigbee.getPermitJoin(),
permit_join_timeout: this.zigbee.getPermitJoinTimeout(),
permit_join_end: this.zigbee.getPermitJoinEnd(),
restart_required: this.restartRequired,
config,
config_schema: settings.schema,
config_schema: settings.schemaJson,
};
await this.mqtt.publish('bridge/info', stringify(payload), {retain: true, qos: 0}, settings.get().mqtt.base_topic, true);
}
async publishDevices(): Promise<void> {
interface Data {
bindings: {cluster: string; target: {type: string; endpoint?: number; ieee_address?: string; id?: number}}[];
configured_reportings: {
cluster: string;
attribute: string | number;
minimum_report_interval: number;
maximum_report_interval: number;
reportable_change: number;
}[];
clusters: {input: string[]; output: string[]};
scenes: Scene[];
}
// XXX: definition<>DefinitionPayload don't match to use `Device[]` type here
const devices: KeyValue[] = [];
const devices: Zigbee2MQTTAPI['bridge/devices'] = [];
for (const device of this.zigbee.devicesIterator()) {
const endpoints: {[s: number]: Data} = {};
const endpoints: (typeof devices)[number]['endpoints'] = {};
for (const endpoint of device.zh.endpoints) {
const data: Data = {
const data: (typeof endpoints)[keyof typeof endpoints] = {
scenes: utils.getScenes(endpoint),
bindings: [],
configured_reportings: [],
@@ -743,7 +726,7 @@ export default class Bridge extends Extension {
};
for (const bind of endpoint.binds) {
const target = utils.isEndpoint(bind.target)
const target = utils.isZHEndpoint(bind.target)
? {type: 'endpoint', ieee_address: bind.target.getDevice().ieeeAddr, endpoint: bind.target.ID}
: {type: 'group', id: bind.target.groupID};
data.bindings.push({cluster: bind.cluster.name, target});
@@ -786,8 +769,7 @@ export default class Bridge extends Extension {
}
async publishGroups(): Promise<void> {
// XXX: id<>ID can't use `Group[]` type
const groups: KeyValue[] = [];
const groups: Zigbee2MQTTAPI['bridge/groups'] = [];
for (const group of this.zigbee.groupsIterator()) {
const members = [];
@@ -809,13 +791,8 @@ export default class Bridge extends Extension {
}
async publishDefinitions(): Promise<void> {
interface ClusterDefinitionPayload {
clusters: Readonly<Record<ClusterName, Readonly<ClusterDefinition>>>;
custom_clusters: {[key: string]: CustomClusters};
}
const data: ClusterDefinitionPayload = {
clusters: Clusters,
const data: Zigbee2MQTTAPI['bridge/definition'] = {
clusters: Zcl.Clusters,
custom_clusters: {},
};
@@ -826,9 +803,9 @@ export default class Bridge extends Extension {
await this.mqtt.publish('bridge/definitions', stringify(data), {retain: true, qos: 0}, settings.get().mqtt.base_topic, true);
}
getDefinitionPayload(device: Device): DefinitionPayload | null {
getDefinitionPayload(device: Device): Zigbee2MQTTDevice['definition'] | undefined {
if (!device.definition) {
return null;
return undefined;
}
// TODO: better typing to avoid @ts-expect-error
@@ -837,17 +814,18 @@ export default class Bridge extends Extension {
let icon = device.options.icon ?? definitionIcon;
if (icon) {
icon = icon.replace('${zigbeeModel}', utils.sanitizeImageParameter(device.zh.modelID));
/* v8 ignore next */
icon = icon.replace('${zigbeeModel}', utils.sanitizeImageParameter(device.zh.modelID ?? ''));
icon = icon.replace('${model}', utils.sanitizeImageParameter(device.definition.model));
}
const payload: DefinitionPayload = {
const payload: Zigbee2MQTTDevice['definition'] = {
model: device.definition.model,
vendor: device.definition.vendor,
description: device.definition.description,
exposes: device.exposes(),
supports_ota: !!device.definition.ota,
options: device.definition.options,
options: device.definition.options ?? [],
icon,
};
+33 -34
View File
@@ -1,5 +1,8 @@
import type {Zigbee2MQTTAPI} from '../types/api';
import bind from 'bind-decorator';
import stringify from 'json-stable-stringify-without-jsonify';
import * as zhc from 'zigbee-herdsman-converters';
import Device from '../model/device';
@@ -15,11 +18,10 @@ export default class Configure extends Extension {
private configuring = new Set();
private attempts: {[s: string]: number} = {};
private topic = `${settings.get().mqtt.base_topic}/bridge/request/device/configure`;
private legacyTopic = `${settings.get().mqtt.base_topic}/bridge/configure`;
@bind private async onReconfigure(data: eventdata.Reconfigure): Promise<void> {
// Disabling reporting unbinds some cluster which could be bound by configure, re-setup.
if (data.device.zh.meta?.hasOwnProperty('configured')) {
if (data.device.zh.meta?.configured !== undefined) {
delete data.device.zh.meta.configured;
data.device.zh.save();
}
@@ -28,38 +30,31 @@ export default class Configure extends Extension {
}
@bind private async onMQTTMessage(data: eventdata.MQTTMessage): Promise<void> {
if (data.topic === this.legacyTopic) {
const device = this.zigbee.resolveEntity(data.message);
if (!device || !(device instanceof Device)) {
logger.error(`Device '${data.message}' does not exist`);
return;
}
if (data.topic === this.topic) {
const message = utils.parseJSON(data.message, data.message) as Zigbee2MQTTAPI['bridge/request/device/configure'];
const ID = typeof message === 'object' ? message.id : message;
let error: string | undefined;
if (!device.definition || !device.definition.configure) {
logger.warning(`Skipping configure of '${device.name}', device does not require this.`);
return;
}
await this.configure(device, 'mqtt_message', true);
} else if (data.topic === this.topic) {
const message = utils.parseJSON(data.message, data.message);
const ID = typeof message === 'object' && message.hasOwnProperty('id') ? message.id : message;
let error = null;
const device = this.zigbee.resolveEntity(ID);
if (!device || !(device instanceof Device)) {
error = `Device '${ID}' does not exist`;
} else if (!device.definition || !device.definition.configure) {
error = `Device '${device.name}' cannot be configured`;
if (ID === undefined) {
error = `Invalid payload`;
} else {
try {
await this.configure(device, 'mqtt_message', true, true);
} catch (e) {
error = `Failed to configure (${e.message})`;
const device = this.zigbee.resolveEntity(ID);
if (!device || !(device instanceof Device)) {
error = `Device '${ID}' does not exist`;
} else if (!device.definition || !device.definition.configure) {
error = `Device '${device.name}' cannot be configured`;
} else {
try {
await this.configure(device, 'mqtt_message', true, true);
} catch (e) {
error = `Failed to configure (${(e as Error).message})`;
}
}
}
const response = utils.getResponse(message, {id: ID}, error);
const response = utils.getResponse<'bridge/response/device/configure'>(message, {id: ID}, error);
await this.mqtt.publish(`bridge/response/device/configure`, stringify(response));
}
}
@@ -76,7 +71,7 @@ export default class Configure extends Extension {
});
this.eventBus.onDeviceJoined(this, async (data) => {
if (data.device.zh.meta.hasOwnProperty('configured')) {
if (data.device.zh.meta.configured !== undefined) {
delete data.device.zh.meta.configured;
data.device.zh.save();
}
@@ -95,12 +90,16 @@ export default class Configure extends Extension {
force = false,
throwError = false,
): Promise<void> {
if (!device.definition?.configure) {
return;
}
if (!force) {
if (device.options.disabled || !device.definition?.configure || !device.zh.interviewCompleted) {
if (device.options.disabled || !device.zh.interviewCompleted) {
return;
}
if (device.zh.meta?.hasOwnProperty('configured')) {
if (device.zh.meta?.configured !== undefined) {
return;
}
@@ -116,7 +115,7 @@ export default class Configure extends Extension {
this.configuring.add(device.ieeeAddr);
if (!this.attempts.hasOwnProperty(device.ieeeAddr)) {
if (this.attempts[device.ieeeAddr] === undefined) {
this.attempts[device.ieeeAddr] = 0;
}
@@ -130,7 +129,7 @@ export default class Configure extends Extension {
} catch (error) {
this.attempts[device.ieeeAddr]++;
const attempt = this.attempts[device.ieeeAddr];
const msg = `Failed to configure '${device.name}', attempt ${attempt} (${error.stack})`;
const msg = `Failed to configure '${device.name}', attempt ${attempt} (${(error as Error).stack})`;
logger.error(msg);
if (throwError) {
-1
View File
@@ -43,7 +43,6 @@ abstract class Extension {
/**
* Is called once the extension has to start
*/
/* istanbul ignore next */
async start(): Promise<void> {}
/**
+50 -24
View File
@@ -1,11 +1,13 @@
import * as zhc from 'zigbee-herdsman-converters';
import type {ExternalDefinitionWithExtend} from 'zigbee-herdsman-converters';
import {addExternalDefinition, removeExternalDefinitions} from 'zigbee-herdsman-converters';
import logger from '../util/logger';
import * as settings from '../util/settings';
import {loadExternalConverter} from '../util/utils';
import Extension from './extension';
import ExternalJSExtension from './externalJS';
export default class ExternalConverters extends Extension {
type TModule = ExternalDefinitionWithExtend | ExternalDefinitionWithExtend[];
export default class ExternalConverters extends ExternalJSExtension<TModule> {
constructor(
zigbee: Zigbee,
mqtt: MQTT,
@@ -16,27 +18,51 @@ export default class ExternalConverters extends Extension {
restartCallback: () => Promise<void>,
addExtension: (extension: Extension) => Promise<void>,
) {
super(zigbee, mqtt, state, publishEntityState, eventBus, enableDisableExtension, restartCallback, addExtension);
super(
zigbee,
mqtt,
state,
publishEntityState,
eventBus,
enableDisableExtension,
restartCallback,
addExtension,
'converter',
'external_converters',
);
}
for (const file of settings.get().external_converters) {
try {
for (const definition of loadExternalConverter(file)) {
const toAdd = {...definition};
delete toAdd['homeassistant'];
zhc.addDefinition(toAdd);
}
logger.info(`Loaded external converter '${file}'`);
} catch (error) {
logger.error(`Failed to load external converter file '${file}' (${error.message})`);
logger.error(
`Probably there is a syntax error in the file or the external converter is not ` +
`compatible with the current Zigbee2MQTT version`,
);
logger.error(
`Note that external converters are not meant for long term usage, it's meant for local ` +
`testing after which a pull request should be created to add out-of-the-box support for the device`,
);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected async removeJS(name: string, mod: TModule): Promise<void> {
removeExternalDefinitions(name);
await this.zigbee.resolveDevicesDefinitions(true);
}
protected async loadJS(name: string, mod: TModule, newName?: string): Promise<void> {
try {
removeExternalDefinitions(name);
const definitions = Array.isArray(mod) ? mod : [mod];
for (const definition of definitions) {
definition.externalConverterName = newName ?? name;
addExternalDefinition(definition);
logger.info(`Loaded external converter '${newName ?? name}'.`);
}
await this.zigbee.resolveDevicesDefinitions(true);
} catch (error) {
logger.error(
/* v8 ignore next */
`Failed to load external converter '${newName ?? name}'. Check the code for syntax error and make sure it is up to date with the current Zigbee2MQTT version.`,
);
logger.warning(
`External converters are not meant for long term usage, but for local testing after which a pull request should be created to add out-of-the-box support for the device`,
);
throw error;
}
}
}
-117
View File
@@ -1,117 +0,0 @@
import bind from 'bind-decorator';
import fs from 'fs';
import stringify from 'json-stable-stringify-without-jsonify';
import path from 'path';
import * as settings from '../util/settings';
import utils from '../util/utils';
import data from './../util/data';
import logger from './../util/logger';
import Extension from './extension';
const requestRegex = new RegExp(`${settings.get().mqtt.base_topic}/bridge/request/extension/(save|remove)`);
export default class ExternalExtension extends Extension {
private requestLookup: {[s: string]: (message: KeyValue) => Promise<MQTTResponse>};
override async start(): Promise<void> {
this.eventBus.onMQTTMessage(this, this.onMQTTMessage);
this.requestLookup = {save: this.saveExtension, remove: this.removeExtension};
await this.loadUserDefinedExtensions();
await this.publishExtensions();
}
private getExtensionsBasePath(): string {
return data.joinPath('extension');
}
private getListOfUserDefinedExtensions(): {name: string; code: string}[] {
const basePath = this.getExtensionsBasePath();
if (fs.existsSync(basePath)) {
return fs
.readdirSync(basePath)
.filter((f) => f.endsWith('.js'))
.map((fileName) => {
const extensionFilePath = path.join(basePath, fileName);
return {name: fileName, code: fs.readFileSync(extensionFilePath, 'utf-8')};
});
} else {
return [];
}
}
@bind private async removeExtension(message: KeyValue): Promise<MQTTResponse> {
const {name} = message;
const extensions = this.getListOfUserDefinedExtensions();
const extensionToBeRemoved = extensions.find((e) => e.name === name);
if (extensionToBeRemoved) {
await this.enableDisableExtension(false, extensionToBeRemoved.name);
const basePath = this.getExtensionsBasePath();
const extensionFilePath = path.join(basePath, path.basename(name));
fs.unlinkSync(extensionFilePath);
await this.publishExtensions();
logger.info(`Extension ${name} removed`);
return utils.getResponse(message, {}, null);
} else {
return utils.getResponse(message, {}, `Extension ${name} doesn't exists`);
}
}
@bind private async saveExtension(message: KeyValue): Promise<MQTTResponse> {
const {name, code} = message;
const ModuleConstructor = utils.loadModuleFromText(code, name) as typeof Extension;
await this.loadExtension(ModuleConstructor);
const basePath = this.getExtensionsBasePath();
/* istanbul ignore else */
if (!fs.existsSync(basePath)) {
fs.mkdirSync(basePath);
}
const extensionFilePath = path.join(basePath, path.basename(name));
fs.writeFileSync(extensionFilePath, code);
await this.publishExtensions();
logger.info(`Extension ${name} loaded`);
return utils.getResponse(message, {}, null);
}
@bind async onMQTTMessage(data: eventdata.MQTTMessage): Promise<void> {
const match = data.topic.match(requestRegex);
if (match && this.requestLookup[match[1].toLowerCase()]) {
const message = utils.parseJSON(data.message, data.message) as KeyValue;
try {
const response = await this.requestLookup[match[1].toLowerCase()](message);
await this.mqtt.publish(`bridge/response/extension/${match[1]}`, stringify(response));
} catch (error) {
logger.error(`Request '${data.topic}' failed with error: '${error.message}'`);
const response = utils.getResponse(message, {}, error.message);
await this.mqtt.publish(`bridge/response/extension/${match[1]}`, stringify(response));
}
}
}
@bind private async loadExtension(ConstructorClass: typeof Extension): Promise<void> {
await this.enableDisableExtension(false, ConstructorClass.name);
// @ts-ignore
await this.addExtension(new ConstructorClass(this.zigbee, this.mqtt, this.state, this.publishEntityState, this.eventBus, settings, logger));
}
private async loadUserDefinedExtensions(): Promise<void> {
for (const extension of this.getListOfUserDefinedExtensions()) {
await this.loadExtension(utils.loadModuleFromText(extension.code, extension.name) as typeof Extension);
}
}
private async publishExtensions(): Promise<void> {
const extensions = this.getListOfUserDefinedExtensions();
await this.mqtt.publish(
'bridge/extensions',
stringify(extensions),
{
retain: true,
qos: 0,
},
settings.get().mqtt.base_topic,
true,
);
}
}
+70
View File
@@ -0,0 +1,70 @@
import type Extension from './extension';
import logger from '../util/logger';
import * as settings from '../util/settings';
import ExternalJSExtension from './externalJS';
type TModule = new (...args: ConstructorParameters<typeof Extension>) => Extension;
export default class ExternalExtensions extends ExternalJSExtension<TModule> {
constructor(
zigbee: Zigbee,
mqtt: MQTT,
state: State,
publishEntityState: PublishEntityState,
eventBus: EventBus,
enableDisableExtension: (enable: boolean, name: string) => Promise<void>,
restartCallback: () => Promise<void>,
addExtension: (extension: Extension) => Promise<void>,
) {
super(
zigbee,
mqtt,
state,
publishEntityState,
eventBus,
enableDisableExtension,
restartCallback,
addExtension,
'extension',
'external_extensions',
);
}
protected async removeJS(name: string, mod: TModule): Promise<void> {
await this.enableDisableExtension(false, mod.name);
}
protected async loadJS(name: string, mod: TModule, newName?: string): Promise<void> {
try {
// stop if already started
await this.enableDisableExtension(false, mod.name);
await this.addExtension(
new mod(
this.zigbee,
this.mqtt,
this.state,
this.publishEntityState,
this.eventBus,
this.enableDisableExtension,
this.restartCallback,
this.addExtension,
// @ts-expect-error additional params that don't fit the internal `Extension` type
settings,
logger,
),
);
/* v8 ignore start */
logger.info(`Loaded external extension '${newName ?? name}'.`);
} catch (error) {
logger.error(
/* v8 ignore next */
`Failed to load external extension '${newName ?? name}'. Check the code for syntax error and make sure it is up to date with the current Zigbee2MQTT version.`,
);
throw error;
}
/* v8 ignore stop */
}
}
+243
View File
@@ -0,0 +1,243 @@
import type {Zigbee2MQTTAPI, Zigbee2MQTTResponse} from '../types/api';
import fs from 'node:fs';
import path from 'node:path';
import bind from 'bind-decorator';
import stringify from 'json-stable-stringify-without-jsonify';
import data from '../util/data';
import logger from '../util/logger';
import * as settings from '../util/settings';
import utils from '../util/utils';
import Extension from './extension';
const SUPPORTED_OPERATIONS = ['save', 'remove'];
export default abstract class ExternalJSExtension<M> extends Extension {
protected folderName: string;
protected mqttTopic: string;
protected requestRegex: RegExp;
protected basePath: string;
protected srcBasePath: string;
constructor(
zigbee: Zigbee,
mqtt: MQTT,
state: State,
publishEntityState: PublishEntityState,
eventBus: EventBus,
enableDisableExtension: (enable: boolean, name: string) => Promise<void>,
restartCallback: () => Promise<void>,
addExtension: (extension: Extension) => Promise<void>,
mqttTopic: string,
folderName: string,
) {
super(zigbee, mqtt, state, publishEntityState, eventBus, enableDisableExtension, restartCallback, addExtension);
this.folderName = folderName;
this.mqttTopic = mqttTopic;
this.requestRegex = new RegExp(`${settings.get().mqtt.base_topic}/bridge/request/${mqttTopic}/(save|remove)`);
this.basePath = data.joinPath(folderName);
// 1-up from this file
this.srcBasePath = path.join(
__dirname,
'..',
// prevent race in vitest with files being manipulated from same location
process.env.VITEST_WORKER_ID ? /* v8 ignore next */ `${folderName}_${Math.floor(Math.random() * 10000)}` : folderName,
);
}
override async start(): Promise<void> {
await super.start();
this.eventBus.onMQTTMessage(this, this.onMQTTMessage);
await this.loadFiles();
await this.publishExternalJS();
}
override async stop(): Promise<void> {
// remove src base path on stop to ensure always back to default
fs.rmSync(this.srcBasePath, {force: true, recursive: true});
await super.stop();
}
private getFilePath(name: string, mkBasePath = false, inSource = false): string {
const basePath = inSource ? this.srcBasePath : this.basePath;
if (mkBasePath && !fs.existsSync(basePath)) {
fs.mkdirSync(basePath, {recursive: true});
}
return path.join(basePath, name);
}
protected getFileCode(name: string): string {
return fs.readFileSync(this.getFilePath(name), 'utf8');
}
protected *getFiles(inSource = false): Generator<{name: string; code: string}> {
const basePath = inSource ? this.srcBasePath : this.basePath;
if (!fs.existsSync(basePath)) {
return;
}
for (const fileName of fs.readdirSync(basePath)) {
if (fileName.endsWith('.js') || fileName.endsWith('.cjs') || fileName.endsWith('.mjs')) {
yield {name: fileName, code: this.getFileCode(fileName)};
}
}
}
@bind async onMQTTMessage(data: eventdata.MQTTMessage): Promise<void> {
const match = data.topic.match(this.requestRegex);
if (match && SUPPORTED_OPERATIONS.includes(match[1].toLowerCase())) {
const message = utils.parseJSON(data.message, data.message);
try {
let response;
if (match[1].toLowerCase() === 'save') {
response = await this.save(
message as Zigbee2MQTTAPI['bridge/request/converter/save'] | Zigbee2MQTTAPI['bridge/request/extension/save'],
);
} else {
response = await this.remove(
message as Zigbee2MQTTAPI['bridge/request/converter/remove'] | Zigbee2MQTTAPI['bridge/request/extension/remove'],
);
}
await this.mqtt.publish(`bridge/response/${this.mqttTopic}/${match[1]}`, stringify(response));
} catch (error) {
logger.error(`Request '${data.topic}' failed with error: '${(error as Error).message}'`);
const response = utils.getResponse(message, {}, `${(error as Error).message}`);
await this.mqtt.publish(`bridge/response/${this.mqttTopic}/${match[1]}`, stringify(response));
}
}
}
protected abstract removeJS(name: string, mod: M): Promise<void>;
protected abstract loadJS(name: string, mod: M, newName?: string): Promise<void>;
@bind private async remove(
message: Zigbee2MQTTAPI['bridge/request/converter/remove'] | Zigbee2MQTTAPI['bridge/request/extension/remove'],
): Promise<Zigbee2MQTTResponse<'bridge/response/converter/remove' | 'bridge/response/extension/remove'>> {
if (!message.name) {
return utils.getResponse(message, {}, `Invalid payload`);
}
const {name} = message;
const srcToBeRemoved = this.getFilePath(name, false, true);
const toBeRemoved = this.getFilePath(name);
if (fs.existsSync(srcToBeRemoved)) {
const mod = await import(this.getImportPath(srcToBeRemoved));
await this.removeJS(name, mod.default);
fs.rmSync(srcToBeRemoved, {force: true});
fs.rmSync(toBeRemoved, {force: true});
logger.info(`${name} (${toBeRemoved}) removed.`);
await this.publishExternalJS();
return utils.getResponse(message, {});
} else {
return utils.getResponse(message, {}, `${name} (${srcToBeRemoved}) doesn't exists`);
}
}
@bind private async save(
message: Zigbee2MQTTAPI['bridge/request/converter/save'] | Zigbee2MQTTAPI['bridge/request/extension/save'],
): Promise<Zigbee2MQTTResponse<'bridge/response/converter/save' | 'bridge/response/extension/save'>> {
if (!message.name || !message.code) {
return utils.getResponse(message, {}, `Invalid payload`);
}
const {name, code} = message;
const srcFilePath = this.getFilePath(name, true, true);
let newName = name;
if (fs.existsSync(srcFilePath)) {
// if file already exist, version it to bypass node module caching
const versionMatch = name.match(/\.(\d+)\.(c|m)?js$/);
if (versionMatch) {
const version = parseInt(versionMatch[1], 10);
newName = name.replace(`.${version}.`, `.${version + 1}.`);
} else {
const ext = path.extname(name);
newName = name.replace(ext, `.1${ext}`);
}
// remove previous version
fs.rmSync(srcFilePath, {force: true});
fs.rmSync(this.getFilePath(name, true, false), {force: true});
}
const newSrcFilePath = this.getFilePath(newName, false /* already created above if needed */, true);
try {
fs.writeFileSync(newSrcFilePath, code, 'utf8');
const mod = await import(this.getImportPath(newSrcFilePath));
await this.loadJS(name, mod.default, newName);
logger.info(`${newName} loaded. Contents written to '${newSrcFilePath}'.`);
// keep original in data folder synced
fs.writeFileSync(this.getFilePath(newName, true, false), code, 'utf8');
await this.publishExternalJS();
return utils.getResponse(message, {});
} catch (error) {
fs.rmSync(newSrcFilePath, {force: true});
// NOTE: original in data folder doesn't get written if invalid
return utils.getResponse(message, {}, `${newName} contains invalid code: ${(error as Error).message}`);
}
}
private async loadFiles(): Promise<void> {
for (const extension of this.getFiles()) {
const srcFilePath = this.getFilePath(extension.name, true, true);
const filePath = this.getFilePath(extension.name);
try {
fs.copyFileSync(filePath, srcFilePath);
const mod = await import(this.getImportPath(srcFilePath));
await this.loadJS(extension.name, mod.default);
} catch (error) {
// change ext so Z2M doesn't try to load it again and again
fs.renameSync(filePath, `${filePath}.invalid`);
fs.rmSync(srcFilePath, {force: true});
logger.error(
`Invalid external ${this.mqttTopic} '${extension.name}' was ignored and renamed to prevent interference with Zigbee2MQTT.`,
);
logger.debug((error as Error).stack!);
}
}
}
private async publishExternalJS(): Promise<void> {
await this.mqtt.publish(
`bridge/${this.mqttTopic}s`,
stringify(Array.from(this.getFiles(true))),
{
retain: true,
qos: 0,
},
settings.get().mqtt.base_topic,
true,
);
}
private getImportPath(filePath: string): string {
// prevent issues on Windows
return path.relative(__dirname, filePath).replaceAll('\\', '/');
}
}
+109 -53
View File
@@ -1,15 +1,24 @@
import type {IncomingMessage, Server, ServerResponse} from 'node:http';
import type {Socket} from 'node:net';
import type {RequestHandler} from 'express-static-gzip';
import assert from 'node:assert';
import {existsSync, readFileSync} from 'node:fs';
import {createServer} from 'node:http';
import {createServer as createSecureServer} from 'node:https';
import {posix} from 'node:path';
import {parse} from 'node:url';
import bind from 'bind-decorator';
import gzipStatic, {RequestHandler} from 'connect-gzip-static';
import expressStaticGzip from 'express-static-gzip';
import finalhandler from 'finalhandler';
import fs from 'fs';
import http from 'http';
import https from 'https';
import stringify from 'json-stable-stringify-without-jsonify';
import net from 'net';
import url from 'url';
import WebSocket from 'ws';
import frontend from 'zigbee2mqtt-frontend';
import data from '../util/data';
import logger from '../util/logger';
import * as settings from '../util/settings';
import utils from '../util/utils';
@@ -18,21 +27,45 @@ import Extension from './extension';
/**
* This extension servers the frontend
*/
export default class Frontend extends Extension {
private mqttBaseTopic = settings.get().mqtt.base_topic;
private host = settings.get().frontend.host;
private port = settings.get().frontend.port;
private sslCert = settings.get().frontend.ssl_cert;
private sslKey = settings.get().frontend.ssl_key;
private authToken = settings.get().frontend.auth_token;
private server: http.Server;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private fileServer: RequestHandler;
private wss: WebSocket.Server = null;
export class Frontend extends Extension {
private mqttBaseTopic: string;
private host: string | undefined;
private port: number;
private sslCert: string | undefined;
private sslKey: string | undefined;
private authToken: string | undefined;
private server!: Server;
private fileServer!: RequestHandler;
private deviceIconsFileServer!: RequestHandler;
private wss!: WebSocket.Server;
private baseUrl: string;
constructor(
zigbee: Zigbee,
mqtt: MQTT,
state: State,
publishEntityState: PublishEntityState,
eventBus: EventBus,
enableDisableExtension: (enable: boolean, name: string) => Promise<void>,
restartCallback: () => Promise<void>,
addExtension: (extension: Extension) => Promise<void>,
) {
super(zigbee, mqtt, state, publishEntityState, eventBus, enableDisableExtension, restartCallback, addExtension);
const frontendSettings = settings.get().frontend;
assert(frontendSettings.enabled, `Frontend extension created with setting 'enabled: false'`);
this.host = frontendSettings.host;
this.port = frontendSettings.port;
this.sslCert = frontendSettings.ssl_cert;
this.sslKey = frontendSettings.ssl_key;
this.authToken = frontendSettings.auth_token;
this.baseUrl = frontendSettings.base_url;
this.mqttBaseTopic = settings.get().mqtt.base_topic;
}
private isHttpsConfigured(): boolean {
if (this.sslCert && this.sslKey) {
if (!fs.existsSync(this.sslCert) || !fs.existsSync(this.sslKey)) {
if (!existsSync(this.sslCert) || !existsSync(this.sslKey)) {
logger.error(`defined ssl_cert '${this.sslCert}' or ssl_key '${this.sslKey}' file path does not exists, server won't be secured.`);
return false;
}
@@ -42,31 +75,38 @@ export default class Frontend extends Extension {
}
override async start(): Promise<void> {
const options = {
enableBrotli: true,
// TODO: https://github.com/Koenkk/zigbee2mqtt/issues/24654 - enable compressed index serving when express-static-gzip is fixed.
index: false,
serveStatic: {
index: 'index.html',
/* v8 ignore start */
setHeaders: (res: ServerResponse, path: string): void => {
if (path.endsWith('index.html')) {
res.setHeader('Cache-Control', 'no-store');
}
},
/* v8 ignore stop */
},
};
this.fileServer = expressStaticGzip(frontend.getPath(), options);
this.deviceIconsFileServer = expressStaticGzip(data.joinPath('device_icons'), options);
this.wss = new WebSocket.Server({noServer: true, path: posix.join(this.baseUrl, 'api')});
this.wss.on('connection', this.onWebSocketConnection);
if (this.isHttpsConfigured()) {
const serverOptions = {
key: fs.readFileSync(this.sslKey),
cert: fs.readFileSync(this.sslCert),
key: readFileSync(this.sslKey!), // valid from `isHttpsConfigured`
cert: readFileSync(this.sslCert!), // valid from `isHttpsConfigured`
};
this.server = https.createServer(serverOptions, this.onRequest);
this.server = createSecureServer(serverOptions, this.onRequest);
} else {
this.server = http.createServer(this.onRequest);
this.server = createServer(this.onRequest);
}
this.server.on('upgrade', this.onUpgrade);
/* istanbul ignore next */
const options = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
setHeaders: (res: any, path: string): void => {
if (path.endsWith('index.html')) {
res.setHeader('Cache-Control', 'no-store');
}
},
};
this.fileServer = gzipStatic(frontend.getPath(), options);
this.wss = new WebSocket.Server({noServer: true});
this.wss.on('connection', this.onWebSocketConnection);
this.eventBus.onMQTTMessagePublished(this, this.onMQTTPublishMessage);
if (!this.host) {
@@ -84,27 +124,44 @@ export default class Frontend extends Extension {
override async stop(): Promise<void> {
await super.stop();
this.wss?.clients.forEach((client) => {
client.send(stringify({topic: 'bridge/state', payload: 'offline'}));
client.send(stringify({topic: 'bridge/state', payload: {state: 'offline'}}));
client.terminate();
});
this.wss?.close();
/* istanbul ignore else */
if (this.server) {
return new Promise((cb: () => void) => this.server.close(cb));
await new Promise((resolve) => this.server?.close(resolve));
}
@bind private onRequest(request: IncomingMessage, response: ServerResponse): void {
const fin = finalhandler(request, response);
const newUrl = posix.relative(this.baseUrl, request.url!);
// The request url is not within the frontend base url, so the relative path starts with '..'
if (newUrl.startsWith('.')) {
return fin();
}
// Attach originalUrl so that static-server can perform a redirect to '/' when serving the root directory.
// This is necessary for the browser to resolve relative assets paths correctly.
request.originalUrl = request.url;
request.url = '/' + newUrl;
request.path = request.url;
if (newUrl.startsWith('device_icons/')) {
request.path = request.path.replace('device_icons/', '');
request.url = request.url.replace('/device_icons', '');
this.deviceIconsFileServer(request, response, fin);
} else {
this.fileServer(request, response, fin);
}
}
@bind private onRequest(request: http.IncomingMessage, response: http.ServerResponse): void {
// @ts-ignore
this.fileServer(request, response, finalhandler(request, response));
}
private authenticate(request: http.IncomingMessage, cb: (authenticate: boolean) => void): void {
const {query} = url.parse(request.url, true);
private authenticate(request: IncomingMessage, cb: (authenticate: boolean) => void): void {
const {query} = parse(request.url!, true);
cb(!this.authToken || this.authToken === query.token);
}
@bind private onUpgrade(request: http.IncomingMessage, socket: net.Socket, head: Buffer): void {
@bind private onUpgrade(request: IncomingMessage, socket: Socket, head: Buffer): void {
this.wss.handleUpgrade(request, socket, head, (ws) => {
this.authenticate(request, (isAuthenticated) => {
if (isAuthenticated) {
@@ -127,7 +184,6 @@ export default class Frontend extends Extension {
});
for (const [topic, payload] of Object.entries(this.mqtt.retainedMessages)) {
/* istanbul ignore else */
if (topic.startsWith(`${this.mqttBaseTopic}/`)) {
ws.send(
stringify({
@@ -142,9 +198,9 @@ export default class Frontend extends Extension {
for (const device of this.zigbee.devicesIterator(utils.deviceNotCoordinator)) {
const payload = this.state.get(device);
const lastSeen = settings.get().advanced.last_seen;
/* istanbul ignore if */
if (lastSeen !== 'disable') {
payload.last_seen = utils.formatDate(device.zh.lastSeen, lastSeen);
payload.last_seen = utils.formatDate(device.zh.lastSeen ?? /* v8 ignore next */ 0, lastSeen);
}
if (device.zh.linkquality !== undefined) {
@@ -156,14 +212,12 @@ export default class Frontend extends Extension {
}
@bind private onMQTTPublishMessage(data: eventdata.MQTTMessagePublished): void {
/* istanbul ignore else */
if (data.topic.startsWith(`${this.mqttBaseTopic}/`)) {
// Send topic without base_topic
const topic = data.topic.substring(this.mqttBaseTopic.length + 1);
const payload = utils.parseJSON(data.payload, data.payload);
for (const client of this.wss.clients) {
/* istanbul ignore else */
if (client.readyState === WebSocket.OPEN) {
client.send(stringify({topic, payload}));
}
@@ -171,3 +225,5 @@ export default class Frontend extends Extension {
}
}
}
export default Frontend;
+150 -275
View File
@@ -1,124 +1,52 @@
import type {Zigbee2MQTTAPI, Zigbee2MQTTResponseEndpoints} from '../types/api';
import assert from 'node:assert';
import bind from 'bind-decorator';
import equals from 'fast-deep-equal/es6';
import stringify from 'json-stable-stringify-without-jsonify';
import * as zhc from 'zigbee-herdsman-converters';
import Device from '../model/device';
import Group from '../model/group';
import logger from '../util/logger';
import * as settings from '../util/settings';
import utils from '../util/utils';
import utils, {isLightExpose} from '../util/utils';
import Extension from './extension';
const TOPIC_REGEX = new RegExp(`^${settings.get().mqtt.base_topic}/bridge/request/group/members/(remove|add|remove_all)$`);
const LEGACY_TOPIC_REGEX = new RegExp(`^${settings.get().mqtt.base_topic}/bridge/group/(.+)/(remove|add|remove_all)$`);
const LEGACY_TOPIC_REGEX_REMOVE_ALL = new RegExp(`^${settings.get().mqtt.base_topic}/bridge/group/remove_all$`);
const STATE_PROPERTIES: Readonly<Record<string, (value: string, exposes: zhc.Expose[]) => boolean>> = {
state: () => true,
brightness: (value, exposes) => exposes.some((e) => e.type === 'light' && e.features.some((f) => f.name === 'brightness')),
color_temp: (value, exposes) => exposes.some((e) => e.type === 'light' && e.features.some((f) => f.name === 'color_temp')),
color: (value, exposes) => exposes.some((e) => e.type === 'light' && e.features.some((f) => f.name === 'color_xy' || f.name === 'color_hs')),
brightness: (value, exposes) => exposes.some((e) => isLightExpose(e) && e.features.some((f) => f.name === 'brightness')),
color_temp: (value, exposes) => exposes.some((e) => isLightExpose(e) && e.features.some((f) => f.name === 'color_temp')),
color: (value, exposes) => exposes.some((e) => isLightExpose(e) && e.features.some((f) => f.name === 'color_xy' || f.name === 'color_hs')),
color_mode: (value, exposes) =>
exposes.some(
(e) =>
e.type === 'light' &&
isLightExpose(e) &&
(e.features.some((f) => f.name === `color_${value}`) || (value === 'color_temp' && e.features.some((f) => f.name === 'color_temp'))),
),
};
interface ParsedMQTTMessage {
type: 'remove' | 'add' | 'remove_all';
resolvedEntityGroup: Group;
resolvedEntityDevice: Device;
error: string;
groupKey: string;
deviceKey: string;
triggeredViaLegacyApi: boolean;
resolvedGroup?: Group;
resolvedDevice?: Device;
resolvedEndpoint?: zh.Endpoint;
groupKey?: string;
deviceKey?: string;
endpointKey?: string | number;
skipDisableReporting: boolean;
resolvedEntityEndpoint: zh.Endpoint;
}
export default class Groups extends Extension {
private legacyApi = settings.get().advanced.legacy_api;
private lastOptimisticState: {[s: string]: KeyValue} = {};
override async start(): Promise<void> {
this.eventBus.onStateChange(this, this.onStateChange);
this.eventBus.onMQTTMessage(this, this.onMQTTMessage);
await this.syncGroupsWithSettings();
}
private async syncGroupsWithSettings(): Promise<void> {
const settingsGroups = settings.getGroups();
const addRemoveFromGroup = async (
action: 'add' | 'remove',
deviceName: string,
groupName: string | number,
endpoint: zh.Endpoint,
group: Group,
): Promise<void> => {
try {
logger.info(`${action === 'add' ? 'Adding' : 'Removing'} '${deviceName}' to group '${groupName}'`);
if (action === 'remove') {
await endpoint.removeFromGroup(group.zh);
} else {
await endpoint.addToGroup(group.zh);
}
} catch (error) {
logger.error(`Failed to ${action} '${deviceName}' from '${groupName}'`);
logger.debug(error.stack);
}
};
for (const settingGroup of settingsGroups) {
const groupID = settingGroup.ID;
const zigbeeGroup = this.zigbee.groupsIterator((g) => g.groupID === groupID).next().value || this.zigbee.createGroup(groupID);
const settingsEndpoints: zh.Endpoint[] = [];
for (const d of settingGroup.devices) {
const parsed = this.zigbee.resolveEntityAndEndpoint(d);
const device = parsed.entity as Device;
if (!device) {
logger.error(`Cannot find '${d}' of group '${settingGroup.friendly_name}'`);
}
if (!parsed.endpoint) {
if (parsed.endpointID) {
logger.error(`Cannot find endpoint '${parsed.endpointID}' of device '${parsed.ID}'`);
}
continue;
}
// In settings but not in zigbee
if (!zigbeeGroup.zh.hasMember(parsed.endpoint)) {
await addRemoveFromGroup('add', device?.name, settingGroup.friendly_name, parsed.endpoint, zigbeeGroup);
}
settingsEndpoints.push(parsed.endpoint);
}
// In zigbee but not in settings
for (const endpoint of zigbeeGroup.zh.members) {
if (!settingsEndpoints.includes(endpoint)) {
const deviceName = settings.getDevice(endpoint.getDevice().ieeeAddr).friendly_name;
await addRemoveFromGroup('remove', deviceName, settingGroup.friendly_name, endpoint, zigbeeGroup);
}
}
}
for (const zigbeeGroup of this.zigbee.groupsIterator((zg) => !settingsGroups.some((sg) => sg.ID === zg.groupID))) {
for (const endpoint of zigbeeGroup.zh.members) {
const deviceName = settings.getDevice(endpoint.getDevice().ieeeAddr).friendly_name;
await addRemoveFromGroup('remove', deviceName, zigbeeGroup.ID, endpoint, zigbeeGroup);
}
}
}
@bind async onStateChange(data: eventdata.StateChange): Promise<void> {
@@ -129,7 +57,7 @@ export default class Groups extends Extension {
}
const payload: KeyValue = {};
let endpointName: string = null;
let endpointName: string | undefined;
const endpointNames: string[] = data.entity instanceof Device ? data.entity.getEndpointNames() : [];
for (let prop of Object.keys(data.update)) {
@@ -159,15 +87,19 @@ export default class Groups extends Extension {
}
if (entity instanceof Device) {
for (const group of groups) {
if (
group.zh.hasMember(entity.endpoint(endpointName)) &&
!equals(this.lastOptimisticState[group.ID], payload) &&
this.shouldPublishPayloadForGroup(group, payload)
) {
this.lastOptimisticState[group.ID] = payload;
const endpoint = entity.endpoint(endpointName);
await this.publishEntityState(group, payload, reason);
if (endpoint) {
for (const group of groups) {
if (
group.zh.hasMember(endpoint) &&
!equals(this.lastOptimisticState[group.ID], payload) &&
this.shouldPublishPayloadForGroup(group, payload)
) {
this.lastOptimisticState[group.ID] = payload;
await this.publishEntityState(group, payload, reason);
}
}
}
} else {
@@ -220,17 +152,30 @@ export default class Groups extends Extension {
}
private shouldPublishPayloadForGroup(group: Group, payload: KeyValue): boolean {
return group.options.off_state === 'last_member_state' || !payload || payload.state !== 'OFF' || this.areAllMembersOff(group);
return (
group.options.off_state === 'last_member_state' ||
!payload ||
(payload.state !== 'OFF' && payload.state !== 'CLOSE') ||
this.areAllMembersOffOrClosed(group)
);
}
private areAllMembersOff(group: Group): boolean {
private areAllMembersOffOrClosed(group: Group): boolean {
for (const member of group.zh.members) {
const device = this.zigbee.resolveEntity(member.getDevice());
const device = this.zigbee.resolveEntity(member.getDevice())!;
if (this.state.exists(device)) {
const state = this.state.get(device);
const endpointNames = device.isDevice() && device.getEndpointNames();
const stateKey =
endpointNames &&
endpointNames.length >= member.ID &&
device.definition?.meta?.multiEndpoint &&
(!device.definition.meta.multiEndpointSkip || !device.definition.meta.multiEndpointSkip.includes('state'))
? `state_${endpointNames[member.ID - 1]}`
: 'state';
if (state.state === 'ON') {
if (state[stateKey] === 'ON' || state[stateKey] === 'OPEN') {
return false;
}
}
@@ -239,217 +184,147 @@ export default class Groups extends Extension {
return true;
}
private async parseMQTTMessage(data: eventdata.MQTTMessage): Promise<ParsedMQTTMessage> {
let type: ParsedMQTTMessage['type'] = null;
let resolvedEntityGroup: ParsedMQTTMessage['resolvedEntityGroup'] = null;
let resolvedEntityDevice: ParsedMQTTMessage['resolvedEntityDevice'] = null;
let resolvedEntityEndpoint: ParsedMQTTMessage['resolvedEntityEndpoint'] = null;
let error: ParsedMQTTMessage['error'] = null;
let groupKey: ParsedMQTTMessage['groupKey'] = null;
let deviceKey: ParsedMQTTMessage['deviceKey'] = null;
let triggeredViaLegacyApi: ParsedMQTTMessage['triggeredViaLegacyApi'] = false;
let skipDisableReporting: ParsedMQTTMessage['skipDisableReporting'] = false;
/* istanbul ignore else */
private parseMQTTMessage(
data: eventdata.MQTTMessage,
): [raw: KeyValue | undefined, parsed: ParsedMQTTMessage | undefined, error: string | undefined] {
const topicRegexMatch = data.topic.match(TOPIC_REGEX);
const legacyTopicRegexRemoveAllMatch = data.topic.match(LEGACY_TOPIC_REGEX_REMOVE_ALL);
const legacyTopicRegexMatch = data.topic.match(LEGACY_TOPIC_REGEX);
if (this.legacyApi && (legacyTopicRegexMatch || legacyTopicRegexRemoveAllMatch)) {
triggeredViaLegacyApi = true;
if (topicRegexMatch) {
const type = topicRegexMatch[1] as 'remove' | 'add' | 'remove_all';
let resolvedGroup;
let groupKey;
let skipDisableReporting = false;
const message = JSON.parse(data.message) as Zigbee2MQTTAPI['bridge/request/group/members/add'];
if (legacyTopicRegexMatch) {
resolvedEntityGroup = this.zigbee.resolveEntity(legacyTopicRegexMatch[1]) as Group;
type = legacyTopicRegexMatch[2] as ParsedMQTTMessage['type'];
if (!resolvedEntityGroup || !(resolvedEntityGroup instanceof Group)) {
logger.error(`Group '${legacyTopicRegexMatch[1]}' does not exist`);
/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
const message = {friendly_name: data.message, group: legacyTopicRegexMatch[1], error: `group doesn't exists`};
await this.mqtt.publish('bridge/log', stringify({type: `device_group_${type}_failed`, message}));
}
return null;
}
} else {
type = 'remove_all';
if (typeof message !== 'object' || message.device == undefined) {
return [message, {type, skipDisableReporting}, 'Invalid payload'];
}
const parsedEntity = this.zigbee.resolveEntityAndEndpoint(data.message);
resolvedEntityDevice = parsedEntity.entity as Device;
if (!resolvedEntityDevice || !(resolvedEntityDevice instanceof Device)) {
logger.error(`Device '${data.message}' does not exist`);
/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
const message = {friendly_name: data.message, group: legacyTopicRegexMatch[1], error: "entity doesn't exists"};
await this.mqtt.publish('bridge/log', stringify({type: `device_group_${type}_failed`, message}));
}
return null;
}
resolvedEntityEndpoint = parsedEntity.endpoint;
if (parsedEntity.endpointID && !resolvedEntityEndpoint) {
logger.error(`Device '${parsedEntity.ID}' does not have endpoint '${parsedEntity.endpointID}'`);
return null;
}
} else if (topicRegexMatch) {
type = topicRegexMatch[1] as 'remove' | 'add' | 'remove_all';
const message = JSON.parse(data.message);
deviceKey = message.device;
skipDisableReporting = 'skip_disable_reporting' in message ? message.skip_disable_reporting : false;
const deviceKey = message.device;
skipDisableReporting = message.skip_disable_reporting != undefined ? message.skip_disable_reporting : false;
if (type !== 'remove_all') {
groupKey = message.group;
resolvedEntityGroup = this.zigbee.resolveEntity(message.group) as Group;
if (!resolvedEntityGroup || !(resolvedEntityGroup instanceof Group)) {
error = `Group '${message.group}' does not exist`;
if (message.group == undefined) {
return [message, {type, skipDisableReporting}, `Invalid payload`];
}
resolvedGroup = this.zigbee.resolveEntity(message.group);
if (!resolvedGroup || !(resolvedGroup instanceof Group)) {
return [message, {type, skipDisableReporting}, `Group '${message.group}' does not exist`];
}
}
const parsed = this.zigbee.resolveEntityAndEndpoint(message.device);
resolvedEntityDevice = parsed?.entity as Device;
const resolvedDevice = this.zigbee.resolveEntity(message.device);
if (!error && (!resolvedEntityDevice || !(resolvedEntityDevice instanceof Device))) {
error = `Device '${message.device}' does not exist`;
if (!resolvedDevice || !(resolvedDevice instanceof Device)) {
return [message, {type, skipDisableReporting}, `Device '${message.device}' does not exist`];
}
if (!error) {
resolvedEntityEndpoint = parsed.endpoint;
const endpointKey = message.endpoint ?? 'default';
const resolvedEndpoint = resolvedDevice.endpoint(message.endpoint);
if (parsed.endpointID && !resolvedEntityEndpoint) {
error = `Device '${parsed.ID}' does not have endpoint '${parsed.endpointID}'`;
}
if (!resolvedEndpoint) {
return [message, {type, skipDisableReporting}, `Device '${resolvedDevice.name}' does not have endpoint '${endpointKey}'`];
}
return [
message,
{
resolvedGroup,
resolvedDevice,
resolvedEndpoint,
type,
groupKey,
deviceKey,
endpointKey,
skipDisableReporting,
},
undefined,
];
} else {
return [undefined, undefined, undefined];
}
return {
resolvedEntityGroup,
resolvedEntityDevice,
type,
error,
groupKey,
deviceKey,
triggeredViaLegacyApi,
skipDisableReporting,
resolvedEntityEndpoint,
};
}
@bind private async onMQTTMessage(data: eventdata.MQTTMessage): Promise<void> {
const parsed = await this.parseMQTTMessage(data);
const [raw, parsed, error] = this.parseMQTTMessage(data);
if (!parsed || !parsed.type) {
if (!raw || !parsed) {
return;
}
const {
resolvedEntityGroup,
resolvedEntityDevice,
type,
triggeredViaLegacyApi,
groupKey,
deviceKey,
skipDisableReporting,
resolvedEntityEndpoint,
} = parsed;
let error = parsed.error;
if (error) {
await this.publishResponse(parsed.type, raw, {}, error);
return;
}
const {resolvedGroup, resolvedDevice, resolvedEndpoint, type, groupKey, deviceKey, endpointKey, skipDisableReporting} = parsed;
const changedGroups: Group[] = [];
if (!error) {
try {
const keys = [
`${resolvedEntityDevice.ieeeAddr}/${resolvedEntityEndpoint.ID}`,
`${resolvedEntityDevice.name}/${resolvedEntityEndpoint.ID}`,
];
const endpointNameLocal = resolvedEntityDevice.endpointName(resolvedEntityEndpoint);
assert(resolvedDevice, '`resolvedDevice` is missing');
assert(resolvedEndpoint, '`resolvedEndpoint` is missing');
if (endpointNameLocal) {
keys.push(`${resolvedEntityDevice.ieeeAddr}/${endpointNameLocal}`);
keys.push(`${resolvedEntityDevice.name}/${endpointNameLocal}`);
try {
if (type === 'add') {
assert(resolvedGroup, '`resolvedGroup` is missing');
logger.info(`Adding '${resolvedDevice.name}' to '${resolvedGroup.name}'`);
await resolvedEndpoint.addToGroup(resolvedGroup.zh);
changedGroups.push(resolvedGroup);
await this.publishResponse<'bridge/response/group/members/add'>(parsed.type, raw, {
device: deviceKey!, // valid from resolved asserts
endpoint: endpointKey!, // valid from resolved asserts
group: groupKey!, // valid from resolved asserts
});
} else if (type === 'remove') {
assert(resolvedGroup, '`resolvedGroup` is missing');
logger.info(`Removing '${resolvedDevice.name}' from '${resolvedGroup.name}'`);
await resolvedEndpoint.removeFromGroup(resolvedGroup.zh);
changedGroups.push(resolvedGroup);
await this.publishResponse<'bridge/response/group/members/remove'>(parsed.type, raw, {
device: deviceKey!, // valid from resolved asserts
endpoint: endpointKey!, // valid from resolved asserts
group: groupKey!, // valid from resolved asserts
});
} else {
// remove_all
logger.info(`Removing '${resolvedDevice.name}' from all groups`);
for (const group of this.zigbee.groupsIterator((g) => g.members.includes(resolvedEndpoint))) {
changedGroups.push(group);
}
if (!endpointNameLocal) {
keys.push(resolvedEntityDevice.name);
keys.push(resolvedEntityDevice.ieeeAddr);
}
if (type === 'add') {
logger.info(`Adding '${resolvedEntityDevice.name}' to '${resolvedEntityGroup.name}'`);
await resolvedEntityEndpoint.addToGroup(resolvedEntityGroup.zh);
settings.addDeviceToGroup(resolvedEntityGroup.ID.toString(), keys);
changedGroups.push(resolvedEntityGroup);
/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
const message = {friendly_name: resolvedEntityDevice.name, group: resolvedEntityGroup.name};
await this.mqtt.publish('bridge/log', stringify({type: `device_group_add`, message}));
}
} else if (type === 'remove') {
logger.info(`Removing '${resolvedEntityDevice.name}' from '${resolvedEntityGroup.name}'`);
await resolvedEntityEndpoint.removeFromGroup(resolvedEntityGroup.zh);
settings.removeDeviceFromGroup(resolvedEntityGroup.ID.toString(), keys);
changedGroups.push(resolvedEntityGroup);
/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
const message = {friendly_name: resolvedEntityDevice.name, group: resolvedEntityGroup.name};
await this.mqtt.publish('bridge/log', stringify({type: `device_group_remove`, message}));
}
} else {
// remove_all
logger.info(`Removing '${resolvedEntityDevice.name}' from all groups`);
for (const group of this.zigbee.groupsIterator((g) => g.members.includes(resolvedEntityEndpoint))) {
changedGroups.push(group);
}
await resolvedEntityEndpoint.removeFromAllGroups();
for (const settingsGroup of settings.getGroups()) {
settings.removeDeviceFromGroup(settingsGroup.ID.toString(), keys);
/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
const message = {friendly_name: resolvedEntityDevice.name};
await this.mqtt.publish('bridge/log', stringify({type: `device_group_remove_all`, message}));
}
}
}
} catch (e) {
error = `Failed to ${type} from group (${e.message})`;
logger.debug(e.stack);
await resolvedEndpoint.removeFromAllGroups();
await this.publishResponse<'bridge/response/group/members/remove_all'>(parsed.type, raw, {
device: deviceKey!, // valid from resolved asserts
endpoint: endpointKey!, // valid from resolved asserts
});
}
} catch (e) {
const errorMsg = `Failed to ${type} from group (${(e as Error).message})`;
await this.publishResponse(parsed.type, raw, {}, errorMsg);
logger.debug((e as Error).stack!);
return;
}
if (!triggeredViaLegacyApi) {
const message = utils.parseJSON(data.message, data.message);
const responseData: KeyValue = {device: deviceKey};
if (groupKey) {
responseData.group = groupKey;
}
await this.mqtt.publish(`bridge/response/group/members/${type}`, stringify(utils.getResponse(message, responseData, error)));
for (const group of changedGroups) {
this.eventBus.emitGroupMembersChanged({group, action: type, endpoint: resolvedEndpoint, skipDisableReporting});
}
}
private async publishResponse<T extends Zigbee2MQTTResponseEndpoints>(
type: ParsedMQTTMessage['type'],
request: KeyValue,
data: Zigbee2MQTTAPI[T],
error?: string,
): Promise<void> {
const response = utils.getResponse(request, data, error);
await this.mqtt.publish(`bridge/response/group/members/${type}`, stringify(response));
if (error) {
logger.error(error);
} else {
for (const group of changedGroups) {
this.eventBus.emitGroupMembersChanged({group, action: type, endpoint: resolvedEntityEndpoint, skipDisableReporting});
}
}
}
}
File diff suppressed because it is too large Load Diff
-420
View File
@@ -1,420 +0,0 @@
import assert from 'assert';
import bind from 'bind-decorator';
import stringify from 'json-stable-stringify-without-jsonify';
import logger from '../../util/logger';
import * as settings from '../../util/settings';
import utils from '../../util/utils';
import Extension from '../extension';
const configRegex = new RegExp(`${settings.get().mqtt.base_topic}/bridge/config/((?:\\w+/get)|(?:\\w+/factory_reset)|(?:\\w+))`);
export default class BridgeLegacy extends Extension {
private lastJoinedDeviceName: string = null;
private supportedOptions: {[s: string]: (topic: string, message: string) => Promise<void> | void};
override async start(): Promise<void> {
this.supportedOptions = {
permit_join: this.permitJoin,
last_seen: this.lastSeen,
elapsed: this.elapsed,
reset: this.reset,
log_level: this.logLevel,
devices: this.devices,
groups: this.groups,
'devices/get': this.devices,
rename: this.rename,
rename_last: this.renameLast,
remove: this.remove,
force_remove: this.forceRemove,
ban: this.ban,
device_options: this.deviceOptions,
add_group: this.addGroup,
remove_group: this.removeGroup,
force_remove_group: this.removeGroup,
whitelist: this.whitelist,
'touchlink/factory_reset': this.touchlinkFactoryReset,
};
this.eventBus.onDeviceJoined(this, (data) => this.onZigbeeEvent_('deviceJoined', data, data.device));
this.eventBus.onDeviceInterview(this, (data) => this.onZigbeeEvent_('deviceInterview', data, data.device));
this.eventBus.onDeviceAnnounce(this, (data) => this.onZigbeeEvent_('deviceAnnounce', data, data.device));
this.eventBus.onDeviceLeave(this, (data) => this.onZigbeeEvent_('deviceLeave', data, null));
this.eventBus.onMQTTMessage(this, this.onMQTTMessage);
await this.publish();
}
@bind async whitelist(topic: string, message: string): Promise<void> {
try {
const entity = settings.getDevice(message);
assert(entity, `Entity '${message}' does not exist`);
settings.addDeviceToPasslist(entity.ID.toString());
logger.info(`Whitelisted '${entity.friendly_name}'`);
await this.mqtt.publish('bridge/log', stringify({type: 'device_whitelisted', message: {friendly_name: entity.friendly_name}}));
} catch (error) {
logger.error(`Failed to whitelist '${message}' '${error}'`);
}
}
@bind deviceOptions(topic: string, message: string): void {
let json = null;
try {
json = JSON.parse(message);
} catch {
logger.error('Failed to parse message as JSON');
return;
}
if (!json.hasOwnProperty('friendly_name') || !json.hasOwnProperty('options')) {
logger.error('Invalid JSON message, should contain "friendly_name" and "options"');
return;
}
const entity = settings.getDevice(json.friendly_name);
assert(entity, `Entity '${json.friendly_name}' does not exist`);
settings.changeEntityOptions(entity.ID.toString(), json.options);
logger.info(`Changed device specific options of '${json.friendly_name}' (${stringify(json.options)})`);
}
@bind async permitJoin(topic: string, message: string): Promise<void> {
await this.zigbee.permitJoin(message.toLowerCase() === 'true');
await this.publish();
}
@bind async reset(): Promise<void> {
try {
await this.zigbee.reset('soft');
logger.info('Soft reset ZNP');
} catch {
logger.error('Soft reset failed');
}
}
@bind lastSeen(topic: string, message: string): void {
const allowed = ['disable', 'ISO_8601', 'epoch', 'ISO_8601_local'];
if (!allowed.includes(message)) {
logger.error(`${message} is not an allowed value, possible: ${allowed}`);
return;
}
settings.set(['advanced', 'last_seen'], message);
logger.info(`Set last_seen to ${message}`);
}
@bind elapsed(topic: string, message: string): void {
const allowed = ['true', 'false'];
if (!allowed.includes(message)) {
logger.error(`${message} is not an allowed value, possible: ${allowed}`);
return;
}
settings.set(['advanced', 'elapsed'], message === 'true');
logger.info(`Set elapsed to ${message}`);
}
@bind async logLevel(topic: string, message: string): Promise<void> {
const level = message.toLowerCase() as settings.LogLevel;
if (settings.LOG_LEVELS.includes(level)) {
logger.info(`Switching log level to '${level}'`);
logger.setLevel(level);
} else {
logger.error(`Could not set log level to '${level}'. Allowed level: '${settings.LOG_LEVELS.join(',')}'`);
}
await this.publish();
}
@bind async devices(topic: string): Promise<void> {
const coordinator = await this.zigbee.getCoordinatorVersion();
const devices: KeyValue[] = [];
for (const device of this.zigbee.devicesIterator()) {
const payload: KeyValue = {
ieeeAddr: device.ieeeAddr,
type: device.zh.type,
networkAddress: device.zh.networkAddress,
};
if (device.zh.type !== 'Coordinator') {
const definition = device.definition;
payload.model = definition ? definition.model : device.zh.modelID;
payload.vendor = definition ? definition.vendor : '-';
payload.description = definition ? definition.description : '-';
payload.friendly_name = device.name;
payload.manufacturerID = device.zh.manufacturerID;
payload.manufacturerName = device.zh.manufacturerName;
payload.powerSource = device.zh.powerSource;
payload.modelID = device.zh.modelID;
payload.hardwareVersion = device.zh.hardwareVersion;
payload.softwareBuildID = device.zh.softwareBuildID;
payload.dateCode = device.zh.dateCode;
payload.lastSeen = device.zh.lastSeen;
} else {
payload.friendly_name = 'Coordinator';
payload.softwareBuildID = coordinator.type;
payload.dateCode = coordinator.meta.revision.toString();
payload.lastSeen = Date.now();
}
devices.push(payload);
}
if (topic.split('/').pop() == 'get') {
await this.mqtt.publish(`bridge/config/devices`, stringify(devices), {}, settings.get().mqtt.base_topic, false, false);
} else {
await this.mqtt.publish('bridge/log', stringify({type: 'devices', message: devices}));
}
}
@bind async groups(): Promise<void> {
const payload = settings.getGroups().map((g) => {
return {...g, ID: Number(g.ID)};
});
await this.mqtt.publish('bridge/log', stringify({type: 'groups', message: payload}));
}
@bind async rename(topic: string, message: string): Promise<void> {
const invalid = `Invalid rename message format expected {"old": "friendly_name", "new": "new_name"} got ${message}`;
let json = null;
try {
json = JSON.parse(message);
} catch {
logger.error(invalid);
return;
}
// Validate message
if (!json.new || !json.old) {
logger.error(invalid);
return;
}
await this._renameInternal(json.old, json.new);
}
@bind async renameLast(topic: string, message: string): Promise<void> {
if (!this.lastJoinedDeviceName) {
logger.error(`Cannot rename last joined device, no device has joined during this session`);
return;
}
await this._renameInternal(this.lastJoinedDeviceName, message);
}
async _renameInternal(from: string, to: string): Promise<void> {
try {
const isGroup = settings.getGroup(from) !== null;
settings.changeFriendlyName(from, to);
logger.info(`Successfully renamed - ${from} to ${to} `);
const entity = this.zigbee.resolveEntity(to);
if (entity.isDevice()) {
this.eventBus.emitEntityRenamed({homeAssisantRename: false, from, to, entity});
}
await this.mqtt.publish('bridge/log', stringify({type: `${isGroup ? 'group' : 'device'}_renamed`, message: {from, to}}));
} catch {
logger.error(`Failed to rename - ${from} to ${to}`);
}
}
@bind async addGroup(topic: string, message: string): Promise<void> {
let id = null;
let name = null;
try {
// json payload with id and friendly_name
const json = JSON.parse(message);
if (json.hasOwnProperty('id')) {
id = json.id;
name = `group_${id}`;
}
if (json.hasOwnProperty('friendly_name')) {
name = json.friendly_name;
}
} catch {
// just friendly_name
name = message;
}
if (name == null) {
logger.error('Failed to add group, missing friendly_name!');
return;
}
const group = settings.addGroup(name, id);
this.zigbee.createGroup(group.ID);
await this.mqtt.publish('bridge/log', stringify({type: `group_added`, message: name}));
logger.info(`Added group '${name}'`);
}
@bind async removeGroup(topic: string, message: string): Promise<void> {
const name = message;
const entity = this.zigbee.resolveEntity(message) as Group;
assert(entity && entity.isGroup(), `Group '${message}' does not exist`);
if (topic.includes('force')) {
entity.zh.removeFromDatabase();
} else {
await entity.zh.removeFromNetwork();
}
settings.removeGroup(message);
await this.mqtt.publish('bridge/log', stringify({type: `group_removed`, message}));
logger.info(`Removed group '${name}'`);
}
@bind async forceRemove(topic: string, message: string): Promise<void> {
await this.removeForceRemoveOrBan('force_remove', message);
}
@bind async remove(topic: string, message: string): Promise<void> {
await this.removeForceRemoveOrBan('remove', message);
}
@bind async ban(topic: string, message: string): Promise<void> {
await this.removeForceRemoveOrBan('ban', message);
}
@bind async removeForceRemoveOrBan(action: string, message: string): Promise<void> {
const entity = this.zigbee.resolveEntity(message.trim()) as Device;
const lookup: KeyValue = {
ban: ['banned', 'Banning', 'ban'],
force_remove: ['force_removed', 'Force removing', 'force remove'],
remove: ['removed', 'Removing', 'remove'],
};
if (!entity) {
logger.error(`Cannot ${lookup[action][2]}, device '${message}' does not exist`);
await this.mqtt.publish('bridge/log', stringify({type: `device_${lookup[action][0]}_failed`, message}));
return;
}
const ieeeAddr = entity.ieeeAddr;
const name = entity.name;
const cleanup = async (): Promise<void> => {
// Fire event
this.eventBus.emitEntityRemoved({id: ieeeAddr, name: name, type: 'device'});
// Remove from configuration.yaml
settings.removeDevice(entity.ieeeAddr);
// Remove from state
this.state.remove(ieeeAddr);
logger.info(`Successfully ${lookup[action][0]} ${entity.name}`);
await this.mqtt.publish('bridge/log', stringify({type: `device_${lookup[action][0]}`, message}));
};
try {
logger.info(`${lookup[action][1]} '${entity.name}'`);
if (action === 'force_remove') {
entity.zh.removeFromDatabase();
} else {
await entity.zh.removeFromNetwork();
}
await cleanup();
} catch (error) {
logger.error(`Failed to ${lookup[action][2]} ${entity.name} (${error})`);
// eslint-disable-next-line
logger.error(`See https://www.zigbee2mqtt.io/guide/usage/mqtt_topics_and_messages.html#zigbee2mqtt-bridge-request for more info`);
await this.mqtt.publish('bridge/log', stringify({type: `device_${lookup[action][0]}_failed`, message}));
}
if (action === 'ban') {
settings.blockDevice(ieeeAddr);
}
}
@bind async onMQTTMessage(data: eventdata.MQTTMessage): Promise<void> {
const {topic, message} = data;
if (!topic.match(configRegex)) {
return;
}
const option = topic.match(configRegex)[1];
if (!this.supportedOptions.hasOwnProperty(option)) {
return;
}
await this.supportedOptions[option](topic, message);
return;
}
async publish(): Promise<void> {
const info = await utils.getZigbee2MQTTVersion();
const coordinator = await this.zigbee.getCoordinatorVersion();
const topic = `bridge/config`;
const payload = {
version: info.version,
commit: info.commitHash,
coordinator,
network: await this.zigbee.getNetworkParameters(),
log_level: logger.getLevel(),
permit_join: this.zigbee.getPermitJoin(),
};
await this.mqtt.publish(topic, stringify(payload), {retain: true, qos: 0});
}
async onZigbeeEvent_(type: string, data: KeyValue, resolvedEntity: Device): Promise<void> {
if (type === 'deviceJoined' && resolvedEntity) {
this.lastJoinedDeviceName = resolvedEntity.name;
}
if (type === 'deviceJoined') {
await this.mqtt.publish('bridge/log', stringify({type: `device_connected`, message: {friendly_name: resolvedEntity.name}}));
} else if (type === 'deviceInterview') {
if (data.status === 'successful') {
if (resolvedEntity.isSupported) {
const {vendor, description, model} = resolvedEntity.definition;
const log = {friendly_name: resolvedEntity.name, model, vendor, description, supported: true};
await this.mqtt.publish('bridge/log', stringify({type: `pairing`, message: 'interview_successful', meta: log}));
} else {
const meta = {friendly_name: resolvedEntity.name, supported: false};
await this.mqtt.publish('bridge/log', stringify({type: `pairing`, message: 'interview_successful', meta}));
}
} else if (data.status === 'failed') {
const meta = {friendly_name: resolvedEntity.name};
await this.mqtt.publish('bridge/log', stringify({type: `pairing`, message: 'interview_failed', meta}));
} else {
/* istanbul ignore else */
if (data.status === 'started') {
const meta = {friendly_name: resolvedEntity.name};
await this.mqtt.publish('bridge/log', stringify({type: `pairing`, message: 'interview_started', meta}));
}
}
} else if (type === 'deviceAnnounce') {
const meta = {friendly_name: resolvedEntity.name};
await this.mqtt.publish('bridge/log', stringify({type: `device_announced`, message: 'announce', meta}));
} else {
/* istanbul ignore else */
if (type === 'deviceLeave') {
const name = data.ieeeAddr;
const meta = {friendly_name: name};
await this.mqtt.publish('bridge/log', stringify({type: `device_removed`, message: 'left_network', meta}));
}
}
}
@bind async touchlinkFactoryReset(): Promise<void> {
logger.info('Starting touchlink factory reset...');
await this.mqtt.publish('bridge/log', stringify({type: `touchlink`, message: 'reset_started', meta: {status: 'started'}}));
const result = await this.zigbee.touchlinkFactoryResetFirst();
if (result) {
logger.info('Successfully factory reset device through Touchlink');
await this.mqtt.publish('bridge/log', stringify({type: `touchlink`, message: 'reset_success', meta: {status: 'success'}}));
} else {
logger.warning('Failed to factory reset device through Touchlink');
await this.mqtt.publish('bridge/log', stringify({type: `touchlink`, message: 'reset_failed', meta: {status: 'failed'}}));
}
}
}
@@ -1,60 +0,0 @@
/* istanbul ignore file */
import bind from 'bind-decorator';
import Device from '../../model/device';
import logger from '../../util/logger';
import * as settings from '../../util/settings';
import Extension from '../extension';
const topicRegex = new RegExp(`^${settings.get().mqtt.base_topic}/bridge/device/(.+)/get_group_membership$`);
export default class DeviceGroupMembership extends Extension {
override async start(): Promise<void> {
this.eventBus.onMQTTMessage(this, this.onMQTTMessage);
}
@bind async onMQTTMessage(data: eventdata.MQTTMessage): Promise<void> {
const match = data.topic.match(topicRegex);
if (!match) {
return null;
}
const parsed = this.zigbee.resolveEntityAndEndpoint(match[1]);
const device = parsed?.entity as Device;
if (!device || !(device instanceof Device)) {
logger.error(`Device '${match[1]}' does not exist`);
return;
}
const endpoint = parsed.endpoint;
if (parsed.endpointID && !endpoint) {
logger.error(`Device '${parsed.ID}' does not have endpoint '${parsed.endpointID}'`);
return;
}
const response = await endpoint.command(`genGroups`, 'getMembership', {groupcount: 0, grouplist: []}, {});
if (!response) {
logger.warning(`Couldn't get group membership of ${device.ieeeAddr}`);
return;
}
let {grouplist} = response;
grouplist = grouplist.map((gid: string) => {
const g = settings.getGroup(gid);
return g ? g.friendly_name : gid;
});
const msgGroupList = `${device.ieeeAddr} is in groups [${grouplist}]`;
let msgCapacity;
if (response.capacity === 254) {
msgCapacity = 'it can be a part of at least 1 more group';
} else {
msgCapacity = `its remaining group capacity is ${response.capacity === 255 ? 'unknown' : response.capacity}`;
}
logger.info(`${msgGroupList} and ${msgCapacity}`);
await this.publishEntityState(device, {group_list: grouplist, group_capacity: response.capacity});
}
}
-199
View File
@@ -1,199 +0,0 @@
import * as zhc from 'zigbee-herdsman-converters';
import logger from '../../util/logger';
import * as settings from '../../util/settings';
import utils from '../../util/utils';
import Extension from '../extension';
const defaultConfiguration = {
minimumReportInterval: 3,
maximumReportInterval: 300,
reportableChange: 1,
};
const ZNLDP12LM = zhc.definitions.find((d) => d.model === 'ZNLDP12LM');
const devicesNotSupportingReporting = [
zhc.definitions.find((d) => d.model === 'CC2530.ROUTER'),
zhc.definitions.find((d) => d.model === 'BASICZBR3'),
zhc.definitions.find((d) => d.model === 'ZM-CSW032-D'),
zhc.definitions.find((d) => d.model === 'TS0001'),
zhc.definitions.find((d) => d.model === 'TS0115'),
];
const reportKey = 1;
const getColorCapabilities = async (endpoint: zh.Endpoint): Promise<{colorTemperature: boolean; colorXY: boolean}> => {
if (endpoint.getClusterAttributeValue('lightingColorCtrl', 'colorCapabilities') === undefined) {
await endpoint.read('lightingColorCtrl', ['colorCapabilities']);
}
const value = endpoint.getClusterAttributeValue('lightingColorCtrl', 'colorCapabilities') as number;
return {
colorTemperature: (value & (1 << 4)) > 0,
colorXY: (value & (1 << 3)) > 0,
};
};
const clusters: {
[s: string]: {
attribute: string;
minimumReportInterval: number;
maximumReportInterval: number;
reportableChange: number;
condition?: (endpoint: zh.Endpoint) => Promise<boolean>;
}[];
} = {
genOnOff: [{attribute: 'onOff', ...defaultConfiguration, minimumReportInterval: 0, reportableChange: 0}],
genLevelCtrl: [{attribute: 'currentLevel', ...defaultConfiguration}],
lightingColorCtrl: [
{
attribute: 'colorTemperature',
...defaultConfiguration,
condition: async (endpoint): Promise<boolean> => (await getColorCapabilities(endpoint)).colorTemperature,
},
{
attribute: 'currentX',
...defaultConfiguration,
condition: async (endpoint): Promise<boolean> => (await getColorCapabilities(endpoint)).colorXY,
},
{
attribute: 'currentY',
...defaultConfiguration,
condition: async (endpoint): Promise<boolean> => (await getColorCapabilities(endpoint)).colorXY,
},
],
closuresWindowCovering: [
{attribute: 'currentPositionLiftPercentage', ...defaultConfiguration},
{attribute: 'currentPositionTiltPercentage', ...defaultConfiguration},
],
};
export default class Report extends Extension {
private queue: Set<string> = new Set();
private failed: Set<string> = new Set();
private enabled = settings.get().advanced.report;
shouldIgnoreClusterForDevice(cluster: string, definition: zhc.Definition): boolean {
if (definition === ZNLDP12LM && cluster === 'closuresWindowCovering') {
// Device announces it but doesn't support it
// https://github.com/Koenkk/zigbee2mqtt/issues/2611
return true;
}
return false;
}
async setupReporting(device: Device): Promise<void> {
if (this.queue.has(device.ieeeAddr) || this.failed.has(device.ieeeAddr)) return;
this.queue.add(device.ieeeAddr);
const term1 = this.enabled ? 'Setup' : 'Disable';
const term2 = this.enabled ? 'setup' : 'disabled';
try {
for (const ep of device.zh.endpoints) {
for (const [cluster, configuration] of Object.entries(clusters)) {
if (ep.supportsInputCluster(cluster) && !this.shouldIgnoreClusterForDevice(cluster, device.definition)) {
logger.debug(`${term1} reporting for '${device.ieeeAddr}' - ${ep.ID} - ${cluster}`);
const items = [];
for (const entry of configuration) {
if (!entry.hasOwnProperty('condition') || (await entry.condition(ep))) {
const toAdd = {...entry};
if (!this.enabled) toAdd.maximumReportInterval = 0xffff;
items.push(toAdd);
delete items[items.length - 1].condition;
}
}
if (this.enabled) {
await ep.bind(cluster, this.zigbee.firstCoordinatorEndpoint());
} else {
await ep.unbind(cluster, this.zigbee.firstCoordinatorEndpoint());
}
await ep.configureReporting(cluster, items);
logger.info(`Successfully ${term2} reporting for '${device.ieeeAddr}' - ${ep.ID} - ${cluster}`);
}
}
}
if (this.enabled) {
device.zh.meta.reporting = reportKey;
} else {
delete device.zh.meta.reporting;
this.eventBus.emitReconfigure({device});
}
this.eventBus.emitDevicesChanged();
} catch (error) {
logger.error(`Failed to ${term1.toLowerCase()} reporting for '${device.ieeeAddr}' - ${error.stack}`);
this.failed.add(device.ieeeAddr);
}
device.zh.save();
this.queue.delete(device.ieeeAddr);
}
shouldSetupReporting(device: Device, messageType: string): boolean {
if (!device || !device.zh || !device.definition) return false;
// Handle messages of type endDeviceAnnce and devIncoming.
// This message is typically send when a device comes online after being powered off
// Ikea TRADFRI tend to forget their reporting after powered off.
// Re-setup reporting.
// Only resetup reporting if configuredReportings was not populated yet,
// else reconfigure is done in zigbee-herdsman-converters ikea.js/bulbOnEvent
// configuredReportings are saved since Zigbee2MQTT 1.17.0
// https://github.com/Koenkk/zigbee2mqtt/issues/966
if (
this.enabled &&
messageType === 'deviceAnnounce' &&
device.isIkeaTradfri() &&
device.zh.endpoints.filter((e) => e.configuredReportings.length === 0).length === device.zh.endpoints.length
) {
return true;
}
// These do not support reproting.
// https://github.com/Koenkk/zigbee-herdsman/issues/110
const philipsIgnoreSw = ['5.127.1.26581', '5.130.1.30000'];
if (device.zh.manufacturerName === 'Philips' && philipsIgnoreSw.includes(device.zh.softwareBuildID)) return false;
if (device.zh.interviewing === true) return false;
if (device.zh.type !== 'Router' || device.zh.powerSource === 'Battery') return false;
// Gledopto devices don't support reporting.
if (devicesNotSupportingReporting.includes(device.definition) || device.definition.vendor === 'Gledopto') return false;
if (this.enabled && device.zh.meta.hasOwnProperty('reporting') && device.zh.meta.reporting === reportKey) {
return false;
}
if (!this.enabled && !device.zh.meta.hasOwnProperty('reporting')) {
return false;
}
return true;
}
override async start(): Promise<void> {
for (const device of this.zigbee.devicesIterator(utils.deviceNotCoordinator)) {
if (this.shouldSetupReporting(device, null)) {
await this.setupReporting(device);
}
}
this.eventBus.onDeviceAnnounce(this, (data) => this.onZigbeeEvent_('deviceAnnounce', data.device));
this.eventBus.onDeviceMessage(this, (data) => this.onZigbeeEvent_('dummy', data.device));
this.eventBus.onDeviceJoined(this, (data) => this.onZigbeeEvent_('dummy', data.device));
this.eventBus.onDeviceNetworkAddressChanged(this, (data) => this.onZigbeeEvent_('dummy', data.device));
}
async onZigbeeEvent_(type: string, device: Device): Promise<void> {
if (this.shouldSetupReporting(device, type)) {
await this.setupReporting(device);
}
}
}
-62
View File
@@ -1,62 +0,0 @@
/* istanbul ignore file */
import logger from '../../util/logger';
// DEPRECATED
import * as settings from '../../util/settings';
import utils from '../../util/utils';
import Extension from '../extension';
/**
* This extensions soft resets the ZNP after a certain timeout.
*/
export default class SoftReset extends Extension {
private timer: NodeJS.Timeout = null;
private timeout = utils.seconds(settings.get().advanced.soft_reset_timeout);
override async start(): Promise<void> {
logger.debug(`Soft reset timeout set to ${this.timeout / 1000} seconds`);
this.resetTimer();
this.eventBus.onDeviceMessage(this, () => this.resetTimer());
this.eventBus.onDeviceAnnounce(this, () => this.resetTimer());
this.eventBus.onDeviceNetworkAddressChanged(this, () => this.resetTimer());
this.eventBus.onDeviceJoined(this, () => this.resetTimer());
this.eventBus.onDeviceInterview(this, () => this.resetTimer());
}
private clearTimer(): void {
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
}
private resetTimer(): void {
if (this.timeout === 0) {
return;
}
this.clearTimer();
this.timer = setTimeout(() => this.handleTimeout(), this.timeout);
}
private async handleTimeout(): Promise<void> {
logger.warning('Soft reset timeout triggered');
try {
await this.zigbee.reset('soft');
logger.warning('Soft reset ZNP due to timeout');
} catch (error) {
logger.warning(`Soft reset failed, trying stop/start (${error.message})`);
await this.zigbee.stop();
logger.warning('Zigbee stopped');
try {
await this.zigbee.start();
} catch (error) {
logger.error(`Failed to restart! (${error.message})`);
}
}
this.resetTimer();
}
}
+43 -70
View File
@@ -1,3 +1,5 @@
import type {Zigbee2MQTTAPI, Zigbee2MQTTNetworkMap} from '../types/api';
import bind from 'bind-decorator';
import stringify from 'json-stable-stringify-without-jsonify';
@@ -6,88 +8,60 @@ import * as settings from '../util/settings';
import utils from '../util/utils';
import Extension from './extension';
interface Link {
source: {ieeeAddr: string; networkAddress: number};
target: {ieeeAddr: string; networkAddress: number};
linkquality: number;
depth: number;
routes: zh.RoutingTableEntry[];
sourceIeeeAddr: string;
targetIeeeAddr: string;
sourceNwkAddr: number;
lqi: number;
relationship: number;
}
interface Topology {
nodes: {
ieeeAddr: string;
friendlyName: string;
type: string;
networkAddress: number;
manufacturerName: string;
modelID: string;
failed: string[];
lastSeen: number;
definition: {model: string; vendor: string; supports: string; description: string};
}[];
links: Link[];
}
const SUPPORTED_FORMATS = ['raw', 'graphviz', 'plantuml'];
/**
* This extension creates a network map
*/
export default class NetworkMap extends Extension {
private legacyApi = settings.get().advanced.legacy_api;
private legacyTopic = `${settings.get().mqtt.base_topic}/bridge/networkmap`;
private legacyTopicRoutes = `${settings.get().mqtt.base_topic}/bridge/networkmap/routes`;
private topic = `${settings.get().mqtt.base_topic}/bridge/request/networkmap`;
private supportedFormats: {[s: string]: (topology: Topology) => KeyValue | string};
override async start(): Promise<void> {
this.eventBus.onMQTTMessage(this, this.onMQTTMessage);
this.supportedFormats = {
raw: this.raw,
graphviz: this.graphviz,
plantuml: this.plantuml,
};
}
@bind async onMQTTMessage(data: eventdata.MQTTMessage): Promise<void> {
/* istanbul ignore else */
if (this.legacyApi) {
if ((data.topic === this.legacyTopic || data.topic === this.legacyTopicRoutes) && this.supportedFormats.hasOwnProperty(data.message)) {
const includeRoutes = data.topic === this.legacyTopicRoutes;
const topology = await this.networkScan(includeRoutes);
let converted = this.supportedFormats[data.message](topology);
converted = data.message === 'raw' ? stringify(converted) : converted;
await this.mqtt.publish(`bridge/networkmap/${data.message}`, converted as string, {});
}
}
if (data.topic === this.topic) {
const message = utils.parseJSON(data.message, data.message);
const message = utils.parseJSON(data.message, data.message) as Zigbee2MQTTAPI['bridge/request/networkmap'];
try {
const type = typeof message === 'object' ? message.type : message;
if (!this.supportedFormats.hasOwnProperty(type)) {
throw new Error(`Type '${type}' not supported, allowed are: ${Object.keys(this.supportedFormats)}`);
if (!SUPPORTED_FORMATS.includes(type)) {
throw new Error(`Type '${type}' not supported, allowed are: ${SUPPORTED_FORMATS.join(',')}`);
}
const routes = typeof message === 'object' && message.routes;
const topology = await this.networkScan(routes);
const value = this.supportedFormats[type](topology);
await this.mqtt.publish('bridge/response/networkmap', stringify(utils.getResponse(message, {routes, type, value}, null)));
let responseData: Zigbee2MQTTAPI['bridge/response/networkmap'];
switch (type) {
case 'raw': {
responseData = {type, routes, value: this.raw(topology)};
break;
}
case 'graphviz': {
responseData = {type, routes, value: this.graphviz(topology)};
break;
}
case 'plantuml': {
responseData = {type, routes, value: this.plantuml(topology)};
break;
}
}
await this.mqtt.publish('bridge/response/networkmap', stringify(utils.getResponse(message, responseData)));
} catch (error) {
await this.mqtt.publish('bridge/response/networkmap', stringify(utils.getResponse(message, {}, error.message)));
await this.mqtt.publish('bridge/response/networkmap', stringify(utils.getResponse(message, {}, (error as Error).message)));
}
}
}
@bind raw(topology: Topology): KeyValue {
raw(topology: Zigbee2MQTTNetworkMap): Zigbee2MQTTNetworkMap {
return topology;
}
@bind graphviz(topology: Topology): string {
graphviz(topology: Zigbee2MQTTNetworkMap): string {
const colors = settings.get().map_options.graphviz.colors;
let text = 'digraph G {\nnode[shape=record];\n';
@@ -153,7 +127,7 @@ export default class NetworkMap extends Extension {
return text.replace(/\0/g, '');
}
@bind plantuml(topology: Topology): string {
plantuml(topology: Zigbee2MQTTNetworkMap): string {
const text = [];
text.push(`' paste into: https://www.planttext.com/`);
@@ -208,7 +182,7 @@ export default class NetworkMap extends Extension {
return text.join(`\n`);
}
async networkScan(includeRoutes: boolean): Promise<Topology> {
async networkScan(includeRoutes: boolean): Promise<Zigbee2MQTTNetworkMap> {
logger.info(`Starting network scan (includeRoutes '${includeRoutes}')`);
const lqis: Map<Device, zh.LQI> = new Map();
const routingTables: Map<Device, zh.RoutingTable> = new Map();
@@ -221,7 +195,7 @@ export default class NetworkMap extends Extension {
} catch {
// Network is possibly congested, sleep 5 seconds to let the network settle.
await utils.sleep(5);
return request();
return await request();
}
};
@@ -234,31 +208,31 @@ export default class NetworkMap extends Extension {
await utils.sleep(1); // sleep 1 second between each scan to reduce stress on network.
try {
const result = await requestWithRetry<zh.LQI>(async () => device.zh.lqi());
const result = await requestWithRetry<zh.LQI>(async () => await device.zh.lqi());
lqis.set(device, result);
logger.debug(`LQI succeeded for '${device.name}'`);
} catch (error) {
failed.get(device).push('lqi');
failed.get(device)!.push('lqi'); // set above
logger.error(`Failed to execute LQI for '${device.name}'`);
logger.debug(error.stack);
logger.debug((error as Error).stack!);
}
if (includeRoutes) {
try {
const result = await requestWithRetry<zh.RoutingTable>(async () => device.zh.routingTable());
const result = await requestWithRetry<zh.RoutingTable>(async () => await device.zh.routingTable());
routingTables.set(device, result);
logger.debug(`Routing table succeeded for '${device.name}'`);
} catch (error) {
failed.get(device).push('routingTable');
failed.get(device)!.push('routingTable'); // set above
logger.error(`Failed to execute routing table for '${device.name}'`);
logger.debug(error.stack);
logger.debug((error as Error).stack!);
}
}
}
logger.info(`Network scan finished`);
const topology: Topology = {nodes: [], links: []};
const topology: Zigbee2MQTTNetworkMap = {nodes: [], links: []};
// XXX: display GP/disabled devices in the map, better feedback than just hiding them?
for (const device of this.zigbee.devicesIterator((d) => d.type !== 'GreenPower')) {
@@ -275,12 +249,12 @@ export default class NetworkMap extends Extension {
supports: Array.from(
new Set(
device.exposes().map((e) => {
return e.name ?? `${e.type} (${e.features.map((f) => f.name).join(', ')})`;
return e.name ?? `${e.type} (${e.features?.map((f) => f.name).join(', ')})`;
}),
),
).join(', '),
}
: null;
: undefined;
topology.nodes.push({
ieeeAddr: device.ieeeAddr,
@@ -289,7 +263,7 @@ export default class NetworkMap extends Extension {
networkAddress: device.zh.networkAddress,
manufacturerName: device.zh.manufacturerName,
modelID: device.zh.modelID,
failed: failed.get(device),
failed: failed.get(device)!,
lastSeen: device.zh.lastSeen,
definition,
});
@@ -308,13 +282,12 @@ export default class NetworkMap extends Extension {
if (neighbor.ieeeAddr === '0x0000000000000000') {
const neighborDevice = this.zigbee.deviceByNetworkAddress(neighbor.networkAddress);
/* istanbul ignore else */
if (neighborDevice) {
neighbor.ieeeAddr = neighborDevice.ieeeAddr;
}
}
const link: Link = {
const link: Zigbee2MQTTNetworkMap['links'][number] = {
source: {ieeeAddr: neighbor.ieeeAddr, networkAddress: neighbor.networkAddress},
target: {ieeeAddr: device.ieeeAddr, networkAddress: device.zh.networkAddress},
linkquality: neighbor.linkquality,
+5 -3
View File
@@ -9,7 +9,7 @@ import Extension from './extension';
export default class OnEvent extends Extension {
override async start(): Promise<void> {
for (const device of this.zigbee.devicesIterator(utils.deviceNotCoordinator)) {
await this.callOnEvent(device, 'start', {});
this.callOnEvent(device, 'start', {}).catch(utils.noop);
}
this.eventBus.onDeviceMessage(this, (data) => this.callOnEvent(data.device, 'message', this.convertData(data)));
@@ -41,11 +41,13 @@ export default class OnEvent extends Extension {
private async callOnEvent(device: Device, type: zhc.OnEventType, data: KeyValue): Promise<void> {
if (device.options.disabled) return;
const state = this.state.get(device);
await zhc.onEvent(type, data, device.zh);
const deviceExposesChanged = (): void => this.eventBus.emitExposesAndDevicesChanged(data.device);
await zhc.onEvent(type, data, device.zh, {deviceExposesChanged});
if (device.definition?.onEvent) {
const options: KeyValue = device.options;
await device.definition.onEvent(type, data, device.zh, options, state);
await device.definition.onEvent(type, data, device.zh, options, state, {deviceExposesChanged});
}
}
}
+110 -152
View File
@@ -1,9 +1,15 @@
import type {Ota} from 'zigbee-herdsman-converters';
import type {Zigbee2MQTTAPI} from '../types/api';
import assert from 'node:assert';
import path from 'node:path';
import bind from 'bind-decorator';
import stringify from 'json-stable-stringify-without-jsonify';
import path from 'path';
import * as URI from 'uri-js';
import {Zcl} from 'zigbee-herdsman';
import * as zhc from 'zigbee-herdsman-converters';
import {ota} from 'zigbee-herdsman-converters';
import Device from '../model/device';
import dataDir from '../util/data';
@@ -12,21 +18,8 @@ import * as settings from '../util/settings';
import utils from '../util/utils';
import Extension from './extension';
function isValidUrl(url: string): boolean {
let parsed;
try {
parsed = URI.parse(url);
} catch {
// istanbul ignore next
return false;
}
return parsed.scheme === 'http' || parsed.scheme === 'https';
}
type UpdateState = 'updating' | 'idle' | 'available';
interface UpdatePayload {
update_available?: boolean;
// eslint-disable-next-line camelcase
update: {
progress?: number;
remaining?: number;
@@ -36,34 +29,33 @@ interface UpdatePayload {
};
}
const legacyTopicRegex = new RegExp(`^${settings.get().mqtt.base_topic}/bridge/ota_update/.+$`);
const topicRegex = new RegExp(`^${settings.get().mqtt.base_topic}/bridge/request/device/ota_update/(update|check)`, 'i');
const topicRegex = new RegExp(`^${settings.get().mqtt.base_topic}/bridge/request/device/ota_update/(update|check)/?(downgrade)?`, 'i');
export default class OTAUpdate extends Extension {
private inProgress = new Set();
private lastChecked: {[s: string]: number} = {};
private legacyApi = settings.get().advanced.legacy_api;
override async start(): Promise<void> {
this.eventBus.onMQTTMessage(this, this.onMQTTMessage);
this.eventBus.onDeviceMessage(this, this.onZigbeeEvent);
if (settings.get().ota.ikea_ota_use_test_url) {
zhc.ota.tradfri.useTestURL();
}
// Let zigbeeOTA module know if the override index file is provided
let overrideOTAIndex = settings.get().ota.zigbee_ota_override_index_location;
if (overrideOTAIndex) {
// If the file name is not a full path, then treat it as a relative to the data directory
if (!isValidUrl(overrideOTAIndex) && !path.isAbsolute(overrideOTAIndex)) {
overrideOTAIndex = dataDir.joinPath(overrideOTAIndex);
}
const otaSettings = settings.get().ota;
// Let OTA module know if the override index file is provided
let overrideIndexLocation = otaSettings.zigbee_ota_override_index_location;
zhc.ota.zigbeeOTA.useIndexOverride(overrideOTAIndex);
// If the file name is not a full path, then treat it as a relative to the data directory
if (overrideIndexLocation && !ota.isValidUrl(overrideIndexLocation) && !path.isAbsolute(overrideIndexLocation)) {
overrideIndexLocation = dataDir.joinPath(overrideIndexLocation);
}
// In order to support local firmware files we need to let zigbeeOTA know where the data directory is
zhc.ota.setDataDir(dataDir.getPath());
ota.setConfiguration({
dataDir: dataDir.getPath(),
overrideIndexLocation,
// TODO: implement me
imageBlockResponseDelay: otaSettings.image_block_response_delay,
defaultMaximumDataSize: otaSettings.default_maximum_data_size,
});
// In case Zigbee2MQTT is restared during an update, progress and remaining values are still in state, remove them.
for (const device of this.zigbee.devicesIterator(utils.deviceNotCoordinator)) {
@@ -77,8 +69,12 @@ export default class OTAUpdate extends Extension {
}
private removeProgressAndRemainingFromState(device: Device): void {
delete this.state.get(device).update?.progress;
delete this.state.get(device).update?.remaining;
const deviceState = this.state.get(device);
if (deviceState.update) {
delete deviceState.update.progress;
delete deviceState.update.remaining;
}
}
@bind private async onZigbeeEvent(data: eventdata.DeviceMessage): Promise<void> {
@@ -86,38 +82,33 @@ export default class OTAUpdate extends Extension {
logger.debug(`Device '${data.device.name}' requested OTA`);
const automaticOTACheckDisabled = settings.get().ota.disable_automatic_update_check;
let supportsOTA = !!data.device.definition.ota;
if (supportsOTA && !automaticOTACheckDisabled) {
if (data.device.definition.ota && !automaticOTACheckDisabled) {
// When a device does a next image request, it will usually do it a few times after each other
// with only 10 - 60 seconds inbetween. It doesn't make sense to check for a new update
// each time, so this interval can be set by the user. The default is 1,440 minutes (one day).
const updateCheckInterval = settings.get().ota.update_check_interval * 1000 * 60;
const check = this.lastChecked.hasOwnProperty(data.device.ieeeAddr)
? Date.now() - this.lastChecked[data.device.ieeeAddr] > updateCheckInterval
: true;
const check =
this.lastChecked[data.device.ieeeAddr] !== undefined
? Date.now() - this.lastChecked[data.device.ieeeAddr] > updateCheckInterval
: true;
if (!check) return;
this.lastChecked[data.device.ieeeAddr] = Date.now();
let availableResult: zhc.OtaUpdateAvailableResult = null;
let availableResult: Ota.UpdateAvailableResult | undefined;
try {
availableResult = await data.device.definition.ota.isUpdateAvailable(data.device.zh, data.data as zhc.ota.ImageInfo);
} catch (e) {
supportsOTA = false;
logger.debug(`Failed to check if update available for '${data.device.name}' (${e.message})`);
// never use 'previous' when responding to device request
availableResult = await ota.isUpdateAvailable(data.device.zh, data.device.otaExtraMetas, data.data as Ota.ImageInfo, false);
} catch (error) {
logger.debug(`Failed to check if update available for '${data.device.name}' (${error})`);
}
const payload = this.getEntityPublishPayload(data.device, availableResult ?? 'idle');
await this.publishEntityState(data.device, payload);
await this.publishEntityState(data.device, this.getEntityPublishPayload(data.device, availableResult ?? 'idle'));
if (availableResult?.available) {
const message = `Update available for '${data.device.name}'`;
logger.info(message);
/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
const meta = {status: 'available', device: data.device.name};
await this.mqtt.publish('bridge/log', stringify({type: `ota_update`, message, meta}));
}
}
}
@@ -133,21 +124,25 @@ export default class OTAUpdate extends Extension {
logger.debug(`Responded to OTA request of '${data.device.name}' with 'NO_IMAGE_AVAILABLE'`);
}
private async readSoftwareBuildIDAndDateCode(device: Device, sendPolicy?: 'immediate'): Promise<{softwareBuildID: string; dateCode: string}> {
private async readSoftwareBuildIDAndDateCode(
device: Device,
sendPolicy?: 'immediate',
): Promise<{softwareBuildID: string; dateCode: string} | undefined> {
try {
const endpoint = device.zh.endpoints.find((e) => e.supportsInputCluster('genBasic'));
assert(endpoint);
const result = await endpoint.read('genBasic', ['dateCode', 'swBuildId'], {sendPolicy});
return {softwareBuildID: result.swBuildId, dateCode: result.dateCode};
} catch {
return null;
return undefined;
}
}
private getEntityPublishPayload(
device: Device,
state: zhc.OtaUpdateAvailableResult | UpdateState,
progress: number = null,
remaining: number = null,
state: Ota.UpdateAvailableResult | UpdateState,
progress?: number,
remaining?: number,
): UpdatePayload {
const deviceUpdateState = this.state.get(device).update;
const payload: UpdatePayload = {
@@ -157,40 +152,41 @@ export default class OTAUpdate extends Extension {
latest_version: typeof state === 'string' ? deviceUpdateState?.latest_version : state.otaFileVersion,
},
};
if (progress !== null) payload.update.progress = progress;
if (remaining !== null) payload.update.remaining = Math.round(remaining);
/* istanbul ignore else */
if (this.legacyApi) {
payload.update_available = typeof state === 'string' ? state === 'available' : state.available;
if (progress !== undefined) {
payload.update.progress = progress;
}
if (remaining !== undefined) {
payload.update.remaining = Math.round(remaining);
}
return payload;
}
@bind async onMQTTMessage(data: eventdata.MQTTMessage): Promise<void> {
if ((!this.legacyApi || !data.topic.match(legacyTopicRegex)) && !data.topic.match(topicRegex)) {
return null;
const topicMatch = data.topic.match(topicRegex);
if (!topicMatch) {
return;
}
const message = utils.parseJSON(data.message, data.message);
const ID = (typeof message === 'object' && message.hasOwnProperty('id') ? message.id : message) as string;
const message = utils.parseJSON(data.message, data.message) as
| Zigbee2MQTTAPI['bridge/request/device/ota_update/check']
| Zigbee2MQTTAPI['bridge/request/device/ota_update/check/downgrade']
| Zigbee2MQTTAPI['bridge/request/device/ota_update/update']
| Zigbee2MQTTAPI['bridge/request/device/ota_update/update/downgrade'];
const ID = (typeof message === 'object' && message['id'] !== undefined ? message.id : message) as string;
const device = this.zigbee.resolveEntity(ID);
const type = data.topic.substring(data.topic.lastIndexOf('/') + 1);
const responseData: {id: string; updateAvailable?: boolean; from?: string; to?: string} = {id: ID};
let error = null;
let errorStack = null;
const type = topicMatch[1];
const downgrade = Boolean(topicMatch[2]);
let error: string | undefined;
let errorStack: string | undefined;
if (!(device instanceof Device)) {
error = `Device '${ID}' does not exist`;
} else if (!device.definition || !device.definition.ota) {
error = `Device '${device.name}' does not support OTA updates`;
/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
const meta = {status: `not_supported`, device: device.name};
await this.mqtt.publish('bridge/log', stringify({type: `ota_update`, message: error, meta}));
}
} else if (this.inProgress.has(device.ieeeAddr)) {
error = `Update or check for update already in progress for '${device.name}'`;
} else {
@@ -200,85 +196,54 @@ export default class OTAUpdate extends Extension {
const msg = `Checking if update available for '${device.name}'`;
logger.info(msg);
/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
const meta = {status: `checking_if_available`, device: device.name};
await this.mqtt.publish('bridge/log', stringify({type: `ota_update`, message: msg, meta}));
}
try {
const availableResult = await device.definition.ota.isUpdateAvailable(device.zh, null);
const availableResult = await ota.isUpdateAvailable(device.zh, device.otaExtraMetas, undefined, downgrade);
const msg = `${availableResult.available ? 'Update' : 'No update'} available for '${device.name}'`;
logger.info(msg);
/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
const meta = {
status: availableResult.available ? 'available' : 'not_available',
device: device.name,
};
await this.mqtt.publish('bridge/log', stringify({type: `ota_update`, message: msg, meta}));
}
await this.publishEntityState(device, this.getEntityPublishPayload(device, availableResult));
const payload = this.getEntityPublishPayload(device, availableResult);
await this.publishEntityState(device, payload);
this.lastChecked[device.ieeeAddr] = Date.now();
responseData.updateAvailable = availableResult.available;
} catch (e) {
error = `Failed to check if update available for '${device.name}' (${e.message})`;
errorStack = e.stack;
const response = utils.getResponse<'bridge/response/device/ota_update/check'>(message, {
id: ID,
update_available: availableResult.available,
});
/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
const meta = {status: `check_failed`, device: device.name};
await this.mqtt.publish('bridge/log', stringify({type: `ota_update`, message: error, meta}));
}
await this.mqtt.publish(`bridge/response/device/ota_update/check`, stringify(response));
} catch (e) {
error = `Failed to check if update available for '${device.name}' (${(e as Error).message})`;
errorStack = (e as Error).stack;
}
} else {
// type === 'update'
const msg = `Updating '${device.name}' to latest firmware`;
const msg = `Updating '${device.name}' to ${downgrade ? 'previous' : 'latest'} firmware`;
logger.info(msg);
/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
const meta = {status: `update_in_progress`, device: device.name};
await this.mqtt.publish('bridge/log', stringify({type: `ota_update`, message: msg, meta}));
}
try {
const onProgress = async (progress: number, remaining: number): Promise<void> => {
const firmwareFrom = await this.readSoftwareBuildIDAndDateCode(device, 'immediate');
const fileVersion = await ota.update(device.zh, device.otaExtraMetas, downgrade, async (progress, remaining) => {
let msg = `Update of '${device.name}' at ${progress.toFixed(2)}%`;
if (remaining) {
msg += `, ≈ ${Math.round(remaining / 60)} minutes remaining`;
}
logger.info(msg);
const payload = this.getEntityPublishPayload(device, 'updating', progress, remaining);
await this.publishEntityState(device, payload);
await this.publishEntityState(device, this.getEntityPublishPayload(device, 'updating', progress, remaining ?? undefined));
});
/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
const meta = {status: `update_progress`, device: device.name, progress};
await this.mqtt.publish('bridge/log', stringify({type: `ota_update`, message: msg, meta}));
}
};
const from_ = await this.readSoftwareBuildIDAndDateCode(device, 'immediate');
const fileVersion = await device.definition.ota.updateToLatest(device.zh, onProgress);
logger.info(`Finished update of '${device.name}'`);
this.removeProgressAndRemainingFromState(device);
const payload = this.getEntityPublishPayload(device, {
available: false,
currentFileVersion: fileVersion,
otaFileVersion: fileVersion,
});
await this.publishEntityState(device, payload);
const to = await this.readSoftwareBuildIDAndDateCode(device);
const [fromS, toS] = [stringify(from_), stringify(to)];
logger.info(`Device '${device.name}' was updated from '${fromS}' to '${toS}'`);
responseData.from = from_ ? utils.toSnakeCase(from_) : null;
responseData.to = to ? utils.toSnakeCase(to) : null;
await this.publishEntityState(
device,
this.getEntityPublishPayload(device, {available: false, currentFileVersion: fileVersion, otaFileVersion: fileVersion}),
);
const firmwareTo = await this.readSoftwareBuildIDAndDateCode(device);
logger.info(() => `Device '${device.name}' was updated from '${stringify(firmwareFrom)}' to '${stringify(firmwareTo)}'`);
/**
* Re-configure after reading software build ID and date code, some devices use a
* custom attribute for this (e.g. Develco SMSZB-120)
@@ -286,39 +251,32 @@ export default class OTAUpdate extends Extension {
this.eventBus.emitReconfigure({device});
this.eventBus.emitDevicesChanged();
/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
const meta = {status: `update_succeeded`, device: device.name, from: from_, to};
await this.mqtt.publish('bridge/log', stringify({type: `ota_update`, message, meta}));
}
const response = utils.getResponse<'bridge/response/device/ota_update/update'>(message, {
id: ID,
from: firmwareFrom ? {software_build_id: firmwareFrom.softwareBuildID, date_code: firmwareFrom.dateCode} : undefined,
to: firmwareTo ? {software_build_id: firmwareTo.softwareBuildID, date_code: firmwareTo.dateCode} : undefined,
});
await this.mqtt.publish(`bridge/response/device/ota_update/update`, stringify(response));
} catch (e) {
logger.debug(`Update of '${device.name}' failed (${e})`);
error = `Update of '${device.name}' failed (${e.message})`;
errorStack = e.stack;
error = `Update of '${device.name}' failed (${(e as Error).message})`;
errorStack = (e as Error).stack;
this.removeProgressAndRemainingFromState(device);
const payload = this.getEntityPublishPayload(device, 'available');
await this.publishEntityState(device, payload);
/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
const meta = {status: `update_failed`, device: device.name};
await this.mqtt.publish('bridge/log', stringify({type: `ota_update`, message: error, meta}));
}
await this.publishEntityState(device, this.getEntityPublishPayload(device, 'available'));
}
}
this.inProgress.delete(device.ieeeAddr);
}
const triggeredViaLegacyApi = data.topic.match(legacyTopicRegex);
if (!triggeredViaLegacyApi) {
const response = utils.getResponse(message, responseData, error);
await this.mqtt.publish(`bridge/response/device/ota_update/${type}`, stringify(response));
}
if (error) {
const response = utils.getResponse(message, {}, error);
await this.mqtt.publish(`bridge/response/device/ota_update/${type}`, stringify(response));
logger.error(error);
if (errorStack) {
logger.debug(errorStack);
}
+84 -101
View File
@@ -1,7 +1,7 @@
import type * as zhc from 'zigbee-herdsman-converters';
import bind from 'bind-decorator';
import stringify from 'json-stable-stringify-without-jsonify';
import * as zhc from 'zigbee-herdsman-converters';
import * as philips from 'zigbee-herdsman-converters/lib/philips';
import Device from '../model/device';
import Group from '../model/group';
@@ -17,39 +17,22 @@ export const loadTopicGetSetRegex = (): void => {
};
loadTopicGetSetRegex();
const stateValues = ['on', 'off', 'toggle', 'open', 'close', 'stop', 'lock', 'unlock'];
const sceneConverterKeys = ['scene_store', 'scene_add', 'scene_remove', 'scene_remove_all', 'scene_rename'];
// Legacy: don't provide default converters anymore, this is required by older z2m installs not saving group members
const defaultGroupConverters = [
zhc.toZigbee.light_onoff_brightness,
zhc.toZigbee.light_color_colortemp,
philips.tz.effect, // Support Hue effects for groups
zhc.toZigbee.ignore_transition,
zhc.toZigbee.cover_position_tilt,
zhc.toZigbee.thermostat_occupied_heating_setpoint,
zhc.toZigbee.tint_scene,
zhc.toZigbee.light_brightness_move,
zhc.toZigbee.light_brightness_step,
zhc.toZigbee.light_colortemp_step,
zhc.toZigbee.light_colortemp_move,
zhc.toZigbee.light_hue_saturation_move,
zhc.toZigbee.light_hue_saturation_step,
];
const STATE_VALUES: ReadonlyArray<string> = ['on', 'off', 'toggle', 'open', 'close', 'stop', 'lock', 'unlock'];
const SCENE_CONVERTER_KEYS: ReadonlyArray<string> = ['scene_store', 'scene_add', 'scene_remove', 'scene_remove_all', 'scene_rename'];
interface ParsedTopic {
ID: string;
endpoint: string;
endpoint: string | undefined;
attribute: string;
type: 'get' | 'set';
}
export default class Publish extends Extension {
async start(): Promise<void> {
override async start(): Promise<void> {
this.eventBus.onMQTTMessage(this, this.onMQTTMessage);
}
parseTopic(topic: string): ParsedTopic | null {
parseTopic(topic: string): ParsedTopic | undefined {
// The function supports the following topic formats (below are for 'set'. 'get' will look the same):
// - <base_topic>/device_name/set (endpoint and attribute is defined in the payload)
// - <base_topic>/device_name/set/attribute (default endpoint used)
@@ -60,7 +43,10 @@ export default class Publish extends Extension {
// Before the get/set is the device name and optional endpoint name.
// After it there will be an optional attribute name.
const match = topic.match(topicGetSetRegex);
if (!match) return null;
if (!match) {
return undefined;
}
const deviceNameAndEndpoint = match[1];
const attribute = match[3];
@@ -70,7 +56,7 @@ export default class Publish extends Extension {
return {ID: entity.ID, endpoint: entity.endpointID, type: match[2] as 'get' | 'set', attribute: attribute};
}
parseMessage(parsedTopic: ParsedTopic, data: eventdata.MQTTMessage): KeyValue | null {
parseMessage(parsedTopic: ParsedTopic, data: eventdata.MQTTMessage): KeyValue | undefined {
if (parsedTopic.attribute) {
try {
return {[parsedTopic.attribute]: JSON.parse(data.message)};
@@ -81,53 +67,25 @@ export default class Publish extends Extension {
try {
return JSON.parse(data.message);
} catch {
if (stateValues.includes(data.message.toLowerCase())) {
if (STATE_VALUES.includes(data.message.toLowerCase())) {
return {state: data.message};
} else {
return null;
return undefined;
}
}
}
}
async legacyLog(payload: KeyValue): Promise<void> {
/* istanbul ignore else */
if (settings.get().advanced.legacy_api) {
await this.mqtt.publish('bridge/log', stringify(payload));
}
}
legacyRetrieveState(
re: Device | Group,
converter: zhc.Tz.Converter,
result: zhc.Tz.ConvertSetResult,
target: zh.Endpoint | zh.Group,
key: string,
meta: zhc.Tz.Meta,
): void {
// It's possible for devices to get out of sync when writing an attribute that's not reportable.
// So here we re-read the value after a specified timeout, this timeout could for example be the
// transition time of a color change or for forcing a state read for devices that don't
// automatically report a new state when set.
// When reporting is requested for a device (report: true in device-specific settings) we won't
// ever issue a read here, as we assume the device will properly report changes.
// Only do this when the retrieve_state option is enabled for this device.
// retrieve_state == deprecated
if (re instanceof Device && result && result.hasOwnProperty('readAfterWriteTime') && re.options.retrieve_state) {
setTimeout(() => converter.convertGet(target, key, meta), result.readAfterWriteTime);
}
}
updateMessageHomeAssistant(message: KeyValue, entityState: KeyValue): void {
/**
* Home Assistant always publishes 'state', even when e.g. only setting
* the color temperature. This would lead to 2 zigbee publishes, where the first one
* (state) is probably unnecessary.
*/
if (settings.get().homeassistant) {
const hasColorTemp = message.hasOwnProperty('color_temp');
const hasColor = message.hasOwnProperty('color');
const hasBrightness = message.hasOwnProperty('brightness');
if (settings.get().homeassistant.enabled) {
const hasColorTemp = message.color_temp !== undefined;
const hasColor = message.color !== undefined;
const hasBrightness = message.brightness !== undefined;
const isOn = entityState.state === 'ON' ? true : false;
if (isOn && (hasColorTemp || hasColor) && !hasBrightness) {
delete message.state;
@@ -138,48 +96,55 @@ export default class Publish extends Extension {
@bind async onMQTTMessage(data: eventdata.MQTTMessage): Promise<void> {
const parsedTopic = this.parseTopic(data.topic);
if (!parsedTopic) return;
if (!parsedTopic) {
return;
}
const re = this.zigbee.resolveEntity(parsedTopic.ID);
if (re == null) {
await this.legacyLog({type: `entity_not_found`, message: {friendly_name: parsedTopic.ID}});
if (!re) {
logger.error(`Entity '${parsedTopic.ID}' is unknown`);
return;
}
// Get entity details
const definition = re instanceof Device ? re.definition : re.membersDefinitions();
let definition: zhc.Definition | zhc.Definition[];
if (re instanceof Device) {
if (!re.definition) {
logger.error(`Cannot publish to unsupported device '${re.name}'`);
return;
}
definition = re.definition;
} else {
definition = re.membersDefinitions();
}
const target = re instanceof Group ? re.zh : re.endpoint(parsedTopic.endpoint);
if (target == null) {
if (!target) {
logger.error(`Device '${re.name}' has no endpoint '${parsedTopic.endpoint}'`);
return;
}
const device = re instanceof Device ? re.zh : null;
// Convert the MQTT message to a Zigbee message.
const message = this.parseMessage(parsedTopic, data);
if (!message) {
logger.error(`Invalid message '${message}', skipping...`);
return;
}
const device = re instanceof Device ? re.zh : undefined;
const entitySettings = re.options;
const entityState = this.state.get(re);
const membersState =
re instanceof Group
? Object.fromEntries(
re.zh.members.map((e) => [e.getDevice().ieeeAddr, this.state.get(this.zigbee.resolveEntity(e.getDevice().ieeeAddr))]),
re.zh.members.map((e) => [e.getDevice().ieeeAddr, this.state.get(this.zigbee.resolveEntity(e.getDevice().ieeeAddr)!)]),
)
: null;
let converters: zhc.Tz.Converter[];
{
if (Array.isArray(definition)) {
const c = new Set(definition.map((d) => d.toZigbee).flat());
if (c.size == 0) converters = defaultGroupConverters;
else converters = Array.from(c);
} else {
converters = definition.toZigbee;
}
}
: undefined;
const converters = this.getDefinitionConverters(definition);
// Convert the MQTT message to a Zigbee message.
const message = this.parseMessage(parsedTopic, data);
if (message == null) {
logger.error(`Invalid message '${message}', skipping...`);
return;
}
this.updateMessageHomeAssistant(message, entityState);
/**
@@ -201,64 +166,73 @@ export default class Publish extends Extension {
const toPublishEntity: {[s: number | string]: Device | Group} = {};
const addToToPublish = (entity: Device | Group, payload: KeyValue): void => {
const ID = entity.ID;
if (!(ID in toPublish)) {
toPublish[ID] = {};
toPublishEntity[ID] = entity;
}
toPublish[ID] = {...toPublish[ID], ...payload};
};
const endpointNames = re instanceof Device ? re.getEndpointNames() : [];
const propertyEndpointRegex = new RegExp(`^(.*?)_(${endpointNames.join('|')})$`);
let scenesChanged = false;
for (const entry of entries) {
let key = entry[0];
const value = entry[1];
let endpointName = parsedTopic.endpoint;
let localTarget = target;
let endpointOrGroupID = utils.isEndpoint(target) ? target.ID : target.groupID;
let endpointOrGroupID = utils.isZHEndpoint(target) ? target.ID : target.groupID;
// When the key has a endpointName included (e.g. state_right), this will override the target.
const propertyEndpointMatch = key.match(propertyEndpointRegex);
if (re instanceof Device && propertyEndpointMatch) {
endpointName = propertyEndpointMatch[2];
key = propertyEndpointMatch[1];
localTarget = re.endpoint(endpointName);
// endpointName is always matched to an existing endpoint of the device
// since `propertyEndpointRegex` only contains valid endpoints for this device.
localTarget = re.endpoint(endpointName)!;
endpointOrGroupID = localTarget.ID;
}
if (!usedConverters.hasOwnProperty(endpointOrGroupID)) usedConverters[endpointOrGroupID] = [];
/* istanbul ignore next */
if (usedConverters[endpointOrGroupID] === undefined) usedConverters[endpointOrGroupID] = [];
// Match any key if the toZigbee converter defines no key.
const converter = converters.find((c) => (!c.key || c.key.includes(key)) && (!c.endpoint || c.endpoint == endpointName));
const converter = converters.find(
(c) =>
(!c.key || c.key.includes(key)) && (re instanceof Group || !c.endpoints || (endpointName && c.endpoints.includes(endpointName))),
);
if (parsedTopic.type === 'set' && usedConverters[endpointOrGroupID].includes(converter)) {
if (parsedTopic.type === 'set' && converter && usedConverters[endpointOrGroupID].includes(converter)) {
// Use a converter for set only once
// (e.g. light_onoff_brightness converters can convert state and brightness)
continue;
}
if (!converter) {
logger.error(`No converter available for '${key}' (${stringify(message[key])})`);
logger.error(`No converter available for '${key}' on '${re.name}': (${stringify(message[key])})`);
continue;
}
// If the endpoint_name name is a number, try to map it to a friendlyName
if (!isNaN(Number(endpointName)) && re.isDevice() && utils.isEndpoint(localTarget) && re.endpointName(localTarget)) {
if (!isNaN(Number(endpointName)) && re.isDevice() && utils.isZHEndpoint(localTarget) && re.endpointName(localTarget)) {
endpointName = re.endpointName(localTarget);
}
// Converter didn't return a result, skip
const entitySettingsKeyValue: KeyValue = entitySettings;
const meta = {
const meta: zhc.Tz.Meta = {
endpoint_name: endpointName,
options: entitySettingsKeyValue,
message: {...message},
logger,
device,
state: entityState,
membersState,
mapped: definition,
/* v8 ignore next */
publish: (payload: KeyValue) => this.publishEntityState(re, payload),
};
// Strip endpoint name from meta.message properties.
@@ -276,7 +250,8 @@ export default class Publish extends Extension {
if (parsedTopic.type === 'set' && converter.convertSet) {
logger.debug(`Publishing '${parsedTopic.type}' '${key}' to '${re.name}'`);
const result = await converter.convertSet(localTarget, key, value, meta);
const optimistic = !entitySettings.hasOwnProperty('optimistic') || entitySettings.optimistic;
const optimistic = entitySettings.optimistic === undefined || entitySettings.optimistic;
if (result && result.state && optimistic) {
const msg = result.state;
@@ -295,11 +270,9 @@ export default class Publish extends Extension {
if (result && result.membersState && optimistic) {
for (const [ieeeAddr, state] of Object.entries(result.membersState)) {
addToToPublish(this.zigbee.resolveEntity(ieeeAddr), state);
addToToPublish(this.zigbee.resolveEntity(ieeeAddr)!, state);
}
}
this.legacyRetrieveState(re, converter, result, localTarget, key, meta);
} else if (parsedTopic.type === 'get' && converter.convertGet) {
logger.debug(`Publishing get '${parsedTopic.type}' '${key}' to '${re.name}'`);
await converter.convertGet(localTarget, key, meta);
@@ -310,11 +283,14 @@ export default class Publish extends Extension {
} catch (error) {
const message = `Publish '${parsedTopic.type}' '${key}' to '${re.name}' failed: '${error}'`;
logger.error(message);
logger.debug(error.stack);
await this.legacyLog({type: `zigbee_publish_error`, message, meta: {friendly_name: re.name}});
logger.debug((error as Error).stack!);
}
usedConverters[endpointOrGroupID].push(converter);
if (!scenesChanged && converter.key) {
scenesChanged = converter.key.some((k) => SCENE_CONVERTER_KEYS.includes(k));
}
}
for (const [ID, payload] of Object.entries(toPublish)) {
@@ -323,9 +299,16 @@ export default class Publish extends Extension {
}
}
const scenesChanged = Object.values(usedConverters).some((cl) => cl.some((c) => c.key.some((k) => sceneConverterKeys.includes(k))));
if (scenesChanged) {
this.eventBus.emitScenesChanged({entity: re});
}
}
private getDefinitionConverters(definition: zhc.Definition | zhc.Definition[]): ReadonlyArray<zhc.Tz.Converter> {
if (Array.isArray(definition)) {
return definition.length ? Array.from(new Set(definition.map((d) => d.toZigbee).flat())) : [];
} else {
return definition?.toZigbee;
}
}
}
+39 -23
View File
@@ -1,6 +1,10 @@
import assert from 'node:assert';
import bind from 'bind-decorator';
import debounce from 'debounce';
import stringify from 'json-stable-stringify-without-jsonify';
import throttle from 'throttleit';
import * as zhc from 'zigbee-herdsman-converters';
import logger from '../util/logger';
@@ -13,8 +17,9 @@ type DebounceFunction = (() => void) & {clear(): void} & {flush(): void};
export default class Receive extends Extension {
private elapsed: {[s: string]: number} = {};
private debouncers: {[s: string]: {payload: KeyValue; publish: DebounceFunction}} = {};
private throttlers: {[s: string]: {publish: PublishEntityState}} = {};
async start(): Promise<void> {
override async start(): Promise<void> {
this.eventBus.onPublishEntityState(this, this.onPublishEntityState);
this.eventBus.onDeviceMessage(this, this.onDeviceMessage);
}
@@ -37,7 +42,7 @@ export default class Receive extends Extension {
}
}
publishDebounce(device: Device, payload: KeyValue, time: number, debounceIgnore: string[]): void {
publishDebounce(device: Device, payload: KeyValue, time: number, debounceIgnore: string[] | undefined): void {
if (!this.debouncers[device.ieeeAddr]) {
this.debouncers[device.ieeeAddr] = {
payload: {},
@@ -65,11 +70,25 @@ export default class Receive extends Extension {
this.debouncers[device.ieeeAddr].publish();
}
async publishThrottle(device: Device, payload: KeyValue, time: number): Promise<void> {
if (!this.throttlers[device.ieeeAddr]) {
this.throttlers[device.ieeeAddr] = {
publish: throttle(this.publishEntityState, time * 1000),
};
}
// Update state cache right away. This makes sure that during throttling cached state is always up to date.
// By updating cache we make sure that state cache is always up-to-date.
this.state.set(device, payload);
await this.throttlers[device.ieeeAddr].publish(device, payload, 'publishThrottle');
}
// if debounce_ignore are specified (Array of strings)
// then all newPayload values with key present in debounce_ignore
// should equal or be undefined in oldPayload
// otherwise payload is conflicted
isPayloadConflicted(newPayload: KeyValue, oldPayload: KeyValue, debounceIgnore: string[] | null): boolean {
isPayloadConflicted(newPayload: KeyValue, oldPayload: KeyValue, debounceIgnore: string[] | undefined): boolean {
let result = false;
Object.keys(oldPayload)
.filter((key) => (debounceIgnore || []).includes(key))
@@ -82,20 +101,12 @@ export default class Receive extends Extension {
return result;
}
shouldProcess(data: eventdata.DeviceMessage): boolean {
if (!data.device.definition || data.device.zh.interviewing) {
logger.debug(`Skipping message, still interviewing`);
return false;
}
return true;
}
@bind async onDeviceMessage(data: eventdata.DeviceMessage): Promise<void> {
/* istanbul ignore next */
/* v8 ignore next */
if (!data.device) return;
if (!this.shouldProcess(data)) {
if (!data.device.definition || data.device.zh.interviewing) {
logger.debug(`Skipping message, still interviewing`);
await utils.publishLastSeen({device: data.device, reason: 'messageEmitted'}, settings.get(), true, this.publishEntityState);
return;
}
@@ -122,6 +133,7 @@ export default class Receive extends Extension {
// - If NO payload is returned do nothing. This is for non-standard behaviour
// for e.g. click switches where we need to count number of clicks and detect long presses.
const publish = async (payload: KeyValue): Promise<void> => {
assert(data.device.definition);
const options: KeyValue = data.device.options;
zhc.postProcessConvertedFromZigbeeMessage(data.device.definition, payload, options);
@@ -134,20 +146,22 @@ export default class Receive extends Extension {
this.elapsed[data.device.ieeeAddr] = now;
}
// Check if we have to debounce
// Check if we have to debounce or throttle
if (data.device.options.debounce) {
this.publishDebounce(data.device, payload, data.device.options.debounce, data.device.options.debounce_ignore);
} else if (data.device.options.throttle) {
await this.publishThrottle(data.device, payload, data.device.options.throttle);
} else {
await this.publishEntityState(data.device, payload);
}
};
const deviceExposesChanged = (): void => {
this.eventBus.emitDevicesChanged();
this.eventBus.emitExposesChanged({device: data.device});
const meta = {
device: data.device.zh,
logger,
state: this.state.get(data.device),
deviceExposesChanged: (): void => this.eventBus.emitExposesAndDevicesChanged(data.device),
};
const meta = {device: data.device.zh, logger, state: this.state.get(data.device), deviceExposesChanged: deviceExposesChanged};
let payload: KeyValue = {};
for (const converter of converters) {
try {
@@ -157,10 +171,12 @@ export default class Receive extends Extension {
if (converted) {
payload = {...payload, ...converted};
}
} catch (error) /* istanbul ignore next */ {
logger.error(`Exception while calling fromZigbee converter: ${error.message}}`);
logger.debug(error.stack);
/* v8 ignore start */
} catch (error) {
logger.error(`Exception while calling fromZigbee converter: ${(error as Error).message}}`);
logger.debug((error as Error).stack!);
}
/* v8 ignore stop */
}
if (!utils.objectIsEmpty(payload)) {
+61 -29
View File
@@ -1,13 +1,22 @@
/* eslint-disable brace-style */
import {CustomClusters} from 'zigbee-herdsman/dist/zspec/zcl/definition/tstype';
import assert from 'node:assert';
import * as zhc from 'zigbee-herdsman-converters';
import {access, Numeric} from 'zigbee-herdsman-converters';
import {CustomClusters} from 'zigbee-herdsman/dist/zspec/zcl/definition/tstype';
import * as settings from '../util/settings';
const LINKQUALITY = new Numeric('linkquality', access.STATE)
.withUnit('lqi')
.withDescription('Link quality (signal strength)')
.withValueMin(0)
.withValueMax(255)
.withCategory('diagnostic');
export default class Device {
public zh: zh.Device;
public definition: zhc.Definition;
private _definitionModelID: string;
public definition?: zhc.Definition;
private _definitionModelID?: string;
get ieeeAddr(): string {
return this.zh.ieeeAddr;
@@ -15,34 +24,41 @@ export default class Device {
get ID(): string {
return this.zh.ieeeAddr;
}
get options(): DeviceOptions {
return {...settings.get().device_options, ...settings.getDevice(this.ieeeAddr)};
get options(): DeviceOptionsWithId {
const deviceOptions = settings.getDevice(this.ieeeAddr) ?? {friendly_name: this.ieeeAddr, ID: this.ieeeAddr};
return {...settings.get().device_options, ...deviceOptions};
}
get name(): string {
return this.zh.type === 'Coordinator' ? 'Coordinator' : this.options?.friendly_name || this.ieeeAddr;
return this.zh.type === 'Coordinator' ? 'Coordinator' : this.options?.friendly_name;
}
get isSupported(): boolean {
return this.zh.type === 'Coordinator' || (this.definition && !this.definition.generated);
return this.zh.type === 'Coordinator' || Boolean(this.definition && !this.definition.generated);
}
get customClusters(): CustomClusters {
return this.zh.customClusters;
}
get otaExtraMetas(): zhc.Ota.ExtraMetas {
return typeof this.definition?.ota === 'object' ? this.definition.ota : {};
}
constructor(device: zh.Device) {
this.zh = device;
}
exposes(): zhc.Expose[] {
/* istanbul ignore if */
const exposes: zhc.Expose[] = [];
assert(this.definition, 'Cannot retreive exposes before definition is resolved');
if (typeof this.definition.exposes == 'function') {
const options: KeyValue = this.options;
return this.definition.exposes(this.zh, options);
exposes.push(...this.definition.exposes(this.zh, options));
} else {
return this.definition.exposes;
exposes.push(...this.definition.exposes);
}
exposes.push(LINKQUALITY);
return exposes;
}
async resolveDefinition(ignoreCache = false): Promise<void> {
async resolveDefinition(ignoreCache: boolean = false): Promise<void> {
if (!this.zh.interviewing && (!this.definition || this._definitionModelID !== this.zh.modelID || ignoreCache)) {
this.definition = await zhc.findByDevice(this.zh, true);
this._definitionModelID = this.zh.modelID;
@@ -55,28 +71,39 @@ export default class Device {
}
}
endpoint(key?: string | number): zh.Endpoint {
let endpoint: zh.Endpoint;
if (key == null || key == '') key = 'default';
endpoint(key?: string | number): zh.Endpoint | undefined {
let endpoint: zh.Endpoint | undefined;
if (key == null || key == '') {
key = 'default';
}
if (!isNaN(Number(key))) {
endpoint = this.zh.getEndpoint(Number(key));
} else if (this.definition?.endpoint) {
const ID = this.definition?.endpoint?.(this.zh)[key];
if (ID) endpoint = this.zh.getEndpoint(ID);
else if (key === 'default') endpoint = this.zh.endpoints[0];
else return null;
if (ID) {
endpoint = this.zh.getEndpoint(ID);
} else if (key === 'default') {
endpoint = this.zh.endpoints[0];
} else {
return undefined;
}
} else {
/* istanbul ignore next */
if (key !== 'default') return null;
if (key !== 'default') {
return undefined;
}
endpoint = this.zh.endpoints[0];
}
return endpoint;
}
endpointName(endpoint: zh.Endpoint): string {
let epName = null;
endpointName(endpoint: zh.Endpoint): string | undefined {
let epName = undefined;
if (this.definition?.endpoint) {
const mapping = this.definition?.endpoint(this.zh);
for (const [name, id] of Object.entries(mapping)) {
@@ -85,22 +112,27 @@ export default class Device {
}
}
}
/* istanbul ignore next */
return epName === 'default' ? null : epName;
/* v8 ignore next */
return epName === 'default' ? undefined : epName;
}
getEndpointNames(): string[] {
return Object.keys(this.definition?.endpoint?.(this.zh) ?? {}).filter((name) => name !== 'default');
}
const names: string[] = [];
isIkeaTradfri(): boolean {
return this.zh.manufacturerID === 4476;
for (const name in this.definition?.endpoint?.(this.zh) ?? {}) {
if (name !== 'default') {
names.push(name);
}
}
return names;
}
isDevice(): this is Device {
return true;
}
/* istanbul ignore next */
isGroup(): this is Group {
return false;
}
+14 -8
View File
@@ -1,23 +1,23 @@
/* eslint-disable brace-style */
import * as zhc from 'zigbee-herdsman-converters';
import * as settings from '../util/settings';
export default class Group {
public zh: zh.Group;
private resolveDevice: (ieeeAddr: string) => Device;
private resolveDevice: (ieeeAddr: string) => Device | undefined;
get ID(): number {
return this.zh.groupID;
}
get options(): GroupOptions {
return {...settings.getGroup(this.ID)};
// XXX: Group always exists in settings
return {...settings.getGroup(this.ID)!};
}
get name(): string {
return this.options?.friendly_name || this.ID.toString();
}
constructor(group: zh.Group, resolveDevice: (ieeeAddr: string) => Device) {
constructor(group: zh.Group, resolveDevice: (ieeeAddr: string) => Device | undefined) {
this.zh = group;
this.resolveDevice = resolveDevice;
}
@@ -27,13 +27,19 @@ export default class Group {
}
membersDevices(): Device[] {
return this.zh.members.map((e) => this.resolveDevice(e.getDevice().ieeeAddr)).filter((d) => d);
return this.zh.members.map((d) => this.resolveDevice(d.getDevice().ieeeAddr)!);
}
membersDefinitions(): zhc.Definition[] {
return this.membersDevices()
.map((d) => d.definition)
.filter((d) => d);
const definitions: zhc.Definition[] = [];
for (const member of this.membersDevices()) {
if (member.definition) {
definitions.push(member.definition);
}
}
return definitions;
}
isDevice(): this is Device {
+82 -68
View File
@@ -1,8 +1,11 @@
import type {QoS} from 'mqtt-packet';
import type {IClientOptions, IClientPublishOptions, MqttClient} from 'mqtt';
import type {Zigbee2MQTTAPI} from './types/api';
import fs from 'node:fs';
import bind from 'bind-decorator';
import fs from 'fs';
import * as mqtt from 'mqtt';
import {connectAsync} from 'mqtt';
import logger from './util/logger';
import * as settings from './util/settings';
@@ -12,13 +15,12 @@ const NS = 'z2m:mqtt';
export default class MQTT {
private publishedTopics: Set<string> = new Set();
private connectionTimer: NodeJS.Timeout;
private client: mqtt.MqttClient;
private connectionTimer?: NodeJS.Timeout;
private client!: MqttClient;
private eventBus: EventBus;
private initialConnect = true;
private republishRetainedTimer: NodeJS.Timeout;
private republishRetainedTimer?: NodeJS.Timeout;
public retainedMessages: {
[s: string]: {payload: string; options: MQTTOptions; skipLog: boolean; skipReceive: boolean; topic: string; base: string};
[s: string]: {payload: string; options: IClientPublishOptions; skipLog: boolean; skipReceive: boolean; topic: string; base: string};
} = {};
constructor(eventBus: EventBus) {
@@ -30,13 +32,14 @@ export default class MQTT {
logger.info(`Connecting to MQTT server at ${mqttSettings.server}`);
const options: mqtt.IClientOptions = {
const options: IClientOptions = {
will: {
topic: `${settings.get().mqtt.base_topic}/bridge/state`,
payload: Buffer.from(utils.availabilityPayload('offline', settings.get())),
payload: Buffer.from(JSON.stringify({state: 'offline'})),
retain: settings.get().mqtt.force_disable_retain ? false : true,
qos: 1,
},
properties: {maximumPacketSize: mqttSettings.maximum_packet_size},
};
if (mqttSettings.version) {
@@ -64,6 +67,9 @@ export default class MQTT {
logger.debug(`Using MQTT login with username: ${mqttSettings.user}`);
options.username = mqttSettings.user;
options.password = mqttSettings.password;
} else if (mqttSettings.user) {
logger.debug(`Using MQTT login with username only: ${mqttSettings.user}`);
options.username = mqttSettings.user;
} else {
logger.debug(`Using MQTT anonymous login`);
}
@@ -73,100 +79,104 @@ export default class MQTT {
options.clientId = mqttSettings.client_id;
}
if (mqttSettings.hasOwnProperty('reject_unauthorized') && !mqttSettings.reject_unauthorized) {
if (mqttSettings.reject_unauthorized !== undefined && !mqttSettings.reject_unauthorized) {
logger.debug(`MQTT reject_unauthorized set false, ignoring certificate warnings.`);
options.rejectUnauthorized = false;
}
return new Promise((resolve, reject) => {
this.client = mqtt.connect(mqttSettings.server, options);
// @ts-ignore https://github.com/Koenkk/zigbee2mqtt/issues/9822
this.client.stream.setMaxListeners(0);
this.eventBus.onPublishAvailability(this, this.publishStateOnline);
this.client = await connectAsync(mqttSettings.server, options);
this.client.on('connect', async () => {
// Set timer at interval to check if connected to MQTT server.
clearTimeout(this.connectionTimer);
this.connectionTimer = setInterval(() => {
if (this.client.reconnecting) {
logger.error('Not connected to MQTT server!');
}
}, utils.seconds(10));
// https://github.com/Koenkk/zigbee2mqtt/issues/9822
this.client.stream.setMaxListeners(0);
logger.info('Connected to MQTT server');
await this.publishStateOnline();
if (!this.initialConnect) {
this.republishRetainedTimer = setTimeout(async () => {
// Republish retained messages in case MQTT broker does not persist them.
// https://github.com/Koenkk/zigbee2mqtt/issues/9629
for (const msg of Object.values(this.retainedMessages)) {
await this.publish(msg.topic, msg.payload, msg.options, msg.base, msg.skipLog, msg.skipReceive);
}
}, 2000);
}
this.initialConnect = false;
this.subscribe(`${settings.get().mqtt.base_topic}/#`);
resolve();
});
this.client.on('error', (err) => {
logger.error(`MQTT error: ${err.message}`);
reject(err);
});
this.client.on('message', this.onMessage);
this.client.on('error', (err) => {
logger.error(`MQTT error: ${err.message}`);
});
}
@bind async publishStateOnline(): Promise<void> {
await this.publish('bridge/state', utils.availabilityPayload('online', settings.get()), {retain: true, qos: 0});
if (mqttSettings.version != undefined && mqttSettings.version >= 5) {
this.client.on('disconnect', (packet) => {
logger.error(`MQTT disconnect: reason ${packet.reasonCode} (${packet.properties?.reasonString})`);
});
}
this.client.on('message', this.onMessage);
await this.onConnect();
this.client.on('connect', this.onConnect);
this.republishRetainedTimer = setTimeout(async () => {
// Republish retained messages in case MQTT broker does not persist them.
// https://github.com/Koenkk/zigbee2mqtt/issues/9629
for (const msg of Object.values(this.retainedMessages)) {
await this.publish(msg.topic, msg.payload, msg.options, msg.base, msg.skipLog, msg.skipReceive);
}
}, 2000);
// Set timer at interval to check if connected to MQTT server.
this.connectionTimer = setInterval(() => {
if (!this.isConnected()) {
logger.error('Not connected to MQTT server!');
}
}, utils.seconds(10));
}
async disconnect(): Promise<void> {
clearTimeout(this.connectionTimer);
await this.publish('bridge/state', utils.availabilityPayload('offline', settings.get()), {retain: true, qos: 0});
clearTimeout(this.republishRetainedTimer);
const stateData: Zigbee2MQTTAPI['bridge/state'] = {state: 'offline'};
await this.publish('bridge/state', JSON.stringify(stateData), {retain: true, qos: 0});
this.eventBus.removeListeners(this);
logger.info('Disconnecting from MQTT server');
this.client?.end();
await this.client?.endAsync();
}
subscribe(topic: string): void {
this.client.subscribe(topic);
async subscribe(topic: string): Promise<void> {
await this.client.subscribeAsync(topic);
}
unsubscribe(topic: string): void {
this.client.unsubscribe(topic);
async unsubscribe(topic: string): Promise<void> {
await this.client.unsubscribeAsync(topic);
}
@bind private async onConnect(): Promise<void> {
logger.info('Connected to MQTT server');
const stateData: Zigbee2MQTTAPI['bridge/state'] = {state: 'online'};
await this.publish('bridge/state', JSON.stringify(stateData), {retain: true, qos: 0});
await this.subscribe(`${settings.get().mqtt.base_topic}/#`);
}
@bind public onMessage(topic: string, message: Buffer): void {
// Since we subscribe to zigbee2mqtt/# we also receive the message we send ourselves, skip these.
if (!this.publishedTopics.has(topic)) {
logger.debug(`Received MQTT message on '${topic}' with data '${message.toString()}'`, NS);
logger.debug(() => `Received MQTT message on '${topic}' with data '${message.toString()}'`, NS);
this.eventBus.emitMQTTMessage({topic, message: message.toString()});
}
if (this.republishRetainedTimer && topic === `${settings.get().mqtt.base_topic}/bridge/info`) {
clearTimeout(this.republishRetainedTimer);
this.republishRetainedTimer = null;
this.republishRetainedTimer = undefined;
}
}
isConnected(): boolean {
return this.client && !this.client.reconnecting;
return this.client && !this.client.reconnecting && !this.client.disconnecting && !this.client.disconnected;
}
async publish(
topic: string,
payload: string,
options: MQTTOptions = {},
options: IClientPublishOptions = {},
base = settings.get().mqtt.base_topic,
skipLog = false,
skipReceive = true,
): Promise<void> {
const defaultOptions: {qos: QoS; retain: boolean} = {qos: 0, retain: false};
const defaultOptions = {qos: 0 as const, retain: false};
topic = `${base}/${topic}`;
if (skipReceive) {
@@ -184,7 +194,6 @@ export default class MQTT {
this.eventBus.emitMQTTMessagePublished({topic, payload, options: {...defaultOptions, ...options}});
if (!this.isConnected()) {
/* istanbul ignore else */
if (!skipLog) {
logger.error(`Not connected to MQTT server!`);
logger.error(`Cannot send message: topic: '${topic}', payload: '${payload}`);
@@ -194,17 +203,22 @@ export default class MQTT {
}
if (!skipLog) {
logger.info(`MQTT publish: topic '${topic}', payload '${payload}'`, NS);
logger.info(() => `MQTT publish: topic '${topic}', payload '${payload}'`, NS);
}
const actualOptions: mqtt.IClientPublishOptions = {...defaultOptions, ...options};
const actualOptions: IClientPublishOptions = {...defaultOptions, ...options};
if (settings.get().mqtt.force_disable_retain) {
actualOptions.retain = false;
}
return new Promise<void>((resolve) => {
this.client.publish(topic, payload, actualOptions, () => resolve());
});
try {
await this.client.publishAsync(topic, payload, actualOptions);
} catch (error) {
if (!skipLog) {
logger.error(`MQTT server error: ${(error as Error).message}`);
logger.error(`Could not send message: topic: '${topic}', payload: '${payload}`);
}
}
}
}
+9 -8
View File
@@ -1,4 +1,5 @@
import fs from 'fs';
import fs from 'node:fs';
import objectAssignDeep from 'object-assign-deep';
import data from './util/data';
@@ -14,7 +15,6 @@ const dontCacheProperties = [
'button',
'button_left',
'button_right',
'click',
'forgotten',
'keyerror',
'step_size',
@@ -28,12 +28,13 @@ const dontCacheProperties = [
'elapsed',
'from_side',
'to_side',
'illuminance_lux', // removed in z2m 2.0.0
];
class State {
private state: {[s: string | number]: KeyValue} = {};
private file = data.joinPath('state.json');
private timer: NodeJS.Timeout = null;
private timer?: NodeJS.Timeout;
constructor(
private readonly eventBus: EventBus,
@@ -66,7 +67,7 @@ class State {
this.state = JSON.parse(fs.readFileSync(this.file, 'utf8'));
logger.debug(`Loaded state from file ${this.file}`);
} catch (error) {
logger.debug(`Failed to load state from file ${this.file} (corrupt file?) (${error.message})`);
logger.debug(`Failed to load state from file ${this.file} (corrupt file?) (${(error as Error).message})`);
}
} else {
logger.debug(`Can't load state from file ${this.file} (doesn't exist)`);
@@ -79,8 +80,8 @@ class State {
const json = JSON.stringify(this.state, null, 4);
try {
fs.writeFileSync(this.file, json, 'utf8');
} catch (e) {
logger.error(`Failed to write state to '${this.file}' (${e.message})`);
} catch (error) {
logger.error(`Failed to write state to '${this.file}' (${error})`);
}
} else {
logger.debug(`Not saving state`);
@@ -88,14 +89,14 @@ class State {
}
exists(entity: Device | Group): boolean {
return this.state.hasOwnProperty(entity.ID);
return this.state[entity.ID] !== undefined;
}
get(entity: Group | Device): KeyValue {
return this.state[entity.ID] || {};
}
set(entity: Group | Device, update: KeyValue, reason: string = null): KeyValue {
set(entity: Group | Device, update: KeyValue, reason?: string): KeyValue {
const fromState = this.state[entity.ID] || {};
const toState = objectAssignDeep({}, fromState, update);
const newCache = {...toState};
+687
View File
@@ -0,0 +1,687 @@
import type * as zhc from 'zigbee-herdsman-converters';
import type {ClusterDefinition, ClusterName, CustomClusters} from 'zigbee-herdsman/dist/zspec/zcl/definition/tstype';
import type {LogLevel, schemaJson} from '../util/settings';
export interface Zigbee2MQTTScene {
id: number;
name: string;
}
interface Zigbee2MQTTDeviceEndpoint {
bindings: Zigbee2MQTTDeviceEndpointBinding[];
configured_reportings: Zigbee2MQTTDeviceEndpointConfiguredReporting[];
clusters: {input: string[]; output: string[]};
scenes: Zigbee2MQTTScene[];
}
interface Zigbee2MQTTDeviceEndpointBinding {
cluster: string;
target: Zigbee2MQTTDeviceEndpointBindingTarget;
}
interface Zigbee2MQTTDeviceEndpointBindingTarget {
type: string;
endpoint?: number;
ieee_address?: string;
id?: number;
}
interface Zigbee2MQTTDeviceEndpointConfiguredReporting {
cluster: string;
attribute: string | number;
minimum_report_interval: number;
maximum_report_interval: number;
reportable_change: number;
}
interface Zigbee2MQTTDeviceDefinition {
model: string;
vendor: string;
description: string;
exposes: zhc.Expose[];
supports_ota: boolean;
options: zhc.Option[];
icon: string;
}
export interface Zigbee2MQTTDevice {
ieee_address: zh.Device['ieeeAddr'];
type: zh.Device['type'];
network_address: zh.Device['networkAddress'];
supported: boolean;
friendly_name: string;
disabled: boolean;
description: string | undefined;
definition: Zigbee2MQTTDeviceDefinition | undefined;
power_source: zh.Device['powerSource'];
software_build_id: zh.Device['softwareBuildID'];
date_code: zh.Device['dateCode'];
model_id: zh.Device['modelID'];
interviewing: zh.Device['interviewing'];
interview_completed: zh.Device['interviewCompleted'];
manufacturer: zh.Device['manufacturerName'];
endpoints: Record<number, Zigbee2MQTTDeviceEndpoint>;
}
export interface Zigbee2MQTTGroupMember {
ieee_address: zh.Device['ieeeAddr'];
endpoint: number;
}
export interface Zigbee2MQTTGroup {
id: number;
friendly_name: 'default_bind_group' | string;
description: string | undefined;
scenes: Zigbee2MQTTScene[];
members: Zigbee2MQTTGroupMember[];
}
export interface Zigbee2MQTTNetworkMap {
nodes: {
ieeeAddr: string;
friendlyName: string;
type: string;
networkAddress: number;
manufacturerName: string | undefined;
modelID: string | undefined;
failed: string[];
lastSeen: number | undefined;
definition?: {model: string; vendor: string; supports: string; description: string};
}[];
links: {
source: {ieeeAddr: string; networkAddress: number};
target: {ieeeAddr: string; networkAddress: number};
linkquality: number;
depth: number;
routes: {
destinationAddress: number;
status: string;
nextHop: number;
}[];
sourceIeeeAddr: string;
targetIeeeAddr: string;
sourceNwkAddr: number;
lqi: number;
relationship: number;
}[];
}
/**
* Zigbee2MQTT state/request/response API endpoints
*/
export interface Zigbee2MQTTAPI {
'bridge/logging': {
message: string;
level: LogLevel;
namespace: string;
};
'bridge/state': {
state: 'online' | 'offline';
};
'bridge/definition': {
clusters: Readonly<Record<ClusterName, Readonly<ClusterDefinition>>>;
custom_clusters: Record<string, CustomClusters>;
};
'bridge/event':
| {
type: 'device_leave' | 'device_joined' | 'device_announce';
data: {
friendly_name: string;
ieee_address: string;
};
}
| {
type: 'device_interview';
data:
| {
friendly_name: string;
ieee_address: string;
status: 'started' | 'failed';
}
| {
friendly_name: string;
ieee_address: string;
status: 'successful';
supported: boolean;
definition: Zigbee2MQTTDeviceDefinition | undefined;
};
};
'bridge/info': {
version: string;
commit: string | undefined;
zigbee_herdsman_converters: {version: string};
zigbee_herdsman: {version: string};
coordinator: {
ieee_address: string;
type: string;
meta: {
[s: string]: number | string;
};
};
network: {
pan_id: number;
/** `0x${string}` 8-len */
extended_pan_id: string;
channel: number;
};
log_level: 'debug' | 'info' | 'warning' | 'error';
permit_join: boolean;
permit_join_end: number | undefined;
restart_required: boolean;
config: Settings;
config_schema: typeof schemaJson;
};
'bridge/devices': Zigbee2MQTTDevice[];
'bridge/groups': Zigbee2MQTTGroup[];
'bridge/request/permit_join':
| {
/** [0-254], 0 meaning disable */
time: number;
device?: string;
}
| `${number}`;
'bridge/response/permit_join': {
/** [0-254], 0 meaning disable */
time: number;
device?: string;
};
'bridge/request/health_check': '';
'bridge/response/health_check': {
/** XXX: currently always returns true */
healthy: boolean;
};
'bridge/request/coordinator_check': '';
'bridge/response/coordinator_check': {
missing_routers: {
ieee_address: string;
friendly_name: string;
}[];
};
'bridge/request/restart': '';
'bridge/response/restart': Record<string, never>;
'bridge/request/networkmap':
| {
type: 'raw' | 'graphviz' | 'plantuml';
routes: boolean;
}
| 'raw'
| 'graphviz'
| 'plantuml';
'bridge/response/networkmap':
| {
type: 'raw';
routes: boolean;
value: Zigbee2MQTTNetworkMap;
}
| {
type: 'graphviz' | 'plantuml';
routes: boolean;
value: string;
};
'bridge/request/extension/save': {
name: string;
code: string;
};
'bridge/response/extension/save': Record<string, never>;
'bridge/request/extension/remove': {
name: string;
};
'bridge/response/extension/remove': Record<string, never>;
'bridge/request/converter/save': {
name: string;
code: string;
};
'bridge/response/converter/save': Record<string, never>;
'bridge/request/converter/remove': {
name: string;
};
'bridge/response/converter/remove': Record<string, never>;
'bridge/request/backup': '';
'bridge/response/backup': {
/** base64 encoded ZIP archive */
zip: string;
};
'bridge/request/install_code/add': {
value: string;
};
'bridge/response/install_code/add': {
value: string;
};
/**
* Applied on-the-fly:
* - newSettings.homeassistant
* - newSettings.advanced?.log_level
* - newSettings.advanced?.log_namespaced_levels
* - newSettings.advanced?.log_debug_namespace_ignore
*/
'bridge/request/options': {
options: Record<string, unknown>;
};
'bridge/response/options': {
restart_required: boolean;
};
'bridge/request/device/bind': {
from: string;
from_endpoint: string | number | 'default';
to: string;
to_endpoint?: string | number;
clusters?: string[];
skip_disable_reporting?: boolean;
};
'bridge/response/device/bind': {
from: string;
from_endpoint: string | number;
to: string;
to_endpoint: string | number | undefined;
clusters: string[];
failed: string[];
};
'bridge/request/device/unbind': {
from: string;
from_endpoint: string | number | 'default';
to: string;
to_endpoint?: string | number;
clusters?: string[];
skip_disable_reporting?: boolean;
};
'bridge/response/device/unbind': {
from: string;
from_endpoint: string | number;
to: string;
to_endpoint: string | number | undefined;
clusters: string[];
failed: string[];
};
'bridge/request/device/configure':
| {
id: string | number;
}
| string;
'bridge/response/device/configure': {
id: string | number;
};
'bridge/request/device/remove': {
id: string;
block?: boolean;
force?: boolean;
};
'bridge/response/device/remove': {
id: string;
block: boolean;
force: boolean;
};
'bridge/request/device/ota_update/check': {
id: string;
};
'bridge/request/device/ota_update/check/downgrade': {
id: string;
};
'bridge/response/device/ota_update/check': {
id: string;
update_available: boolean;
};
'bridge/request/device/ota_update/update': {
id: string;
};
'bridge/request/device/ota_update/update/downgrade': {
id: string;
};
'bridge/response/device/ota_update/update': {
id: string;
from:
| {
software_build_id: string;
date_code: string;
}
| undefined;
to:
| {
software_build_id: string;
date_code: string;
}
| undefined;
};
'bridge/request/device/interview': {
id: string | number;
};
'bridge/response/device/interview': {
id: string | number;
};
'bridge/request/device/generate_external_definition': {
id: string | number;
};
'bridge/response/device/generate_external_definition': {
id: string | number;
source: string;
};
'bridge/request/device/options': {
id: string;
options: Record<string, unknown>;
};
'bridge/response/device/options': {
id: string;
from: Record<string, unknown>;
to: Record<string, unknown>;
restart_required: boolean;
};
'bridge/request/device/rename':
| {
last: true;
from?: string;
to: string;
homeassistant_rename?: boolean;
}
| {
last: false | undefined;
from: string;
to: string;
homeassistant_rename?: boolean;
};
'bridge/response/device/rename': {
from: string;
to: string;
homeassistant_rename: boolean;
};
'bridge/request/device/configure_reporting': {
id: string;
endpoint: string | number;
cluster: string | number;
attribute: string | number | {ID: number; type: number};
minimum_report_interval: number;
maximum_report_interval: number;
reportable_change: number;
option: Record<string, unknown>;
};
'bridge/response/device/configure_reporting': {
id: string;
endpoint: string | number;
cluster: string | number;
attribute: string | number | {ID: number; type: number};
minimum_report_interval: number;
maximum_report_interval: number;
reportable_change: number;
};
'bridge/request/group/remove': {
id: string;
force?: boolean;
};
'bridge/response/group/remove': {
id: string;
force: boolean;
};
'bridge/request/group/add': {
friendly_name: string;
id: string;
};
'bridge/response/group/add': {
friendly_name: string;
id: number;
};
'bridge/request/group/rename': {
from: string;
to: string;
homeassistant_rename?: boolean;
};
'bridge/response/group/rename': {
from: string;
to: string;
homeassistant_rename: boolean;
};
'bridge/request/group/options': {
id: string;
options: Record<string, unknown>;
};
'bridge/response/group/options': {
id: string;
from: Record<string, unknown>;
to: Record<string, unknown>;
restart_required: boolean;
};
'bridge/request/group/members/add': {
device: string;
group: string;
endpoint: string | number | 'default';
skip_disable_reporting?: boolean;
};
'bridge/response/group/members/add': {
device: string;
group: string;
endpoint: string | number | 'default';
};
'bridge/request/group/members/remove': {
device: string;
group: string;
endpoint: string | number | 'default';
skip_disable_reporting?: boolean;
};
'bridge/response/group/members/remove': {
device: string;
group: string;
endpoint: string | number | 'default';
};
'bridge/request/group/members/remove_all': {
device: string;
endpoint: string | number | 'default';
skip_disable_reporting?: boolean;
};
'bridge/response/group/members/remove_all': {
device: string;
endpoint: string | number | 'default';
};
'bridge/request/touchlink/factory_reset':
| {
ieee_address: string;
channel: number;
}
| '';
'bridge/response/touchlink/factory_reset':
| {
ieee_address: string;
channel: number;
}
| Record<string, never>;
'bridge/request/touchlink/scan': '';
'bridge/response/touchlink/scan': {
found: {
ieee_address: string;
channel: number;
}[];
};
'bridge/request/touchlink/identify': {
ieee_address: string;
channel: number;
};
'bridge/response/touchlink/identify': {
ieee_address: string;
channel: number;
};
/**
* entity state response
*/
'{friendlyName}': {
[key: string]: unknown;
};
'{friendlyName}/availability': {
state: 'online' | 'offline';
};
/** entity set request */
'{friendlyName}/set': {
[key: string]: unknown;
};
/** entity get request */
'{friendlyName}/get': {
[key: string]: unknown;
};
}
export type Zigbee2MQTTRequestEndpoints =
| 'bridge/request/permit_join'
| 'bridge/request/health_check'
| 'bridge/request/coordinator_check'
| 'bridge/request/restart'
| 'bridge/request/networkmap'
| 'bridge/request/extension/save'
| 'bridge/request/extension/remove'
| 'bridge/request/converter/save'
| 'bridge/request/converter/remove'
| 'bridge/request/backup'
| 'bridge/request/install_code/add'
| 'bridge/request/options'
| 'bridge/request/device/bind'
| 'bridge/request/device/unbind'
| 'bridge/request/device/configure'
| 'bridge/request/device/remove'
| 'bridge/request/device/ota_update/check'
| 'bridge/request/device/ota_update/check/downgrade'
| 'bridge/request/device/ota_update/update'
| 'bridge/request/device/ota_update/update/downgrade'
| 'bridge/request/device/interview'
| 'bridge/request/device/generate_external_definition'
| 'bridge/request/device/options'
| 'bridge/request/device/rename'
| 'bridge/request/device/configure_reporting'
| 'bridge/request/group/remove'
| 'bridge/request/group/add'
| 'bridge/request/group/rename'
| 'bridge/request/group/options'
| 'bridge/request/group/members/add'
| 'bridge/request/group/members/remove'
| 'bridge/request/group/members/remove_all'
| 'bridge/request/touchlink/factory_reset'
| 'bridge/request/touchlink/scan'
| 'bridge/request/touchlink/identify';
export type Zigbee2MQTTResponseEndpoints =
| 'bridge/response/permit_join'
| 'bridge/response/health_check'
| 'bridge/response/coordinator_check'
| 'bridge/response/restart'
| 'bridge/response/networkmap'
| 'bridge/response/extension/save'
| 'bridge/response/extension/remove'
| 'bridge/response/converter/save'
| 'bridge/response/converter/remove'
| 'bridge/response/backup'
| 'bridge/response/install_code/add'
| 'bridge/response/options'
| 'bridge/response/device/bind'
| 'bridge/response/device/unbind'
| 'bridge/response/device/configure'
| 'bridge/response/device/remove'
| 'bridge/response/device/ota_update/check'
| 'bridge/response/device/ota_update/check'
| 'bridge/response/device/ota_update/update'
| 'bridge/response/device/ota_update/update'
| 'bridge/response/device/interview'
| 'bridge/response/device/generate_external_definition'
| 'bridge/response/device/options'
| 'bridge/response/device/rename'
| 'bridge/response/device/configure_reporting'
| 'bridge/response/group/remove'
| 'bridge/response/group/add'
| 'bridge/response/group/rename'
| 'bridge/response/group/options'
| 'bridge/response/group/members/add'
| 'bridge/response/group/members/remove'
| 'bridge/response/group/members/remove_all'
| 'bridge/response/touchlink/factory_reset'
| 'bridge/response/touchlink/scan'
| 'bridge/response/touchlink/identify';
export type Zigbee2MQTTRequest<T extends Zigbee2MQTTRequestEndpoints> = {
transaction?: string;
} & Zigbee2MQTTAPI[T];
export type Zigbee2MQTTResponseOK<T extends Zigbee2MQTTResponseEndpoints> = {
status: 'ok';
data: Zigbee2MQTTAPI[T];
transaction?: string;
};
export type Zigbee2MQTTResponseError = {
status: 'error';
data: Record<string, never>;
error: string;
transaction?: string;
};
export type Zigbee2MQTTResponse<T extends Zigbee2MQTTResponseEndpoints> = Zigbee2MQTTResponseOK<T> | Zigbee2MQTTResponseError;
-3
View File
@@ -1,3 +0,0 @@
declare module 'mkdir-recursive' {
export function mkdirSync(path: string): void;
}
+51 -78
View File
@@ -1,25 +1,17 @@
/* eslint-disable camelcase */
import type TypeEventBus from 'lib/eventBus';
import type TypeExtension from 'lib/extension/extension';
import type TypeDevice from 'lib/model/device';
import type TypeGroup from 'lib/model/group';
import type TypeMQTT from 'lib/mqtt';
import type TypeState from 'lib/state';
import type TypeZigbee from 'lib/zigbee';
import type {QoS} from 'mqtt-packet';
import type {
NetworkParameters as ZHNetworkParameters,
CoordinatorVersion as ZHCoordinatorVersion,
LQI as ZHLQI,
RoutingTable as ZHRoutingTable,
RoutingTableEntry as ZHRoutingTableEntry,
} from 'zigbee-herdsman/dist/adapter/tstype';
import type * as ZHEvents from 'zigbee-herdsman/dist/controller/events';
import type {Device as ZHDevice, Group as ZHGroup, Endpoint as ZHEndpoint} from 'zigbee-herdsman/dist/controller/model';
import type {AdapterTypes as ZHAdapterTypes, Events as ZHEvents, Models as ZHModels} from 'zigbee-herdsman';
import type {Cluster as ZHCluster, FrameControl as ZHFrameControl} from 'zigbee-herdsman/dist/zspec/zcl/definition/tstype';
import type * as zhc from 'zigbee-herdsman-converters';
import {LogLevel} from 'lib/util/settings';
import type TypeEventBus from '../eventBus';
import type TypeExtension from '../extension/extension';
import type TypeDevice from '../model/device';
import type TypeGroup from '../model/group';
import type TypeMQTT from '../mqtt';
import type TypeState from '../state';
import type TypeZigbee from '../zigbee';
import {LogLevel} from '../util/settings';
type OptionalProps<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
declare global {
// Define some class types as global
@@ -32,21 +24,7 @@ declare global {
type Extension = TypeExtension;
// Types
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type ExternalDefinition = zhc.Definition & {homeassistant: any};
interface MQTTResponse {
data: KeyValue;
status: 'error' | 'ok';
error?: string;
transaction?: string;
}
interface MQTTOptions {
qos?: QoS;
retain?: boolean;
properties?: {messageExpiryInterval: number};
}
type Scene = {id: number; name: string};
type StateChangeReason = 'publishDebounce' | 'groupOptimistic' | 'lastSeenChanged' | 'publishCached';
type StateChangeReason = 'publishDebounce' | 'groupOptimistic' | 'lastSeenChanged' | 'publishCached' | 'publishThrottle';
type PublishEntityState = (entity: Device | Group, payload: KeyValue, stateChangeReason?: StateChangeReason) => Promise<void>;
type RecursivePartial<T> = {[P in keyof T]?: RecursivePartial<T[P]>};
interface KeyValue {
@@ -56,17 +34,15 @@ declare global {
// zigbee-herdsman
namespace zh {
type Endpoint = ZHEndpoint;
type Device = ZHDevice;
type Group = ZHGroup;
type LQI = ZHLQI;
type RoutingTable = ZHRoutingTable;
type RoutingTableEntry = ZHRoutingTableEntry;
type CoordinatorVersion = ZHCoordinatorVersion;
type NetworkParameters = ZHNetworkParameters;
type Cluster = ZHCluster;
type Endpoint = ZHModels.Endpoint;
type Device = ZHModels.Device;
type Group = ZHModels.Group;
type LQI = ZHAdapterTypes.LQI;
type RoutingTable = ZHAdapterTypes.RoutingTable;
type CoordinatorVersion = ZHAdapterTypes.CoordinatorVersion;
type NetworkParameters = ZHAdapterTypes.NetworkParameters;
interface Bind {
cluster: zh.Cluster;
cluster: ZHCluster;
target: zh.Endpoint | zh.Group;
}
}
@@ -80,7 +56,7 @@ declare global {
entity: Device | Group;
from: KeyValue;
to: KeyValue;
reason: string | null;
reason?: string;
update: KeyValue;
};
type PermitJoinChanged = ZHEvents.PermitJoinChangedPayload;
@@ -97,7 +73,7 @@ declare global {
type Reconfigure = {device: Device};
type DeviceLeave = {ieeeAddr: string; name: string};
type GroupMembersChanged = {group: Group; action: 'remove' | 'add' | 'remove_all'; endpoint: zh.Endpoint; skipDisableReporting: boolean};
type PublishEntityState = {entity: Group | Device; message: KeyValue; stateChangeReason: StateChangeReason; payload: KeyValue};
type PublishEntityState = {entity: Group | Device; message: KeyValue; stateChangeReason?: StateChangeReason; payload: KeyValue};
type DeviceMessage = {
type: ZHEvents.MessagePayloadType;
device: Device;
@@ -112,20 +88,20 @@ declare global {
}
// Settings
// eslint-disable camelcase
interface Settings {
homeassistant?: {
version?: number;
homeassistant: {
enabled: boolean;
discovery_topic: string;
status_topic: string;
legacy_entity_attributes: boolean;
legacy_triggers: boolean;
experimental_event_entities: boolean;
legacy_action_sensor: boolean;
};
permit_join?: boolean;
availability?: {
availability: {
enabled: boolean;
active: {timeout: number};
passive: {timeout: number};
};
external_converters: string[];
mqtt: {
base_topic: string;
include_device_information: boolean;
@@ -140,11 +116,12 @@ declare global {
cert?: string;
client_id?: string;
reject_unauthorized?: boolean;
maximum_packet_size: number;
};
serial: {
disable_led: boolean;
port?: string;
adapter?: 'deconz' | 'zstack' | 'ezsp' | 'zigate' | 'ember';
adapter?: 'deconz' | 'zstack' | 'ezsp' | 'zigate' | 'ember' | 'zboss' | 'zoh';
baudrate?: number;
rtscts?: boolean;
};
@@ -174,23 +151,26 @@ declare global {
update_check_interval: number;
disable_automatic_update_check: boolean;
zigbee_ota_override_index_location?: string;
ikea_ota_use_test_url?: boolean;
image_block_response_delay?: number;
default_maximum_data_size?: number;
};
frontend?: {
frontend: {
enabled: boolean;
auth_token?: string;
host?: string;
port?: number;
port: number;
base_url: string;
url?: string;
ssl_cert?: string;
ssl_key?: string;
notification_filter?: string[];
};
devices?: {[s: string]: DeviceOptions};
groups?: {[s: string]: GroupOptions};
devices: {[s: string]: DeviceOptions};
groups: {[s: string]: Omit<GroupOptions, 'ID'>};
device_options: KeyValue;
advanced: {
legacy_api: boolean;
legacy_availability_payload: boolean;
log_rotation: boolean;
log_console_json: boolean;
log_symlink_current: boolean;
log_output: ('console' | 'file' | 'syslog')[];
log_directory: string;
@@ -200,11 +180,12 @@ declare global {
log_syslog: KeyValue;
log_debug_to_mqtt_frontend: boolean;
log_debug_namespace_ignore: string;
log_directories_to_keep: number;
pan_id: number | 'GENERATE';
ext_pan_id: number[] | 'GENERATE';
channel: number;
adapter_concurrent: number | null;
adapter_delay: number | null;
adapter_concurrent?: number;
adapter_delay?: number;
cache_state: boolean;
cache_state_persistent: boolean;
cache_state_send_on_startup: boolean;
@@ -214,46 +195,38 @@ declare global {
timestamp_format: string;
output: 'json' | 'attribute' | 'attribute_and_json';
transmit_power?: number;
// Everything below is deprecated
availability_timeout?: number;
availability_blocklist?: string[];
availability_passlist?: string[];
availability_blacklist?: string[];
availability_whitelist?: string[];
soft_reset_timeout: number;
report: boolean;
};
}
interface DeviceOptions {
ID?: string;
disabled?: boolean;
retention?: number;
availability?: boolean | {timeout: number};
optimistic?: boolean;
retrieve_state?: boolean;
debounce?: number;
debounce_ignore?: string[];
throttle?: number;
filtered_attributes?: string[];
filtered_cache?: string[];
filtered_optimistic?: string[];
icon?: string;
homeassistant?: KeyValue;
legacy?: boolean;
friendly_name: string;
description?: string;
qos?: 0 | 1 | 2;
}
interface DeviceOptionsWithId extends DeviceOptions {
ID: string;
}
interface GroupOptions {
devices?: string[];
ID?: number;
ID: number;
optimistic?: boolean;
off_state?: 'all_members_off' | 'last_member_state';
filtered_attributes?: string[];
filtered_cache?: string[];
filtered_optimistic?: string[];
retrieve_state?: boolean;
homeassistant?: KeyValue;
friendly_name: string;
description?: string;
+14
View File
@@ -0,0 +1,14 @@
declare module 'unix-dgram' {
import {EventEmitter} from 'events';
import {Buffer} from 'buffer';
export class UnixDgramSocket extends EventEmitter {
send(buf: Buffer, callback?: (err?: Error) => void): void;
send(buf: Buffer, offset: number, length: number, path: string, callback?: (err?: Error) => void): void;
bind(path: string): void;
connect(remotePath: string): void;
close(): void;
}
export function createSocket(type: 'unix_dgram', listener?: (msg: Buffer) => void): UnixDgramSocket;
}
+11 -4
View File
@@ -2,8 +2,15 @@ declare module 'zigbee2mqtt-frontend' {
export function getPath(): string;
}
declare module 'connect-gzip-static' {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type RequestHandler = (req: any, res: any) => void;
export default function gzipStatic(root: string, options?: Record<string, unknown>): RequestHandler;
declare module 'http' {
interface IncomingMessage {
originalUrl?: string;
path?: string;
}
}
declare module 'express-static-gzip' {
import {IncomingMessage, ServerResponse} from 'node:http';
export type RequestHandler = (req: IncomingMessage, res: ServerResponse, finalhandler: (err: unknown) => void) => void;
export default function expressStaticGzip(root: string, options?: Record<string, unknown>): RequestHandler;
}
+7 -15
View File
@@ -1,17 +1,10 @@
import path from 'path';
import path from 'node:path';
let dataPath: string = null;
function load(): void {
if (process.env.ZIGBEE2MQTT_DATA) {
dataPath = process.env.ZIGBEE2MQTT_DATA;
} else {
dataPath = path.join(__dirname, '..', '..', 'data');
dataPath = path.normalize(dataPath);
}
function setPath(): string {
return process.env.ZIGBEE2MQTT_DATA ? process.env.ZIGBEE2MQTT_DATA : path.normalize(path.join(__dirname, '..', '..', 'data'));
}
load();
let dataPath = setPath();
function joinPath(file: string): string {
return path.resolve(dataPath, file);
@@ -21,9 +14,8 @@ function getPath(): string {
return dataPath;
}
// eslint-disable-next-line camelcase
function testingOnlyReload(): void {
load();
function _testReload(): void {
dataPath = setPath();
}
export default {joinPath, getPath, testingOnlyReload};
export default {joinPath, getPath, _testReload};
+56 -44
View File
@@ -1,8 +1,8 @@
import assert from 'assert';
import fs from 'fs';
import fx from 'mkdir-recursive';
import assert from 'node:assert';
import fs from 'node:fs';
import path from 'node:path';
import moment from 'moment';
import path from 'path';
import {rimrafSync} from 'rimraf';
import winston from 'winston';
@@ -11,13 +11,20 @@ import * as settings from './settings';
const NAMESPACE_SEPARATOR = ':';
class Logger {
// @ts-expect-error initalized in `init`
private level: settings.LogLevel;
// @ts-expect-error initalized in `init`
private output: string[];
// @ts-expect-error initalized in `init`
private directory: string;
// @ts-expect-error initalized in `init`
private logger: winston.Logger;
// @ts-expect-error initalized in `init`
private fileTransport: winston.transports.FileTransportInstance;
private debugNamespaceIgnoreRegex?: RegExp;
// @ts-expect-error initalized in `init`
private namespacedLevels: Record<string, settings.LogLevel>;
// @ts-expect-error initalized in `init`
private cachedNamespacedLevels: Record<string, settings.LogLevel>;
public init(): void {
@@ -49,15 +56,15 @@ class Logger {
this.logger.add(
new winston.transports.Console({
silent: consoleSilenced,
// winston.config.syslog.levels sets 'warning' as 'red'
format: winston.format.combine(
winston.format.colorize({colors: {debug: 'blue', info: 'green', warning: 'yellow', error: 'red'}}),
winston.format.printf(
/* istanbul ignore next */ (info) => {
return `[${info.timestamp}] ${info.level}: \t${info.message}`;
},
),
),
format: settings.get().advanced.log_console_json
? winston.format.json()
: winston.format.combine(
// winston.config.syslog.levels sets 'warning' as 'red'
winston.format.colorize({colors: {debug: 'blue', info: 'green', warning: 'yellow', error: 'red'}}),
winston.format.printf((info) => {
return `[${info.timestamp}] ${info.level}: \t${info.message}`;
}),
),
}),
);
@@ -65,15 +72,18 @@ class Logger {
logging += `, file (filename: ${logFilename})`;
// Make sure that log directory exists when not logging to stdout only
fx.mkdirSync(this.directory);
fs.mkdirSync(this.directory, {recursive: true});
if (settings.get().advanced.log_symlink_current) {
const current = settings.get().advanced.log_directory.replace('%TIMESTAMP%', 'current');
const actual = './' + timestamp;
/* istanbul ignore next */
/* v8 ignore start */
if (fs.existsSync(current)) {
fs.unlinkSync(current);
}
/* v8 ignore stop */
fs.symlinkSync(actual, current);
}
@@ -81,11 +91,9 @@ class Logger {
// NOTE: the initiation of the logger even when not added as transport tries to create the logging directory
const transportFileOptions: winston.transports.FileTransportOptions = {
filename: path.join(this.directory, logFilename),
format: winston.format.printf(
/* istanbul ignore next */ (info) => {
return `[${info.timestamp}] ${info.level}: \t${info.message}`;
},
),
format: winston.format.printf((info) => {
return `[${info.timestamp}] ${info.level}: \t${info.message}`;
}),
};
if (settings.get().advanced.log_rotation) {
@@ -96,27 +104,29 @@ class Logger {
this.fileTransport = new winston.transports.File(transportFileOptions);
this.logger.add(this.fileTransport);
this.cleanup();
}
/* istanbul ignore next */
/* v8 ignore start */
if (this.output.includes('syslog')) {
logging += `, syslog`;
// eslint-disable-next-line
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-unused-expressions
require('winston-syslog').Syslog;
const options: KeyValue = {
app_name: 'Zigbee2MQTT',
format: winston.format.printf((info) => info.message),
format: winston.format.printf((info) => info.message as string),
...settings.get().advanced.log_syslog,
};
if (options.hasOwnProperty('type')) {
if (options['type'] !== undefined) {
options.type = options.type.toString();
}
// @ts-expect-error untyped transport
this.logger.add(new winston.transports.Syslog(options));
}
/* v8 ignore stop */
this.setDebugNamespaceIgnore(settings.get().advanced.log_debug_namespace_ignore);
@@ -182,36 +192,37 @@ class Logger {
return this.cachedNamespacedLevels[namespace];
}
private log(level: settings.LogLevel, message: string, namespace: string): void {
private log(level: settings.LogLevel, messageOrLambda: string | (() => string), namespace: string): void {
const nsLevel = this.cacheNamespacedLevel(namespace);
if (settings.LOG_LEVELS.indexOf(level) <= settings.LOG_LEVELS.indexOf(nsLevel)) {
const message: string = messageOrLambda instanceof Function ? messageOrLambda() : messageOrLambda;
this.logger.log(level, `${namespace}: ${message}`);
}
}
public error(message: string, namespace: string = 'z2m'): void {
this.log('error', message, namespace);
public error(messageOrLambda: string | (() => string), namespace: string = 'z2m'): void {
this.log('error', messageOrLambda, namespace);
}
public warning(message: string, namespace: string = 'z2m'): void {
this.log('warning', message, namespace);
public warning(messageOrLambda: string | (() => string), namespace: string = 'z2m'): void {
this.log('warning', messageOrLambda, namespace);
}
public info(message: string, namespace: string = 'z2m'): void {
this.log('info', message, namespace);
public info(messageOrLambda: string | (() => string), namespace: string = 'z2m'): void {
this.log('info', messageOrLambda, namespace);
}
public debug(message: string, namespace: string = 'z2m'): void {
public debug(messageOrLambda: string | (() => string), namespace: string = 'z2m'): void {
if (this.debugNamespaceIgnoreRegex?.test(namespace)) {
return;
}
this.log('debug', message, namespace);
this.log('debug', messageOrLambda, namespace);
}
// Cleanup any old log directory.
public cleanup(): void {
private cleanup(): void {
if (settings.get().advanced.log_directory.includes('%TIMESTAMP%')) {
const rootDirectory = path.join(this.directory, '..');
@@ -221,7 +232,7 @@ class Logger {
});
directories.sort((a: KeyValue, b: KeyValue) => b.birth - a.birth);
directories = directories.slice(10, directories.length);
directories = directories.slice(settings.get().advanced.log_directories_to_keep, directories.length);
directories.forEach((dir) => {
this.debug(`Removing old log directory '${dir.path}'`);
rimrafSync(dir.path);
@@ -231,14 +242,13 @@ class Logger {
// Workaround for https://github.com/winstonjs/winston/issues/1629.
// https://github.com/Koenkk/zigbee2mqtt/pull/10905
/* istanbul ignore next */
/* v8 ignore start */
public async end(): Promise<void> {
this.logger.end();
await new Promise<void>((resolve) => {
if (!this.fileTransport) {
process.nextTick(resolve);
} else {
// Only flush the file transport, don't end logger itself as log() might still be called
// causing a UnhandledPromiseRejection (`Error: write after end`). Flushing the file transport
// ensures the log files are written before stopping.
if (this.fileTransport) {
await new Promise<void>((resolve) => {
// @ts-expect-error workaround
if (this.fileTransport._dest) {
// @ts-expect-error workaround
@@ -247,9 +257,11 @@ class Logger {
// @ts-expect-error workaround
this.fileTransport.on('open', () => this.fileTransport._dest.on('finish', resolve));
}
}
});
this.fileTransport.end();
});
}
}
/* v8 ignore stop */
}
export default new Logger();
+579
View File
@@ -0,0 +1,579 @@
import {existsSync, mkdirSync} from 'node:fs';
import {createServer} from 'node:http';
import {parse} from 'node:querystring';
import {findAllDevices} from 'zigbee-herdsman/dist/adapter/adapterDiscovery';
import data from './data';
import * as settings from './settings';
type OnboardSettings = {
mqtt_base_topic?: string;
mqtt_server?: string;
mqtt_user?: string;
mqtt_password?: string;
serial_port?: string;
serial_adapter?: Settings['serial']['adapter'];
serial_baudrate?: string;
serial_rtscts?: 'on';
network_channel?: string;
network_key?: string;
network_pan_id?: string;
network_ext_pan_id?: string;
frontend_enabled?: 'on';
frontend_port?: string;
homeassistant_enabled?: 'on';
log_level?: Settings['advanced']['log_level'];
};
function escapeHtml(s: string): string {
return s.replace(/[^0-9A-Za-z \-_.]/g, (c) => `&#${c.charCodeAt(0)};`);
}
function generateHtmlDone(frontendUrl: string | undefined): string {
return `
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Zigbee2MQTT Onboarding</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.classless.min.css">
</head>
<body>
<main>
<h1>Zigbee2MQTT Onboarding</h1>
<p>Settings saved.</p>
<p>Zigbee2MQTT is now starting...</p>
<small>${frontendUrl ? `Redirecting to Zigbee2MQTT frontend at <a href="${frontendUrl}">${frontendUrl}</a> in 30 seconds.` : 'You can close this page.'}</small>
</main>
${frontendUrl ? `<script>setTimeout(() => { window.location.replace("${frontendUrl}"); }, 30000);</script>` : ''}
</body>
</html>
`;
}
function generateHtmlForm(currentSettings: RecursivePartial<Settings>, devices: Awaited<ReturnType<typeof findAllDevices>>): string {
let devicesSelect = '';
if (devices.length > 0) {
devicesSelect += '<select id="found_device" onchange="setFoundDevice(this)">';
devicesSelect += '<option value="">Select a device</option>';
for (const device of devices) {
// just in case name has commas, remove them to not mess with `split` logic
const deviceStr = `${device.name.replaceAll(',', '')}, ${device.path}, ${device.adapter ?? 'unknown'}`;
devicesSelect += `<option value="${deviceStr}">${deviceStr}</option>`;
}
devicesSelect += '</select>';
devicesSelect += '<small>Optionally allows to configure coordinator port and type (if known) automatically.</small>';
} else {
devicesSelect = '<small>No device found</small>';
}
let generateCheckbox = '';
if (
Array.isArray(currentSettings.advanced?.network_key) ||
typeof currentSettings.advanced?.pan_id === 'number' ||
Array.isArray(currentSettings.advanced?.ext_pan_id)
) {
generateCheckbox = `
<label for="generate_network">
<input
type="checkbox"
id="generate_network"
onclick="setGenerate(this)"
${process.env.ZIGBEE2MQTT_CONFIG_ADVANCED_NETWORK_KEY || process.env.ZIGBEE2MQTT_CONFIG_ADVANCED_PAN_ID || process.env.ZIGBEE2MQTT_CONFIG_ADVANCED_EXT_PAN_ID ? 'disabled' : ''}>
Generate network?
</label>
`;
}
/* v8 ignore start */
return `
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Zigbee2MQTT Onboarding</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.classless.min.css">
</head>
<body>
<main>
<h1>Zigbee2MQTT Onboarding</h1>
<p>Set the base configuration to start Zigbee2MQTT.</p>
<p>Optional fields will either be ignored or fallback to defaults if not set (see appropriate documentation page for more details).</p>
<p>If a field is disabled, it means <a href="https://www.zigbee2mqtt.io/guide/configuration/#environment-variables" target="_blank">environment variables</a> are being used to override specific values (for example, through the Home Assistant add-on configuration page).</p>
<hr>
<form method="post">
<fieldset ${process.env.ZIGBEE2MQTT_CONFIG_SERIAL || process.env.ZIGBEE2MQTT_CONFIG_SERIAL_PORT || process.env.ZIGBEE2MQTT_CONFIG_SERIAL_ADAPTER ? 'disabled' : ''}>
<label for="found_device">Found Devices</label>
${devicesSelect}
</fieldset>
<fieldset ${process.env.ZIGBEE2MQTT_CONFIG_SERIAL ? 'disabled' : ''}>
<label for="serial_port">Coordinator/Adapter Port/Path</label>
<input
type="text"
id="serial_port"
name="serial_port"
value="${currentSettings.serial?.port ?? ''}"
required
${process.env.ZIGBEE2MQTT_CONFIG_SERIAL_PORT ? 'disabled' : ''}>
<label for="serial_adapter">Coordinator/Adapter Type/Stack/Driver</label>
<select id="serial_adapter" name="serial_adapter" required ${process.env.ZIGBEE2MQTT_CONFIG_SERIAL_ADAPTER ? 'disabled' : ''}>
<option value="zstack" ${currentSettings.serial?.adapter === 'zstack' ? 'selected' : ''}>zstack</option>
<option value="ember" ${currentSettings.serial?.adapter === 'ember' ? 'selected' : ''}>ember</option>
<option value="deconz" ${currentSettings.serial?.adapter === 'deconz' ? 'selected' : ''}>deconz</option>
<option value="zigate" ${currentSettings.serial?.adapter === 'zigate' ? 'selected' : ''}>zigate</option>
<option value="zboss" ${currentSettings.serial?.adapter === 'zboss' ? 'selected' : ''}>zboss</option>
</select>
<label for="serial_baudrate">Coordinator/Adapter Baudrate</label>
<select id="serial_baudrate" name="serial_baudrate" ${process.env.ZIGBEE2MQTT_CONFIG_SERIAL_BAUDRATE ? 'disabled' : ''}>
<option value="38400" ${currentSettings.serial?.baudrate === 38400 ? 'selected' : ''}>38400</option>
<option value="57600" ${currentSettings.serial?.baudrate === 57600 ? 'selected' : ''}>57600</option>
<option value="115200" ${!currentSettings.serial?.baudrate || currentSettings.serial?.baudrate === 115200 ? 'selected' : ''}>115200</option>
<option value="230400" ${currentSettings.serial?.baudrate === 230400 ? 'selected' : ''}>230400</option>
<option value="460800" ${currentSettings.serial?.baudrate === 460800 ? 'selected' : ''}>460800</option>
<option value="921600" ${currentSettings.serial?.baudrate === 921600 ? 'selected' : ''}>921600</option>
</select>
<small>Can be ignored for networked coordinators (TCP).</small>
<label for="serial_rtscts">Coordinator/Adapter Hardware Flow Control ("rtscts: true")</label>
<input
type="checkbox"
id="serial_rtscts"
name="serial_rtscts"
${currentSettings.serial?.rtscts ? 'checked' : ''}
style="margin-bottom: 1rem;">
<small>Can be ignored for networked coordinators (TCP).</small>
</fieldset>
<small>
<a href="https://www.zigbee2mqtt.io/guide/configuration/adapter-settings.html" target="_blank">https://www.zigbee2mqtt.io/guide/configuration/adapter-settings.html</a>
</small>
<hr>
<fieldset ${process.env.ZIGBEE2MQTT_CONFIG_ADVANCED ? 'disabled' : ''}>
<label for="closest_wifi_channel">Closest WiFi Channel</label>
<input
type="number"
min="0"
max="14"
id="closest_wifi_channel"
value="0"
onclick="setBestZigbeeChannel(this)"
${process.env.ZIGBEE2MQTT_CONFIG_ADVANCED_CHANNEL ? 'disabled' : ''}>
<small>Optionally set to your closest WiFi channel to pick the best value for "Network channel" below.</small>
<label for="network_channel">Network Channel</label>
<input
type="number"
min="11"
max="26"
id="network_channel"
name="network_channel"
value="${currentSettings.advanced?.channel ?? '25'}"
required
${process.env.ZIGBEE2MQTT_CONFIG_ADVANCED_CHANNEL ? 'disabled' : ''}>
</fieldset>
<fieldset ${process.env.ZIGBEE2MQTT_CONFIG_ADVANCED ? 'disabled' : ''}>
${generateCheckbox}
<label for="network_key">Network Key</label>
<input
type="text"
id="network_key"
name="network_key"
value="${currentSettings.advanced?.network_key ?? 'GENERATE'}"
pattern="^([0-9]+(,[0-9]+){15})|GENERATE$"
required
${process.env.ZIGBEE2MQTT_CONFIG_ADVANCED_NETWORK_KEY ? 'disabled' : ''}>
<label for="network_pan_id">Network PAN ID</label>
<input
type="text"
id="network_pan_id"
name="network_pan_id"
value="${currentSettings.advanced?.pan_id ?? 'GENERATE'}"
pattern="^([0-9]{1,5})|GENERATE$"
required
${process.env.ZIGBEE2MQTT_CONFIG_ADVANCED_PAN_ID ? 'disabled' : ''}>
<label for="network_ext_pan_id">Network Extended PAN ID</label>
<input
type="text"
id="network_ext_pan_id"
name="network_ext_pan_id"
value="${currentSettings.advanced?.ext_pan_id ?? 'GENERATE'}"
pattern="^([0-9]+(,[0-9]+){7})|GENERATE$"
required
${process.env.ZIGBEE2MQTT_CONFIG_ADVANCED_EXT_PAN_ID ? 'disabled' : ''}>
</fieldset>
<small>
<a href="https://www.zigbee2mqtt.io/guide/configuration/zigbee-network.html" target="_blank">https://www.zigbee2mqtt.io/guide/configuration/zigbee-network.html</a>
</small>
<hr>
<fieldset ${process.env.ZIGBEE2MQTT_CONFIG_MQTT ? 'disabled' : ''}>
<label for="mqtt_base_topic">MQTT Base Topic</label>
<input
type="text"
id="mqtt_base_topic"
name="mqtt_base_topic"
value="${currentSettings.mqtt?.base_topic ?? 'zigbee2mqtt'}"
required
${process.env.ZIGBEE2MQTT_CONFIG_MQTT_BASE_TOPIC ? 'disabled' : ''}>
<label for="mqtt_server">MQTT Server</label>
<input
type="text"
id="mqtt_server"
name="mqtt_server"
value="${currentSettings.mqtt?.server ?? 'mqtt://localhost:1883'}"
required
${process.env.ZIGBEE2MQTT_CONFIG_MQTT_SERVER ? 'disabled' : ''}>
<label for="mqtt_user">MQTT User</label>
<input
type="text"
id="mqtt_user"
name="mqtt_user"
value="${currentSettings.mqtt?.user ?? ''}"
${process.env.ZIGBEE2MQTT_CONFIG_MQTT_USER ? 'disabled' : ''}>
<small>Optional. Set only if using authentication.</small>
<label for="mqtt_password">MQTT Password</label>
<input
type="password"
id="mqtt_password"
name="mqtt_password"
value="${currentSettings.mqtt?.password ?? ''}"
${process.env.ZIGBEE2MQTT_CONFIG_MQTT_PASSWORD ? 'disabled' : ''}>
<small>Optional. Set only if using authentication.</small>
</fieldset>
<small>
<a href="https://www.zigbee2mqtt.io/guide/configuration/mqtt.html" target="_blank">https://www.zigbee2mqtt.io/guide/configuration/mqtt.html</a>
</small>
<hr>
<fieldset ${process.env.ZIGBEE2MQTT_CONFIG_FRONTEND ? 'disabled' : ''}>
<label for="frontend_enabled">
<input
type="checkbox"
id="frontend_enabled"
name="frontend_enabled"
${currentSettings.frontend?.enabled ? 'checked' : ''}
${process.env.ZIGBEE2MQTT_CONFIG_FRONTEND_ENABLED ? 'disabled' : ''}>
Frontend enabled?
</label>
<label for="frontend_port">Frontend Port</label>
<input
type="number"
min="0"
max="65535"
id="frontend_port"
name="frontend_port"
value="${currentSettings.frontend?.port ?? '8080'}"
required
${process.env.ZIGBEE2MQTT_CONFIG_FRONTEND_PORT ? 'disabled' : ''}>
</fieldset>
<small>
<a href="https://www.zigbee2mqtt.io/guide/configuration/frontend.html" target="_blank">https://www.zigbee2mqtt.io/guide/configuration/frontend.html</a>
</small>
<fieldset ${process.env.ZIGBEE2MQTT_CONFIG_HOMEASSISTANT ? 'disabled' : ''}>
<label for="homeassistant_enabled" ${process.env.ZIGBEE2MQTT_CONFIG_HOMEASSISTANT_ENABLED ? 'disabled' : ''}>
<input type="checkbox" id="homeassistant_enabled" name="homeassistant_enabled" ${currentSettings.homeassistant?.enabled ? 'checked' : ''}>
Home Assistant enabled?
</label>
</fieldset>
<small>
<a href="https://www.zigbee2mqtt.io/guide/configuration/homeassistant.html" target="_blank">https://www.zigbee2mqtt.io/guide/configuration/homeassistant.html</a>
</small>
<hr>
<fieldset ${process.env.ZIGBEE2MQTT_CONFIG_ADVANCED ? 'disabled' : ''}>
<label for="log_level">Log Level</label>
<select id="log_level" name="log_level" ${process.env.ZIGBEE2MQTT_CONFIG_ADVANCED_LOG_LEVEL ? 'disabled' : ''}>
<option value="error" ${currentSettings.advanced?.log_level === 'error' ? 'selected' : ''}>error</option>
<option value="warning" ${currentSettings.advanced?.log_level === 'warning' ? 'selected' : ''}>warning</option>
<option value="info" ${!currentSettings.advanced?.log_level || currentSettings.advanced?.log_level === 'info' ? 'selected' : ''}>info</option>
<option value="debug" ${currentSettings.advanced?.log_level === 'debug' ? 'selected' : ''}>debug</option>
</select>
</fieldset>
<small>
<a href="https://www.zigbee2mqtt.io/guide/configuration/logging.html" target="_blank">https://www.zigbee2mqtt.io/guide/configuration/logging.html</a>
</small>
<hr>
<input type="submit" value="Submit">
</form>
</main>
<script>
function setFoundDevice(e) {
if (!e.value) {
return;
}
const [, path, adapter] = e.value.split(", ");
const serialPortEl = document.querySelector("#serial_port");
serialPortEl.value = path;
const serialAdapterEl = document.querySelector("#serial_adapter");
if (['zstack', 'ember', 'deconz', 'zigate', 'zboss'].includes(adapter)) {
serialAdapterEl.value = adapter;
} else {
serialAdapterEl.value = '';
}
}
function setBestZigbeeChannel(e) {
const wifiChannel = parseInt(e.value, 10);
const networkChannelEl = document.querySelector("#network_channel");
if (wifiChannel >= 11) {
// WiFi 11-14
networkChannelEl.value = 15;
} else if (wifiChannel >= 6) {
// WiFi 6-10
networkChannelEl.value = 11;
} else {
// WiFi 1-5
networkChannelEl.value = 25;
}
}
function setGenerate(e) {
document.querySelector("#network_key").value = e.checked ? "GENERATE" : "${currentSettings.advanced?.network_key ?? 'GENERATE'}";
document.querySelector("#network_pan_id").value = e.checked ? "GENERATE" : "${currentSettings.advanced?.pan_id ?? 'GENERATE'}";
document.querySelector("#network_ext_pan_id").value = e.checked ? "GENERATE" : "${currentSettings.advanced?.ext_pan_id ?? 'GENERATE'}";
}
</script>
</body>
</html>
`;
/* v8 ignore stop */
}
function generateHtmlError(errors: string): string {
return `
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Zigbee2MQTT Onboarding</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.classless.min.css">
</head>
<body>
<main>
<h1>Zigbee2MQTT configuration is not valid</h1>
<p style="color: #F00;">Found the following errors:</p>
${errors}
<hr>
<p>If you don't know how to solve this, read <a href="https://www.zigbee2mqtt.io/guide/configuration" target="_blank">https://www.zigbee2mqtt.io/guide/configuration</a></p>
<form method="post" action="/">
<input type="submit" value="Close">
</form>
</main>
</body>
</html>
`;
}
function getServerUrl(): URL {
return new URL(process.env.Z2M_ONBOARD_URL ?? 'http://0.0.0.0:8080');
}
async function startOnboardingServer(): Promise<boolean> {
const currentSettings = settings.get();
const serverUrl = getServerUrl();
let server: ReturnType<typeof createServer> | undefined;
let failed = false;
const success = await new Promise<boolean>((resolve) => {
server = createServer(async (req, res) => {
if (req.method === 'POST') {
if (failed) {
res.end(() => {
resolve(false);
});
} else {
let body = '';
req.on('data', (chunk) => {
body += chunk;
});
req.on('end', () => {
const result = parse(body) as unknown as OnboardSettings;
console.log(JSON.stringify(currentSettings));
console.log(JSON.stringify(result));
const frontendEnabled = result.frontend_enabled === 'on';
const updatedSettings: RecursivePartial<Settings> = {
mqtt: {
base_topic: result.mqtt_base_topic,
server: result.mqtt_server,
user: result.mqtt_user || undefined, // empty string => removed
password: result.mqtt_password || undefined, // empty string => removed
},
serial: {
port: result.serial_port,
adapter: result.serial_adapter,
baudrate: result.serial_baudrate ? Number.parseInt(result.serial_baudrate, 10) : undefined,
rtscts: result.serial_rtscts === 'on',
},
advanced: {
log_level: result.log_level,
channel: result.network_channel ? Number.parseInt(result.network_channel, 10) : undefined,
network_key: result.network_key
? result.network_key === 'GENERATE'
? result.network_key
: result.network_key.split(',').map((v) => Number.parseInt(v, 10))
: undefined,
pan_id: result.network_pan_id
? result.network_pan_id === 'GENERATE'
? result.network_pan_id
: Number.parseInt(result.network_pan_id, 10)
: undefined,
ext_pan_id: result.network_ext_pan_id
? result.network_ext_pan_id === 'GENERATE'
? result.network_ext_pan_id
: result.network_ext_pan_id.split(',').map((v) => Number.parseInt(v, 10))
: undefined,
},
frontend: {
enabled: frontendEnabled,
port: result.frontend_port ? Number.parseInt(result.frontend_port, 10) : undefined,
},
homeassistant: {
enabled: result.homeassistant_enabled === 'on',
},
};
try {
settings.apply(updatedSettings);
// to redirect, make sure frontend "will be" enabled, and host isn't socket
const redirect =
!process.env.Z2M_ONBOARD_NO_REDIRECT &&
frontendEnabled &&
(!currentSettings.frontend?.host || !currentSettings.frontend.host.startsWith('/'));
const protocol = currentSettings.frontend?.ssl_cert && currentSettings.frontend.ssl_key ? 'https' : 'http';
res.setHeader('Content-Type', 'text/html');
res.writeHead(200);
res.end(
generateHtmlDone(
redirect
? /* v8 ignore next */ `${protocol}://${currentSettings.frontend?.host ?? 'localhost'}:${currentSettings.frontend?.port ?? '8080'}${currentSettings.frontend?.base_url ?? '/'}`
: undefined,
),
() => {
resolve(true);
},
);
} catch (error) {
console.error(`Failed to apply configuration: ${(error as Error).message}`);
failed = true;
if (process.env.Z2M_ONBOARD_NO_FAILURE_PAGE) {
res.end(() => {
resolve(false);
});
} else {
res.setHeader('Content-Type', 'text/html');
res.writeHead(406);
res.end(generateHtmlError(`<p>${escapeHtml((error as Error).message)}</p>`));
}
}
});
}
} else {
res.setHeader('Content-Type', 'text/html');
res.writeHead(200);
res.end(generateHtmlForm(currentSettings, await findAllDevices()));
}
});
server.listen(parseInt(serverUrl.port), serverUrl.hostname, () => {
console.log(`Onboarding page is available at ${serverUrl.href}`);
});
});
await new Promise((resolve) => server?.close(resolve));
return success;
}
async function startFailureServer(errors: string): Promise<void> {
const serverUrl = getServerUrl();
let server: ReturnType<typeof createServer> | undefined;
await new Promise<void>((resolve) => {
server = createServer(async (req, res) => {
if (req.method === 'POST') {
res.end(() => {
resolve();
});
} else {
res.setHeader('Content-Type', 'text/html');
res.writeHead(406);
res.end(generateHtmlError(errors));
}
});
server.listen(parseInt(serverUrl.port), serverUrl.hostname, () => {
console.error(`Failure page is available at ${serverUrl.href}`);
});
});
await new Promise((resolve) => server?.close(resolve));
}
export async function onboard(): Promise<boolean> {
if (!existsSync(data.getPath())) {
mkdirSync(data.getPath(), {recursive: true});
}
const confExists = existsSync(data.joinPath('configuration.yaml'));
if (!confExists) {
settings.writeMinimalDefaults();
} else {
// migrate first
const {migrateIfNecessary} = await import('./settingsMigration.js');
migrateIfNecessary();
// trigger initial writing of `ZIGBEE2MQTT_CONFIG_*` ENVs
settings.write();
}
// use `configuration.yaml` file to detect "brand new install"
// env allows to re-run onboard even with existing install
if (!process.env.Z2M_ONBOARD_NO_SERVER && (process.env.Z2M_ONBOARD_FORCE_RUN || !confExists)) {
const success = await startOnboardingServer();
if (!success) {
return false;
}
}
settings.reRead();
const errors = settings.validate();
if (errors.length > 0) {
let pErrors: string = '';
console.error(`\n\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!`);
console.error(' READ THIS CAREFULLY\n');
console.error(`Refusing to start because configuration is not valid, found the following errors:`);
for (const error of errors) {
console.error(`- ${error}`);
pErrors += `<p>- ${escapeHtml(error)}</p>`;
}
console.error(`\nIf you don't know how to solve this, read https://www.zigbee2mqtt.io/guide/configuration`);
console.error(`\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n`);
if (!process.env.Z2M_ONBOARD_NO_SERVER && !process.env.Z2M_ONBOARD_NO_FAILURE_PAGE) {
await startFailureServer(pErrors);
}
return false;
}
return true;
}
+68
View File
@@ -0,0 +1,68 @@
import type {UnixDgramSocket} from 'unix-dgram';
import {platform} from 'node:os';
import logger from './logger';
/**
* Handle sd_notify protocol, @see https://www.freedesktop.org/software/systemd/man/latest/sd_notify.html
* No-op if running on unsupported platforms or without Type=notify
* Soft-fails if improperly setup (this is not necessary for Zigbee2MQTT to function properly)
*/
export async function initSdNotify(): Promise<{notifyStopping: () => void; stop: () => void} | undefined> {
if (!process.env.NOTIFY_SOCKET) {
return;
}
let socket: UnixDgramSocket | undefined;
try {
const {createSocket} = await import('unix-dgram');
socket = createSocket('unix_dgram');
} catch (error) {
if (platform() !== 'win32' || process.env.WSL_DISTRO_NAME) {
// not on plain Windows
logger.error(`Could not init sd_notify: ${(error as Error).message}`);
logger.debug((error as Error).stack!);
} else {
// this should not happen
logger.warning(`NOTIFY_SOCKET env is set: ${(error as Error).message}`);
}
return;
}
const sendToSystemd = (msg: string): void => {
const buffer = Buffer.from(msg);
socket.send(buffer, 0, buffer.byteLength, process.env.NOTIFY_SOCKET!, (err) => {
if (err) {
logger.warning(`Failed to send "${msg}" to systemd: ${err.message}`);
}
});
};
const notifyStopping = (): void => sendToSystemd('STOPPING=1');
sendToSystemd('READY=1');
const wdUSec = process.env.WATCHDOG_USEC !== undefined ? Math.max(0, parseInt(process.env.WATCHDOG_USEC, 10)) : -1;
if (wdUSec > 0) {
// Convert us to ms, send twice as frequently as the timeout
const watchdogInterval = setInterval(() => sendToSystemd('WATCHDOG=1'), wdUSec / 1000 / 2);
return {
notifyStopping,
stop: (): void => clearInterval(watchdogInterval),
};
}
if (wdUSec !== -1) {
logger.warning(`WATCHDOG_USEC invalid: "${process.env.WATCHDOG_USEC}", parsed to "${wdUSec}"`);
}
return {
notifyStopping,
stop: (): void => {},
};
}
+195 -275
View File
@@ -3,110 +3,93 @@
"properties": {
"homeassistant": {
"title": "Home Assistant integration",
"requiresRestart": true,
"description": "Home Assistant integration (MQTT discovery)",
"default": false,
"oneOf": [
{
"type": "object",
"properties": {
"enabled": {
"type": "boolean",
"title": "Home Assistant (simple)"
"title": "Enabled",
"description": "Enable Home Assistant integration",
"default": false,
"requiresRestart": true
},
{
"type": "object",
"title": "Home Assistant (advanced)",
"properties": {
"legacy_triggers": {
"type": "boolean",
"title": "Home Assistant legacy triggers",
"description": "Home Assistant legacy triggers, when enabled Zigbee2mqt will send an empty 'action' or 'click' after one has been send. A 'sensor_action' and 'sensor_click' will be discoverd",
"default": true
},
"discovery_topic": {
"type": "string",
"title": "Homeassistant discovery topic",
"description": "Home Assistant discovery topic",
"requiresRestart": true,
"examples": ["homeassistant"]
},
"legacy_entity_attributes": {
"type": "boolean",
"title": "Home Assistant legacy entity attributes",
"description": "Home Assistant legacy entity attributes, when enabled Zigbee2MQTT will add state attributes to each entity, additional to the separate entities and devices it already creates",
"default": true
},
"status_topic": {
"type": "string",
"title": "Home Assistant status topic",
"description": "Home Assistant status topic",
"requiresRestart": true,
"examples": ["homeassistant/status"]
}
}
"discovery_topic": {
"type": "string",
"title": "Homeassistant discovery topic",
"description": "Home Assistant discovery topic",
"default": "homeassistant",
"requiresRestart": true,
"examples": ["homeassistant"]
},
"status_topic": {
"type": "string",
"title": "Home Assistant status topic",
"description": "Home Assistant status topic",
"default": "homeassistant/status",
"requiresRestart": true,
"examples": ["homeassistant/status"]
},
"legacy_action_sensor": {
"type": "boolean",
"title": "Home Assistant legacy action sensors",
"description": "Home Assistant legacy actions sensor, when enabled a action sensor will be discoverd and an empty `action` will be send after every published action.",
"default": false
},
"experimental_event_entities": {
"type": "boolean",
"title": "Home Assistant experimental event entities",
"description": "Home Assistant experimental event entities, when enabled Zigbee2MQTT will add event entities for exposed actions. The events and attributes are currently deemed experimental and subject to change.",
"default": false
}
]
},
"permit_join": {
"type": "boolean",
"default": false,
"title": "Permit join",
"description": "Allow new devices to join (re-applied at restart)"
},
"required": []
},
"availability": {
"oneOf": [
{
"type": "boolean",
"title": "Availability (simple)"
},
{
"type": "object",
"title": "Availability (advanced)",
"properties": {
"active": {
"type": "object",
"title": "Active",
"requiresRestart": true,
"description": "Options for active devices (routers/mains powered)",
"properties": {
"timeout": {
"type": "number",
"title": "Timeout",
"requiresRestart": true,
"default": 10,
"description": "Time after which an active device will be marked as offline in minutes"
}
}
},
"passive": {
"type": "object",
"title": "Passive",
"requiresRestart": true,
"description": "Options for passive devices (mostly battery powered)",
"properties": {
"timeout": {
"type": "number",
"title": "Timeout",
"requiresRestart": true,
"default": 1500,
"description": "Time after which an passive device will be marked as offline in minutes"
}
}
}
}
}
],
"type": "object",
"title": "Availability",
"requiresRestart": true,
"description": "Checks whether devices are online/offline"
},
"external_converters": {
"type": "array",
"title": "External converters",
"description": "You can define external converters to e.g. add support for a DiY device",
"requiresRestart": true,
"items": {
"type": "string"
"description": "Checks whether devices are online/offline",
"properties": {
"enabled": {
"type": "boolean",
"title": "Enabled",
"description": "Enable availability checks",
"default": false,
"requiresRestart": true
},
"active": {
"type": "object",
"title": "Active",
"requiresRestart": true,
"description": "Options for active devices (routers/mains powered)",
"properties": {
"timeout": {
"type": "number",
"title": "Timeout",
"requiresRestart": true,
"default": 10,
"description": "Time after which an active device will be marked as offline in minutes"
}
},
"required": ["timeout"]
},
"passive": {
"type": "object",
"title": "Passive",
"requiresRestart": true,
"description": "Options for passive devices (mostly battery powered)",
"properties": {
"timeout": {
"type": "number",
"title": "Timeout",
"requiresRestart": true,
"default": 1500,
"description": "Time after which an passive device will be marked as offline in minutes"
}
},
"required": ["timeout"]
}
},
"examples": ["DIYRuZ_FreePad.js"]
"required": []
},
"mqtt": {
"type": "object",
@@ -201,8 +184,17 @@
"type": "boolean",
"title": "Force disable retain",
"requiresRestart": true,
"description": "Disable retain for all send messages. ONLY enable if you MQTT broker doesn't support retained message (e.g. AWS IoT core, Azure IoT Hub, Google Cloud IoT core, IBM Watson IoT Platform). Enabling will break the Home Assistant integration",
"description": "Disable retain for all send messages. ONLY enable if your MQTT broker doesn't support retained message (e.g. AWS IoT core, Azure IoT Hub, Google Cloud IoT core, IBM Watson IoT Platform). Enabling will break the Home Assistant integration",
"default": false
},
"maximum_packet_size": {
"type": "number",
"title": "Maximum packet size",
"requiresRestart": true,
"description": "Specifies the maximum allowed packet length (in bytes) that the server can send to Zigbee2MQTT. NOTE: The same value exists in your MQTT broker but for the length the client can send to it instead.",
"default": 1048576,
"minimum": 20,
"maximum": 268435456
}
},
"required": ["server"]
@@ -227,9 +219,8 @@
},
"adapter": {
"type": ["string"],
"enum": ["deconz", "zstack", "zigate", "ezsp", "auto", "ember", "zboss"],
"enum": ["deconz", "zstack", "zigate", "ezsp", "ember", "zboss", "zoh"],
"title": "Adapter",
"default": "auto",
"requiresRestart": true,
"description": "Adapter type, not needed unless you are experiencing problems"
},
@@ -337,75 +328,99 @@
"description": "Zigbee devices may request a firmware update, and do so frequently, causing Zigbee2MQTT to reach out to third party servers. If you disable these device initiated checks, you can still initiate a firmware update check manually.",
"default": false
},
"ikea_ota_use_test_url": {
"type": "boolean",
"title": "IKEA TRADFRI OTA use test url",
"requiresRestart": true,
"description": "Use IKEA TRADFRI OTA test server, see OTA updates documentation",
"default": false
},
"zigbee_ota_override_index_location": {
"type": ["string", "null"],
"title": "OTA index override file name",
"requiresRestart": true,
"description": "Location of override OTA index file",
"examples": ["index.json"]
},
"image_block_response_delay": {
"type": "number",
"title": "Image block response delay",
"description": "Limits the rate of requests (in milliseconds) during OTA updates to reduce network congestion. You can increase this value if your network appears unstable during OTA.",
"default": 250,
"minimum": 50,
"requiresRestart": true
},
"default_maximum_data_size": {
"type": "number",
"title": "Default maximum data size",
"description": "The size of file chunks sent during an update (in bytes). Note: This value may get ignored for manufacturers that require specific values.",
"default": 50,
"minimum": 10,
"maximum": 100,
"requiresRestart": true
}
}
},
"frontend": {
"oneOf": [
{
"type": "object",
"title": "Frontend",
"properties": {
"enabled": {
"type": "boolean",
"title": "Frontend (simple)"
"title": "Enabled",
"description": "Enable frontend",
"default": false,
"requiresRestart": true
},
{
"type": "object",
"title": "Frontend (advanced)",
"properties": {
"port": {
"type": "number",
"title": "Port",
"description": "Frontend binding port. Ignored when using a unix domain socket",
"default": 8080,
"requiresRestart": true
},
"host": {
"type": ["string", "null"],
"title": "Bind host",
"description": "Frontend binding host. Binds to a unix socket when an absolute path is given instead.",
"examples": ["127.0.0.1", "::1", "/run/zigbee2mqtt/zigbee2mqtt.sock"],
"requiresRestart": true
},
"auth_token": {
"type": ["string", "null"],
"title": "Auth token",
"description": "Enables authentication, disabled by default",
"requiresRestart": true
},
"url": {
"type": ["string", "null"],
"title": "URL",
"description": "URL on which the frontend can be reached, currently only used for the Home Assistant device configuration page",
"requiresRestart": true
},
"ssl_cert": {
"type": ["string", "null"],
"title": "Certificate file path",
"description": "SSL Certificate file path for exposing HTTPS. The sibling property 'ssl_key' must be set for HTTPS to be activated.",
"requiresRestart": true
},
"ssl_key": {
"type": ["string", "null"],
"title": "key file path",
"description": "SSL key file path for exposing HTTPS. The sibling property 'ssl_cert' must be set for HTTPS to be activated.",
"requiresRestart": true
}
"port": {
"type": "number",
"title": "Port",
"description": "Frontend binding port. Ignored when using a unix domain socket",
"default": 8080,
"requiresRestart": true
},
"host": {
"type": ["string", "null"],
"title": "Bind host",
"description": "Frontend binding host. Binds to a unix socket when an absolute path is given instead.",
"examples": ["127.0.0.1", "::1", "/run/zigbee2mqtt/zigbee2mqtt.sock"],
"requiresRestart": true
},
"auth_token": {
"type": ["string", "null"],
"title": "Auth token",
"description": "Enables authentication, disabled by default",
"requiresRestart": true
},
"url": {
"type": ["string", "null"],
"title": "URL",
"description": "URL on which the frontend can be reached, currently only used for the Home Assistant device configuration page",
"requiresRestart": true
},
"ssl_cert": {
"type": ["string", "null"],
"title": "Certificate file path",
"description": "SSL Certificate file path for exposing HTTPS. The sibling property 'ssl_key' must be set for HTTPS to be activated.",
"requiresRestart": true
},
"ssl_key": {
"type": ["string", "null"],
"title": "key file path",
"description": "SSL key file path for exposing HTTPS. The sibling property 'ssl_cert' must be set for HTTPS to be activated.",
"requiresRestart": true
},
"base_url": {
"type": "string",
"pattern": "^\\/.*",
"title": "Base URL",
"description": "Base URL for the frontend. If hosted under a subpath, e.g. 'http://localhost:8080/z2m', set this to '/z2m'",
"default": "/",
"requiresRestart": true
},
"notification_filter": {
"title": "Notification Filter",
"description": "Hide frontend notifications matching specified regex strings. Example: 'z2m: Failed to ping.*'",
"type": "array",
"items": {
"type": "string"
}
}
],
"title": "Frontend",
"requiresRestart": true
},
"required": []
},
"devices": {
"type": "object",
@@ -437,20 +452,6 @@
"type": "object",
"title": "Advanced",
"properties": {
"legacy_api": {
"type": "boolean",
"title": "Legacy API",
"requiresRestart": true,
"description": "Disables the legacy api (false = disable)",
"default": true
},
"legacy_availability_payload": {
"type": "boolean",
"title": "Legacy availability payload",
"requiresRestart": true,
"description": "Payload to be used for device availability and bridge/state topics. true = text, false = JSON",
"default": true
},
"log_rotation": {
"type": "boolean",
"title": "Log rotation",
@@ -458,6 +459,13 @@
"description": "Log rotation",
"default": true
},
"log_console_json": {
"type": "boolean",
"title": "Console json log",
"requiresRestart": true,
"description": "Console json log",
"default": false
},
"log_symlink_current": {
"type": "boolean",
"title": "Log symlink current",
@@ -492,7 +500,7 @@
},
"log_level": {
"type": "string",
"enum": ["error", "warning", "info", "debug", "warn"],
"enum": ["error", "warning", "info", "debug"],
"title": "Log level",
"description": "Logging level",
"default": "info"
@@ -525,6 +533,14 @@
"default": "",
"examples": ["^zhc:legacy:fz:(tuya|moes)", "^zhc:legacy:fz:(tuya|moes)|^zh:ember:uart:|^zh:controller"]
},
"log_directories_to_keep": {
"type": "number",
"title": "Number of past log folders to keep",
"description": "Number of log directories to keep before deleting the oldest one",
"default": 10,
"minimum": 5,
"maximum": 1000
},
"log_syslog": {
"requiresRestart": true,
"oneOf": [
@@ -724,105 +740,8 @@
"enum": ["attribute_and_json", "attribute", "json"],
"title": "MQTT output type",
"description": "Examples when 'state' of a device is published json: topic: 'zigbee2mqtt/my_bulb' payload '{\"state\": \"ON\"}' attribute: topic 'zigbee2mqtt/my_bulb/state' payload 'ON' attribute_and_json: both json and attribute (see above)"
},
"homeassistant_discovery_topic": {
"type": "string",
"title": "Homeassistant discovery topic",
"description": "Home Assistant discovery topic",
"requiresRestart": true,
"examples": ["homeassistant"]
},
"homeassistant_legacy_entity_attributes": {
"type": "boolean",
"title": "Home Assistant legacy entity attributes",
"description": "Home Assistant legacy entity attributes, when enabled Zigbee2MQTT will add state attributes to each entity, additional to the separate entities and devices it already creates",
"default": true
},
"homeassistant_status_topic": {
"type": "string",
"title": "Home Assistant status topic",
"description": "Home Assistant status topic",
"requiresRestart": true,
"examples": ["homeassistant/status"]
},
"homeassistant_legacy_triggers": {
"type": "boolean",
"title": "Home Assistant legacy triggers",
"description": "Home Assistant legacy triggers, when enabled Zigbee2mqt will send an empty 'action' or 'click' after one has been send. A 'sensor_action' and 'sensor_click' will be discovered",
"default": true
},
"soft_reset_timeout": {
"type": "number",
"minimum": 0,
"requiresRestart": true,
"title": "Soft reset timeout (deprecated)",
"description": "Soft reset ZNP after timeout",
"readOnly": true
},
"report": {
"type": "boolean",
"title": "Reporting",
"requiresRestart": true,
"readOnly": true,
"description": "Enables report feature (deprecated)"
},
"baudrate": {
"type": "number",
"title": "Baudrate (deprecated)",
"requiresRestart": true,
"description": "Baud rate speed for serial port, this can be anything firmware support but default is 115200 for Z-Stack and EZSP, 38400 for Deconz, however note that some EZSP firmware need 57600",
"examples": [38400, 57600, 115200]
},
"rtscts": {
"type": "boolean",
"title": "RTS / CTS (deprecated)",
"requiresRestart": true,
"description": "RTS / CTS Hardware Flow Control for serial port"
},
"ikea_ota_use_test_url": {
"type": "boolean",
"title": "IKEA TRADFRI OTA use test url (deprecated)",
"requiresRestart": true,
"description": "Use IKEA TRADFRI OTA test server, see OTA updates documentation",
"default": false
}
}
},
"experimental": {
"type": "object",
"title": "Experimental (deprecated)",
"properties": {
"transmit_power": {
"type": ["number", "null"],
"title": "Transmit power",
"requiresRestart": true,
"description": "Transmit power of adapter, only available for Z-Stack (CC253*/CC2652/CC1352) adapters, CC2652 = 5dbm, CC1352 max is = 20dbm (5dbm default)"
},
"output": {
"type": "string",
"enum": ["attribute_and_json", "attribute", "json"],
"title": "MQTT output type",
"description": "Examples when 'state' of a device is published json: topic: 'zigbee2mqtt/my_bulb' payload '{\"state\": \"ON\"}' attribute: topic 'zigbee2mqtt/my_bulb/state' payload 'ON' attribute_and_json: both json and attribute (see above)"
}
}
},
"whitelist": {
"readOnly": true,
"type": "array",
"requiresRestart": true,
"title": "Whitelist (deprecated, use passlist)",
"items": {
"type": "string"
}
},
"ban": {
"readOnly": true,
"type": "array",
"requiresRestart": true,
"title": "Ban (deprecated, use blocklist)",
"items": {
"type": "string"
}
}
},
"required": ["mqtt"],
@@ -857,10 +776,17 @@
"title": "QoS",
"description": "QoS level for MQTT messages of this device"
},
"throttle": {
"type": "number",
"title": "Throttle",
"description": "The minimum time between payloads in seconds. Payloads received whilst the device is being throttled will be discarded",
"requiresRestart": true
},
"debounce": {
"type": "number",
"title": "Debounce",
"description": "Debounces messages of this device"
"description": "Debounces messages of this device",
"requiresRestart": true
},
"debounce_ignore": {
"type": "array",
@@ -907,7 +833,7 @@
"icon": {
"type": "string",
"title": "Icon",
"description": "The user-defined device icon for the frontend. It can be a full URL link to an image (e.g. https://SOME.SITE/MODEL123.jpg) (you cannot use a path to a local file) or base64 encoded data URL (e.g. image/svg+xml;base64,PHN2ZyB3aW....R0aD)"
"description": "The user-defined device icon for the frontend. It can be a full URL link to an image (e.g. https://SOME.SITE/MODEL123.jpg) or a path to a local file inside the `device_icons` directory."
},
"homeassistant": {
"type": ["object", "null"],
@@ -932,12 +858,6 @@
"retain": {
"type": "boolean"
},
"devices": {
"type": "array",
"items": {
"type": "string"
}
},
"optimistic": {
"type": "boolean"
},
@@ -950,7 +870,7 @@
"title": "Group off state",
"default": "auto",
"requiresRestart": true,
"description": "Control when to publish state OFF for a group. 'all_members_off': only publish state OFF when all group members are in state OFF, 'last_member_state': publish state OFF whenever one of its members changes to OFF"
"description": "Control when to publish state OFF or CLOSE for a group. 'all_members_off': only publish state OFF/CLOSE when all group members are in state OFF/CLOSE, 'last_member_state': publish state OFF whenever one of its members changes to OFF"
},
"filtered_attributes": {
"type": "array",
+220 -299
View File
@@ -1,38 +1,22 @@
import path from 'node:path';
import Ajv, {ValidateFunction} from 'ajv';
import objectAssignDeep from 'object-assign-deep';
import path from 'path';
import data from './data';
import schemaJson from './settings.schema.json';
import utils from './utils';
import yaml from './yaml';
export let schema = schemaJson;
// @ts-expect-error
schema = {};
objectAssignDeep(schema, schemaJson);
// Remove legacy settings from schema
{
delete schema.properties.advanced.properties.homeassistant_discovery_topic;
delete schema.properties.advanced.properties.homeassistant_legacy_entity_attributes;
delete schema.properties.advanced.properties.homeassistant_legacy_triggers;
delete schema.properties.advanced.properties.homeassistant_status_topic;
delete schema.properties.advanced.properties.soft_reset_timeout;
delete schema.properties.advanced.properties.report;
delete schema.properties.advanced.properties.baudrate;
delete schema.properties.advanced.properties.rtscts;
delete schema.properties.advanced.properties.ikea_ota_use_test_url;
delete schema.properties.experimental;
delete schemaJson.properties.whitelist;
delete schemaJson.properties.ban;
}
import yaml, {YAMLFileException} from './yaml';
export {schemaJson};
// When updating also update:
// - https://github.com/Koenkk/zigbee2mqtt/blob/dev/data/configuration.example.yaml#L2
export const CURRENT_VERSION = 4;
/** NOTE: by order of priority, lower index is lower level (more important) */
export const LOG_LEVELS: readonly string[] = ['error', 'warning', 'info', 'debug'] as const;
export type LogLevel = (typeof LOG_LEVELS)[number];
export type LogLevel = 'error' | 'warning' | 'info' | 'debug';
// DEPRECATED ZIGBEE2MQTT_CONFIG: https://github.com/Koenkk/zigbee2mqtt/issues/4697
const file = process.env.ZIGBEE2MQTT_CONFIG ?? data.joinPath('configuration.yaml');
const CONFIG_FILE_PATH = data.joinPath('configuration.yaml');
const NULLABLE_SETTINGS = ['homeassistant'];
const ajvSetting = new Ajv({allErrors: true}).addKeyword('requiresRestart').compile(schemaJson);
const ajvRestartRequired = new Ajv({allErrors: true}).addKeyword({keyword: 'requiresRestart', validate: (s: unknown) => !s}).compile(schemaJson);
@@ -42,13 +26,30 @@ const ajvRestartRequiredDeviceOptions = new Ajv({allErrors: true})
const ajvRestartRequiredGroupOptions = new Ajv({allErrors: true})
.addKeyword({keyword: 'requiresRestart', validate: (s: unknown) => !s})
.compile(schemaJson.definitions.group);
const defaults: RecursivePartial<Settings> = {
permit_join: false,
external_converters: [],
export const defaults: RecursivePartial<Settings> = {
homeassistant: {
enabled: false,
discovery_topic: 'homeassistant',
status_topic: 'homeassistant/status',
legacy_action_sensor: false,
experimental_event_entities: false,
},
availability: {
enabled: false,
active: {timeout: 10},
passive: {timeout: 1500},
},
frontend: {
enabled: false,
port: 8080,
base_url: '/',
},
mqtt: {
base_topic: 'zigbee2mqtt',
include_device_information: false,
force_disable_retain: false,
// 1MB = roughly 3.5KB per device * 300 devices for `/bridge/devices`
maximum_packet_size: 1048576,
},
serial: {
disable_led: false,
@@ -78,26 +79,28 @@ const defaults: RecursivePartial<Settings> = {
ota: {
update_check_interval: 24 * 60,
disable_automatic_update_check: false,
image_block_response_delay: 250,
default_maximum_data_size: 50,
},
device_options: {},
advanced: {
legacy_api: true,
legacy_availability_payload: true,
log_rotation: true,
log_console_json: false,
log_symlink_current: false,
log_output: ['console', 'file'],
log_directory: path.join(data.getPath(), 'log', '%TIMESTAMP%'),
log_file: 'log.log',
log_level: /* istanbul ignore next */ process.env.DEBUG ? 'debug' : 'info',
log_level: /* v8 ignore next */ process.env.DEBUG ? 'debug' : 'info',
log_namespaced_levels: {},
log_syslog: {},
log_debug_to_mqtt_frontend: false,
log_debug_namespace_ignore: '',
log_directories_to_keep: 10,
pan_id: 0x1a62,
ext_pan_id: [0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd, 0xdd],
channel: 11,
adapter_concurrent: null,
adapter_delay: null,
adapter_concurrent: undefined,
adapter_delay: undefined,
cache_state: true,
cache_state_persistent: true,
cache_state_send_on_startup: true,
@@ -106,21 +109,18 @@ const defaults: RecursivePartial<Settings> = {
network_key: [1, 3, 5, 7, 9, 11, 13, 15, 0, 2, 4, 6, 8, 10, 12, 13],
timestamp_format: 'YYYY-MM-DD HH:mm:ss',
output: 'json',
// Everything below is deprecated
availability_blocklist: [],
availability_passlist: [],
availability_blacklist: [],
availability_whitelist: [],
soft_reset_timeout: 0,
report: false,
},
};
let _settings: Partial<Settings>;
let _settingsWithDefaults: Settings;
let _settings: Partial<Settings> | undefined;
let _settingsWithDefaults: Settings | undefined;
function loadSettingsWithDefaults(): void {
_settingsWithDefaults = objectAssignDeep({}, defaults, getInternalSettings()) as Settings;
if (!_settings) {
_settings = read();
}
_settingsWithDefaults = objectAssignDeep({}, defaults, getPersistedSettings()) as Settings;
if (!_settingsWithDefaults.devices) {
_settingsWithDefaults.devices = {};
@@ -129,88 +129,6 @@ function loadSettingsWithDefaults(): void {
if (!_settingsWithDefaults.groups) {
_settingsWithDefaults.groups = {};
}
if (_settingsWithDefaults.homeassistant) {
const defaults = {discovery_topic: 'homeassistant', status_topic: 'hass/status', legacy_entity_attributes: true, legacy_triggers: true};
const sLegacy = {};
if (_settingsWithDefaults.advanced) {
for (const key of [
'homeassistant_legacy_triggers',
'homeassistant_discovery_topic',
'homeassistant_legacy_entity_attributes',
'homeassistant_status_topic',
]) {
// @ts-expect-error
if (_settingsWithDefaults.advanced[key] !== undefined) {
// @ts-expect-error
sLegacy[key.replace('homeassistant_', '')] = _settingsWithDefaults.advanced[key];
}
}
}
const s = typeof _settingsWithDefaults.homeassistant === 'object' ? _settingsWithDefaults.homeassistant : {};
// @ts-expect-error
_settingsWithDefaults.homeassistant = {};
objectAssignDeep(_settingsWithDefaults.homeassistant, defaults, sLegacy, s);
}
if (_settingsWithDefaults.availability || _settingsWithDefaults.advanced?.availability_timeout) {
const defaults = {};
const s = typeof _settingsWithDefaults.availability === 'object' ? _settingsWithDefaults.availability : {};
// @ts-expect-error
_settingsWithDefaults.availability = {};
objectAssignDeep(_settingsWithDefaults.availability, defaults, s);
}
if (_settingsWithDefaults.frontend) {
const defaults = {port: 8080, auth_token: false};
const s = typeof _settingsWithDefaults.frontend === 'object' ? _settingsWithDefaults.frontend : {};
_settingsWithDefaults.frontend = {};
objectAssignDeep(_settingsWithDefaults.frontend, defaults, s);
}
if (_settings.advanced?.hasOwnProperty('baudrate') && _settings.serial?.baudrate == null) {
// @ts-expect-error
_settingsWithDefaults.serial.baudrate = _settings.advanced.baudrate;
}
if (_settings.advanced?.hasOwnProperty('rtscts') && _settings.serial?.rtscts == null) {
// @ts-expect-error
_settingsWithDefaults.serial.rtscts = _settings.advanced.rtscts;
}
if (_settings.advanced?.hasOwnProperty('ikea_ota_use_test_url') && _settings.ota?.ikea_ota_use_test_url == null) {
// @ts-expect-error
_settingsWithDefaults.ota.ikea_ota_use_test_url = _settings.advanced.ikea_ota_use_test_url;
}
// @ts-expect-error
if (_settings.experimental?.hasOwnProperty('transmit_power') && _settings.advanced?.transmit_power == null) {
// @ts-expect-error
_settingsWithDefaults.advanced.transmit_power = _settings.experimental.transmit_power;
}
// @ts-expect-error
if (_settings.experimental?.hasOwnProperty('output') && _settings.advanced?.output == null) {
// @ts-expect-error
_settingsWithDefaults.advanced.output = _settings.experimental.output;
}
if (_settings.advanced?.log_level === 'warn') {
_settingsWithDefaults.advanced.log_level = 'warning';
}
// @ts-expect-error
if (_settingsWithDefaults.ban) {
// @ts-expect-error
_settingsWithDefaults.blocklist.push(..._settingsWithDefaults.ban);
}
// @ts-expect-error
if (_settingsWithDefaults.whitelist) {
// @ts-expect-error
_settingsWithDefaults.passlist.push(..._settingsWithDefaults.whitelist);
}
}
function parseValueRef(text: string): {filename: string; key: string} | null {
@@ -227,26 +145,58 @@ function parseValueRef(text: string): {filename: string; key: string} | null {
}
}
function write(): void {
const settings = getInternalSettings();
export function writeMinimalDefaults(): void {
const minimal = {
version: CURRENT_VERSION,
mqtt: {
base_topic: defaults.mqtt!.base_topic,
server: 'mqtt://localhost:1883',
},
serial: {},
advanced: {
log_level: defaults.advanced!.log_level,
channel: defaults.advanced!.channel,
network_key: 'GENERATE',
pan_id: 'GENERATE',
ext_pan_id: 'GENERATE',
},
frontend: {
enabled: defaults.frontend!.enabled,
port: defaults.frontend!.port,
},
homeassistant: {
enabled: defaults.homeassistant!.enabled,
},
} as Partial<Settings>;
applyEnvironmentVariables(minimal);
yaml.writeIfChanged(CONFIG_FILE_PATH, minimal);
_settings = read();
loadSettingsWithDefaults();
}
export function write(): void {
const settings = getPersistedSettings();
const toWrite: KeyValue = objectAssignDeep({}, settings);
// Read settings to check if we have to split devices/groups into separate file.
const actual = yaml.read(file);
const actual = yaml.read(CONFIG_FILE_PATH);
// In case the setting is defined in a separate file (e.g. !secret network_key) update it there.
for (const path of [
for (const [ns, key] of [
['mqtt', 'server'],
['mqtt', 'user'],
['mqtt', 'password'],
['advanced', 'network_key'],
['frontend', 'auth_token'],
]) {
if (actual[path[0]] && actual[path[0]][path[1]]) {
const ref = parseValueRef(actual[path[0]][path[1]]);
if (actual[ns] && actual[ns][key]) {
const ref = parseValueRef(actual[ns][key]);
if (ref) {
yaml.updateIfChanged(data.joinPath(ref.filename), ref.key, toWrite[path[0]][path[1]]);
toWrite[path[0]][path[1]] = actual[path[0]][path[1]];
yaml.updateIfChanged(data.joinPath(ref.filename), ref.key, toWrite[ns][key]);
toWrite[ns][key] = actual[ns][key];
}
}
}
@@ -259,12 +209,12 @@ function write(): void {
// If an array, only write to first file and only devices which are not in the other files.
if (Array.isArray(actual[type])) {
actual[type]
.filter((f: string, i: number) => i !== 0)
.map((f: string) => yaml.readIfExists(data.joinPath(f), {}))
.map((c: KeyValue) => Object.keys(c))
// @ts-expect-error
.forEach((k: string) => delete content[k]);
// skip i==0
for (let i = 1; i < actual[type].length; i++) {
for (const key in yaml.readIfExists(data.joinPath(actual[type][i]))) {
delete content[key];
}
}
}
yaml.writeIfChanged(data.joinPath(fileToWrite), content);
@@ -275,28 +225,33 @@ function write(): void {
writeDevicesOrGroups('devices');
writeDevicesOrGroups('groups');
yaml.writeIfChanged(file, toWrite);
applyEnvironmentVariables(toWrite);
yaml.writeIfChanged(CONFIG_FILE_PATH, toWrite);
_settings = read();
loadSettingsWithDefaults();
}
export function validate(): string[] {
try {
getInternalSettings();
getPersistedSettings();
} catch (error) {
if (error.name === 'YAMLException') {
if (error instanceof YAMLFileException) {
return [`Your YAML file: '${error.file}' is invalid (use https://jsonformatter.org/yaml-validator to find and fix the issue)`];
}
return [error.message];
return [`${error}`];
}
if (!ajvSetting(_settings)) {
return ajvSetting.errors.map((v) => `${v.instancePath.substring(1)} ${v.message}`);
// When `ajvSetting()` return false it always has `errors`.
return ajvSetting.errors!.map((v) => `${v.instancePath.substring(1)} ${v.message}`);
}
const errors = [];
if (
_settings.advanced &&
_settings.advanced.network_key &&
@@ -330,12 +285,16 @@ export function validate(): string[] {
if (names.includes(e.friendly_name)) errors.push(`Duplicate friendly_name '${e.friendly_name}' found`);
errors.push(...utils.validateFriendlyName(e.friendly_name));
names.push(e.friendly_name);
if ('icon' in e && e.icon && !e.icon.startsWith('http://') && !e.icon.startsWith('https://') && !e.icon.startsWith('device_icons/')) {
errors.push(`Device icon of '${e.friendly_name}' should start with 'device_icons/', got '${e.icon}'`);
}
if (e.qos != null && ![0, 1, 2].includes(e.qos)) {
errors.push(`QOS for '${e.friendly_name}' not valid, should be 0, 1 or 2 got ${e.qos}`);
}
};
const settingsWithDefaults = get();
Object.values(settingsWithDefaults.devices).forEach((d) => check(d));
Object.values(settingsWithDefaults.groups).forEach((g) => check(g));
@@ -347,35 +306,21 @@ export function validate(): string[] {
}
}
const checkAvailabilityList = (list: string[], type: string): void => {
list.forEach((e) => {
if (!getDevice(e)) {
errors.push(`Non-existing entity '${e}' specified in '${type}'`);
}
});
};
checkAvailabilityList(settingsWithDefaults.advanced.availability_blacklist, 'availability_blacklist');
checkAvailabilityList(settingsWithDefaults.advanced.availability_whitelist, 'availability_whitelist');
checkAvailabilityList(settingsWithDefaults.advanced.availability_blocklist, 'availability_blocklist');
checkAvailabilityList(settingsWithDefaults.advanced.availability_passlist, 'availability_passlist');
return errors;
}
function read(): Settings {
const s = yaml.read(file) as Settings;
applyEnvironmentVariables(s);
function read(): Partial<Settings> {
const s = yaml.read(CONFIG_FILE_PATH) as Partial<Settings>;
// Read !secret MQTT username and password if set
// eslint-disable-next-line
const interpretValue = (value: any): any => {
const ref = parseValueRef(value);
if (ref) {
return yaml.read(data.joinPath(ref.filename))[ref.key];
} else {
return value;
const interpretValue = <T>(value: T): T => {
if (typeof value === 'string') {
const ref = parseValueRef(value);
if (ref) {
return yaml.read(data.joinPath(ref.filename))[ref.key];
}
}
return value;
};
if (s.mqtt?.user) {
@@ -401,12 +346,11 @@ function read(): Settings {
// Read devices/groups configuration from separate file if specified.
const readDevicesOrGroups = (type: 'devices' | 'groups'): void => {
if (typeof s[type] === 'string' || (Array.isArray(s[type]) && Array(s[type]).length > 0)) {
/* eslint-disable-line */
const files: string[] = Array.isArray(s[type]) ? s[type] : [s[type]];
s[type] = {};
for (const file of files) {
const content = yaml.readIfExists(data.joinPath(file), {});
/* eslint-disable-line */ // @ts-expect-error
const content = yaml.readIfExists(data.joinPath(file));
// @ts-expect-error noMutate not typed properly
s[type] = objectAssignDeep.noMutate(s[type], content);
}
}
@@ -420,35 +364,39 @@ function read(): Settings {
function applyEnvironmentVariables(settings: Partial<Settings>): void {
const iterate = (obj: KeyValue, path: string[]): void => {
Object.keys(obj).forEach((key) => {
for (const key in obj) {
if (key !== 'type') {
if (key !== 'properties' && obj[key]) {
const type = (obj[key].type || 'object').toString();
const envPart = path.reduce((acc, val) => `${acc}${val}_`, '');
const envVariableName = `ZIGBEE2MQTT_CONFIG_${envPart}${key}`.toUpperCase();
if (process.env[envVariableName]) {
const envVariable = process.env[envVariableName];
if (envVariable) {
const setting = path.reduce((acc, val) => {
/* eslint-disable-line */ // @ts-expect-error
// @ts-expect-error ignore typing
acc[val] = acc[val] || {};
/* eslint-disable-line */ // @ts-expect-error
// @ts-expect-error ignore typing
return acc[val];
}, settings);
if (type.indexOf('object') >= 0 || type.indexOf('array') >= 0) {
try {
setting[key] = JSON.parse(process.env[envVariableName]);
setting[key as keyof Settings] = JSON.parse(envVariable);
} catch {
setting[key] = process.env[envVariableName];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
setting[key as keyof Settings] = envVariable as any;
}
} else if (type.indexOf('number') >= 0) {
/* eslint-disable-line */ // @ts-expect-error
setting[key] = process.env[envVariableName] * 1;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
setting[key as keyof Settings] = ((envVariable as unknown as number) * 1) as any;
} else if (type.indexOf('boolean') >= 0) {
setting[key] = process.env[envVariableName].toLowerCase() === 'true';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
setting[key as keyof Settings] = (envVariable.toLowerCase() === 'true') as any;
} else {
/* istanbul ignore else */
if (type.indexOf('string') >= 0) {
setting[key] = process.env[envVariableName];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
setting[key as keyof Settings] = envVariable as any;
}
}
}
@@ -456,18 +404,26 @@ function applyEnvironmentVariables(settings: Partial<Settings>): void {
if (typeof obj[key] === 'object' && obj[key]) {
const newPath = [...path];
if (key !== 'properties' && key !== 'oneOf' && !Number.isInteger(Number(key))) {
newPath.push(key);
}
iterate(obj[key], newPath);
}
}
});
}
};
iterate(schemaJson.properties, []);
}
function getInternalSettings(): Partial<Settings> {
/**
* Get the settings actually written in the yaml.
* Env vars are applied on top.
* Defaults merged on startup are not included.
*/
export function getPersistedSettings(): Partial<Settings> {
if (!_settings) {
_settings = read();
}
@@ -480,12 +436,12 @@ export function get(): Settings {
loadSettingsWithDefaults();
}
return _settingsWithDefaults;
return _settingsWithDefaults!;
}
export function set(path: string[], value: string | number | boolean | KeyValue): void {
/* eslint-disable-next-line */
let settings: any = getInternalSettings();
let settings: any = getPersistedSettings();
for (let i = 0; i < path.length; i++) {
const key = path[i];
@@ -503,51 +459,53 @@ export function set(path: string[], value: string | number | boolean | KeyValue)
write();
}
export function apply(settings: Record<string, unknown>): boolean {
getInternalSettings(); // Ensure _settings is initialized.
/* eslint-disable-line */ // @ts-expect-error
export function apply(settings: Record<string, unknown>, throwOnError: boolean = true): boolean {
getPersistedSettings(); // Ensure _settings is initialized.
// @ts-expect-error noMutate not typed properly
const newSettings = objectAssignDeep.noMutate(_settings, settings);
utils.removeNullPropertiesFromObject(newSettings, NULLABLE_SETTINGS);
ajvSetting(newSettings);
const errors = ajvSetting.errors && ajvSetting.errors.filter((e) => e.keyword !== 'required');
if (errors?.length) {
const error = errors[0];
throw new Error(`${error.instancePath.substring(1)} ${error.message}`);
if (throwOnError) {
const errors = ajvSetting.errors && ajvSetting.errors.filter((e) => e.keyword !== 'required');
if (errors?.length) {
const error = errors[0];
throw new Error(`${error.instancePath.substring(1)} ${error.message}`);
}
}
_settings = newSettings;
write();
ajvRestartRequired(settings);
const restartRequired = ajvRestartRequired.errors && !!ajvRestartRequired.errors.find((e) => e.keyword === 'requiresRestart');
const restartRequired = Boolean(ajvRestartRequired.errors && !!ajvRestartRequired.errors.find((e) => e.keyword === 'requiresRestart'));
return restartRequired;
}
export function getGroup(IDorName: string | number): GroupOptions {
export function getGroup(IDorName: string | number): GroupOptions | undefined {
const settings = get();
const byID = settings.groups[IDorName];
if (byID) {
return {devices: [], ...byID, ID: Number(IDorName)};
return {...byID, ID: Number(IDorName)};
}
for (const [ID, group] of Object.entries(settings.groups)) {
if (group.friendly_name === IDorName) {
return {devices: [], ...group, ID: Number(ID)};
return {...group, ID: Number(ID)};
}
}
return null;
}
export function getGroups(): GroupOptions[] {
const settings = get();
return Object.entries(settings.groups).map(([ID, group]) => {
return {devices: [], ...group, ID: Number(ID)};
});
return undefined;
}
function getGroupThrowIfNotExists(IDorName: string): GroupOptions {
const group = getGroup(IDorName);
if (!group) {
throw new Error(`Group '${IDorName}' does not exist`);
}
@@ -555,9 +513,10 @@ function getGroupThrowIfNotExists(IDorName: string): GroupOptions {
return group;
}
export function getDevice(IDorName: string): DeviceOptions {
export function getDevice(IDorName: string): DeviceOptionsWithId | undefined {
const settings = get();
const byID = settings.devices[IDorName];
if (byID) {
return {...byID, ID: IDorName};
}
@@ -568,10 +527,10 @@ export function getDevice(IDorName: string): DeviceOptions {
}
}
return null;
return undefined;
}
function getDeviceThrowIfNotExists(IDorName: string): DeviceOptions {
function getDeviceThrowIfNotExists(IDorName: string): DeviceOptionsWithId {
const device = getDevice(IDorName);
if (!device) {
throw new Error(`Device '${IDorName}' does not exist`);
@@ -580,12 +539,12 @@ function getDeviceThrowIfNotExists(IDorName: string): DeviceOptions {
return device;
}
export function addDevice(ID: string): DeviceOptions {
export function addDevice(ID: string): DeviceOptionsWithId {
if (getDevice(ID)) {
throw new Error(`Device '${ID}' already exists`);
}
const settings = getInternalSettings();
const settings = getPersistedSettings();
if (!settings.devices) {
settings.devices = {};
@@ -593,25 +552,12 @@ export function addDevice(ID: string): DeviceOptions {
settings.devices[ID] = {friendly_name: ID};
write();
return getDevice(ID);
}
export function addDeviceToPasslist(ID: string): void {
const settings = getInternalSettings();
if (!settings.passlist) {
settings.passlist = [];
}
if (settings.passlist.includes(ID)) {
throw new Error(`Device '${ID}' already in passlist`);
}
settings.passlist.push(ID);
write();
return getDevice(ID)!; // valid from creation above
}
export function blockDevice(ID: string): void {
const settings = getInternalSettings();
const settings = getPersistedSettings();
if (!settings.blocklist) {
settings.blocklist = [];
}
@@ -622,41 +568,35 @@ export function blockDevice(ID: string): void {
export function removeDevice(IDorName: string): void {
const device = getDeviceThrowIfNotExists(IDorName);
const settings = getInternalSettings();
delete settings.devices[device.ID];
// Remove device from groups
if (settings.groups) {
const regex = new RegExp(`^(${device.friendly_name}|${device.ID})(/[^/]+)?$`);
for (const group of Object.values(settings.groups).filter((g) => g.devices)) {
group.devices = group.devices.filter((device) => !device.match(regex));
}
}
const settings = getPersistedSettings();
delete settings.devices?.[device.ID];
write();
}
export function addGroup(name: string, ID?: string): GroupOptions {
utils.validateFriendlyName(name, true);
if (getGroup(name) || getDevice(name)) {
throw new Error(`friendly_name '${name}' is already in use`);
}
const settings = getInternalSettings();
const settings = getPersistedSettings();
if (!settings.groups) {
settings.groups = {};
}
if (ID == null) {
if (ID == undefined) {
// look for free ID
ID = '1';
while (settings.groups.hasOwnProperty(ID)) {
while (settings.groups[ID]) {
ID = (Number.parseInt(ID) + 1).toString();
}
} else {
// ensure provided ID is not in use
ID = ID.toString();
if (settings.groups.hasOwnProperty(ID)) {
if (settings.groups[ID]) {
throw new Error(`Group ID '${ID}' is already in use`);
}
}
@@ -664,71 +604,45 @@ export function addGroup(name: string, ID?: string): GroupOptions {
settings.groups[ID] = {friendly_name: name};
write();
return getGroup(ID);
}
function groupGetDevice(group: {devices?: string[]}, keys: string[]): string {
for (const device of group.devices ?? []) {
if (keys.includes(device)) return device;
}
return null;
}
export function addDeviceToGroup(IDorName: string, keys: string[]): void {
const groupID = getGroupThrowIfNotExists(IDorName).ID;
const settings = getInternalSettings();
const group = settings.groups[groupID];
if (!groupGetDevice(group, keys)) {
if (!group.devices) group.devices = [];
group.devices.push(keys[0]);
write();
}
}
export function removeDeviceFromGroup(IDorName: string, keys: string[]): void {
const groupID = getGroupThrowIfNotExists(IDorName).ID;
const settings = getInternalSettings();
const group = settings.groups[groupID];
if (!group.devices) {
return;
}
const key = groupGetDevice(group, keys);
if (key) {
group.devices = group.devices.filter((d) => d != key);
write();
}
return getGroup(ID)!; // valid from creation above
}
export function removeGroup(IDorName: string | number): void {
const groupID = getGroupThrowIfNotExists(IDorName.toString()).ID;
const settings = getInternalSettings();
delete settings.groups[groupID];
const groupID = getGroupThrowIfNotExists(IDorName.toString()).ID!;
const settings = getPersistedSettings();
delete settings.groups![groupID];
write();
}
export function changeEntityOptions(IDorName: string, newOptions: KeyValue): boolean {
const settings = getInternalSettings();
const settings = getPersistedSettings();
delete newOptions.friendly_name;
delete newOptions.devices;
let validator: ValidateFunction;
if (getDevice(IDorName)) {
objectAssignDeep(settings.devices[getDevice(IDorName).ID], newOptions);
utils.removeNullPropertiesFromObject(settings.devices[getDevice(IDorName).ID], NULLABLE_SETTINGS);
const device = getDevice(IDorName);
if (device) {
objectAssignDeep(settings.devices![device.ID], newOptions);
utils.removeNullPropertiesFromObject(settings.devices![device.ID], NULLABLE_SETTINGS);
validator = ajvRestartRequiredDeviceOptions;
} else if (getGroup(IDorName)) {
objectAssignDeep(settings.groups[getGroup(IDorName).ID], newOptions);
utils.removeNullPropertiesFromObject(settings.groups[getGroup(IDorName).ID], NULLABLE_SETTINGS);
validator = ajvRestartRequiredGroupOptions;
} else {
throw new Error(`Device or group '${IDorName}' does not exist`);
const group = getGroup(IDorName);
if (group) {
objectAssignDeep(settings.groups![group.ID], newOptions);
utils.removeNullPropertiesFromObject(settings.groups![group.ID], NULLABLE_SETTINGS);
validator = ajvRestartRequiredGroupOptions;
} else {
throw new Error(`Device or group '${IDorName}' does not exist`);
}
}
write();
validator(newOptions);
const restartRequired = validator.errors && !!validator.errors.find((e) => e.keyword === 'requiresRestart');
const restartRequired = Boolean(validator.errors && !!validator.errors.find((e) => e.keyword === 'requiresRestart'));
return restartRequired;
}
@@ -738,30 +652,37 @@ export function changeFriendlyName(IDorName: string, newName: string): void {
throw new Error(`friendly_name '${newName}' is already in use`);
}
const settings = getInternalSettings();
if (getDevice(IDorName)) {
settings.devices[getDevice(IDorName).ID].friendly_name = newName;
} else if (getGroup(IDorName)) {
settings.groups[getGroup(IDorName).ID].friendly_name = newName;
const settings = getPersistedSettings();
const device = getDevice(IDorName);
if (device) {
settings.devices![device.ID].friendly_name = newName;
} else {
throw new Error(`Device or group '${IDorName}' does not exist`);
const group = getGroup(IDorName);
if (group) {
settings.groups![group.ID].friendly_name = newName;
} else {
throw new Error(`Device or group '${IDorName}' does not exist`);
}
}
write();
}
export function reRead(): void {
_settings = null;
getInternalSettings();
_settingsWithDefaults = null;
_settings = undefined;
getPersistedSettings();
_settingsWithDefaults = undefined;
get();
}
export const testing = {
write,
clear: (): void => {
_settings = null;
_settingsWithDefaults = null;
_settings = undefined;
_settingsWithDefaults = undefined;
},
defaults,
CURRENT_VERSION,
};
+589
View File
@@ -0,0 +1,589 @@
import {copyFileSync, writeFileSync} from 'node:fs';
import data from './data';
import * as settings from './settings';
import utils from './utils';
interface SettingsMigration {
path: string[];
note: string;
noteIf?: (previousValue: unknown) => boolean;
}
interface SettingsAdd extends Omit<SettingsMigration, 'noteIf'> {
value: unknown;
}
type SettingsRemove = SettingsMigration;
interface SettingsChange extends SettingsMigration {
previousValueAnyOf?: unknown[];
newValue: unknown;
}
interface SettingsTransfer extends SettingsMigration {
newPath: string[];
}
interface SettingsCustomHandler extends Omit<SettingsMigration, 'path'> {
execute: (currentSettings: Partial<Settings>) => [validPath: boolean, previousValue: unknown, changed: boolean];
}
const SUPPORTED_VERSIONS: Settings['version'][] = [undefined, 2, 3, settings.CURRENT_VERSION];
function backupSettings(version: number): void {
const filePath = data.joinPath('configuration.yaml');
copyFileSync(filePath, filePath.replace('.yaml', `_backup_v${version}.yaml`));
}
/**
* Set the given path in given settings to given value. If requested, create path.
*
* @param currentSettings
* @param path
* @param value
* @param createPathIfNotExist
* @returns Returns true if value was set, false if not.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function setValue(currentSettings: any, path: string[], value: unknown, createPathIfNotExist: boolean = false): boolean {
for (let i = 0; i < path.length; i++) {
const key = path[i];
if (i === path.length - 1) {
currentSettings[key] = value;
} else {
if (!currentSettings[key]) {
if (createPathIfNotExist) {
currentSettings[key] = {};
/* v8 ignore start */
} else {
// invalid path
// ignored in test since currently call is always guarded by get-validated path, so this is never reached
return false;
}
/* v8 ignore stop */
}
currentSettings = currentSettings[key];
}
}
return true;
}
/**
* Get the value at the given path in given settings.
*
* @param currentSettings
* @param path
* @returns
* - true if path was valid
* - the value at path
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function getValue(currentSettings: any, path: string[]): [validPath: boolean, value: unknown] {
for (let i = 0; i < path.length; i++) {
const key = path[i];
const value = currentSettings[key];
if (i === path.length - 1) {
return [value !== undefined, value];
} else {
if (!value) {
// invalid path
break;
}
currentSettings = value;
}
}
return [false, undefined];
}
/**
* Add a value at given path, path is created as needed.
* @param currentSettings
* @param addition
*/
function addValue(currentSettings: Partial<Settings>, addition: SettingsAdd): void {
setValue(currentSettings, addition.path, addition.value, true);
}
/**
* Remove value at given path, if path is valid.
* Value is actually set to undefined, which triggers removal when `settings.apply` is called.
* @param currentSettings
* @param removal
* @returns
*/
function removeValue(currentSettings: Partial<Settings>, removal: SettingsRemove): [validPath: boolean, previousValue: unknown] {
const [validPath, previousValue] = getValue(currentSettings, removal.path);
if (validPath && previousValue != undefined) {
setValue(currentSettings, removal.path, undefined);
}
return [validPath, previousValue];
}
/**
* Change value at given path, if path is valid, and value matched one of the defined values (if any).
* @param currentSettings
* @param change
* @returns
*/
function changeValue(currentSettings: Partial<Settings>, change: SettingsChange): [validPath: boolean, previousValue: unknown, changed: boolean] {
const [validPath, previousValue] = getValue(currentSettings, change.path);
let changed: boolean = false;
if (validPath && previousValue !== change.newValue) {
if (!change.previousValueAnyOf || change.previousValueAnyOf.includes(previousValue)) {
setValue(currentSettings, change.path, change.newValue);
changed = true;
}
}
return [validPath, previousValue, changed];
}
/**
* Transfer value at given path, to new path.
* Given path must be valid.
* New path must not be valid or new path value must be nullish, otherwise given path is removed only.
* Value at given path is actually set to undefined, which triggers removal when `settings.apply` is called.
* New path is created as needed.
* @param currentSettings
* @param transfer
* @returns
*/
function transferValue(
currentSettings: Partial<Settings>,
transfer: SettingsTransfer,
): [validPath: boolean, previousValue: unknown, transfered: boolean] {
const [validPath, previousValue] = getValue(currentSettings, transfer.path);
const [destValidPath, destValue] = getValue(currentSettings, transfer.newPath);
const transfered = validPath && previousValue != undefined && (!destValidPath || destValue == undefined || Array.isArray(destValue));
// no point in set if already undefined
if (validPath && previousValue != undefined) {
setValue(currentSettings, transfer.path, undefined);
}
if (transfered) {
if (Array.isArray(previousValue) && Array.isArray(destValue)) {
setValue(currentSettings, transfer.newPath, [...previousValue, ...destValue], true);
} else {
setValue(currentSettings, transfer.newPath, previousValue, true);
}
}
return [validPath, previousValue, transfered];
}
const noteIfWasTrue = (previousValue: unknown): boolean => previousValue === true;
const noteIfWasDefined = (previousValue: unknown): boolean => previousValue != undefined;
const noteIfWasNonEmptyArray = (previousValue: unknown): boolean => Array.isArray(previousValue) && previousValue.length > 0;
function migrateToTwo(
currentSettings: Partial<Settings>,
transfers: SettingsTransfer[],
changes: SettingsChange[],
additions: SettingsAdd[],
removals: SettingsRemove[],
customHandlers: SettingsCustomHandler[],
): void {
transfers.push(
{
path: ['advanced', 'homeassistant_discovery_topic'],
note: `HA discovery_topic was moved from advanced.homeassistant_discovery_topic to homeassistant.discovery_topic.`,
noteIf: noteIfWasDefined,
newPath: ['homeassistant', 'discovery_topic'],
},
{
path: ['advanced', 'homeassistant_status_topic'],
note: `HA status_topic was moved from advanced.homeassistant_status_topic to homeassistant.status_topic.`,
noteIf: noteIfWasDefined,
newPath: ['homeassistant', 'status_topic'],
},
{
path: ['advanced', 'baudrate'],
note: `Baudrate was moved from advanced.baudrate to serial.baudrate.`,
noteIf: noteIfWasDefined,
newPath: ['serial', 'baudrate'],
},
{
path: ['advanced', 'rtscts'],
note: `RTSCTS was moved from advanced.rtscts to serial.rtscts.`,
noteIf: noteIfWasDefined,
newPath: ['serial', 'rtscts'],
},
{
path: ['experimental', 'transmit_power'],
note: `Transmit power was moved from experimental.transmit_power to advanced.transmit_power.`,
noteIf: noteIfWasDefined,
newPath: ['advanced', 'transmit_power'],
},
{
path: ['experimental', 'output'],
note: `Output was moved from experimental.output to advanced.output.`,
noteIf: noteIfWasDefined,
newPath: ['advanced', 'output'],
},
{
path: ['ban'],
note: `ban was renamed to passlist.`,
noteIf: noteIfWasDefined,
newPath: ['blocklist'],
},
{
path: ['whitelist'],
note: `whitelist was renamed to passlist.`,
noteIf: noteIfWasDefined,
newPath: ['passlist'],
},
);
changes.push({
path: ['advanced', 'log_level'],
note: `Log level 'warn' has been renamed to 'warning'.`,
noteIf: (previousValue): boolean => previousValue === 'warn',
previousValueAnyOf: ['warn'],
newValue: 'warning',
});
additions.push({
path: ['version'],
note: `Migrated settings to version 2`,
value: 2,
});
const haLegacyTriggers: SettingsRemove = {
path: ['homeassistant', 'legacy_triggers'],
note: `Action and click sensors have been removed (homeassistant.legacy_triggers setting). This means all sensor.*_action and sensor.*_click entities are removed. Use the MQTT device trigger instead.`,
noteIf: noteIfWasTrue,
};
const haLegacyEntityAttrs: SettingsRemove = {
path: ['homeassistant', 'legacy_entity_attributes'],
note: `Entity attributes (homeassistant.legacy_entity_attributes setting) has been removed. This means that entities discovered by Zigbee2MQTT will no longer have entity attributes (Home Assistant entity attributes are accessed via e.g. states.binary_sensor.my_sensor.attributes).`,
noteIf: noteIfWasTrue,
};
const otaIkeaUseTestUrl: SettingsRemove = {
path: ['ota', 'ikea_ota_use_test_url'],
note: `Due to the OTA rework, the ota.ikea_ota_use_test_url option has been removed.`,
noteIf: noteIfWasTrue,
};
removals.push(
haLegacyTriggers,
haLegacyEntityAttrs,
{
path: ['advanced', 'homeassistant_legacy_triggers'],
note: haLegacyTriggers.note,
noteIf: haLegacyTriggers.noteIf,
},
{
path: ['advanced', 'homeassistant_legacy_entity_attributes'],
note: haLegacyEntityAttrs.note,
noteIf: haLegacyEntityAttrs.noteIf,
},
{
path: ['permit_join'],
note: `The permit_join setting has been removed, use the frontend or MQTT to permit joining.`,
noteIf: noteIfWasTrue,
},
otaIkeaUseTestUrl,
{
path: ['advanced', 'ikea_ota_use_test_url'],
note: otaIkeaUseTestUrl.note,
noteIf: otaIkeaUseTestUrl.noteIf,
},
{
path: ['advanced', 'legacy_api'],
note: `The MQTT legacy API has been removed (advanced.legacy_api setting). See link below for affected topics.`,
noteIf: noteIfWasTrue,
},
{
path: ['advanced', 'legacy_availability_payload'],
note: `Due to the removal of advanced.legacy_availability_payload, zigbee2mqtt/bridge/state will now always be a JSON object ({"state":"online"} or {"state":"offline"})`,
noteIf: noteIfWasTrue,
},
{
path: ['advanced', 'soft_reset_timeout'],
note: `Removed deprecated: Soft reset feature (advanced.soft_reset_timeout setting)`,
noteIf: noteIfWasDefined,
},
{
path: ['advanced', 'report'],
note: `Removed deprecated: Report feature (advanced.report setting)`,
noteIf: noteIfWasTrue,
},
{
path: ['advanced', 'availability_timeout'],
note: `Removed deprecated: advanced.availability_timeout availability settings`,
noteIf: noteIfWasDefined,
},
{
path: ['advanced', 'availability_blocklist'],
note: `Removed deprecated: advanced.availability_blocklist availability settings`,
noteIf: noteIfWasNonEmptyArray,
},
{
path: ['advanced', 'availability_passlist'],
note: `Removed deprecated: advanced.availability_passlist availability settings`,
noteIf: noteIfWasNonEmptyArray,
},
{
path: ['advanced', 'availability_blacklist'],
note: `Removed deprecated: advanced.availability_blacklist availability settings`,
noteIf: noteIfWasNonEmptyArray,
},
{
path: ['advanced', 'availability_whitelist'],
note: `Removed deprecated: advanced.availability_whitelist availability settings`,
noteIf: noteIfWasNonEmptyArray,
},
{
path: ['device_options', 'legacy'],
note: `Removed everything that was enabled through device_options.legacy. See link below for affected devices.`,
noteIf: noteIfWasTrue,
},
{
path: ['experimental'],
note: `The entire experimental section was removed.`,
noteIf: noteIfWasDefined,
},
{
path: ['external_converters'],
note: `External converters are now automatically loaded from the 'data/external_converters' directory without requiring settings to be set. Make sure your external converters are still needed (might be supported out-of-the-box now), and if so, move them to that directory.`,
noteIf: noteIfWasNonEmptyArray,
},
);
// note only once
const noteEntityOptionsRetrieveState = `Retrieve state option ((devices|groups).xyz.retrieve_state setting)`;
for (const deviceKey in currentSettings.devices) {
removals.push({
path: ['devices', deviceKey, 'retrieve_state'],
note: noteEntityOptionsRetrieveState,
noteIf: noteIfWasTrue,
});
}
for (const groupKey in currentSettings.groups) {
removals.push({
path: ['groups', groupKey, 'retrieve_state'],
note: noteEntityOptionsRetrieveState,
noteIf: noteIfWasTrue,
});
removals.push({
path: ['groups', groupKey, 'devices'],
note: `Removed configuring group members through configuration.yaml (groups.xyz.devices setting). This will not impact current group members; however, you will no longer be able to add or remove devices from a group through the configuration.yaml.`,
noteIf: noteIfWasDefined,
});
}
customHandlers.push();
}
function migrateToThree(
currentSettings: Partial<Settings>,
transfers: SettingsTransfer[],
changes: SettingsChange[],
additions: SettingsAdd[],
removals: SettingsRemove[],
customHandlers: SettingsCustomHandler[],
): void {
transfers.push();
changes.push({
path: ['version'],
note: `Migrated settings to version 3`,
newValue: 3,
});
additions.push();
removals.push();
const changeToObject = (currentSettings: Partial<Settings>, path: string[]): ReturnType<SettingsCustomHandler['execute']> => {
const [validPath, previousValue] = getValue(currentSettings, path);
if (validPath) {
if (typeof previousValue === 'boolean') {
setValue(currentSettings, path, {enabled: previousValue});
} else {
setValue(currentSettings, path, {enabled: true, ...(previousValue as object)});
}
}
return [validPath, previousValue, validPath];
};
customHandlers.push(
{
note: `Property 'homeassistant' is now always an object.`,
noteIf: () => true,
execute: (currentSettings) => changeToObject(currentSettings, ['homeassistant']),
},
{
note: `Property 'frontend' is now always an object.`,
noteIf: () => true,
execute: (currentSettings) => changeToObject(currentSettings, ['frontend']),
},
{
note: `Property 'availability' is now always an object.`,
noteIf: () => true,
execute: (currentSettings) => changeToObject(currentSettings, ['availability']),
},
);
}
function migrateToFour(
currentSettings: Partial<Settings>,
transfers: SettingsTransfer[],
changes: SettingsChange[],
additions: SettingsAdd[],
removals: SettingsRemove[],
customHandlers: SettingsCustomHandler[],
): void {
transfers.push();
changes.push({
path: ['version'],
note: `Migrated settings to version 4`,
newValue: 4,
});
additions.push();
removals.push();
const saveBase64DeviceIconsAsImage = (currentSettings: Partial<Settings>): ReturnType<SettingsCustomHandler['execute']> => {
const [validPath, previousValue] = getValue(currentSettings, ['devices']);
let changed = false;
if (validPath) {
for (const deviceKey in currentSettings.devices) {
const base64Match = utils.matchBase64File(currentSettings.devices[deviceKey].icon);
if (base64Match) {
changed = true;
currentSettings.devices[deviceKey].icon = utils.saveBase64DeviceIcon(base64Match);
}
}
}
return [validPath, previousValue, changed];
};
customHandlers.push({
note: `Device icons are now saved as images.`,
noteIf: () => true,
execute: (currentSettings) => saveBase64DeviceIconsAsImage(currentSettings),
});
}
/**
* Order of execution:
* - Transfer
* - Change
* - Add
* - Remove
*
* Should allow the most flexibility whenever combination of migrations is necessary (e.g. Transfer + Change)
*/
export function migrateIfNecessary(): void {
const currentSettings = settings.getPersistedSettings();
if (!SUPPORTED_VERSIONS.includes(currentSettings.version)) {
throw new Error(
`Your configuration.yaml has an unsupported version ${currentSettings.version}, expected one of ${SUPPORTED_VERSIONS.map((v) => String(v)).join(',')}.`,
);
}
/* v8 ignore next */
const finalVersion = process.env.VITEST_WORKER_ID ? settings.testing.CURRENT_VERSION : settings.CURRENT_VERSION;
if (currentSettings.version === finalVersion) {
// when same version as current, nothing to do
return;
}
while (currentSettings.version !== finalVersion) {
let migrationNotesFileName: string | undefined;
// don't duplicate outputs
const migrationNotes: Set<string> = new Set();
const transfers: SettingsTransfer[] = [];
const changes: SettingsChange[] = [];
const additions: SettingsAdd[] = [];
const removals: SettingsRemove[] = [];
const customHandlers: SettingsCustomHandler[] = [];
backupSettings(currentSettings.version || 1);
// each version should only bump to the next version so as to gradually migrate if necessary
if (currentSettings.version == undefined) {
// migrating from 1 (`version` did not exist) to 2
migrationNotesFileName = 'migration-1-to-2.log';
migrateToTwo(currentSettings, transfers, changes, additions, removals, customHandlers);
} else if (currentSettings.version === 2) {
migrationNotesFileName = 'migration-2-to-3.log';
migrateToThree(currentSettings, transfers, changes, additions, removals, customHandlers);
} else if (currentSettings.version === 3) {
migrationNotesFileName = 'migration-3-to-4.log';
migrateToFour(currentSettings, transfers, changes, additions, removals, customHandlers);
}
for (const transfer of transfers) {
const [validPath, previousValue, transfered] = transferValue(currentSettings, transfer);
if (validPath && (!transfer.noteIf || transfer.noteIf(previousValue))) {
migrationNotes.add(`[${transfered ? 'TRANSFER' : 'REMOVAL'}] ${transfer.note}`);
}
}
for (const change of changes) {
const [validPath, previousValue, changed] = changeValue(currentSettings, change);
if (validPath && changed && (!change.noteIf || change.noteIf(previousValue))) {
migrationNotes.add(`[CHANGE] ${change.note}`);
}
}
for (const addition of additions) {
addValue(currentSettings, addition);
migrationNotes.add(`[ADDITION] ${addition.note}`);
}
for (const removal of removals) {
const [validPath, previousValue] = removeValue(currentSettings, removal);
if (validPath && (!removal.noteIf || removal.noteIf(previousValue))) {
migrationNotes.add(`[REMOVAL] ${removal.note}`);
}
}
for (const customHandler of customHandlers) {
const [validPath, previousValue, changed] = customHandler.execute(currentSettings);
if (validPath && changed && (!customHandler.noteIf || customHandler.noteIf(previousValue))) {
migrationNotes.add(`[SPECIAL] ${customHandler.note}`);
}
}
if (migrationNotesFileName && migrationNotes.size > 0) {
migrationNotes.add(`For more details, see https://github.com/Koenkk/zigbee2mqtt/discussions/24198`);
const migrationNotesFilePath = data.joinPath(migrationNotesFileName);
writeFileSync(migrationNotesFilePath, Array.from(migrationNotes).join(`\r\n\r\n`), 'utf8');
console.log(`Migration notes written in ${migrationNotesFilePath}`);
}
}
// don't throw, onboarding will validate at end of process
settings.apply(currentSettings as unknown as Record<string, unknown>, false);
settings.reRead();
}
+118 -136
View File
@@ -1,13 +1,25 @@
import type * as zhc from 'zigbee-herdsman-converters';
import type {Zigbee2MQTTAPI, Zigbee2MQTTResponse, Zigbee2MQTTResponseEndpoints, Zigbee2MQTTScene} from '../types/api';
import {exec} from 'child_process';
import assert from 'node:assert';
import crypto from 'node:crypto';
import fs from 'node:fs';
import path from 'node:path';
import equals from 'fast-deep-equal/es6';
import fs from 'fs';
import humanizeDuration from 'humanize-duration';
import path from 'path';
import vm from 'vm';
import data from './data';
const BASE64_IMAGE_REGEX = new RegExp(`data:image/(?<extension>.+);base64,(?<data>.+)`);
function pad(num: number): string {
const norm = Math.floor(Math.abs(num));
return (norm < 10 ? '0' : '') + norm;
}
// construct a local ISO8601 string (instead of UTC-based)
// Example:
// - ISO8601 (UTC) = 2019-03-01T15:32:45.941+0000
@@ -15,10 +27,6 @@ import data from './data';
function toLocalISOString(date: Date): string {
const tzOffset = -date.getTimezoneOffset();
const plusOrMinus = tzOffset >= 0 ? '+' : '-';
const pad = (num: number): string => {
const norm = Math.floor(Math.abs(num));
return (norm < 10 ? '0' : '') + norm;
};
return (
date.getFullYear() +
@@ -43,42 +51,35 @@ function capitalize(s: string): string {
return s[0].toUpperCase() + s.slice(1);
}
async function getZigbee2MQTTVersion(includeCommitHash = true): Promise<{commitHash: string; version: string}> {
const git = await import('git-last-commit');
const packageJSON = await import('../..' + '/package.json');
export async function getZigbee2MQTTVersion(includeCommitHash = true): Promise<{commitHash?: string; version: string}> {
const packageJSON = (await import('../../package.json', {with: {type: 'json'}})).default;
const version = packageJSON.version;
let commitHash: string | undefined;
if (!includeCommitHash) {
return {version: packageJSON.version, commitHash: null};
return {version, commitHash};
}
return new Promise((resolve) => {
const version = packageJSON.version;
return await new Promise((resolve) => {
exec('git rev-parse --short=8 HEAD', (error, stdout) => {
commitHash = stdout.trim();
git.getLastCommit((err: Error, commit: {shortHash: string}) => {
let commitHash = null;
if (err) {
if (error || commitHash === '') {
try {
commitHash = fs.readFileSync(path.join(__dirname, '..', '..', 'dist', '.hash'), 'utf-8');
} catch {
/* istanbul ignore next */
commitHash = 'unknown';
}
} else {
commitHash = commit.shortHash;
}
commitHash = commitHash.trim();
resolve({commitHash, version});
});
});
}
async function getDependencyVersion(depend: string): Promise<{version: string}> {
const modulePath = path.dirname(require.resolve(depend));
const packageJSONPath = path.join(modulePath.slice(0, modulePath.indexOf(depend) + depend.length), 'package.json');
const packageJSON = await import(packageJSONPath);
const version = packageJSON.version;
const packageJsonPath = require.resolve(`${depend}/package.json`);
const version = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')).version;
return {version};
}
@@ -100,7 +101,7 @@ function objectIsEmpty(object: object): boolean {
function objectHasProperties(object: {[s: string]: unknown}, properties: string[]): boolean {
for (const property of properties) {
if (!object.hasOwnProperty(property)) {
if (object[property] === undefined) {
return false;
}
}
@@ -118,17 +119,39 @@ function equalsPartial(object: KeyValue, expected: KeyValue): boolean {
return true;
}
function getObjectProperty(object: KeyValue, key: string, defaultValue: unknown): unknown {
return object && object.hasOwnProperty(key) ? object[key] : defaultValue;
function getObjectProperty<T>(object: KeyValue, key: string, defaultValue: NoInfer<T>): T {
return object && object[key] !== undefined ? object[key] : defaultValue;
}
function getResponse(request: KeyValue | string, data: KeyValue, error: string): MQTTResponse {
const response: MQTTResponse = {data, status: error ? 'error' : 'ok'};
if (error) response.error = error;
if (typeof request === 'object' && request.hasOwnProperty('transaction')) {
response.transaction = request.transaction;
function getResponse<T extends Zigbee2MQTTResponseEndpoints>(
request: KeyValue | string,
data: Zigbee2MQTTAPI[T],
error?: string,
): Zigbee2MQTTResponse<T> {
if (error !== undefined) {
const response: Zigbee2MQTTResponse<T> = {
data: {}, // always return an empty `data` payload on error
status: 'error',
error: error,
};
if (typeof request === 'object' && request.transaction !== undefined) {
response.transaction = request.transaction;
}
return response;
} else {
const response: Zigbee2MQTTResponse<T> = {
data, // valid from error check
status: 'ok',
};
if (typeof request === 'object' && request.transaction !== undefined) {
response.transaction = request.transaction;
}
return response;
}
return response;
}
function parseJSON(value: string, fallback: string): KeyValue | string {
@@ -139,48 +162,6 @@ function parseJSON(value: string, fallback: string): KeyValue | string {
}
}
function loadModuleFromText(moduleCode: string, name?: string): unknown {
const moduleFakePath = path.join(__dirname, '..', '..', 'data', 'extension', name || 'externally-loaded.js');
const sandbox = {
require: require,
module: {},
console,
setTimeout,
clearTimeout,
setInterval,
clearInterval,
setImmediate,
clearImmediate,
};
vm.runInNewContext(moduleCode, sandbox, moduleFakePath);
/* eslint-disable-line */ // @ts-ignore
return sandbox.module.exports;
}
function loadModuleFromFile(modulePath: string): unknown {
const moduleCode = fs.readFileSync(modulePath, {encoding: 'utf8'});
return loadModuleFromText(moduleCode);
}
export function* loadExternalConverter(moduleName: string): Generator<ExternalDefinition> {
let converter;
if (moduleName.endsWith('.js')) {
converter = loadModuleFromFile(data.joinPath(moduleName));
} else {
// eslint-disable-next-line @typescript-eslint/no-require-imports
converter = require(moduleName);
}
if (Array.isArray(converter)) {
for (const item of converter) {
yield item;
}
} else {
yield converter;
}
}
/**
* Delete all keys from passed object that have null/undefined values.
*
@@ -189,8 +170,12 @@ export function* loadExternalConverter(moduleName: string): Generator<ExternalDe
*/
function removeNullPropertiesFromObject(obj: KeyValue, ignoreKeys: string[] = []): void {
for (const key of Object.keys(obj)) {
if (ignoreKeys.includes(key)) continue;
if (ignoreKeys.includes(key)) {
continue;
}
const value = obj[key];
if (value == null) {
delete obj[key];
} else if (typeof value === 'object') {
@@ -204,26 +189,6 @@ function toNetworkAddressHex(value: number): string {
return `0x${'0'.repeat(4 - hex.length)}${hex}`;
}
// eslint-disable-next-line
function toSnakeCase(value: string | KeyValue): any {
if (typeof value === 'object') {
value = {...value};
for (const key of Object.keys(value)) {
const keySnakeCase = toSnakeCase(key);
if (key !== keySnakeCase) {
value[keySnakeCase] = value[key];
delete value[key];
}
}
return value;
} else {
return value
.replace(/\.?([A-Z])/g, (x, y) => '_' + y.toLowerCase())
.replace(/^_/, '')
.replace('_i_d', '_id');
}
}
function charRange(start: string, stop: string): number[] {
const result = [];
for (let idx = start.charCodeAt(0), end = stop.charCodeAt(0); idx <= end; ++idx) {
@@ -299,32 +264,19 @@ function isAvailabilityEnabledForEntity(entity: Device | Group, settings: Settin
return !!entity.options.availability;
}
// availability_timeout = deprecated
if (!(settings.advanced.availability_timeout || settings.availability)) {
if (!settings.availability.enabled) {
return false;
}
const passlist = settings.advanced.availability_passlist.concat(settings.advanced.availability_whitelist);
if (passlist.length > 0) {
return passlist.includes(entity.name) || passlist.includes(entity.ieeeAddr);
}
const blocklist = settings.advanced.availability_blacklist.concat(settings.advanced.availability_blocklist);
if (blocklist.length > 0) {
return !blocklist.includes(entity.name) && !blocklist.includes(entity.ieeeAddr);
}
return true;
}
function isEndpoint(obj: unknown): obj is zh.Endpoint {
return obj.constructor.name.toLowerCase() === 'endpoint';
function isZHEndpoint(obj: unknown): obj is zh.Endpoint {
return obj?.constructor.name.toLowerCase() === 'endpoint';
}
function flatten<Type>(arr: Type[][]): Type[] {
return [].concat(...arr);
return ([] as Type[]).concat(...arr);
}
function arrayUnique<Type>(arr: Type[]): Type[] {
@@ -332,11 +284,7 @@ function arrayUnique<Type>(arr: Type[]): Type[] {
}
function isZHGroup(obj: unknown): obj is zh.Group {
return obj.constructor.name.toLowerCase() === 'group';
}
function availabilityPayload(state: 'online' | 'offline', settings: Settings): string {
return settings.advanced.legacy_availability_payload ? state : JSON.stringify({state});
return obj?.constructor.name.toLowerCase() === 'group';
}
const hours = (hours: number): number => 1000 * 60 * 60 * hours;
@@ -362,7 +310,7 @@ async function publishLastSeen(
}
}
function filterProperties(filter: string[], data: KeyValue): void {
function filterProperties(filter: string[] | undefined, data: KeyValue): void {
if (filter) {
for (const property of Object.keys(data)) {
if (filter.find((p) => property.match(`^${p}$`))) {
@@ -372,22 +320,38 @@ function filterProperties(filter: string[], data: KeyValue): void {
}
}
export function isNumericExposeFeature(feature: zhc.Feature): feature is zhc.Numeric {
return feature?.type === 'numeric';
export function isNumericExpose(expose: zhc.Expose): expose is zhc.Numeric {
return expose?.type === 'numeric';
}
export function isEnumExposeFeature(feature: zhc.Feature): feature is zhc.Enum {
return feature?.type === 'enum';
export function assertEnumExpose(expose: zhc.Expose): asserts expose is zhc.Enum {
assert(expose?.type === 'enum');
}
export function isBinaryExposeFeature(feature: zhc.Feature): feature is zhc.Binary {
return feature?.type === 'binary';
export function assertNumericExpose(expose: zhc.Expose): asserts expose is zhc.Numeric {
assert(expose?.type === 'numeric');
}
function getScenes(entity: zh.Endpoint | zh.Group): Scene[] {
const scenes: {[id: number]: Scene} = {};
const endpoints = isEndpoint(entity) ? [entity] : entity.members;
const groupID = isEndpoint(entity) ? 0 : entity.groupID;
export function assertBinaryExpose(expose: zhc.Expose): asserts expose is zhc.Binary {
assert(expose?.type === 'binary');
}
export function isEnumExpose(expose: zhc.Expose): expose is zhc.Enum {
return expose?.type === 'enum';
}
export function isBinaryExpose(expose: zhc.Expose): expose is zhc.Binary {
return expose?.type === 'binary';
}
export function isLightExpose(expose: zhc.Expose): expose is zhc.Light {
return expose.type === 'light';
}
function getScenes(entity: zh.Endpoint | zh.Group): Zigbee2MQTTScene[] {
const scenes: {[id: number]: Zigbee2MQTTScene} = {};
const endpoints = isZHEndpoint(entity) ? [entity] : entity.members;
const groupID = isZHEndpoint(entity) ? 0 : entity.groupID;
for (const endpoint of endpoints) {
for (const [key, data] of Object.entries(endpoint.meta?.scenes || {})) {
@@ -407,10 +371,32 @@ function deviceNotCoordinator(device: zh.Device): boolean {
return device.type !== 'Coordinator';
}
/* istanbul ignore next */
function matchBase64File(value: string | undefined): {extension: string; data: string} | false {
if (value !== undefined) {
const match = value.match(BASE64_IMAGE_REGEX);
if (match) {
assert(match.groups?.extension && match.groups?.data);
return {extension: match.groups.extension, data: match.groups.data};
}
}
return false;
}
function saveBase64DeviceIcon(base64Match: {extension: string; data: string}): string {
const md5Hash = crypto.createHash('md5').update(base64Match.data).digest('hex');
const fileSettings = `device_icons/${md5Hash}.${base64Match.extension}`;
const file = path.join(data.getPath(), fileSettings);
fs.mkdirSync(path.dirname(file), {recursive: true});
fs.writeFileSync(file, base64Match.data, {encoding: 'base64'});
return fileSettings;
}
/* v8 ignore next */
const noop = (): void => {};
export default {
matchBase64File,
saveBase64DeviceIcon,
capitalize,
getZigbee2MQTTVersion,
getDependencyVersion,
@@ -421,12 +407,9 @@ export default {
getObjectProperty,
getResponse,
parseJSON,
loadModuleFromText,
loadModuleFromFile,
removeNullPropertiesFromObject,
toNetworkAddressHex,
toSnakeCase,
isEndpoint,
isZHEndpoint,
isZHGroup,
hours,
minutes,
@@ -436,7 +419,6 @@ export default {
sanitizeImageParameter,
isAvailabilityEnabledForEntity,
publishLastSeen,
availabilityPayload,
getAllFiles,
filterProperties,
flatten,
+25 -7
View File
@@ -1,26 +1,44 @@
import assert from 'node:assert';
import fs from 'node:fs';
import equals from 'fast-deep-equal/es6';
import fs from 'fs';
import yaml from 'js-yaml';
import yaml, {YAMLException} from 'js-yaml';
export class YAMLFileException extends YAMLException {
file: string;
constructor(error: YAMLException, file: string) {
super(error.reason, error.mark);
this.name = 'YAMLFileException';
this.cause = error.cause;
this.message = error.message;
this.stack = error.stack;
this.file = file;
}
}
function read(file: string): KeyValue {
try {
const result = yaml.load(fs.readFileSync(file, 'utf8'));
return (result as KeyValue) ?? {};
assert(result instanceof Object);
return result as KeyValue;
} catch (error) {
if (error.name === 'YAMLException') {
error.file = file;
if (error instanceof YAMLException) {
throw new YAMLFileException(error, file);
}
throw error;
}
}
function readIfExists(file: string, default_?: KeyValue): KeyValue {
return fs.existsSync(file) ? read(file) : default_;
function readIfExists(file: string, fallback: KeyValue = {}): KeyValue {
return fs.existsSync(file) ? read(file) : fallback;
}
function writeIfChanged(file: string, content: KeyValue): void {
const before = readIfExists(file);
if (!equals(before, content)) {
fs.writeFileSync(file, yaml.dump(content));
}
+66 -58
View File
@@ -1,8 +1,11 @@
import type {Events as ZHEvents} from 'zigbee-herdsman';
import {randomInt} from 'node:crypto';
import bind from 'bind-decorator';
import {randomInt} from 'crypto';
import stringify from 'json-stable-stringify-without-jsonify';
import {Controller} from 'zigbee-herdsman';
import * as ZHEvents from 'zigbee-herdsman/dist/controller/events';
import Device from './model/device';
import Group from './model/group';
@@ -14,6 +17,7 @@ import utils from './util/utils';
const entityIDRegex = new RegExp(`^(.+?)(?:/([^/]+))?$`);
export default class Zigbee {
// @ts-expect-error initialized in start
private herdsman: Controller;
private eventBus: EventBus;
private groupLookup: {[s: number]: Group} = {};
@@ -26,16 +30,15 @@ export default class Zigbee {
async start(): Promise<'reset' | 'resumed' | 'restored'> {
const infoHerdsman = await utils.getDependencyVersion('zigbee-herdsman');
logger.info(`Starting zigbee-herdsman (${infoHerdsman.version})`);
const panId = settings.get().advanced.pan_id;
const extPanId = settings.get().advanced.ext_pan_id;
const networkKey = settings.get().advanced.network_key;
const herdsmanSettings = {
network: {
panID: settings.get().advanced.pan_id === 'GENERATE' ? this.generatePanID() : (settings.get().advanced.pan_id as number),
extendedPanID:
settings.get().advanced.ext_pan_id === 'GENERATE' ? this.generateExtPanID() : (settings.get().advanced.ext_pan_id as number[]),
panID: panId === 'GENERATE' ? this.generatePanID() : panId,
extendedPanID: extPanId === 'GENERATE' ? this.generateExtPanID() : extPanId,
channelList: [settings.get().advanced.channel],
networkKey:
settings.get().advanced.network_key === 'GENERATE'
? this.generateNetworkKey()
: (settings.get().advanced.network_key as number[]),
networkKey: networkKey === 'GENERATE' ? this.generateNetworkKey() : networkKey,
},
databasePath: data.joinPath('database.db'),
databaseBackupPath: data.joinPath('database.db.backup'),
@@ -55,8 +58,10 @@ export default class Zigbee {
acceptJoiningDeviceHandler: this.acceptJoiningDeviceHandler,
};
const herdsmanSettingsLog = JSON.stringify(herdsmanSettings).replaceAll(JSON.stringify(herdsmanSettings.network.networkKey), '"HIDDEN"');
logger.debug(`Using zigbee-herdsman with settings: '${stringify(herdsmanSettingsLog)}'`);
logger.debug(
() =>
`Using zigbee-herdsman with settings: '${stringify(JSON.stringify(herdsmanSettings).replaceAll(JSON.stringify(herdsmanSettings.network.networkKey), '"HIDDEN"'))}'`,
);
let startResult;
try {
@@ -67,30 +72,28 @@ export default class Zigbee {
throw error;
}
for (const device of this.devicesIterator(utils.deviceNotCoordinator)) {
await device.resolveDefinition();
}
await this.resolveDevicesDefinitions();
this.herdsman.on('adapterDisconnected', () => this.eventBus.emitAdapterDisconnected());
this.herdsman.on('lastSeenChanged', (data: ZHEvents.LastSeenChangedPayload) => {
this.eventBus.emitLastSeenChanged({device: this.resolveDevice(data.device.ieeeAddr), reason: data.reason});
this.eventBus.emitLastSeenChanged({device: this.resolveDevice(data.device.ieeeAddr)!, reason: data.reason});
});
this.herdsman.on('permitJoinChanged', (data: ZHEvents.PermitJoinChangedPayload) => {
this.eventBus.emitPermitJoinChanged(data);
});
this.herdsman.on('deviceNetworkAddressChanged', (data: ZHEvents.DeviceNetworkAddressChangedPayload) => {
const device = this.resolveDevice(data.device.ieeeAddr);
const device = this.resolveDevice(data.device.ieeeAddr)!;
logger.debug(`Device '${device.name}' changed network address`);
this.eventBus.emitDeviceNetworkAddressChanged({device});
});
this.herdsman.on('deviceAnnounce', (data: ZHEvents.DeviceAnnouncePayload) => {
const device = this.resolveDevice(data.device.ieeeAddr);
const device = this.resolveDevice(data.device.ieeeAddr)!;
logger.debug(`Device '${device.name}' announced itself`);
this.eventBus.emitDeviceAnnounce({device});
});
this.herdsman.on('deviceInterview', async (data: ZHEvents.DeviceInterviewPayload) => {
const device = this.resolveDevice(data.device.ieeeAddr);
/* istanbul ignore if */ if (!device) return; // Prevent potential race
/* v8 ignore next */ if (!device) return; // Prevent potential race
await device.resolveDefinition();
const d = {device, status: data.status};
this.logDeviceInterview(d);
@@ -98,7 +101,7 @@ export default class Zigbee {
});
this.herdsman.on('deviceJoined', async (data: ZHEvents.DeviceJoinedPayload) => {
const device = this.resolveDevice(data.device.ieeeAddr);
/* istanbul ignore if */ if (!device) return; // Prevent potential race
/* v8 ignore next */ if (!device) return; // Prevent potential race
await device.resolveDefinition();
logger.info(`Device '${device.name}' joined`);
this.eventBus.emitDeviceJoined({device});
@@ -109,12 +112,13 @@ export default class Zigbee {
this.eventBus.emitDeviceLeave({ieeeAddr: data.ieeeAddr, name});
});
this.herdsman.on('message', async (data: ZHEvents.MessagePayload) => {
const device = this.resolveDevice(data.device.ieeeAddr);
const device = this.resolveDevice(data.device.ieeeAddr)!;
await device.resolveDefinition();
logger.debug(
`Received Zigbee message from '${device.name}', type '${data.type}', ` +
() =>
`Received Zigbee message from '${device.name}', type '${data.type}', ` +
`cluster '${data.cluster}', data '${stringify(data.data)}' from endpoint ${data.endpoint.ID}` +
(data.hasOwnProperty('groupID') ? ` with groupID ${data.groupID}` : ``) +
(data['groupID'] !== undefined ? ` with groupID ${data.groupID}` : ``) +
(device.zh.type === 'Coordinator' ? `, ignoring since it is from coordinator` : ``),
);
if (device.zh.type === 'Coordinator') return;
@@ -133,7 +137,7 @@ export default class Zigbee {
try {
await device.zh.removeFromNetwork();
} catch (error) {
logger.error(`Failed to remove '${device.ieeeAddr}' (${error.message})`);
logger.error(`Failed to remove '${device.ieeeAddr}' (${(error as Error).message})`);
}
};
@@ -157,7 +161,7 @@ export default class Zigbee {
logger.info(`Successfully interviewed '${name}', device has successfully been paired`);
if (data.device.isSupported) {
const {vendor, description, model} = data.device.definition;
const {vendor, description, model} = data.device.definition!;
logger.info(`Device '${name}' is supported, identified as: ${vendor} ${description} (${model})`);
} else {
logger.warning(
@@ -193,7 +197,7 @@ export default class Zigbee {
}
async getCoordinatorVersion(): Promise<zh.CoordinatorVersion> {
return this.herdsman.getCoordinatorVersion();
return await this.herdsman.getCoordinatorVersion();
}
isStopping(): boolean {
@@ -201,20 +205,16 @@ export default class Zigbee {
}
async backup(): Promise<void> {
return this.herdsman.backup();
return await this.herdsman.backup();
}
async coordinatorCheck(): Promise<{missingRouters: Device[]}> {
const check = await this.herdsman.coordinatorCheck();
return {missingRouters: check.missingRouters.map((d) => this.resolveDevice(d.ieeeAddr))};
return {missingRouters: check.missingRouters.map((d) => this.resolveDevice(d.ieeeAddr)!)};
}
async getNetworkParameters(): Promise<zh.NetworkParameters> {
return this.herdsman.getNetworkParameters();
}
async reset(type: 'soft' | 'hard'): Promise<void> {
await this.herdsman.reset(type);
return await this.herdsman.getNetworkParameters();
}
async stop(): Promise<void> {
@@ -227,25 +227,27 @@ export default class Zigbee {
return this.herdsman.getPermitJoin();
}
getPermitJoinTimeout(): number {
return this.herdsman.getPermitJoinTimeout();
getPermitJoinEnd(): number | undefined {
return this.herdsman.getPermitJoinEnd();
}
async permitJoin(permit: boolean, device?: Device, time: number = undefined): Promise<void> {
if (permit) {
async permitJoin(time: number, device?: Device): Promise<void> {
if (time > 0) {
logger.info(`Zigbee: allowing new devices to join${device ? ` via ${device.name}` : ''}.`);
} else {
logger.info('Zigbee: disabling joining new devices.');
}
if (device && permit) {
await this.herdsman.permitJoin(permit, device.zh, time);
} else {
await this.herdsman.permitJoin(permit, undefined, time);
await this.herdsman.permitJoin(time, device?.zh);
}
async resolveDevicesDefinitions(ignoreCache: boolean = false): Promise<void> {
for (const device of this.devicesIterator(utils.deviceNotCoordinator)) {
await device.resolveDefinition(ignoreCache);
}
}
@bind private resolveDevice(ieeeAddr: string): Device {
@bind private resolveDevice(ieeeAddr: string): Device | undefined {
if (!this.deviceLookup[ieeeAddr]) {
const device = this.herdsman.getDeviceByIeeeAddr(ieeeAddr);
if (device) {
@@ -269,16 +271,20 @@ export default class Zigbee {
return this.groupLookup[groupID];
}
resolveEntity(key: string | number | zh.Device): Device | Group {
resolveEntity(key: string | number | zh.Device): Device | Group | undefined {
if (typeof key === 'object') {
return this.resolveDevice(key.ieeeAddr);
} else if (typeof key === 'string' && key.toLowerCase() === 'coordinator') {
return this.resolveDevice(this.herdsman.getDevicesByType('Coordinator')[0].ieeeAddr);
} else {
const settingsDevice = settings.getDevice(key.toString());
if (settingsDevice) return this.resolveDevice(settingsDevice.ID);
if (settingsDevice) {
return this.resolveDevice(settingsDevice.ID);
}
const groupSettings = settings.getGroup(key);
if (groupSettings) {
const group = this.resolveGroup(groupSettings.ID);
// If group does not exist, create it (since it's already in configuration.yaml)
@@ -287,7 +293,7 @@ export default class Zigbee {
}
}
resolveEntityAndEndpoint(ID: string): {ID: string; entity: Device | Group; endpointID: string; endpoint: zh.Endpoint} {
resolveEntityAndEndpoint(ID: string): {ID: string; entity: Device | Group | undefined; endpointID?: string; endpoint?: zh.Endpoint} {
// This function matches the following entity formats:
// device_name (just device name)
// device_name/ep_name (device name and endpoint numeric ID or name)
@@ -297,25 +303,27 @@ export default class Zigbee {
// The function tries to find an exact match first
let entityName = ID;
let deviceOrGroup = this.resolveEntity(ID);
let endpointNameOrID = undefined;
let endpointNameOrID: string | undefined;
// If exact match did not happenc, try matching a device_name/endpoint pattern
// If exact match did not happen, try matching a device_name/endpoint pattern
if (!deviceOrGroup) {
// First split the input token by the latest slash
const match = ID.match(entityIDRegex);
// Get the resulting IDs from the match
entityName = match[1];
deviceOrGroup = this.resolveEntity(match[1]);
endpointNameOrID = match[2];
if (match) {
// Get the resulting IDs from the match
entityName = match[1];
deviceOrGroup = this.resolveEntity(entityName);
endpointNameOrID = match[2];
}
}
// If the function returns non-null endpoint name, but the endpoint field is null, then
// it means that endpoint was not matched because there is no such endpoint on the device
// (or the entity is a group)
const endpoint = deviceOrGroup?.isDevice() ? deviceOrGroup.endpoint(endpointNameOrID) : null;
const endpoint = deviceOrGroup?.isDevice() ? deviceOrGroup.endpoint(endpointNameOrID) : undefined;
return {ID: entityName, entity: deviceOrGroup, endpointID: endpointNameOrID, endpoint: endpoint};
return {ID: entityName, entity: deviceOrGroup, endpointID: endpointNameOrID, endpoint};
}
firstCoordinatorEndpoint(): zh.Endpoint {
@@ -327,7 +335,7 @@ export default class Zigbee {
groupPredicate?: (value: zh.Group) => boolean,
): Generator<Device | Group> {
for (const device of this.herdsman.getDevicesIterator(devicePredicate)) {
yield this.resolveDevice(device.ieeeAddr);
yield this.resolveDevice(device.ieeeAddr)!;
}
for (const group of this.herdsman.getGroupsIterator(groupPredicate)) {
@@ -343,7 +351,7 @@ export default class Zigbee {
*devicesIterator(predicate?: (value: zh.Device) => boolean): Generator<Device> {
for (const device of this.herdsman.getDevicesIterator(predicate)) {
yield this.resolveDevice(device.ieeeAddr);
yield this.resolveDevice(device.ieeeAddr)!;
}
}
@@ -373,11 +381,11 @@ export default class Zigbee {
}
async touchlinkFactoryResetFirst(): Promise<boolean> {
return this.herdsman.touchlinkFactoryResetFirst();
return await this.herdsman.touchlinkFactoryResetFirst();
}
async touchlinkFactoryReset(ieeeAddr: string, channel: number): Promise<boolean> {
return this.herdsman.touchlinkFactoryReset(ieeeAddr, channel);
return await this.herdsman.touchlinkFactoryReset(ieeeAddr, channel);
}
async addInstallCode(installCode: string): Promise<void> {
@@ -389,7 +397,7 @@ export default class Zigbee {
}
async touchlinkScan(): Promise<{ieeeAddr: string; channel: number}[]> {
return this.herdsman.touchlinkScan();
return await this.herdsman.touchlinkScan();
}
createGroup(ID: number): Group {
@@ -397,7 +405,7 @@ export default class Zigbee {
return this.resolveGroup(ID);
}
deviceByNetworkAddress(networkAddress: number): Device {
deviceByNetworkAddress(networkAddress: number): Device | undefined {
const device = this.herdsman.getDeviceByNetworkAddress(networkAddress);
return device && this.resolveDevice(device.ieeeAddr);
}
-10536
View File
File diff suppressed because it is too large Load Diff
+44 -55
View File
@@ -1,14 +1,15 @@
{
"name": "zigbee2mqtt",
"version": "1.40.0",
"version": "2.2.1",
"description": "Zigbee to MQTT bridge using Zigbee-herdsman",
"main": "index.js",
"packageManager": "pnpm@10.4.1",
"repository": {
"type": "git",
"url": "git+https://github.com/Koenkk/zigbee2mqtt.git"
},
"engines": {
"node": "^18 || ^20 || ^22"
"node": "^18 || ^20 || ^22 || ^23"
},
"keywords": [
"xiaomi",
@@ -21,14 +22,17 @@
],
"scripts": {
"build": "tsc && node index.js writehash",
"build-watch": "tsc --watch",
"eslint": "eslint lib/ --max-warnings=0",
"build:watch": "tsc --watch",
"eslint": "eslint --max-warnings=0",
"pretty:write": "prettier --write .",
"pretty:check": "prettier --check .",
"start": "node index.js",
"test-with-coverage": "jest test --silent --maxWorkers=50% --coverage",
"test": "jest test --silent --maxWorkers=50%",
"test-watch": "jest test --silent --maxWorkers=25% --watch"
"check": "pnpm run pretty:check && pnpm run eslint",
"test": "vitest run --config ./test/vitest.config.mts",
"test:coverage": "vitest run --config ./test/vitest.config.mts --coverage",
"test:watch": "vitest watch --config ./test/vitest.config.mts",
"prepack": "pnpm run clean && pnpm run build",
"clean": "rimraf coverage dist tsconfig.tsbuildinfo"
},
"author": "Koen Kanters",
"license": "GPL-3.0",
@@ -39,79 +43,64 @@
"dependencies": {
"ajv": "^8.17.1",
"bind-decorator": "^1.0.11",
"connect-gzip-static": "3.0.1",
"debounce": "^2.1.0",
"debounce": "^2.2.0",
"express-static-gzip": "^2.2.0",
"fast-deep-equal": "^3.1.3",
"finalhandler": "^1.2.0",
"git-last-commit": "^1.0.1",
"finalhandler": "^2.1.0",
"humanize-duration": "^3.32.1",
"js-yaml": "^4.1.0",
"json-stable-stringify-without-jsonify": "^1.0.1",
"jszip": "^3.10.1",
"mkdir-recursive": "^0.4.0",
"moment": "^2.30.1",
"mqtt": "^5.10.0",
"mqtt": "^5.10.4",
"object-assign-deep": "^0.4.0",
"rimraf": "^6.0.1",
"semver": "^7.6.3",
"semver": "^7.7.1",
"source-map-support": "^0.5.21",
"uri-js": "^4.4.1",
"winston": "^3.14.2",
"throttleit": "^2.1.0",
"winston": "^3.17.0",
"winston-syslog": "^2.7.1",
"winston-transport": "^4.7.1",
"ws": "^8.18.0",
"zigbee-herdsman": "0.57.1",
"zigbee-herdsman-converters": "20.8.4",
"zigbee2mqtt-frontend": "0.7.4"
"winston-transport": "^4.9.0",
"ws": "^8.18.1",
"zigbee-herdsman": "3.4.11",
"zigbee-herdsman-converters": "23.20.1",
"zigbee2mqtt-frontend": "0.9.7"
},
"devDependencies": {
"@babel/core": "^7.25.2",
"@babel/plugin-proposal-decorators": "^7.24.7",
"@babel/preset-env": "^7.25.4",
"@babel/preset-typescript": "^7.24.7",
"@eslint/core": "^0.12.0",
"@eslint/js": "^9.23.0",
"@ianvs/prettier-plugin-sort-imports": "^4.4.1",
"@types/finalhandler": "^1.2.3",
"@types/humanize-duration": "^3.27.4",
"@types/jest": "^29.5.12",
"@types/js-yaml": "^4.0.9",
"@types/node": "^22.5.0",
"@types/node": "^22.13.14",
"@types/object-assign-deep": "^0.4.3",
"@types/readable-stream": "4.0.15",
"@types/ws": "8.5.12",
"@typescript-eslint/eslint-plugin": "^8.2.0",
"@typescript-eslint/parser": "^8.2.0",
"babel-jest": "^29.7.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-jest": "^28.8.0",
"eslint-plugin-perfectionist": "^3.2.0",
"jest": "^29.7.0",
"prettier": "^3.3.3",
"@types/readable-stream": "4.0.18",
"@types/serve-static": "^1.15.7",
"@types/ws": "8.18.0",
"@vitest/coverage-v8": "^3.0.9",
"eslint": "^9.23.0",
"eslint-config-prettier": "^10.1.1",
"prettier": "^3.5.3",
"tmp": "^0.2.3",
"typescript": "^5.5.4"
"typescript": "^5.8.2",
"typescript-eslint": "^8.28.0",
"vitest": "^3.0.9"
},
"overrides": {
"zigbee-herdsman-converters": {
"pnpm": {
"overrides": {
"zigbee-herdsman": "$zigbee-herdsman"
}
},
"jest": {
"coverageThreshold": {
"global": {
"branches": 100,
"functions": 100,
"lines": 100,
"statements": 100
}
},
"collectCoverageFrom": [
"lib/**/*.js",
"lib/**/*.ts"
"onlyBuiltDependencies": [
"@serialport/bindings-cpp",
"esbuild",
"unix-dgram"
]
},
"bin": {
"zigbee2mqtt": "cli.js"
},
"optionalDependencies": {
"sd-notify": "^2.8.0"
"unix-dgram": "^2.0.6"
}
}
+3519
View File
File diff suppressed because it is too large Load Diff
+3 -2
View File
@@ -49,7 +49,8 @@ if (fs.existsSync(commitUserFile)) {
commitUserLookup = JSON.parse(fs.readFileSync(commitUserFile, 'utf8'));
}
const whiteLabels = zhc.definitions.filter((d) => d.whiteLabel).flatMap((d) => d.whiteLabel);
const definitions = require('zigbee-herdsman-converters/devices/index').default.map((d) => zhc.prepareDefinition(d));
const whiteLabels = definitions.filter((d) => d.whiteLabel).flatMap((d) => d.whiteLabel);
const capitalizeFirstChar = (str) => str.charAt(0).toUpperCase() + str.slice(1);
for (const changelog of changelogs) {
@@ -97,7 +98,7 @@ for (const changelog of changelogs) {
if (localContext === 'add') {
for (const model of message.split(',')) {
const definition = zhc.definitions.find((d) => d.model === model.trim());
const definition = definitions.find((d) => d.model === model.trim());
const whiteLabel = whiteLabels.find((d) => d.model === model.trim());
const match = definition || whiteLabel;
if (match) {
-75
View File
@@ -1,75 +0,0 @@
#!/bin/bash
function zigbee2mqtt-show-short-info {
echo "Setup for Zigbee2MQTT bridge."
}
function zigbee2mqtt-show-long-info {
echo "This script installs the Zigbee2MQTT bridge"
}
function zigbee2mqtt-show-copyright-info {
echo "Original concept by Landrash <https://github.com/landrash>."
}
function zigbee2mqtt-install-package {
echo -n "Installing dependencies : "
node=$(which npm)
if [ -z "${node}" ]; then #Installing NodeJS if not already installed.
printf "Downloading and installing NodeJS...\\n"
curl -sL https://deb.nodesource.com/setup_10.x | bash -
apt install -y nodejs
fi
echo "Cloning Zigbee2MQTT git repository"
git clone https://github.com/Koenkk/zigbee2mqtt.git /opt/zigbee2mqtt
chown -R pi:pi /opt/zigbee2mqtt
echo "Running install. This might take a while and can produce some expected errors"
cd /opt/zigbee2mqtt || exit
su pi -c "npm ci"
echo "Creating service file zigbee2mqtt.service"
service_path="/etc/systemd/system/zigbee2mqtt.service"
echo "[Unit]
Description=zigbee2mqtt
After=network.target
[Service]
Type=notify
ExecStart=/usr/bin/node index.js
WorkingDirectory=/opt/zigbee2mqtt
StandardOutput=inherit
StandardError=inherit
WatchdogSec=10s
Restart=always
User=pi
[Install]
WantedBy=multi-user.target" > $service_path
echo "Checking the installation..."
if [ ! -f /opt/zigbee2mqtt/data/configuration.yaml ]; then
validation=""
else
validation="ok"
fi
if [ ! -z "${validation}" ]; then
echo
echo -e "\\e[32mInstallation done..\\e[0m"
echo -e "Update of configuration.yaml is required found at /opt/zigbee2mqtt/data/"
echo -e "Some further configuration is required and details can be found here https://www.zigbee2mqtt.io"
echo
echo -e "Service can be started after configuration but running sudo systemctl start zigbee2mqtt"
echo
else
echo
echo -e "\\e[31mInstallation failed..."
echo
return 1
fi
return 0
}
[[ "$_" == "$0" ]] && zigbee2mqtt-install-package
+2 -2
View File
@@ -18,5 +18,5 @@ const sandbox = {
};
vm.runInNewContext(moduleCode, sandbox, moduleFakePath);
const converter = sandbox.module.exports;
assert(!converter.toZigbee.includes(undefined));
assert(!converter.fromZigbee.includes(undefined));
assert(!converter.toZigbee || !converter.toZigbee.includes(undefined));
assert(!converter.fromZigbee || !converter.fromZigbee.includes(undefined));
+2 -2
View File
@@ -65,12 +65,12 @@ class ZStackNvMemEraser {
if (len != 0) {
console.log(`NVMEM item #${id} - deleting, size: ${len}`);
if (needOsal) {
await this.znp.request(Subsystem.SYS, 'osalNvDelete', {id: id, len: len}, null, null, [
await this.znp.request(Subsystem.SYS, 'osalNvDelete', {id: id, len: len}, undefined, undefined, [
ZnpCommandStatus.SUCCESS,
ZnpCommandStatus.NV_ITEM_INITIALIZED,
]);
} else {
await this.znp.request(Subsystem.SYS, 'nvDelete', {sysid: NvSystemIds.ZSTACK, itemid: id, subid: 0}, null, null, [
await this.znp.request(Subsystem.SYS, 'nvDelete', {sysid: NvSystemIds.ZSTACK, itemid: id, subid: 0}, undefined, undefined, [
ZnpCommandStatus.SUCCESS,
ZnpCommandStatus.NV_ITEM_INITIALIZED,
]);
@@ -1,19 +1,7 @@
const homeassistantSwitch = {
type: 'switch',
object_id: 'switch',
discovery_payload: {
payload_off: 'OFF',
payload_on: 'ON',
value_template: '{{ value_json.state }}',
command_topic: true,
},
};
const mockDevices = [
{
mock: 1,
model: 'external_converters_device_1',
homeassistant: [homeassistantSwitch],
zigbeeModel: ['external_converter_device_1'],
vendor: 'external_1',
description: 'external_1',
@@ -1,9 +1,11 @@
const {posix} = require('node:path');
const mockDevice = {
mock: true,
zigbeeModel: ['external_converter_device'],
vendor: 'external',
model: 'external_converter_device',
description: 'external',
description: posix.join('external', 'converter'),
fromZigbee: [],
toZigbee: [],
exposes: [],
@@ -0,0 +1,22 @@
export default [
{
mock: 1,
model: 'external_converters_device_1',
zigbeeModel: ['external_converter_device_1'],
vendor: 'external_1',
description: 'external_1',
fromZigbee: [],
toZigbee: [],
exposes: [],
},
{
mock: 2,
model: 'external_converters_device_2',
zigbeeModel: ['external_converter_device_2'],
vendor: 'external_2',
description: 'external_2',
fromZigbee: [],
toZigbee: [],
exposes: [],
},
];
@@ -0,0 +1,12 @@
import {posix} from 'node:path';
import {identify} from 'zigbee-herdsman-converters/lib/modernExtend';
export default {
mock: true,
zigbeeModel: ['external_converter_device'],
vendor: 'external',
model: 'external_converter_device',
description: posix.join('external', 'converter'),
extend: [identify()],
};
@@ -0,0 +1,16 @@
class Example2 {
constructor(zigbee, mqtt, state, publishEntityState, eventBus) {
this.mqtt = mqtt;
this.mqtt.publish('example2/extension', 'call2 from constructor');
}
start() {
this.mqtt.publish('example2/extension', 'call2 from start');
}
stop() {
this.mqtt.publish('example/extension', 'call2 from stop');
}
}
module.exports = Example2;
@@ -5,7 +5,11 @@ class Example {
}
start() {
this.mqtt.publish('example/extension', 'test');
this.mqtt.publish('example/extension', 'call from start');
}
stop() {
this.mqtt.publish('example/extension', 'call from stop');
}
}
@@ -0,0 +1,14 @@
export default class Example2 {
constructor(zigbee, mqtt, state, publishEntityState, eventBus) {
this.mqtt = mqtt;
this.mqtt.publish('example2/extension', 'call2 from constructor');
}
start() {
this.mqtt.publish('example2/extension', 'call2 from start');
}
stop() {
this.mqtt.publish('example/extension', 'call2 from stop');
}
}
@@ -0,0 +1,14 @@
export default class Example {
constructor(zigbee, mqtt, state, publishEntityState, eventBus) {
this.mqtt = mqtt;
this.mqtt.publish('example/extension', 'call from constructor');
}
start() {
this.mqtt.publish('example/extension', 'call from start');
}
stop() {
this.mqtt.publish('example/extension', 'call from stop');
}
}
-943
View File
@@ -1,943 +0,0 @@
const data = require('./stub/data');
const logger = require('./stub/logger');
const zigbeeHerdsman = require('./stub/zigbeeHerdsman');
const MQTT = require('./stub/mqtt');
const settings = require('../lib/util/settings');
const Controller = require('../lib/controller');
const flushPromises = require('./lib/flushPromises');
const stringify = require('json-stable-stringify-without-jsonify');
jest.mock('debounce', () => jest.fn((fn) => fn));
const debounce = require('debounce');
describe('Bind', () => {
let controller;
const mockClear = (device) => {
for (const endpoint of device.endpoints) {
endpoint.read.mockClear();
endpoint.write.mockClear();
endpoint.configureReporting.mockClear();
endpoint.bind = jest.fn();
endpoint.bind.mockClear();
endpoint.unbind.mockClear();
}
};
let resetExtension = async () => {
await controller.enableDisableExtension(false, 'Bind');
await controller.enableDisableExtension(true, 'Bind');
};
beforeAll(async () => {
jest.useFakeTimers();
controller = new Controller(jest.fn(), jest.fn());
await controller.start();
await flushPromises();
});
beforeEach(async () => {
data.writeDefaultConfiguration();
settings.reRead();
zigbeeHerdsman.groups.group_1.members = [];
zigbeeHerdsman.devices.bulb_color.getEndpoint(1).configureReporting.mockClear();
zigbeeHerdsman.devices.bulb_color.getEndpoint(1).bind.mockClear();
zigbeeHerdsman.devices.bulb_color_2.getEndpoint(1).read.mockClear();
debounce.mockClear();
await resetExtension();
MQTT.publish.mockClear();
});
afterAll(async () => {
jest.useRealTimers();
});
it('Should bind to device and configure reporting', async () => {
const device = zigbeeHerdsman.devices.remote;
const target = zigbeeHerdsman.devices.bulb_color.getEndpoint(1);
const endpoint = device.getEndpoint(1);
// Setup
const originalDeviceOutputClusters = device.getEndpoint(1).outputClusters;
device.getEndpoint(1).outputClusters = [...device.getEndpoint(1).outputClusters, 768];
const originalTargetBinds = target.binds;
target.binds = [{cluster: {name: 'genLevelCtrl'}, target: zigbeeHerdsman.devices.coordinator.getEndpoint(1)}];
target.getClusterAttributeValue.mockImplementationOnce((cluster, value) => undefined);
mockClear(device);
target.configureReporting.mockImplementationOnce(() => {
throw new Error('timeout');
});
MQTT.events.message('zigbee2mqtt/bridge/request/device/bind', stringify({transaction: '1234', from: 'remote', to: 'bulb_color'}));
await flushPromises();
expect(target.read).toHaveBeenCalledWith('lightingColorCtrl', ['colorCapabilities']);
expect(endpoint.bind).toHaveBeenCalledTimes(4);
expect(endpoint.bind).toHaveBeenCalledWith('genOnOff', target);
expect(endpoint.bind).toHaveBeenCalledWith('genLevelCtrl', target);
expect(endpoint.bind).toHaveBeenCalledWith('genScenes', target);
expect(endpoint.bind).toHaveBeenCalledWith('lightingColorCtrl', target);
expect(target.configureReporting).toHaveBeenCalledTimes(3);
expect(target.configureReporting).toHaveBeenCalledWith('genOnOff', [
{attribute: 'onOff', maximumReportInterval: 3600, minimumReportInterval: 0, reportableChange: 0},
]);
expect(target.configureReporting).toHaveBeenCalledWith('genLevelCtrl', [
{attribute: 'currentLevel', maximumReportInterval: 3600, minimumReportInterval: 5, reportableChange: 1},
]);
expect(target.configureReporting).toHaveBeenCalledWith('lightingColorCtrl', [
{attribute: 'colorTemperature', minimumReportInterval: 5, maximumReportInterval: 3600, reportableChange: 1},
{attribute: 'currentX', minimumReportInterval: 5, maximumReportInterval: 3600, reportableChange: 1},
{attribute: 'currentY', minimumReportInterval: 5, maximumReportInterval: 3600, reportableChange: 1},
]);
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({
transaction: '1234',
data: {from: 'remote', to: 'bulb_color', clusters: ['genScenes', 'genOnOff', 'genLevelCtrl', 'lightingColorCtrl'], failed: []},
status: 'ok',
}),
{retain: false, qos: 0},
expect.any(Function),
);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bridge/devices', expect.any(String), {retain: true, qos: 0}, expect.any(Function));
// Teardown
target.binds = originalTargetBinds;
device.getEndpoint(1).outputClusters = originalDeviceOutputClusters;
});
it('Filters out unsupported clusters for reporting setup', async () => {
const device = zigbeeHerdsman.devices.remote;
const target = zigbeeHerdsman.devices.bulb_color.getEndpoint(1);
const endpoint = device.getEndpoint(1);
// Setup
const originalDeviceInputClusters = device.getEndpoint(1).inputClusters;
device.getEndpoint(1).inputClusters = [...device.getEndpoint(1).inputClusters, 8];
const originalDeviceOutputClusters = device.getEndpoint(1).outputClusters;
device.getEndpoint(1).outputClusters = [...device.getEndpoint(1).outputClusters, 768];
const originalTargetInputClusters = target.inputClusters;
target.inputClusters = [...originalTargetInputClusters];
target.inputClusters.splice(originalTargetInputClusters.indexOf(8), 1); // remove genLevelCtrl
const originalTargetOutputClusters = target.outputClusters;
target.outputClusters = [...target.outputClusters, 8];
const originalTargetBinds = target.binds;
target.binds = [{cluster: {name: 'genLevelCtrl'}, target: zigbeeHerdsman.devices.coordinator.getEndpoint(1)}];
target.getClusterAttributeValue.mockImplementationOnce((cluster, value) => undefined);
mockClear(device);
target.configureReporting.mockImplementationOnce(() => {
throw new Error('timeout');
});
MQTT.events.message('zigbee2mqtt/bridge/request/device/bind', stringify({transaction: '1234', from: 'remote', to: 'bulb_color'}));
await flushPromises();
expect(target.read).toHaveBeenCalledWith('lightingColorCtrl', ['colorCapabilities']);
expect(endpoint.bind).toHaveBeenCalledTimes(4);
expect(endpoint.bind).toHaveBeenCalledWith('genOnOff', target);
expect(endpoint.bind).toHaveBeenCalledWith('genLevelCtrl', target);
expect(endpoint.bind).toHaveBeenCalledWith('genScenes', target);
expect(endpoint.bind).toHaveBeenCalledWith('lightingColorCtrl', target);
expect(target.configureReporting).toHaveBeenCalledTimes(2);
expect(target.configureReporting).toHaveBeenCalledWith('genOnOff', [
{attribute: 'onOff', maximumReportInterval: 3600, minimumReportInterval: 0, reportableChange: 0},
]);
// expect(target.configureReporting).toHaveBeenCalledWith("genLevelCtrl",[{"attribute": "currentLevel", "maximumReportInterval": 3600, "minimumReportInterval": 5, "reportableChange": 1}]);
expect(target.configureReporting).toHaveBeenCalledWith('lightingColorCtrl', [
{attribute: 'colorTemperature', minimumReportInterval: 5, maximumReportInterval: 3600, reportableChange: 1},
{attribute: 'currentX', minimumReportInterval: 5, maximumReportInterval: 3600, reportableChange: 1},
{attribute: 'currentY', minimumReportInterval: 5, maximumReportInterval: 3600, reportableChange: 1},
]);
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({
transaction: '1234',
data: {from: 'remote', to: 'bulb_color', clusters: ['genScenes', 'genOnOff', 'genLevelCtrl', 'lightingColorCtrl'], failed: []},
status: 'ok',
}),
{retain: false, qos: 0},
expect.any(Function),
);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bridge/devices', expect.any(String), {retain: true, qos: 0}, expect.any(Function));
// Teardown
target.binds = originalTargetBinds;
target.inputClusters = originalTargetInputClusters;
target.outputClusters = originalTargetOutputClusters;
device.getEndpoint(1).inputClusters = originalDeviceInputClusters;
device.getEndpoint(1).outputClusters = originalDeviceOutputClusters;
});
it('Filters out reporting setup based on bind status', async () => {
const device = zigbeeHerdsman.devices.remote;
const target = zigbeeHerdsman.devices.bulb_color.getEndpoint(1);
const endpoint = device.getEndpoint(1);
// Setup
const originalDeviceOutputClusters = device.getEndpoint(1).outputClusters;
device.getEndpoint(1).outputClusters = [...device.getEndpoint(1).outputClusters, 768];
const originalTargetBinds = target.binds;
target.binds = [{cluster: {name: 'genLevelCtrl'}, target: zigbeeHerdsman.devices.coordinator.getEndpoint(1)}];
target.getClusterAttributeValue.mockImplementationOnce((cluster, value) => undefined);
mockClear(device);
target.configureReporting.mockImplementationOnce(() => {
throw new Error('timeout');
});
const originalTargetCR = target.configuredReportings;
target.configuredReportings = [
{
cluster: {name: 'genLevelCtrl'},
attribute: {name: 'currentLevel', ID: 0},
minimumReportInterval: 0,
maximumReportInterval: 3600,
reportableChange: 0,
},
];
MQTT.events.message('zigbee2mqtt/bridge/request/device/bind', stringify({transaction: '1234', from: 'remote', to: 'bulb_color'}));
await flushPromises();
expect(target.read).toHaveBeenCalledWith('lightingColorCtrl', ['colorCapabilities']);
expect(endpoint.bind).toHaveBeenCalledTimes(4);
expect(endpoint.bind).toHaveBeenCalledWith('genOnOff', target);
expect(endpoint.bind).toHaveBeenCalledWith('genLevelCtrl', target);
expect(endpoint.bind).toHaveBeenCalledWith('genScenes', target);
expect(endpoint.bind).toHaveBeenCalledWith('lightingColorCtrl', target);
expect(target.configureReporting).toHaveBeenCalledTimes(2);
expect(target.configureReporting).toHaveBeenCalledWith('genOnOff', [
{attribute: 'onOff', maximumReportInterval: 3600, minimumReportInterval: 0, reportableChange: 0},
]);
// expect(target.configureReporting).toHaveBeenCalledWith("genLevelCtrl",[{"attribute": "currentLevel", "maximumReportInterval": 3600, "minimumReportInterval": 5, "reportableChange": 1}]);
expect(target.configureReporting).toHaveBeenCalledWith('lightingColorCtrl', [
{attribute: 'colorTemperature', minimumReportInterval: 5, maximumReportInterval: 3600, reportableChange: 1},
{attribute: 'currentX', minimumReportInterval: 5, maximumReportInterval: 3600, reportableChange: 1},
{attribute: 'currentY', minimumReportInterval: 5, maximumReportInterval: 3600, reportableChange: 1},
]);
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({
transaction: '1234',
data: {from: 'remote', to: 'bulb_color', clusters: ['genScenes', 'genOnOff', 'genLevelCtrl', 'lightingColorCtrl'], failed: []},
status: 'ok',
}),
{retain: false, qos: 0},
expect.any(Function),
);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bridge/devices', expect.any(String), {retain: true, qos: 0}, expect.any(Function));
// Teardown
target.configuredReportings = originalTargetCR;
target.binds = originalTargetBinds;
device.getEndpoint(1).outputClusters = originalDeviceOutputClusters;
});
it('Should bind only specified clusters', async () => {
const device = zigbeeHerdsman.devices.remote;
const target = zigbeeHerdsman.devices.bulb_color.getEndpoint(1);
const endpoint = device.getEndpoint(1);
mockClear(device);
MQTT.events.message('zigbee2mqtt/bridge/request/device/bind', stringify({from: 'remote', to: 'bulb_color', clusters: ['genOnOff']}));
await flushPromises();
expect(endpoint.bind).toHaveBeenCalledTimes(1);
expect(endpoint.bind).toHaveBeenCalledWith('genOnOff', target);
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({data: {from: 'remote', to: 'bulb_color', clusters: ['genOnOff'], failed: []}, status: 'ok'}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it('Should log error when there is nothing to bind', async () => {
const device = zigbeeHerdsman.devices.bulb_color;
const endpoint = device.getEndpoint(1);
mockClear(device);
logger.error.mockClear();
MQTT.events.message('zigbee2mqtt/bridge/request/device/bind', stringify({from: 'remote', to: 'button'}));
await flushPromises();
expect(endpoint.bind).toHaveBeenCalledTimes(0);
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({data: {from: 'remote', to: 'button', clusters: [], failed: []}, status: 'error', error: 'Nothing to bind'}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it('Should unbind', async () => {
const device = zigbeeHerdsman.devices.remote;
const target = zigbeeHerdsman.devices.bulb_color.getEndpoint(1);
// setup
target.configureReporting.mockImplementationOnce(() => {
throw new Error('timeout');
});
const originalRemoteBinds = device.getEndpoint(1).binds;
device.getEndpoint(1).binds = [];
const originalTargetBinds = target.binds;
target.binds = [
{cluster: {name: 'genOnOff'}, target: zigbeeHerdsman.devices.coordinator.getEndpoint(1)},
{cluster: {name: 'genLevelCtrl'}, target: zigbeeHerdsman.devices.coordinator.getEndpoint(1)},
{cluster: {name: 'lightingColorCtrl'}, target: zigbeeHerdsman.devices.coordinator.getEndpoint(1)},
];
const endpoint = device.getEndpoint(1);
mockClear(device);
delete zigbeeHerdsman.devices.bulb_color.meta.configured;
expect(zigbeeHerdsman.devices.bulb_color.meta.configured).toBe(undefined);
MQTT.events.message('zigbee2mqtt/bridge/request/device/unbind', stringify({from: 'remote', to: 'bulb_color'}));
await flushPromises();
expect(endpoint.unbind).toHaveBeenCalledTimes(3);
expect(endpoint.unbind).toHaveBeenCalledWith('genOnOff', target);
expect(endpoint.unbind).toHaveBeenCalledWith('genLevelCtrl', target);
expect(endpoint.unbind).toHaveBeenCalledWith('genScenes', target);
// Disable reporting
expect(target.configureReporting).toHaveBeenCalledTimes(3);
expect(target.configureReporting).toHaveBeenCalledWith('genOnOff', [
{attribute: 'onOff', maximumReportInterval: 0xffff, minimumReportInterval: 0, reportableChange: 0},
]);
expect(target.configureReporting).toHaveBeenCalledWith('genLevelCtrl', [
{attribute: 'currentLevel', maximumReportInterval: 0xffff, minimumReportInterval: 5, reportableChange: 1},
]);
expect(target.configureReporting).toHaveBeenCalledWith('lightingColorCtrl', [
{attribute: 'colorTemperature', minimumReportInterval: 5, maximumReportInterval: 0xffff, reportableChange: 1},
{attribute: 'currentX', minimumReportInterval: 5, maximumReportInterval: 0xffff, reportableChange: 1},
{attribute: 'currentY', minimumReportInterval: 5, maximumReportInterval: 0xffff, reportableChange: 1},
]);
expect(zigbeeHerdsman.devices.bulb_color.meta.configured).toBe(332242049);
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/unbind',
stringify({data: {from: 'remote', to: 'bulb_color', clusters: ['genScenes', 'genOnOff', 'genLevelCtrl'], failed: []}, status: 'ok'}),
{retain: false, qos: 0},
expect.any(Function),
);
// Teardown
target.binds = originalTargetBinds;
device.getEndpoint(1).binds = originalRemoteBinds;
});
it('Should unbind coordinator', async () => {
const device = zigbeeHerdsman.devices.remote;
const target = zigbeeHerdsman.devices.coordinator.getEndpoint(1);
const endpoint = device.getEndpoint(1);
mockClear(device);
endpoint.unbind.mockClear();
MQTT.events.message('zigbee2mqtt/bridge/request/device/unbind', stringify({from: 'remote', to: 'Coordinator'}));
await flushPromises();
expect(endpoint.unbind).toHaveBeenCalledTimes(3);
expect(endpoint.unbind).toHaveBeenCalledWith('genOnOff', target);
expect(endpoint.unbind).toHaveBeenCalledWith('genLevelCtrl', target);
expect(endpoint.unbind).toHaveBeenCalledWith('genScenes', target);
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/unbind',
stringify({data: {from: 'remote', to: 'Coordinator', clusters: ['genScenes', 'genOnOff', 'genLevelCtrl'], failed: []}, status: 'ok'}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it('Should bind to groups', async () => {
const device = zigbeeHerdsman.devices.remote;
const target = zigbeeHerdsman.groups.group_1;
const target1Member = zigbeeHerdsman.devices.bulb.getEndpoint(1);
const endpoint = device.getEndpoint(1);
target.members.push(target1Member);
target1Member.configureReporting.mockClear();
mockClear(device);
MQTT.events.message('zigbee2mqtt/bridge/request/device/bind', stringify({from: 'remote', to: 'group_1'}));
await flushPromises();
expect(endpoint.bind).toHaveBeenCalledTimes(3);
expect(endpoint.bind).toHaveBeenCalledWith('genOnOff', target);
expect(endpoint.bind).toHaveBeenCalledWith('genLevelCtrl', target);
expect(endpoint.bind).toHaveBeenCalledWith('genScenes', target);
expect(target1Member.configureReporting).toHaveBeenCalledTimes(2);
expect(target1Member.configureReporting).toHaveBeenCalledWith('genOnOff', [
{attribute: 'onOff', maximumReportInterval: 3600, minimumReportInterval: 0, reportableChange: 0},
]);
expect(target1Member.configureReporting).toHaveBeenCalledWith('genLevelCtrl', [
{attribute: 'currentLevel', maximumReportInterval: 3600, minimumReportInterval: 5, reportableChange: 1},
]);
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({data: {from: 'remote', to: 'group_1', clusters: ['genScenes', 'genOnOff', 'genLevelCtrl'], failed: []}, status: 'ok'}),
{retain: false, qos: 0},
expect.any(Function),
);
// Should configure reproting for device added to group
target1Member.configureReporting.mockClear();
await MQTT.events.message('zigbee2mqtt/bridge/group/group_1/add', 'bulb');
await flushPromises();
expect(target1Member.configureReporting).toHaveBeenCalledTimes(2);
expect(target1Member.configureReporting).toHaveBeenCalledWith('genOnOff', [
{attribute: 'onOff', maximumReportInterval: 3600, minimumReportInterval: 0, reportableChange: 0},
]);
expect(target1Member.configureReporting).toHaveBeenCalledWith('genLevelCtrl', [
{attribute: 'currentLevel', maximumReportInterval: 3600, minimumReportInterval: 5, reportableChange: 1},
]);
});
it('Should unbind from group', async () => {
const device = zigbeeHerdsman.devices.remote;
const target = zigbeeHerdsman.groups.group_1;
const target1Member = zigbeeHerdsman.devices.bulb.getEndpoint(1);
const endpoint = device.getEndpoint(1);
target.members.push(target1Member);
target1Member.configureReporting.mockClear();
mockClear(device);
MQTT.events.message('zigbee2mqtt/bridge/request/device/unbind', stringify({from: 'remote', to: 'group_1'}));
await flushPromises();
expect(endpoint.unbind).toHaveBeenCalledTimes(3);
expect(endpoint.unbind).toHaveBeenCalledWith('genOnOff', target);
expect(endpoint.unbind).toHaveBeenCalledWith('genLevelCtrl', target);
expect(endpoint.unbind).toHaveBeenCalledWith('genScenes', target);
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/unbind',
stringify({data: {from: 'remote', to: 'group_1', clusters: ['genScenes', 'genOnOff', 'genLevelCtrl'], failed: []}, status: 'ok'}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it('Should unbind from group with skip_disable_reporting=true', async () => {
const device = zigbeeHerdsman.devices.remote;
const target = zigbeeHerdsman.groups.group_1;
const target1Member = zigbeeHerdsman.devices.bulb_2.getEndpoint(1);
const endpoint = device.getEndpoint(1);
target.members.push(target1Member);
// The device unbind mock doesn't remove binds, therefore remove them here already otherwise configure reporiting is not disabled.
const originalBinds = endpoint.binds;
endpoint.binds = [];
target1Member.binds = [
{cluster: {name: 'genLevelCtrl'}, target: zigbeeHerdsman.devices.coordinator.getEndpoint(1)},
{cluster: {name: 'genOnOff'}, target: zigbeeHerdsman.devices.coordinator.getEndpoint(1)},
];
target1Member.configureReporting.mockClear();
mockClear(device);
MQTT.events.message('zigbee2mqtt/bridge/request/device/unbind', stringify({from: 'remote', to: 'group_1', skip_disable_reporting: true}));
await flushPromises();
expect(endpoint.unbind).toHaveBeenCalledTimes(3);
// with skip_disable_reporting set to false, we don't expect it to reconfigure reporting
expect(target1Member.configureReporting).toHaveBeenCalledTimes(0);
endpoint.binds = originalBinds;
});
it('Should unbind from group with skip_disable_reporting=false', async () => {
const device = zigbeeHerdsman.devices.remote;
const target = zigbeeHerdsman.groups.group_1;
const target1Member = zigbeeHerdsman.devices.bulb_2.getEndpoint(1);
const endpoint = device.getEndpoint(1);
target.members.push(target1Member);
// The device unbind mock doesn't remove binds, therefore remove them here already otherwise configure reporiting is not disabled.
const originalBinds = endpoint.binds;
endpoint.binds = [];
target1Member.binds = [
{cluster: {name: 'genLevelCtrl'}, target: zigbeeHerdsman.devices.coordinator.getEndpoint(1)},
{cluster: {name: 'genOnOff'}, target: zigbeeHerdsman.devices.coordinator.getEndpoint(1)},
];
target1Member.configureReporting.mockClear();
mockClear(device);
MQTT.events.message('zigbee2mqtt/bridge/request/device/unbind', stringify({from: 'remote', to: 'group_1', skip_disable_reporting: false}));
await flushPromises();
expect(endpoint.unbind).toHaveBeenCalledTimes(3);
// with skip_disable_reporting set, we expect it to reconfigure reporting
expect(target1Member.configureReporting).toHaveBeenCalledTimes(2);
expect(target1Member.configureReporting).toHaveBeenCalledWith('genLevelCtrl', [
{attribute: 'currentLevel', maximumReportInterval: 65535, minimumReportInterval: 5, reportableChange: 1},
]);
expect(target1Member.configureReporting).toHaveBeenCalledWith('genOnOff', [
{attribute: 'onOff', maximumReportInterval: 65535, minimumReportInterval: 0, reportableChange: 0},
]);
endpoint.binds = originalBinds;
});
it('Should bind to group by number', async () => {
const device = zigbeeHerdsman.devices.remote;
const target = zigbeeHerdsman.groups.group_1;
const endpoint = device.getEndpoint(1);
mockClear(device);
MQTT.events.message('zigbee2mqtt/bridge/request/device/bind', stringify({from: 'remote', to: '1'}));
await flushPromises();
expect(endpoint.bind).toHaveBeenCalledTimes(3);
expect(endpoint.bind).toHaveBeenCalledWith('genOnOff', target);
expect(endpoint.bind).toHaveBeenCalledWith('genLevelCtrl', target);
expect(endpoint.bind).toHaveBeenCalledWith('genScenes', target);
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({data: {from: 'remote', to: '1', clusters: ['genScenes', 'genOnOff', 'genLevelCtrl'], failed: []}, status: 'ok'}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it('Should log when bind fails', async () => {
logger.error.mockClear();
const device = zigbeeHerdsman.devices.remote;
const endpoint = device.getEndpoint(1);
mockClear(device);
endpoint.bind.mockImplementation(() => {
throw new Error('failed');
});
MQTT.events.message('zigbee2mqtt/bridge/request/device/bind', stringify({from: 'remote', to: 'bulb_color'}));
await flushPromises();
expect(endpoint.bind).toHaveBeenCalledTimes(3);
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({
data: {from: 'remote', to: 'bulb_color', clusters: [], failed: ['genScenes', 'genOnOff', 'genLevelCtrl']},
status: 'error',
error: 'Failed to bind',
}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it('Should bind from non default endpoints', async () => {
const device = zigbeeHerdsman.devices.remote;
const target = zigbeeHerdsman.devices.QBKG03LM.getEndpoint(3);
const endpoint = device.getEndpoint(2);
mockClear(device);
MQTT.events.message('zigbee2mqtt/bridge/request/device/bind', stringify({from: 'remote/ep2', to: 'wall_switch_double/right'}));
await flushPromises();
expect(endpoint.bind).toHaveBeenCalledTimes(1);
expect(endpoint.bind).toHaveBeenCalledWith('genOnOff', target);
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({data: {from: 'remote/ep2', to: 'wall_switch_double/right', clusters: ['genOnOff'], failed: []}, status: 'ok'}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it('Should bind server clusters to client clusters', async () => {
const device = zigbeeHerdsman.devices.temperature_sensor;
const target = zigbeeHerdsman.devices.heating_actuator.getEndpoint(1);
const endpoint = device.getEndpoint(1);
mockClear(device);
MQTT.events.message('zigbee2mqtt/bridge/request/device/bind', stringify({from: 'temperature_sensor', to: 'heating_actuator'}));
await flushPromises();
expect(endpoint.bind).toHaveBeenCalledTimes(1);
expect(endpoint.bind).toHaveBeenCalledWith('msTemperatureMeasurement', target);
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({data: {from: 'temperature_sensor', to: 'heating_actuator', clusters: ['msTemperatureMeasurement'], failed: []}, status: 'ok'}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it('Should bind to default endpoint returned by endpoints()', async () => {
const device = zigbeeHerdsman.devices.remote;
const target = zigbeeHerdsman.devices.QBKG04LM.getEndpoint(2);
const endpoint = device.getEndpoint(2);
mockClear(device);
MQTT.events.message('zigbee2mqtt/bridge/request/device/bind', stringify({from: 'remote/ep2', to: 'wall_switch'}));
await flushPromises();
expect(endpoint.bind).toHaveBeenCalledTimes(1);
expect(endpoint.bind).toHaveBeenCalledWith('genOnOff', target);
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({data: {from: 'remote/ep2', to: 'wall_switch', clusters: ['genOnOff'], failed: []}, status: 'ok'}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it('Should unbind from default_bind_group', async () => {
const device = zigbeeHerdsman.devices.remote;
const target = 'default_bind_group';
const endpoint = device.getEndpoint(1);
mockClear(device);
MQTT.events.message('zigbee2mqtt/bridge/request/device/unbind', stringify({from: 'remote', to: target}));
await flushPromises();
expect(endpoint.unbind).toHaveBeenCalledTimes(3);
expect(endpoint.unbind).toHaveBeenCalledWith('genOnOff', 901);
expect(endpoint.unbind).toHaveBeenCalledWith('genLevelCtrl', 901);
expect(endpoint.unbind).toHaveBeenCalledWith('genScenes', 901);
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/unbind',
stringify({
data: {from: 'remote', to: 'default_bind_group', clusters: ['genScenes', 'genOnOff', 'genLevelCtrl'], failed: []},
status: 'ok',
}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it('Error bind fails when source device does not exist', async () => {
const device = zigbeeHerdsman.devices.remote;
const target = zigbeeHerdsman.devices.bulb_color.getEndpoint(1);
const endpoint = device.getEndpoint(1);
mockClear(device);
MQTT.events.message('zigbee2mqtt/bridge/request/device/bind', stringify({from: 'remote_not_existing', to: 'bulb_color'}));
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({
data: {from: 'remote_not_existing', to: 'bulb_color'},
status: 'error',
error: "Source device 'remote_not_existing' does not exist",
}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it("Error bind fails when source device's endpoint does not exist", async () => {
const device = zigbeeHerdsman.devices.remote;
const target = zigbeeHerdsman.devices.bulb_color.getEndpoint(1);
const endpoint = device.getEndpoint(1);
mockClear(device);
MQTT.events.message('zigbee2mqtt/bridge/request/device/bind', stringify({from: 'remote/not_existing_endpoint', to: 'bulb_color'}));
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({
data: {from: 'remote/not_existing_endpoint', to: 'bulb_color'},
status: 'error',
error: "Source device 'remote' does not have endpoint 'not_existing_endpoint'",
}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it('Error bind fails when target device or group does not exist', async () => {
const device = zigbeeHerdsman.devices.remote;
const target = zigbeeHerdsman.devices.bulb_color.getEndpoint(1);
const endpoint = device.getEndpoint(1);
mockClear(device);
MQTT.events.message('zigbee2mqtt/bridge/request/device/bind', stringify({from: 'remote', to: 'bulb_color_not_existing'}));
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({
data: {from: 'remote', to: 'bulb_color_not_existing'},
status: 'error',
error: "Target device or group 'bulb_color_not_existing' does not exist",
}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it("Error bind fails when target device's endpoint does not exist", async () => {
const device = zigbeeHerdsman.devices.remote;
const target = zigbeeHerdsman.devices.bulb_color.getEndpoint(1);
const endpoint = device.getEndpoint(1);
mockClear(device);
MQTT.events.message('zigbee2mqtt/bridge/request/device/bind', stringify({from: 'remote', to: 'bulb_color/not_existing_endpoint'}));
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({
data: {from: 'remote', to: 'bulb_color/not_existing_endpoint'},
status: 'error',
error: "Target device 'bulb_color' does not have endpoint 'not_existing_endpoint'",
}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it('Legacy api: Should bind', async () => {
const device = zigbeeHerdsman.devices.remote;
const target = zigbeeHerdsman.devices.bulb_color.getEndpoint(1);
const endpoint = device.getEndpoint(1);
mockClear(device);
MQTT.events.message('zigbee2mqtt/bridge/bind/remote', 'bulb_color');
await flushPromises();
expect(endpoint.bind).toHaveBeenCalledTimes(3);
expect(endpoint.bind).toHaveBeenCalledWith('genOnOff', target);
expect(endpoint.bind).toHaveBeenCalledWith('genLevelCtrl', target);
expect(endpoint.bind).toHaveBeenCalledWith('genScenes', target);
expect(MQTT.publish.mock.calls[0][0]).toStrictEqual('zigbee2mqtt/bridge/log');
expect(JSON.parse(MQTT.publish.mock.calls[0][1])).toStrictEqual({
type: 'device_bind',
message: {from: 'remote', to: 'bulb_color', cluster: 'genScenes'},
});
expect(MQTT.publish.mock.calls[1][0]).toStrictEqual('zigbee2mqtt/bridge/log');
expect(JSON.parse(MQTT.publish.mock.calls[1][1])).toStrictEqual({
type: 'device_bind',
message: {from: 'remote', to: 'bulb_color', cluster: 'genOnOff'},
});
expect(MQTT.publish.mock.calls[2][0]).toStrictEqual('zigbee2mqtt/bridge/log');
expect(JSON.parse(MQTT.publish.mock.calls[2][1])).toStrictEqual({
type: 'device_bind',
message: {from: 'remote', to: 'bulb_color', cluster: 'genLevelCtrl'},
});
});
it('Legacy api: Should log error when there is nothing to bind', async () => {
const device = zigbeeHerdsman.devices.bulb_color;
const endpoint = device.getEndpoint(1);
mockClear(device);
logger.error.mockClear();
MQTT.events.message('zigbee2mqtt/bridge/bind/remote', 'button');
await flushPromises();
expect(endpoint.bind).toHaveBeenCalledTimes(0);
expect(logger.error).toHaveBeenCalledWith(`Nothing to bind from 'remote' to 'button'`);
});
it('Legacy api: Should unbind', async () => {
const device = zigbeeHerdsman.devices.remote;
const target = zigbeeHerdsman.devices.bulb_color.getEndpoint(1);
const endpoint = device.getEndpoint(1);
mockClear(device);
MQTT.events.message('zigbee2mqtt/bridge/unbind/remote', 'bulb_color');
await flushPromises();
expect(endpoint.unbind).toHaveBeenCalledTimes(3);
expect(endpoint.unbind).toHaveBeenCalledWith('genOnOff', target);
expect(endpoint.unbind).toHaveBeenCalledWith('genLevelCtrl', target);
expect(endpoint.unbind).toHaveBeenCalledWith('genScenes', target);
expect(MQTT.publish.mock.calls[0][0]).toStrictEqual('zigbee2mqtt/bridge/log');
expect(JSON.parse(MQTT.publish.mock.calls[0][1])).toStrictEqual({
type: 'device_unbind',
message: {from: 'remote', to: 'bulb_color', cluster: 'genScenes'},
});
expect(MQTT.publish.mock.calls[1][0]).toStrictEqual('zigbee2mqtt/bridge/log');
expect(JSON.parse(MQTT.publish.mock.calls[1][1])).toStrictEqual({
type: 'device_unbind',
message: {from: 'remote', to: 'bulb_color', cluster: 'genOnOff'},
});
expect(MQTT.publish.mock.calls[2][0]).toStrictEqual('zigbee2mqtt/bridge/log');
expect(JSON.parse(MQTT.publish.mock.calls[2][1])).toStrictEqual({
type: 'device_unbind',
message: {from: 'remote', to: 'bulb_color', cluster: 'genLevelCtrl'},
});
});
it('Legacy api: Should unbind coordinator', async () => {
const device = zigbeeHerdsman.devices.remote;
const target = zigbeeHerdsman.devices.coordinator.getEndpoint(1);
const endpoint = device.getEndpoint(1);
mockClear(device);
endpoint.unbind.mockClear();
MQTT.events.message('zigbee2mqtt/bridge/unbind/remote', 'Coordinator');
await flushPromises();
expect(endpoint.unbind).toHaveBeenCalledTimes(3);
expect(endpoint.unbind).toHaveBeenCalledWith('genOnOff', target);
expect(endpoint.unbind).toHaveBeenCalledWith('genLevelCtrl', target);
expect(endpoint.unbind).toHaveBeenCalledWith('genScenes', target);
expect(MQTT.publish.mock.calls[0][0]).toStrictEqual('zigbee2mqtt/bridge/log');
expect(JSON.parse(MQTT.publish.mock.calls[0][1])).toStrictEqual({
type: 'device_unbind',
message: {from: 'remote', to: 'Coordinator', cluster: 'genScenes'},
});
expect(MQTT.publish.mock.calls[1][0]).toStrictEqual('zigbee2mqtt/bridge/log');
expect(JSON.parse(MQTT.publish.mock.calls[1][1])).toStrictEqual({
type: 'device_unbind',
message: {from: 'remote', to: 'Coordinator', cluster: 'genOnOff'},
});
expect(MQTT.publish.mock.calls[2][0]).toStrictEqual('zigbee2mqtt/bridge/log');
expect(JSON.parse(MQTT.publish.mock.calls[2][1])).toStrictEqual({
type: 'device_unbind',
message: {from: 'remote', to: 'Coordinator', cluster: 'genLevelCtrl'},
});
});
it('Legacy api: Should bind to groups', async () => {
const device = zigbeeHerdsman.devices.remote;
const target = zigbeeHerdsman.groups.group_1;
const endpoint = device.getEndpoint(1);
mockClear(device);
MQTT.events.message('zigbee2mqtt/bridge/bind/remote', 'group_1');
await flushPromises();
expect(endpoint.bind).toHaveBeenCalledTimes(3);
expect(endpoint.bind).toHaveBeenCalledWith('genOnOff', target);
expect(endpoint.bind).toHaveBeenCalledWith('genLevelCtrl', target);
expect(endpoint.bind).toHaveBeenCalledWith('genScenes', target);
expect(MQTT.publish.mock.calls[0][0]).toStrictEqual('zigbee2mqtt/bridge/log');
expect(JSON.parse(MQTT.publish.mock.calls[0][1])).toStrictEqual({
type: 'device_bind',
message: {from: 'remote', to: 'group_1', cluster: 'genScenes'},
});
expect(MQTT.publish.mock.calls[1][0]).toStrictEqual('zigbee2mqtt/bridge/log');
expect(JSON.parse(MQTT.publish.mock.calls[1][1])).toStrictEqual({
type: 'device_bind',
message: {from: 'remote', to: 'group_1', cluster: 'genOnOff'},
});
expect(MQTT.publish.mock.calls[2][0]).toStrictEqual('zigbee2mqtt/bridge/log');
expect(JSON.parse(MQTT.publish.mock.calls[2][1])).toStrictEqual({
type: 'device_bind',
message: {from: 'remote', to: 'group_1', cluster: 'genLevelCtrl'},
});
});
it('Legacy api: Should bind to group by number', async () => {
const device = zigbeeHerdsman.devices.remote;
const target = zigbeeHerdsman.groups.group_1;
const endpoint = device.getEndpoint(1);
mockClear(device);
MQTT.events.message('zigbee2mqtt/bridge/bind/remote', '1');
await flushPromises();
expect(endpoint.bind).toHaveBeenCalledTimes(3);
expect(endpoint.bind).toHaveBeenCalledWith('genOnOff', target);
expect(endpoint.bind).toHaveBeenCalledWith('genLevelCtrl', target);
expect(endpoint.bind).toHaveBeenCalledWith('genScenes', target);
expect(MQTT.publish.mock.calls[0][0]).toStrictEqual('zigbee2mqtt/bridge/log');
expect(JSON.parse(MQTT.publish.mock.calls[0][1])).toStrictEqual({
type: 'device_bind',
message: {from: 'remote', to: 'group_1', cluster: 'genScenes'},
});
expect(MQTT.publish.mock.calls[1][0]).toStrictEqual('zigbee2mqtt/bridge/log');
expect(JSON.parse(MQTT.publish.mock.calls[1][1])).toStrictEqual({
type: 'device_bind',
message: {from: 'remote', to: 'group_1', cluster: 'genOnOff'},
});
expect(MQTT.publish.mock.calls[2][0]).toStrictEqual('zigbee2mqtt/bridge/log');
expect(JSON.parse(MQTT.publish.mock.calls[2][1])).toStrictEqual({
type: 'device_bind',
message: {from: 'remote', to: 'group_1', cluster: 'genLevelCtrl'},
});
});
it('Legacy api: Should log when bind fails', async () => {
logger.error.mockClear();
const device = zigbeeHerdsman.devices.remote;
const endpoint = device.getEndpoint(1);
mockClear(device);
endpoint.bind.mockImplementationOnce(() => {
throw new Error('failed');
});
MQTT.events.message('zigbee2mqtt/bridge/bind/remote', 'bulb_color');
await flushPromises();
expect(logger.error).toHaveBeenCalledWith("Failed to bind cluster 'genScenes' from 'remote' to 'bulb_color' (Error: failed)");
expect(endpoint.bind).toHaveBeenCalledTimes(3);
});
it('Legacy api: Should bind from non default endpoints', async () => {
const device = zigbeeHerdsman.devices.remote;
const target = zigbeeHerdsman.devices.QBKG03LM.getEndpoint(3);
const endpoint = device.getEndpoint(2);
mockClear(device);
MQTT.events.message('zigbee2mqtt/bridge/bind/remote/ep2', 'wall_switch_double/right');
await flushPromises();
expect(endpoint.bind).toHaveBeenCalledTimes(1);
expect(endpoint.bind).toHaveBeenCalledWith('genOnOff', target);
});
it('Legacy api: Should bind to default endpoint returned by endpoints()', async () => {
const device = zigbeeHerdsman.devices.remote;
const target = zigbeeHerdsman.devices.QBKG04LM.getEndpoint(2);
const endpoint = device.getEndpoint(2);
mockClear(device);
MQTT.events.message('zigbee2mqtt/bridge/bind/remote/ep2', 'wall_switch');
await flushPromises();
expect(endpoint.bind).toHaveBeenCalledTimes(1);
expect(endpoint.bind).toHaveBeenCalledWith('genOnOff', target);
});
it('Legacy api: Should unbind from default_bind_group', async () => {
const device = zigbeeHerdsman.devices.remote;
const target = 'default_bind_group';
const endpoint = device.getEndpoint(1);
mockClear(device);
MQTT.events.message('zigbee2mqtt/bridge/unbind/remote', target);
await flushPromises();
expect(endpoint.unbind).toHaveBeenCalledTimes(3);
expect(endpoint.unbind).toHaveBeenCalledWith('genOnOff', 901);
expect(endpoint.unbind).toHaveBeenCalledWith('genLevelCtrl', 901);
expect(endpoint.unbind).toHaveBeenCalledWith('genScenes', 901);
expect(MQTT.publish.mock.calls[0][0]).toStrictEqual('zigbee2mqtt/bridge/log');
expect(JSON.parse(MQTT.publish.mock.calls[0][1])).toStrictEqual({
type: 'device_unbind',
message: {from: 'remote', to: 'default_bind_group', cluster: 'genScenes'},
});
expect(MQTT.publish.mock.calls[1][0]).toStrictEqual('zigbee2mqtt/bridge/log');
expect(JSON.parse(MQTT.publish.mock.calls[1][1])).toStrictEqual({
type: 'device_unbind',
message: {from: 'remote', to: 'default_bind_group', cluster: 'genOnOff'},
});
expect(MQTT.publish.mock.calls[2][0]).toStrictEqual('zigbee2mqtt/bridge/log');
expect(JSON.parse(MQTT.publish.mock.calls[2][1])).toStrictEqual({
type: 'device_unbind',
message: {from: 'remote', to: 'default_bind_group', cluster: 'genLevelCtrl'},
});
});
it('Should poll bounded Hue bulb when receiving message from Hue dimmer', async () => {
const remote = zigbeeHerdsman.devices.remote;
const data = {button: 3, unknown1: 3145728, type: 2, unknown2: 0, time: 1};
const payload = {
data,
cluster: 'manuSpecificPhilips',
device: remote,
endpoint: remote.getEndpoint(2),
type: 'commandHueNotification',
linkquality: 10,
groupID: 0,
};
await zigbeeHerdsman.events.message(payload);
await flushPromises();
expect(debounce).toHaveBeenCalledTimes(1);
expect(zigbeeHerdsman.devices.bulb_color.getEndpoint(1).read).toHaveBeenCalledWith('genLevelCtrl', ['currentLevel']);
});
it('Should poll bounded Hue bulb when receiving message from scene controller', async () => {
const remote = zigbeeHerdsman.devices.bj_scene_switch;
const data = {action: 'recall_2_row_1'};
zigbeeHerdsman.devices.bulb_color_2.getEndpoint(1).read.mockImplementationOnce(() => {
throw new Error('failed');
});
const payload = {
data,
cluster: 'genScenes',
device: remote,
endpoint: remote.getEndpoint(10),
type: 'commandRecall',
linkquality: 10,
groupID: 0,
};
await zigbeeHerdsman.events.message(payload);
await flushPromises();
// Calls to three clusters are expected in this case
expect(debounce).toHaveBeenCalledTimes(3);
expect(zigbeeHerdsman.devices.bulb_color_2.getEndpoint(1).read).toHaveBeenCalledWith('genOnOff', ['onOff']);
expect(zigbeeHerdsman.devices.bulb_color_2.getEndpoint(1).read).toHaveBeenCalledWith('genLevelCtrl', ['currentLevel']);
expect(zigbeeHerdsman.devices.bulb_color_2.getEndpoint(1).read).toHaveBeenCalledWith('lightingColorCtrl', [
'currentX',
'currentY',
'colorTemperature',
]);
});
it('Should poll grouped Hue bulb when receiving message from TRADFRI remote', async () => {
zigbeeHerdsman.devices.bulb_color_2.getEndpoint(1).read.mockClear();
zigbeeHerdsman.devices.bulb_2.getEndpoint(1).read.mockClear();
const remote = zigbeeHerdsman.devices.tradfri_remote;
const data = {stepmode: 0, stepsize: 43, transtime: 5};
const payload = {
data,
cluster: 'genLevelCtrl',
device: remote,
endpoint: remote.getEndpoint(1),
type: 'commandStepWithOnOff',
linkquality: 10,
groupID: 15071,
};
await zigbeeHerdsman.events.message(payload);
await flushPromises();
expect(debounce).toHaveBeenCalledTimes(2);
expect(zigbeeHerdsman.devices.bulb_color_2.getEndpoint(1).read).toHaveBeenCalledTimes(2);
expect(zigbeeHerdsman.devices.bulb_color_2.getEndpoint(1).read).toHaveBeenCalledWith('genLevelCtrl', ['currentLevel']);
expect(zigbeeHerdsman.devices.bulb_color_2.getEndpoint(1).read).toHaveBeenCalledWith('genOnOff', ['onOff']);
// Should also only debounce once
await zigbeeHerdsman.events.message(payload);
await flushPromises();
expect(debounce).toHaveBeenCalledTimes(2);
expect(zigbeeHerdsman.devices.bulb_color_2.getEndpoint(1).read).toHaveBeenCalledTimes(4);
// Should only call Hue bulb, not e.g. tradfri
expect(zigbeeHerdsman.devices.bulb_2.getEndpoint(1).read).toHaveBeenCalledTimes(0);
});
});
-992
View File
@@ -1,992 +0,0 @@
process.env.NOTIFY_SOCKET = 'mocked';
const data = require('./stub/data');
const logger = require('./stub/logger');
const zigbeeHerdsman = require('./stub/zigbeeHerdsman');
const MQTT = require('./stub/mqtt');
const path = require('path');
const settings = require('../lib/util/settings');
const Controller = require('../lib/controller');
const stringify = require('json-stable-stringify-without-jsonify');
const flushPromises = require('./lib/flushPromises');
const tmp = require('tmp');
const mocksClear = [
zigbeeHerdsman.permitJoin,
MQTT.end,
zigbeeHerdsman.stop,
logger.debug,
MQTT.publish,
MQTT.connect,
zigbeeHerdsman.devices.bulb_color.removeFromNetwork,
zigbeeHerdsman.devices.bulb.removeFromNetwork,
logger.error,
];
const fs = require('fs');
const LOG_MQTT_NS = 'z2m:mqtt';
jest.mock(
'sd-notify',
() => {
return {
watchdogInterval: () => {
return 3000;
},
startWatchdogMode: (interval) => {},
stopWatchdogMode: () => {},
ready: () => {},
stopping: () => {},
};
},
{virtual: true},
);
describe('Controller', () => {
let controller;
let mockExit;
beforeAll(async () => {
jest.useFakeTimers();
});
beforeEach(() => {
MQTT.restoreOnMock();
zigbeeHerdsman.returnDevices.splice(0);
mockExit = jest.fn();
data.writeDefaultConfiguration();
settings.reRead();
controller = new Controller(jest.fn(), mockExit);
mocksClear.forEach((m) => m.mockClear());
settings.reRead();
data.writeDefaultState();
});
afterAll(async () => {
jest.useRealTimers();
});
it('Start controller', async () => {
settings.set(['advanced', 'transmit_power'], 14);
await controller.start();
expect(zigbeeHerdsman.constructor).toHaveBeenCalledWith({
network: {
panID: 6754,
extendedPanID: [221, 221, 221, 221, 221, 221, 221, 221],
channelList: [11],
networkKey: [1, 3, 5, 7, 9, 11, 13, 15, 0, 2, 4, 6, 8, 10, 12, 13],
},
databasePath: path.join(data.mockDir, 'database.db'),
databaseBackupPath: path.join(data.mockDir, 'database.db.backup'),
backupPath: path.join(data.mockDir, 'coordinator_backup.json'),
acceptJoiningDeviceHandler: expect.any(Function),
adapter: {concurrent: null, delay: null, disableLED: false, transmitPower: 14},
serialPort: {baudRate: undefined, rtscts: undefined, path: '/dev/dummy'},
});
expect(zigbeeHerdsman.start).toHaveBeenCalledTimes(1);
expect(zigbeeHerdsman.setTransmitPower).toHaveBeenCalledTimes(0);
expect(zigbeeHerdsman.permitJoin).toHaveBeenCalledTimes(1);
expect(zigbeeHerdsman.permitJoin).toHaveBeenCalledWith(true, undefined, undefined);
expect(logger.info).toHaveBeenCalledWith(`Currently ${Object.values(zigbeeHerdsman.devices).length - 1} devices are joined.`);
expect(logger.info).toHaveBeenCalledWith(
'bulb (0x000b57fffec6a5b2): LED1545G12 - IKEA TRADFRI bulb E26/E27, white spectrum, globe, opal, 980 lm (Router)',
);
expect(logger.info).toHaveBeenCalledWith('remote (0x0017880104e45517): 324131092621 - Philips Hue dimmer switch (EndDevice)');
expect(logger.info).toHaveBeenCalledWith('0x0017880104e45518 (0x0017880104e45518): Not supported (EndDevice)');
expect(MQTT.connect).toHaveBeenCalledTimes(1);
expect(MQTT.connect).toHaveBeenCalledWith('mqtt://localhost', {
will: {payload: Buffer.from('offline'), retain: true, topic: 'zigbee2mqtt/bridge/state', qos: 1},
});
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bulb',
stringify({state: 'ON', brightness: 50, color_temp: 370, linkquality: 99}),
{retain: true, qos: 0},
expect.any(Function),
);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/remote', stringify({brightness: 255}), {retain: true, qos: 0}, expect.any(Function));
});
it('Start controller when permit join fails', async () => {
zigbeeHerdsman.permitJoin.mockImplementationOnce(() => {
throw new Error('failed!');
});
await controller.start();
expect(zigbeeHerdsman.permitJoin).toHaveBeenCalledTimes(1);
expect(MQTT.connect).toHaveBeenCalledTimes(1);
});
it('Start controller with specific MQTT settings', async () => {
const ca = tmp.fileSync().name;
fs.writeFileSync(ca, 'ca');
const key = tmp.fileSync().name;
fs.writeFileSync(key, 'key');
const cert = tmp.fileSync().name;
fs.writeFileSync(cert, 'cert');
const configuration = {
base_topic: 'zigbee2mqtt',
server: 'mqtt://localhost',
keepalive: 30,
ca,
cert,
key,
password: 'pass',
user: 'user1',
client_id: 'my_client_id',
reject_unauthorized: false,
version: 5,
};
settings.set(['mqtt'], configuration);
await controller.start();
await flushPromises();
expect(MQTT.connect).toHaveBeenCalledTimes(1);
const expected = {
will: {payload: Buffer.from('offline'), retain: true, topic: 'zigbee2mqtt/bridge/state', qos: 1},
keepalive: 30,
ca: Buffer.from([99, 97]),
key: Buffer.from([107, 101, 121]),
cert: Buffer.from([99, 101, 114, 116]),
password: 'pass',
username: 'user1',
clientId: 'my_client_id',
rejectUnauthorized: false,
protocolVersion: 5,
};
expect(MQTT.connect).toHaveBeenCalledWith('mqtt://localhost', expected);
});
it('Should generate network_key, pan_id and ext_pan_id when set to GENERATE', async () => {
settings.set(['advanced', 'network_key'], 'GENERATE');
settings.set(['advanced', 'pan_id'], 'GENERATE');
settings.set(['advanced', 'ext_pan_id'], 'GENERATE');
await controller.start();
await flushPromises();
expect(zigbeeHerdsman.constructor.mock.calls[0][0].network.networkKey.length).toStrictEqual(16);
expect(zigbeeHerdsman.constructor.mock.calls[0][0].network.extendedPanID.length).toStrictEqual(8);
expect(zigbeeHerdsman.constructor.mock.calls[0][0].network.panID).toStrictEqual(expect.any(Number));
expect(data.read().advanced.network_key.length).toStrictEqual(16);
expect(data.read().advanced.ext_pan_id.length).toStrictEqual(8);
expect(data.read().advanced.pan_id).toStrictEqual(expect.any(Number));
});
it('Start controller should publish cached states', async () => {
data.writeDefaultState();
await controller.start();
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bulb',
stringify({state: 'ON', brightness: 50, color_temp: 370, linkquality: 99}),
{qos: 0, retain: true},
expect.any(Function),
);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/remote', stringify({brightness: 255}), {qos: 0, retain: true}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/group_1', stringify({state: 'ON'}), {qos: 0, retain: false}, expect.any(Function));
});
it('Start controller should not publish cached states when disabled', async () => {
settings.set(['advanced', 'cache_state_send_on_startup'], false);
data.writeDefaultState();
await controller.start();
await flushPromises();
const publishedTopics = MQTT.publish.mock.calls.map((m) => m[0]);
expect(publishedTopics).toEqual(expect.not.arrayContaining(['zigbee2mqtt/bulb', 'zigbee2mqtt/remote']));
});
it('Start controller should not publish cached states when cache_state is false', async () => {
settings.set(['advanced', 'cache_state'], false);
data.writeDefaultState();
await controller.start();
await flushPromises();
expect(MQTT.publish).not.toHaveBeenCalledWith(
'zigbee2mqtt/bulb',
`{"state":"ON","brightness":50,"color_temp":370,"linkquality":99}`,
{qos: 0, retain: true},
expect.any(Function),
);
expect(MQTT.publish).not.toHaveBeenCalledWith('zigbee2mqtt/remote', `{"brightness":255}`, {qos: 0, retain: true}, expect.any(Function));
});
it('Log when MQTT client is unavailable', async () => {
await controller.start();
await flushPromises();
logger.error.mockClear();
controller.mqtt.client.reconnecting = true;
jest.advanceTimersByTime(11 * 1000);
expect(logger.error).toHaveBeenCalledWith('Not connected to MQTT server!');
controller.mqtt.client.reconnecting = false;
});
it('Dont publish to mqtt when client is unavailable', async () => {
await controller.start();
await flushPromises();
logger.error.mockClear();
controller.mqtt.client.reconnecting = true;
const device = controller.zigbee.resolveEntity('bulb');
await controller.publishEntityState(device, {
state: 'ON',
brightness: 50,
color_temp: 370,
color: {r: 100, g: 50, b: 10},
dummy: {1: 'yes', 2: 'no'},
});
await flushPromises();
expect(logger.error).toHaveBeenCalledTimes(2);
expect(logger.error).toHaveBeenCalledWith('Not connected to MQTT server!');
expect(logger.error).toHaveBeenCalledWith(
'Cannot send message: topic: \'zigbee2mqtt/bulb\', payload: \'{"brightness":50,"color":{"b":10,"g":50,"r":100},"color_temp":370,"dummy":{"1":"yes","2":"no"},"linkquality":99,"state":"ON"}',
);
controller.mqtt.client.reconnecting = false;
});
it('Load empty state when state file does not exist', async () => {
data.removeState();
await controller.start();
await flushPromises();
expect(controller.state.state).toStrictEqual({});
});
it('Should remove device not on passlist on startup', async () => {
settings.set(['passlist'], [zigbeeHerdsman.devices.bulb_color.ieeeAddr]);
zigbeeHerdsman.devices.bulb.removeFromNetwork.mockImplementationOnce(() => {
throw new Error('dummy');
});
await controller.start();
await flushPromises();
expect(zigbeeHerdsman.devices.bulb_color.removeFromNetwork).toHaveBeenCalledTimes(0);
expect(zigbeeHerdsman.devices.bulb.removeFromNetwork).toHaveBeenCalledTimes(1);
});
it('Should remove device on blocklist on startup', async () => {
settings.set(['blocklist'], [zigbeeHerdsman.devices.bulb_color.ieeeAddr]);
await controller.start();
await flushPromises();
expect(zigbeeHerdsman.devices.bulb_color.removeFromNetwork).toHaveBeenCalledTimes(1);
expect(zigbeeHerdsman.devices.bulb.removeFromNetwork).toHaveBeenCalledTimes(0);
});
it('Start controller fails', async () => {
zigbeeHerdsman.start.mockImplementationOnce(() => {
throw new Error('failed');
});
await controller.start();
expect(mockExit).toHaveBeenCalledTimes(1);
});
it('Start controller fails due to MQTT', async () => {
MQTT.on.mockImplementation((type, handler) => {
if (type === 'error') handler({message: 'addr not found'});
});
await controller.start();
await flushPromises();
expect(logger.error).toHaveBeenCalledWith('MQTT error: addr not found');
expect(logger.error).toHaveBeenCalledWith('MQTT failed to connect, exiting... (addr not found)');
expect(mockExit).toHaveBeenCalledTimes(1);
expect(mockExit).toHaveBeenCalledWith(1, false);
});
it('Start controller with permit join true', async () => {
settings.set(['permit_join'], false);
await controller.start();
expect(zigbeeHerdsman.permitJoin).toHaveBeenCalledTimes(1);
expect(zigbeeHerdsman.permitJoin).toHaveBeenCalledWith(false, undefined, undefined);
});
it('Start controller and stop with restart', async () => {
await controller.start();
await controller.stop(true);
expect(MQTT.end).toHaveBeenCalledTimes(1);
expect(zigbeeHerdsman.stop).toHaveBeenCalledTimes(1);
expect(mockExit).toHaveBeenCalledTimes(1);
expect(mockExit).toHaveBeenCalledWith(0, true);
});
it('Start controller and stop', async () => {
zigbeeHerdsman.stop.mockImplementationOnce(() => {
throw new Error('failed');
});
await controller.start();
await controller.stop();
expect(MQTT.end).toHaveBeenCalledTimes(1);
expect(zigbeeHerdsman.stop).toHaveBeenCalledTimes(1);
expect(mockExit).toHaveBeenCalledTimes(1);
expect(mockExit).toHaveBeenCalledWith(1, false);
});
it('Start controller adapter disconnects', async () => {
zigbeeHerdsman.stop.mockImplementationOnce(() => {
throw new Error('failed');
});
await controller.start();
await zigbeeHerdsman.events.adapterDisconnected();
await flushPromises();
expect(MQTT.end).toHaveBeenCalledTimes(1);
expect(zigbeeHerdsman.stop).toHaveBeenCalledTimes(1);
expect(mockExit).toHaveBeenCalledTimes(1);
expect(mockExit).toHaveBeenCalledWith(1, false);
});
it('Handle mqtt message', async () => {
await controller.start();
logger.debug.mockClear();
await MQTT.events.message('dummytopic', 'dummymessage');
expect(logger.debug).toHaveBeenCalledWith("Received MQTT message on 'dummytopic' with data 'dummymessage'", LOG_MQTT_NS);
});
it('Skip MQTT messages on topic we published to', async () => {
await controller.start();
logger.debug.mockClear();
await MQTT.events.message('zigbee2mqtt/skip-this-topic', 'skipped');
expect(logger.debug).toHaveBeenCalledWith("Received MQTT message on 'zigbee2mqtt/skip-this-topic' with data 'skipped'", LOG_MQTT_NS);
logger.debug.mockClear();
await controller.mqtt.publish('skip-this-topic', '', {});
await MQTT.events.message('zigbee2mqtt/skip-this-topic', 'skipped');
expect(logger.debug).toHaveBeenCalledTimes(0);
});
it('On zigbee event message', async () => {
await controller.start();
const device = zigbeeHerdsman.devices.bulb;
const payload = {
device,
endpoint: device.getEndpoint(1),
type: 'attributeReport',
linkquality: 10,
cluster: 'genBasic',
data: {modelId: device.modelID},
};
await zigbeeHerdsman.events.message(payload);
await flushPromises();
expect(logger.debug).toHaveBeenCalledWith(
`Received Zigbee message from 'bulb', type 'attributeReport', cluster 'genBasic', data '{"modelId":"TRADFRI bulb E27 WS opal 980lm"}' from endpoint 1`,
);
});
it('On zigbee event message with group ID', async () => {
await controller.start();
const device = zigbeeHerdsman.devices.bulb;
const payload = {
device,
endpoint: device.getEndpoint(1),
type: 'attributeReport',
linkquality: 10,
groupID: 0,
cluster: 'genBasic',
data: {modelId: device.modelID},
};
await zigbeeHerdsman.events.message(payload);
await flushPromises();
expect(logger.debug).toHaveBeenCalledWith(
`Received Zigbee message from 'bulb', type 'attributeReport', cluster 'genBasic', data '{"modelId":"TRADFRI bulb E27 WS opal 980lm"}' from endpoint 1 with groupID 0`,
);
});
it('Should add entities which are missing from configuration but are in database to configuration', async () => {
await controller.start();
const device = zigbeeHerdsman.devices.notInSettings;
expect(settings.getDevice(device.ieeeAddr)).not.toBeNull();
});
it('On zigbee deviceJoined', async () => {
await controller.start();
const device = zigbeeHerdsman.devices.bulb;
const payload = {device};
await zigbeeHerdsman.events.deviceJoined(payload);
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/log',
stringify({type: 'device_connected', message: {friendly_name: 'bulb'}}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it('acceptJoiningDeviceHandler reject device on blocklist', async () => {
await controller.start();
const device = zigbeeHerdsman.devices.bulb;
settings.set(['blocklist'], [device.ieeeAddr]);
const handler = zigbeeHerdsman.constructor.mock.calls[0][0].acceptJoiningDeviceHandler;
expect(await handler(device.ieeeAddr)).toBe(false);
});
it('acceptJoiningDeviceHandler accept device not on blocklist', async () => {
await controller.start();
const device = zigbeeHerdsman.devices.bulb;
settings.set(['blocklist'], ['123']);
const handler = zigbeeHerdsman.constructor.mock.calls[0][0].acceptJoiningDeviceHandler;
expect(await handler(device.ieeeAddr)).toBe(true);
});
it('acceptJoiningDeviceHandler accept device on passlist', async () => {
await controller.start();
const device = zigbeeHerdsman.devices.bulb;
settings.set(['passlist'], [device.ieeeAddr]);
const handler = zigbeeHerdsman.constructor.mock.calls[0][0].acceptJoiningDeviceHandler;
expect(await handler(device.ieeeAddr)).toBe(true);
});
it('acceptJoiningDeviceHandler reject device not in passlist', async () => {
await controller.start();
const device = zigbeeHerdsman.devices.bulb;
settings.set(['passlist'], ['123']);
const handler = zigbeeHerdsman.constructor.mock.calls[0][0].acceptJoiningDeviceHandler;
expect(await handler(device.ieeeAddr)).toBe(false);
});
it('acceptJoiningDeviceHandler should prefer passlist above blocklist', async () => {
await controller.start();
const device = zigbeeHerdsman.devices.bulb;
settings.set(['passlist'], [device.ieeeAddr]);
settings.set(['blocklist'], [device.ieeeAddr]);
const handler = zigbeeHerdsman.constructor.mock.calls[0][0].acceptJoiningDeviceHandler;
expect(await handler(device.ieeeAddr)).toBe(true);
});
it('acceptJoiningDeviceHandler accept when not on blocklist and passlist', async () => {
await controller.start();
const device = zigbeeHerdsman.devices.bulb;
const handler = zigbeeHerdsman.constructor.mock.calls[0][0].acceptJoiningDeviceHandler;
expect(await handler(device.ieeeAddr)).toBe(true);
});
it('Shouldnt crash when two device join events are received', async () => {
await controller.start();
const device = zigbeeHerdsman.devices.bulb;
const payload = {device};
zigbeeHerdsman.events.deviceJoined(payload);
zigbeeHerdsman.events.deviceJoined(payload);
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/log',
stringify({type: 'device_connected', message: {friendly_name: 'bulb'}}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it('On zigbee deviceInterview started', async () => {
await controller.start();
const device = zigbeeHerdsman.devices.bulb;
const payload = {device, status: 'started'};
await zigbeeHerdsman.events.deviceInterview(payload);
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/log',
stringify({type: 'pairing', message: 'interview_started', meta: {friendly_name: 'bulb'}}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it('On zigbee deviceInterview failed', async () => {
await controller.start();
const device = zigbeeHerdsman.devices.bulb;
const payload = {device, status: 'failed'};
await zigbeeHerdsman.events.deviceInterview(payload);
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/log',
stringify({type: 'pairing', message: 'interview_failed', meta: {friendly_name: 'bulb'}}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it('On zigbee deviceInterview successful supported', async () => {
await controller.start();
const device = zigbeeHerdsman.devices.bulb;
const payload = {device, status: 'successful'};
await zigbeeHerdsman.events.deviceInterview(payload);
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/log',
stringify({
type: 'pairing',
message: 'interview_successful',
meta: {
friendly_name: 'bulb',
model: 'LED1545G12',
vendor: 'IKEA',
description: 'TRADFRI bulb E26/E27, white spectrum, globe, opal, 980 lm',
supported: true,
},
}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it('On zigbee deviceInterview successful not supported', async () => {
await controller.start();
const device = zigbeeHerdsman.devices.unsupported;
const payload = {device, status: 'successful'};
await zigbeeHerdsman.events.deviceInterview(payload);
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/log',
stringify({type: 'pairing', message: 'interview_successful', meta: {friendly_name: '0x0017880104e45518', supported: false}}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it('On zigbee event device announce', async () => {
await controller.start();
const device = zigbeeHerdsman.devices.bulb;
const payload = {device};
await zigbeeHerdsman.events.deviceAnnounce(payload);
await flushPromises();
expect(logger.debug).toHaveBeenCalledWith(`Device 'bulb' announced itself`);
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/log',
stringify({type: 'device_announced', message: 'announce', meta: {friendly_name: 'bulb'}}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it('On zigbee event device leave (removed from database and settings)', async () => {
await controller.start();
zigbeeHerdsman.returnDevices.push('0x00124b00120144ae');
settings.set(['devices'], {});
MQTT.publish.mockClear();
const device = zigbeeHerdsman.devices.bulb;
const payload = {ieeeAddr: device.ieeeAddr};
await zigbeeHerdsman.events.deviceLeave(payload);
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/log',
stringify({type: 'device_removed', message: 'left_network', meta: {friendly_name: '0x000b57fffec6a5b2'}}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it('On zigbee event device leave (removed from database and NOT settings)', async () => {
await controller.start();
zigbeeHerdsman.returnDevices.push('0x00124b00120144ae');
const device = zigbeeHerdsman.devices.bulb;
MQTT.publish.mockClear();
const payload = {ieeeAddr: device.ieeeAddr};
await zigbeeHerdsman.events.deviceLeave(payload);
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/log',
stringify({type: 'device_removed', message: 'left_network', meta: {friendly_name: '0x000b57fffec6a5b2'}}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it('Publish entity state attribute output', async () => {
await controller.start();
settings.set(['experimental', 'output'], 'attribute');
MQTT.publish.mockClear();
const device = controller.zigbee.resolveEntity('bulb');
await controller.publishEntityState(device, {
dummy: {1: 'yes', 2: 'no'},
color: {r: 100, g: 50, b: 10},
state: 'ON',
test: undefined,
test1: null,
color_temp: 370,
brightness: 50,
});
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb/state', 'ON', {qos: 0, retain: true}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb/brightness', '50', {qos: 0, retain: true}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb/color_temp', '370', {qos: 0, retain: true}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb/color', '100,50,10', {qos: 0, retain: true}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb/dummy-1', 'yes', {qos: 0, retain: true}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb/dummy-2', 'no', {qos: 0, retain: true}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb/test1', '', {qos: 0, retain: true}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb/test', '', {qos: 0, retain: true}, expect.any(Function));
});
it('Publish entity state attribute_json output', async () => {
await controller.start();
settings.set(['experimental', 'output'], 'attribute_and_json');
MQTT.publish.mockClear();
const device = controller.zigbee.resolveEntity('bulb');
await controller.publishEntityState(device, {state: 'ON', brightness: 200, color_temp: 370, linkquality: 99});
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledTimes(5);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb/state', 'ON', {qos: 0, retain: true}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb/brightness', '200', {qos: 0, retain: true}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb/color_temp', '370', {qos: 0, retain: true}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb/linkquality', '99', {qos: 0, retain: true}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bulb',
stringify({state: 'ON', brightness: 200, color_temp: 370, linkquality: 99}),
{qos: 0, retain: true},
expect.any(Function),
);
});
it('Publish entity state attribute_json output filtered', async () => {
await controller.start();
settings.set(['experimental', 'output'], 'attribute_and_json');
settings.set(['devices', zigbeeHerdsman.devices.bulb.ieeeAddr, 'filtered_attributes'], ['color_temp', 'linkquality']);
MQTT.publish.mockClear();
const device = controller.zigbee.resolveEntity('bulb');
await controller.publishEntityState(device, {state: 'ON', brightness: 200, color_temp: 370, linkquality: 99});
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledTimes(3);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb/state', 'ON', {qos: 0, retain: true}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb/brightness', '200', {qos: 0, retain: true}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bulb',
stringify({state: 'ON', brightness: 200}),
{qos: 0, retain: true},
expect.any(Function),
);
});
it('Publish entity state attribute_json output filtered (device_options)', async () => {
await controller.start();
settings.set(['experimental', 'output'], 'attribute_and_json');
settings.set(['device_options', 'filtered_attributes'], ['color_temp', 'linkquality']);
MQTT.publish.mockClear();
const device = controller.zigbee.resolveEntity('bulb');
await controller.publishEntityState(device, {state: 'ON', brightness: 200, color_temp: 370, linkquality: 99});
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledTimes(3);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb/state', 'ON', {qos: 0, retain: true}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb/brightness', '200', {qos: 0, retain: true}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bulb',
stringify({state: 'ON', brightness: 200}),
{qos: 0, retain: true},
expect.any(Function),
);
});
it('Publish entity state attribute_json output filtered cache', async () => {
await controller.start();
settings.set(['advanced', 'output'], 'attribute_and_json');
settings.set(['devices', zigbeeHerdsman.devices.bulb.ieeeAddr, 'filtered_cache'], ['linkquality']);
MQTT.publish.mockClear();
const device = controller.zigbee.resolveEntity('bulb');
expect(controller.state.state[device.ieeeAddr]).toStrictEqual({brightness: 50, color_temp: 370, linkquality: 99, state: 'ON'});
await controller.publishEntityState(device, {state: 'ON', brightness: 200, color_temp: 370, linkquality: 87});
await flushPromises();
expect(controller.state.state[device.ieeeAddr]).toStrictEqual({brightness: 200, color_temp: 370, state: 'ON'});
expect(MQTT.publish).toHaveBeenCalledTimes(5);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb/state', 'ON', {qos: 0, retain: true}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb/brightness', '200', {qos: 0, retain: true}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb/linkquality', '87', {qos: 0, retain: true}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bulb',
stringify({state: 'ON', brightness: 200, color_temp: 370, linkquality: 87}),
{qos: 0, retain: true},
expect.any(Function),
);
});
it('Publish entity state attribute_json output filtered cache (device_options)', async () => {
await controller.start();
settings.set(['advanced', 'output'], 'attribute_and_json');
settings.set(['device_options', 'filtered_cache'], ['linkquality']);
MQTT.publish.mockClear();
const device = controller.zigbee.resolveEntity('bulb');
expect(controller.state.state[device.ieeeAddr]).toStrictEqual({brightness: 50, color_temp: 370, linkquality: 99, state: 'ON'});
await controller.publishEntityState(device, {state: 'ON', brightness: 200, color_temp: 370, linkquality: 87});
await flushPromises();
expect(controller.state.state[device.ieeeAddr]).toStrictEqual({brightness: 200, color_temp: 370, state: 'ON'});
expect(MQTT.publish).toHaveBeenCalledTimes(5);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb/state', 'ON', {qos: 0, retain: true}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb/brightness', '200', {qos: 0, retain: true}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb/linkquality', '87', {qos: 0, retain: true}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bulb',
stringify({state: 'ON', brightness: 200, color_temp: 370, linkquality: 87}),
{qos: 0, retain: true},
expect.any(Function),
);
});
it('Publish entity state with device information', async () => {
await controller.start();
settings.set(['mqtt', 'include_device_information'], true);
MQTT.publish.mockClear();
let device = controller.zigbee.resolveEntity('bulb');
await controller.publishEntityState(device, {state: 'ON'});
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bulb',
stringify({
state: 'ON',
brightness: 50,
color_temp: 370,
linkquality: 99,
device: {
friendlyName: 'bulb',
model: 'LED1545G12',
ieeeAddr: '0x000b57fffec6a5b2',
networkAddress: 40369,
type: 'Router',
manufacturerID: 4476,
powerSource: 'Mains (single phase)',
dateCode: null,
softwareBuildID: null,
},
}),
{qos: 0, retain: true},
expect.any(Function),
);
// Unsupported device should have model "unknown"
device = controller.zigbee.resolveEntity('unsupported2');
await controller.publishEntityState(device, {state: 'ON'});
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/unsupported2',
stringify({
state: 'ON',
device: {
friendlyName: 'unsupported2',
model: 'notSupportedModelID',
ieeeAddr: '0x0017880104e45529',
networkAddress: 6536,
type: 'EndDevice',
manufacturerID: 0,
powerSource: 'Battery',
dateCode: null,
softwareBuildID: null,
},
}),
{qos: 0, retain: false},
expect.any(Function),
);
});
it('Should publish entity state without retain', async () => {
await controller.start();
settings.set(['devices', zigbeeHerdsman.devices.bulb.ieeeAddr, 'retain'], false);
MQTT.publish.mockClear();
const device = controller.zigbee.resolveEntity('bulb');
await controller.publishEntityState(device, {state: 'ON'});
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bulb',
stringify({state: 'ON', brightness: 50, color_temp: 370, linkquality: 99}),
{qos: 0, retain: false},
expect.any(Function),
);
});
it('Should publish entity state with retain', async () => {
await controller.start();
settings.set(['devices', zigbeeHerdsman.devices.bulb.ieeeAddr, 'retain'], true);
MQTT.publish.mockClear();
const device = controller.zigbee.resolveEntity('bulb');
await controller.publishEntityState(device, {state: 'ON'});
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bulb',
stringify({state: 'ON', brightness: 50, color_temp: 370, linkquality: 99}),
{qos: 0, retain: true},
expect.any(Function),
);
});
it('Should publish entity state with expiring retention', async () => {
await controller.start();
settings.set(['mqtt', 'version'], 5);
settings.set(['devices', zigbeeHerdsman.devices.bulb.ieeeAddr, 'retain'], true);
settings.set(['devices', zigbeeHerdsman.devices.bulb.ieeeAddr, 'retention'], 37);
MQTT.publish.mockClear();
const device = controller.zigbee.resolveEntity('bulb');
await controller.publishEntityState(device, {state: 'ON'});
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bulb',
stringify({state: 'ON', brightness: 50, color_temp: 370, linkquality: 99}),
{qos: 0, retain: true, properties: {messageExpiryInterval: 37}},
expect.any(Function),
);
});
it('Publish entity state no empty messages', async () => {
data.writeEmptyState();
await controller.start();
MQTT.publish.mockClear();
const device = controller.zigbee.resolveEntity('bulb');
await controller.publishEntityState(device, {});
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledTimes(0);
});
it('Should allow to disable state persistency', async () => {
settings.set(['advanced', 'cache_state_persistent'], false);
data.removeState();
await controller.start();
MQTT.publish.mockClear();
const device = controller.zigbee.resolveEntity('bulb');
await controller.publishEntityState(device, {state: 'ON'});
await controller.publishEntityState(device, {brightness: 200});
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledTimes(2);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb', stringify({state: 'ON'}), {qos: 0, retain: true}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/bulb',
stringify({state: 'ON', brightness: 200}),
{qos: 0, retain: true},
expect.any(Function),
);
await controller.stop();
expect(data.stateExists()).toBeFalsy();
});
it('Shouldnt crash when it cannot save state', async () => {
data.removeState();
await controller.start();
logger.error.mockClear();
controller.state.file = '/';
await controller.state.save();
expect(logger.error).toHaveBeenCalledWith(expect.stringMatching(/Failed to write state to \'\/\'/));
});
it('Publish should not cache when set', async () => {
settings.set(['advanced', 'cache_state'], false);
data.writeEmptyState();
await controller.start();
MQTT.publish.mockClear();
const device = controller.zigbee.resolveEntity('bulb');
await controller.publishEntityState(device, {state: 'ON'});
await controller.publishEntityState(device, {brightness: 200});
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledTimes(2);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb', stringify({state: 'ON'}), {qos: 0, retain: true}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb', stringify({brightness: 200}), {qos: 0, retain: true}, expect.any(Function));
});
it('Should start when state is corrupted', async () => {
fs.writeFileSync(path.join(data.mockDir, 'state.json'), 'corrupted');
await controller.start();
await flushPromises();
expect(controller.state.state).toStrictEqual({});
});
it('Start controller with force_disable_retain', async () => {
settings.set(['mqtt', 'force_disable_retain'], true);
await controller.start();
await flushPromises();
expect(MQTT.connect).toHaveBeenCalledTimes(1);
const expected = {
will: {payload: Buffer.from('offline'), retain: false, topic: 'zigbee2mqtt/bridge/state', qos: 1},
};
expect(MQTT.connect).toHaveBeenCalledWith('mqtt://localhost', expected);
});
it('Should republish retained messages on MQTT reconnect', async () => {
await controller.start();
MQTT.publish.mockClear();
MQTT.events['connect']();
await jest.advanceTimersByTimeAsync(2500); // before any startup configure triggers
expect(MQTT.publish).toHaveBeenCalledTimes(14);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bridge/info', expect.any(String), {retain: true, qos: 0}, expect.any(Function));
});
it('Should not republish retained messages on MQTT reconnect when retained message are sent', async () => {
await controller.start();
MQTT.publish.mockClear();
MQTT.events['connect']();
await flushPromises();
await MQTT.events.message('zigbee2mqtt/bridge/info', 'dummy');
await jest.advanceTimersByTimeAsync(2500); // before any startup configure triggers
expect(MQTT.publish).toHaveBeenCalledTimes(1);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bridge/state', expect.any(String), {retain: true, qos: 0}, expect.any(Function));
});
it('Should prevent any message being published with retain flag when force_disable_retain is set', async () => {
settings.set(['mqtt', 'force_disable_retain'], true);
await controller.mqtt.connect();
MQTT.publish.mockClear();
await controller.mqtt.publish('fo', 'bar', {retain: true});
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledTimes(1);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/fo', 'bar', {retain: false, qos: 0}, expect.any(Function));
});
it('Should disable legacy options on new network start', async () => {
settings.set(['homeassistant'], true);
settings.reRead();
expect(settings.get().homeassistant.legacy_entity_attributes).toBeTruthy();
expect(settings.get().advanced.legacy_api).toBeTruthy();
zigbeeHerdsman.start.mockReturnValueOnce('reset');
await controller.start();
expect(settings.get().homeassistant.legacy_entity_attributes).toBeFalsy();
expect(settings.get().advanced.legacy_api).toBeFalsy();
});
it('Should publish last seen changes', async () => {
settings.set(['advanced', 'last_seen'], 'epoch');
await controller.start();
await flushPromises();
MQTT.publish.mockClear();
const device = zigbeeHerdsman.devices.remote;
await zigbeeHerdsman.events.lastSeenChanged({device, reason: 'deviceAnnounce'});
expect(MQTT.publish).toHaveBeenCalledTimes(1);
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/remote',
stringify({brightness: 255, last_seen: 1000}),
{qos: 0, retain: true},
expect.any(Function),
);
});
it('Should not publish last seen changes when reason is messageEmitted', async () => {
settings.set(['advanced', 'last_seen'], 'epoch');
await controller.start();
await flushPromises();
MQTT.publish.mockClear();
const device = zigbeeHerdsman.devices.remote;
await zigbeeHerdsman.events.lastSeenChanged({device, reason: 'messageEmitted'});
expect(MQTT.publish).toHaveBeenCalledTimes(0);
});
it('Ignore messages from coordinator', async () => {
// https://github.com/Koenkk/zigbee2mqtt/issues/9218
await controller.start();
const device = zigbeeHerdsman.devices.coordinator;
const payload = {
device,
endpoint: device.getEndpoint(1),
type: 'attributeReport',
linkquality: 10,
cluster: 'genBasic',
data: {modelId: device.modelID},
};
await zigbeeHerdsman.events.message(payload);
await flushPromises();
expect(logger.debug).toHaveBeenCalledWith(
`Received Zigbee message from 'Coordinator', type 'attributeReport', cluster 'genBasic', data '{"modelId":null}' from endpoint 1, ignoring since it is from coordinator`,
);
});
it('Should remove state of removed device when stopped', async () => {
await controller.start();
const device = controller.zigbee.resolveEntity('bulb');
expect(controller.state.state[device.ieeeAddr]).toStrictEqual({brightness: 50, color_temp: 370, linkquality: 99, state: 'ON'});
device.zh.isDeleted = true;
await controller.stop();
expect(controller.state.state[device.ieeeAddr]).toStrictEqual(undefined);
});
it('EventBus should handle errors', async () => {
const eventbus = controller.eventBus;
const callback = jest.fn().mockImplementation(async () => {
throw new Error('Whoops!');
});
eventbus.onStateChange('test', callback);
eventbus.emitStateChange({});
await flushPromises();
expect(callback).toHaveBeenCalledTimes(1);
expect(logger.error).toHaveBeenCalledWith(`EventBus error 'String/stateChange': Whoops!`);
});
});
File diff suppressed because it is too large Load Diff
+7 -7
View File
@@ -1,8 +1,8 @@
const logger = require('./stub/logger');
const data = require('../lib/util/data').default;
const path = require('path');
const tmp = require('tmp');
const fs = require('fs');
import path from 'node:path';
import tmp from 'tmp';
import data from '../lib/util/data';
describe('Data', () => {
describe('Get path', () => {
@@ -15,13 +15,13 @@ describe('Data', () => {
it('Should return correct path when ZIGBEE2MQTT_DATA set', () => {
const expected = tmp.dirSync().name;
process.env.ZIGBEE2MQTT_DATA = expected;
data.testingOnlyReload();
data._testReload();
const actual = data.getPath();
expect(actual).toBe(expected);
expect(data.joinPath('test')).toStrictEqual(path.join(expected, 'test'));
expect(data.joinPath('/test')).toStrictEqual(path.resolve(expected, '/test'));
delete process.env.ZIGBEE2MQTT_DATA;
data.testingOnlyReload();
data._testReload();
});
});
});
@@ -1,52 +1,64 @@
import data from './stub/data';
import logger from './stub/logger';
import MQTT from './stub/mqtt';
import zigbeeHerdsman from './stub/zigbeeHerdsman';
import * as data from '../mocks/data';
import {mockLogger} from '../mocks/logger';
import {events as mockMQTTEvents, mockMQTTPublishAsync} from '../mocks/mqtt';
import {flushPromises} from '../mocks/utils';
import {devices, events as mockZHEvents, returnDevices} from '../mocks/zigbeeHerdsman';
import assert from 'node:assert';
import utils from '../lib/util/utils';
import * as settings from '../lib/util/settings';
import Controller from '../lib/controller';
import Availability from '../lib/extension/availability';
import flushPromises from './lib/flushPromises';
import stringify from 'json-stable-stringify-without-jsonify';
const mocks = [MQTT.publish, logger.warning, logger.info];
const devices = zigbeeHerdsman.devices;
zigbeeHerdsman.returnDevices.push(
...[devices.bulb_color.ieeeAddr, devices.bulb_color_2.ieeeAddr, devices.coordinator.ieeeAddr, devices.remote.ieeeAddr],
import {Controller} from '../../lib/controller';
import Availability from '../../lib/extension/availability';
import * as settings from '../../lib/util/settings';
import utils from '../../lib/util/utils';
const mocksClear = [mockMQTTPublishAsync, mockLogger.warning, mockLogger.info];
returnDevices.push(
devices.bulb_color.ieeeAddr,
devices.bulb_color_2.ieeeAddr,
devices.coordinator.ieeeAddr,
devices.remote.ieeeAddr,
devices.TS0601_thermostat.ieeeAddr,
devices.bulb_2.ieeeAddr,
devices.ZNCZ02LM.ieeeAddr,
devices.GLEDOPTO_2ID.ieeeAddr,
devices.QBKG03LM.ieeeAddr,
devices.hue_twilight.ieeeAddr,
);
describe('Availability', () => {
let controller;
describe('Extension: Availability', () => {
let controller: Controller;
let resetExtension = async () => {
await controller.enableDisableExtension(false, 'Availability');
await controller.enableDisableExtension(true, 'Availability');
const resetExtension = async (): Promise<void> => {
await controller.removeExtension(controller.getExtension('Availability')!);
await controller.addExtension(new Availability(...controller.extensionArgs));
};
const setTimeAndAdvanceTimers = async (value) => {
jest.setSystemTime(Date.now() + value);
await jest.advanceTimersByTimeAsync(value);
const setTimeAndAdvanceTimers = async (value: number): Promise<void> => {
vi.setSystemTime(Date.now() + value);
await vi.advanceTimersByTimeAsync(value);
};
beforeAll(async () => {
jest.spyOn(utils, 'sleep').mockImplementation(async (seconds) => {});
jest.useFakeTimers();
vi.spyOn(utils, 'sleep').mockImplementation(vi.fn());
vi.useFakeTimers();
settings.reRead();
settings.set(['availability'], true);
controller = new Controller(jest.fn(), jest.fn());
settings.set(['availability'], {enabled: true});
controller = new Controller(vi.fn(), vi.fn());
await controller.start();
await flushPromises();
});
beforeEach(async () => {
jest.setSystemTime(utils.minutes(1));
vi.setSystemTime(utils.minutes(1));
data.writeDefaultConfiguration();
settings.reRead();
settings.set(['availability'], true);
settings.set(['availability'], {enabled: true});
settings.set(['devices', devices.bulb_color_2.ieeeAddr, 'availability'], false);
Object.values(devices).forEach((d) => (d.lastSeen = utils.minutes(1)));
mocks.forEach((m) => m.mockClear());
mocksClear.forEach((m) => m.mockClear());
await resetExtension();
Object.values(devices).forEach((d) => d.ping.mockClear());
});
@@ -54,19 +66,21 @@ describe('Availability', () => {
afterEach(async () => {});
afterAll(async () => {
await controller.stop();
jest.useRealTimers();
await controller?.stop();
await flushPromises();
vi.useRealTimers();
});
it('Should publish availability on startup for device where it is enabled for', async () => {
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability', 'online', {retain: true, qos: 1}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/remote/availability', 'online', {retain: true, qos: 1}, expect.any(Function));
expect(MQTT.publish).not.toHaveBeenCalledWith(
'zigbee2mqtt/bulb_color_2/availability',
'online',
{retain: true, qos: 1},
expect.any(Function),
);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability', stringify({state: 'online'}), {
retain: true,
qos: 1,
});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/remote/availability', stringify({state: 'online'}), {retain: true, qos: 1});
expect(mockMQTTPublishAsync).not.toHaveBeenCalledWith('zigbee2mqtt/bulb_color_2/availability', stringify({state: 'online'}), {
retain: true,
qos: 1,
});
});
it('Should ping on startup for enabled and unavailable devices', async () => {
@@ -93,38 +107,44 @@ describe('Availability', () => {
});
it('Should publish offline for active device when not seen for 10 minutes', async () => {
MQTT.publish.mockClear();
mockMQTTPublishAsync.mockClear();
await setTimeAndAdvanceTimers(utils.minutes(5));
expect(devices.bulb_color.ping).toHaveBeenCalledTimes(0);
await setTimeAndAdvanceTimers(utils.minutes(7));
expect(devices.bulb_color.ping).toHaveBeenCalledTimes(1);
expect(devices.bulb_color.ping).toHaveBeenNthCalledWith(1, true);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability', 'offline', {retain: true, qos: 1}, expect.any(Function));
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability', stringify({state: 'offline'}), {
retain: true,
qos: 1,
});
});
it('Shouldnt do anything for a device when availability: false is set for device', async () => {
await zigbeeHerdsman.events.lastSeenChanged({device: devices.bulb_color_2}); // Coverage satisfaction
await mockZHEvents.lastSeenChanged({device: devices.bulb_color_2}); // Coverage satisfaction
await setTimeAndAdvanceTimers(utils.minutes(12));
expect(devices.bulb_color_2.ping).toHaveBeenCalledTimes(0);
});
it('Should publish offline for passive device when not seen for 25 hours', async () => {
MQTT.publish.mockClear();
mockMQTTPublishAsync.mockClear();
await setTimeAndAdvanceTimers(utils.hours(26));
expect(devices.remote.ping).toHaveBeenCalledTimes(0);
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/remote/availability', 'offline', {retain: true, qos: 1}, expect.any(Function));
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/remote/availability', stringify({state: 'offline'}), {retain: true, qos: 1});
});
it('Should reset ping timer when device last seen changes for active device', async () => {
MQTT.publish.mockClear();
mockMQTTPublishAsync.mockClear();
await setTimeAndAdvanceTimers(utils.minutes(5));
expect(devices.bulb_color.ping).toHaveBeenCalledTimes(0);
await zigbeeHerdsman.events.lastSeenChanged({device: devices.bulb_color});
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability', 'offline', {retain: true, qos: 1}, expect.any(Function));
await mockZHEvents.lastSeenChanged({device: devices.bulb_color});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability', stringify({state: 'offline'}), {
retain: true,
qos: 1,
});
await setTimeAndAdvanceTimers(utils.minutes(7));
expect(devices.bulb_color.ping).toHaveBeenCalledTimes(0);
@@ -135,9 +155,9 @@ describe('Availability', () => {
});
it('Should ping again when first ping fails', async () => {
MQTT.publish.mockClear();
mockMQTTPublishAsync.mockClear();
await zigbeeHerdsman.events.lastSeenChanged({device: devices.bulb_color});
await mockZHEvents.lastSeenChanged({device: devices.bulb_color});
devices.bulb_color.ping.mockImplementationOnce(() => {
throw new Error('failed');
@@ -150,13 +170,13 @@ describe('Availability', () => {
});
it('Should reset ping timer when device last seen changes for passive device', async () => {
MQTT.publish.mockClear();
mockMQTTPublishAsync.mockClear();
await setTimeAndAdvanceTimers(utils.hours(24));
expect(devices.remote.ping).toHaveBeenCalledTimes(0);
await zigbeeHerdsman.events.lastSeenChanged({device: devices.remote});
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/remote/availability', 'offline', {retain: true, qos: 1}, expect.any(Function));
await mockZHEvents.lastSeenChanged({device: devices.remote});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/remote/availability', stringify({state: 'offline'}), {retain: true, qos: 1});
await setTimeAndAdvanceTimers(utils.hours(25));
expect(devices.remote.ping).toHaveBeenCalledTimes(0);
@@ -166,15 +186,21 @@ describe('Availability', () => {
});
it('Should immediately mark device as online when it lastSeen changes', async () => {
MQTT.publish.mockClear();
mockMQTTPublishAsync.mockClear();
await setTimeAndAdvanceTimers(utils.minutes(15));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability', 'offline', {retain: true, qos: 1}, expect.any(Function));
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability', stringify({state: 'offline'}), {
retain: true,
qos: 1,
});
devices.bulb_color.lastSeen = Date.now();
await zigbeeHerdsman.events.lastSeenChanged({device: devices.bulb_color});
await mockZHEvents.lastSeenChanged({device: devices.bulb_color});
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability', 'online', {retain: true, qos: 1}, expect.any(Function));
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability', stringify({state: 'online'}), {
retain: true,
qos: 1,
});
});
it('Should allow to change availability timeout via device options', async () => {
@@ -199,7 +225,7 @@ describe('Availability', () => {
});
it('Should allow to change availability timeout via avaiability options', async () => {
settings.set(['availability'], {active: {timeout: 30}});
settings.set(['availability', 'active', 'timeout'], 30);
await resetExtension();
devices.bulb_color.ping.mockClear();
@@ -214,7 +240,7 @@ describe('Availability', () => {
await setTimeAndAdvanceTimers(utils.minutes(9));
expect(devices.bulb_color.ping).toHaveBeenCalledTimes(0);
await zigbeeHerdsman.events.deviceLeave({ieeeAddr: devices.bulb_color.ieeeAddr});
await mockZHEvents.deviceLeave({ieeeAddr: devices.bulb_color.ieeeAddr});
await flushPromises();
await setTimeAndAdvanceTimers(utils.minutes(3));
@@ -225,7 +251,7 @@ describe('Availability', () => {
await setTimeAndAdvanceTimers(utils.minutes(9));
expect(devices.bulb_color.ping).toHaveBeenCalledTimes(0);
MQTT.events.message('zigbee2mqtt/bridge/request/device/remove', stringify({id: 'bulb_color'}));
mockMQTTEvents.message('zigbee2mqtt/bridge/request/device/remove', stringify({id: 'bulb_color'}));
await flushPromises();
await setTimeAndAdvanceTimers(utils.minutes(3));
@@ -233,7 +259,7 @@ describe('Availability', () => {
});
it('Should allow to be disabled', async () => {
settings.set(['availability'], false);
settings.set(['availability'], {enabled: false});
await resetExtension();
devices.bulb_color.ping.mockClear();
@@ -242,7 +268,7 @@ describe('Availability', () => {
});
it('Should allow to enable availability for just one device', async () => {
settings.set(['availability'], false);
settings.set(['availability'], {enabled: false});
settings.set(['devices', devices.bulb_color.ieeeAddr, 'availability'], true);
await resetExtension();
@@ -253,18 +279,19 @@ describe('Availability', () => {
});
it('Should retrieve device state when it reconnects', async () => {
//@ts-expect-error private
// @ts-expect-error private
const device = controller.zigbee.resolveEntity(devices.bulb_color.ieeeAddr);
//@ts-expect-error private
// @ts-expect-error private
controller.state.set(device, {state: 'OFF'});
const endpoint = devices.bulb_color.getEndpoint(1);
assert(endpoint);
endpoint.read.mockClear();
await zigbeeHerdsman.events.deviceAnnounce({device: devices.bulb_color});
await mockZHEvents.deviceAnnounce({device: devices.bulb_color});
await flushPromises();
await setTimeAndAdvanceTimers(utils.seconds(1));
await zigbeeHerdsman.events.deviceAnnounce({device: devices.bulb_color});
await mockZHEvents.deviceAnnounce({device: devices.bulb_color});
await flushPromises();
expect(endpoint.read).toHaveBeenCalledTimes(0);
@@ -274,7 +301,7 @@ describe('Availability', () => {
expect(endpoint.read).toHaveBeenCalledWith('genOnOff', ['onOff']);
endpoint.read.mockClear();
await zigbeeHerdsman.events.deviceAnnounce({device: devices.bulb_color});
await mockZHEvents.deviceAnnounce({device: devices.bulb_color});
await flushPromises();
endpoint.read.mockImplementationOnce(() => {
throw new Error('');
@@ -284,61 +311,30 @@ describe('Availability', () => {
});
it('Should republish availability when device is renamed', async () => {
MQTT.publish.mockClear();
mockMQTTPublishAsync.mockClear();
MQTT.events.message('zigbee2mqtt/bridge/request/device/rename', stringify({from: 'bulb_color', to: 'bulb_new_name'}));
mockMQTTEvents.message('zigbee2mqtt/bridge/request/device/rename', stringify({from: 'bulb_color', to: 'bulb_new_name'}));
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability', null, {retain: true, qos: 1}, expect.any(Function));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_new_name/availability', 'online', {retain: true, qos: 1}, expect.any(Function));
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_color/availability', '', {retain: true, qos: 1});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_new_name/availability', stringify({state: 'online'}), {
retain: true,
qos: 1,
});
await setTimeAndAdvanceTimers(utils.hours(12));
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/bulb_new_name/availability', 'offline', {retain: true, qos: 1}, expect.any(Function));
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_new_name/availability', stringify({state: 'offline'}), {
retain: true,
qos: 1,
});
});
it('Should publish availability payload in JSON format', async () => {
settings.set(['advanced', 'legacy_availability_payload'], false);
await resetExtension();
devices.remote.ping.mockClear();
MQTT.publish.mockClear();
mockMQTTPublishAsync.mockClear();
await setTimeAndAdvanceTimers(utils.hours(26));
expect(devices.remote.ping).toHaveBeenCalledTimes(0);
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/remote/availability',
stringify({state: 'offline'}),
{retain: true, qos: 1},
expect.any(Function),
);
});
it('Deprecated - should allow to block via advanced.availability_blocklist', async () => {
settings.set(['advanced', 'availability_blocklist'], [devices.bulb_color.ieeeAddr]);
await resetExtension();
devices.bulb_color.ping.mockClear();
await setTimeAndAdvanceTimers(utils.minutes(12));
expect(devices.bulb_color.ping).toHaveBeenCalledTimes(0);
});
it('Deprecated - should allow to pass certain devices via availability_passlist', async () => {
settings.set(['advanced', 'availability_passlist'], [devices.bulb_color_2.ieeeAddr]);
settings.changeEntityOptions(devices.bulb_color_2.ieeeAddr, {availability: null});
await resetExtension();
devices.bulb_color.ping.mockClear();
devices.bulb_color_2.ping.mockClear();
await setTimeAndAdvanceTimers(utils.minutes(12));
expect(devices.bulb_color.ping).toHaveBeenCalledTimes(0);
expect(devices.bulb_color_2.ping).toHaveBeenCalledTimes(1);
});
it('Deprecated - should allow to enable via availability_timeout', async () => {
settings.set(['availability'], false);
settings.set(['advanced', 'availability_timeout'], 60);
await resetExtension();
devices.bulb_color.ping.mockClear();
await setTimeAndAdvanceTimers(utils.minutes(12));
expect(devices.bulb_color.ping).toHaveBeenCalledTimes(1);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/remote/availability', stringify({state: 'offline'}), {retain: true, qos: 1});
});
it('Should publish availability for groups', async () => {
@@ -346,53 +342,49 @@ describe('Availability', () => {
await resetExtension();
devices.bulb_color_2.ping.mockClear();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/group_tradfri_remote/availability',
'online',
{retain: true, qos: 1},
expect.any(Function),
);
MQTT.publish.mockClear();
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/group_tradfri_remote/availability', stringify({state: 'online'}), {
retain: true,
qos: 1,
});
mockMQTTPublishAsync.mockClear();
await setTimeAndAdvanceTimers(utils.minutes(12));
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/group_tradfri_remote/availability',
'offline',
{retain: true, qos: 1},
expect.any(Function),
);
MQTT.publish.mockClear();
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/group_tradfri_remote/availability', stringify({state: 'offline'}), {
retain: true,
qos: 1,
});
mockMQTTPublishAsync.mockClear();
devices.bulb_color_2.lastSeen = Date.now();
await zigbeeHerdsman.events.lastSeenChanged({device: devices.bulb_color_2});
await mockZHEvents.lastSeenChanged({device: devices.bulb_color_2});
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
'zigbee2mqtt/group_tradfri_remote/availability',
'online',
{retain: true, qos: 1},
expect.any(Function),
);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/group_tradfri_remote/availability', stringify({state: 'online'}), {
retain: true,
qos: 1,
});
});
it('Should clear the ping queue on stop', async () => {
//@ts-expect-error private
const availability = controller.extensions.find((extension) => extension instanceof Availability);
const publishAvailabilitySpy = jest.spyOn(availability, 'publishAvailability');
const availability = controller.getExtension('Availability')! as Availability;
// @ts-expect-error private
const publishAvailabilitySpy = vi.spyOn(availability, 'publishAvailability');
devices.bulb_color.ping.mockImplementationOnce(() => new Promise((resolve) => setTimeout(resolve, 1000)));
// @ts-expect-error private
availability.addToPingQueue(devices.bulb_color);
// @ts-expect-error private
availability.addToPingQueue(devices.bulb_color_2);
await availability.stop();
await setTimeAndAdvanceTimers(utils.minutes(1));
// @ts-expect-error private
expect(availability.pingQueue).toEqual([]);
// Validate the stop-interrupt implicitly by checking that it prevents further function invocations
expect(publishAvailabilitySpy).not.toHaveBeenCalled();
devices.bulb_color.ping = jest.fn(); // ensure reset
devices.bulb_color.ping = vi.fn(); // ensure reset
});
it('Should prevent instance restart', async () => {
//@ts-expect-error private
const availability = controller.extensions.find((extension) => extension instanceof Availability);
const availability = controller.getExtension('Availability')! as Availability;
await availability.stop();
+777
View File
@@ -0,0 +1,777 @@
import * as data from '../mocks/data';
import {mockDebounce} from '../mocks/debounce';
import {mockLogger} from '../mocks/logger';
import {events as mockMQTTEvents, mockMQTTPublishAsync} from '../mocks/mqtt';
import {flushPromises} from '../mocks/utils';
import {Device, devices, groups, events as mockZHEvents} from '../mocks/zigbeeHerdsman';
import stringify from 'json-stable-stringify-without-jsonify';
import {Controller} from '../../lib/controller';
import Bind from '../../lib/extension/bind';
import * as settings from '../../lib/util/settings';
const mocksClear = [
mockDebounce,
mockMQTTPublishAsync,
devices.bulb_color.getEndpoint(1)!.configureReporting,
devices.bulb_color.getEndpoint(1)!.bind,
devices.bulb_color_2.getEndpoint(1)!.read,
];
describe('Extension: Bind', () => {
let controller: Controller;
const resetExtension = async (): Promise<void> => {
await controller.removeExtension(controller.getExtension('Bind')!);
await controller.addExtension(new Bind(...controller.extensionArgs));
};
const mockClear = (device: Device): void => {
for (const endpoint of device.endpoints) {
endpoint.read.mockClear();
endpoint.write.mockClear();
endpoint.configureReporting.mockClear();
endpoint.bind = vi.fn();
endpoint.bind.mockClear();
endpoint.unbind.mockClear();
}
};
beforeAll(async () => {
vi.useFakeTimers();
controller = new Controller(vi.fn(), vi.fn());
await controller.start();
await flushPromises();
});
beforeEach(async () => {
data.writeDefaultConfiguration();
settings.reRead();
groups.group_1.members = [];
await resetExtension();
mocksClear.forEach((m) => m.mockClear());
});
afterAll(async () => {
await controller?.stop();
await flushPromises();
vi.useRealTimers();
});
it('Should bind to device and configure reporting', async () => {
const device = devices.remote;
const target = devices.bulb_color.getEndpoint(1)!;
const endpoint = device.getEndpoint(1)!;
// Setup
const originalDeviceOutputClusters = device.getEndpoint(1)!.outputClusters;
device.getEndpoint(1)!.outputClusters = [...device.getEndpoint(1)!.outputClusters, 768];
const originalTargetBinds = target.binds;
target.binds = [{cluster: {name: 'genLevelCtrl'}, target: devices.coordinator.getEndpoint(1)!}];
target.getClusterAttributeValue.mockReturnValueOnce(undefined);
mockClear(device);
target.configureReporting.mockImplementationOnce(() => {
throw new Error('timeout');
});
mockMQTTEvents.message('zigbee2mqtt/bridge/request/device/bind', stringify({transaction: '1234', from: 'remote', to: 'bulb_color'}));
await flushPromises();
expect(target.read).toHaveBeenCalledWith('lightingColorCtrl', ['colorCapabilities']);
expect(endpoint.bind).toHaveBeenCalledTimes(4);
expect(endpoint.bind).toHaveBeenCalledWith('genOnOff', target);
expect(endpoint.bind).toHaveBeenCalledWith('genLevelCtrl', target);
expect(endpoint.bind).toHaveBeenCalledWith('genScenes', target);
expect(endpoint.bind).toHaveBeenCalledWith('lightingColorCtrl', target);
expect(target.configureReporting).toHaveBeenCalledTimes(3);
expect(target.configureReporting).toHaveBeenCalledWith('genOnOff', [
{attribute: 'onOff', maximumReportInterval: 3600, minimumReportInterval: 0, reportableChange: 0},
]);
expect(target.configureReporting).toHaveBeenCalledWith('genLevelCtrl', [
{attribute: 'currentLevel', maximumReportInterval: 3600, minimumReportInterval: 5, reportableChange: 1},
]);
expect(target.configureReporting).toHaveBeenCalledWith('lightingColorCtrl', [
{attribute: 'colorTemperature', minimumReportInterval: 5, maximumReportInterval: 3600, reportableChange: 1},
{attribute: 'currentX', minimumReportInterval: 5, maximumReportInterval: 3600, reportableChange: 1},
{attribute: 'currentY', minimumReportInterval: 5, maximumReportInterval: 3600, reportableChange: 1},
]);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({
transaction: '1234',
data: {
from: 'remote',
from_endpoint: 'default',
to: 'bulb_color',
clusters: ['genScenes', 'genOnOff', 'genLevelCtrl', 'lightingColorCtrl'],
failed: [],
},
status: 'ok',
}),
{retain: false, qos: 0},
);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bridge/devices', expect.any(String), {retain: true, qos: 0});
// Teardown
target.binds = originalTargetBinds;
device.getEndpoint(1)!.outputClusters = originalDeviceOutputClusters;
});
it('Should throw error on invalid payload', async () => {
mockMQTTPublishAsync.mockClear();
mockMQTTEvents.message('zigbee2mqtt/bridge/request/device/bind', stringify({fromz: 'remote', to: 'bulb_color'}));
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({data: {}, status: 'error', error: 'Invalid payload'}),
{retain: false, qos: 0},
);
});
it('Filters out unsupported clusters for reporting setup', async () => {
const device = devices.remote;
const target = devices.bulb_color.getEndpoint(1)!;
const endpoint = device.getEndpoint(1)!;
// Setup
const originalDeviceInputClusters = device.getEndpoint(1)!.inputClusters;
device.getEndpoint(1)!.inputClusters = [...device.getEndpoint(1)!.inputClusters, 8];
const originalDeviceOutputClusters = device.getEndpoint(1)!.outputClusters;
device.getEndpoint(1)!.outputClusters = [...device.getEndpoint(1)!.outputClusters, 768];
const originalTargetInputClusters = target.inputClusters;
target.inputClusters = [...originalTargetInputClusters];
target.inputClusters.splice(originalTargetInputClusters.indexOf(8), 1); // remove genLevelCtrl
const originalTargetOutputClusters = target.outputClusters;
target.outputClusters = [...target.outputClusters, 8];
const originalTargetBinds = target.binds;
target.binds = [{cluster: {name: 'genLevelCtrl'}, target: devices.coordinator.getEndpoint(1)!}];
target.getClusterAttributeValue.mockReturnValueOnce(undefined);
mockClear(device);
target.configureReporting.mockImplementationOnce(() => {
throw new Error('timeout');
});
mockMQTTEvents.message('zigbee2mqtt/bridge/request/device/bind', stringify({transaction: '1234', from: 'remote', to: 'bulb_color'}));
await flushPromises();
expect(target.read).toHaveBeenCalledWith('lightingColorCtrl', ['colorCapabilities']);
expect(endpoint.bind).toHaveBeenCalledTimes(4);
expect(endpoint.bind).toHaveBeenCalledWith('genOnOff', target);
expect(endpoint.bind).toHaveBeenCalledWith('genLevelCtrl', target);
expect(endpoint.bind).toHaveBeenCalledWith('genScenes', target);
expect(endpoint.bind).toHaveBeenCalledWith('lightingColorCtrl', target);
expect(target.configureReporting).toHaveBeenCalledTimes(2);
expect(target.configureReporting).toHaveBeenCalledWith('genOnOff', [
{attribute: 'onOff', maximumReportInterval: 3600, minimumReportInterval: 0, reportableChange: 0},
]);
// expect(target.configureReporting).toHaveBeenCalledWith("genLevelCtrl",[{"attribute": "currentLevel", "maximumReportInterval": 3600, "minimumReportInterval": 5, "reportableChange": 1}]);
expect(target.configureReporting).toHaveBeenCalledWith('lightingColorCtrl', [
{attribute: 'colorTemperature', minimumReportInterval: 5, maximumReportInterval: 3600, reportableChange: 1},
{attribute: 'currentX', minimumReportInterval: 5, maximumReportInterval: 3600, reportableChange: 1},
{attribute: 'currentY', minimumReportInterval: 5, maximumReportInterval: 3600, reportableChange: 1},
]);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({
transaction: '1234',
data: {
from: 'remote',
from_endpoint: 'default',
to: 'bulb_color',
clusters: ['genScenes', 'genOnOff', 'genLevelCtrl', 'lightingColorCtrl'],
failed: [],
},
status: 'ok',
}),
{retain: false, qos: 0},
);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bridge/devices', expect.any(String), {retain: true, qos: 0});
// Teardown
target.binds = originalTargetBinds;
target.inputClusters = originalTargetInputClusters;
target.outputClusters = originalTargetOutputClusters;
device.getEndpoint(1)!.inputClusters = originalDeviceInputClusters;
device.getEndpoint(1)!.outputClusters = originalDeviceOutputClusters;
});
it('Filters out reporting setup based on bind status', async () => {
const device = devices.remote;
const target = devices.bulb_color.getEndpoint(1)!;
const endpoint = device.getEndpoint(1)!;
// Setup
const originalDeviceOutputClusters = device.getEndpoint(1)!.outputClusters;
device.getEndpoint(1)!.outputClusters = [...device.getEndpoint(1)!.outputClusters, 768];
const originalTargetBinds = target.binds;
target.binds = [{cluster: {name: 'genLevelCtrl'}, target: devices.coordinator.getEndpoint(1)!}];
target.getClusterAttributeValue.mockReturnValueOnce(undefined);
mockClear(device);
target.configureReporting.mockImplementationOnce(() => {
throw new Error('timeout');
});
const originalTargetCR = target.configuredReportings;
target.configuredReportings = [
{
cluster: {name: 'genLevelCtrl'},
attribute: {name: 'currentLevel', ID: 0},
minimumReportInterval: 0,
maximumReportInterval: 3600,
reportableChange: 0,
},
];
mockMQTTEvents.message('zigbee2mqtt/bridge/request/device/bind', stringify({transaction: '1234', from: 'remote', to: 'bulb_color'}));
await flushPromises();
expect(target.read).toHaveBeenCalledWith('lightingColorCtrl', ['colorCapabilities']);
expect(endpoint.bind).toHaveBeenCalledTimes(4);
expect(endpoint.bind).toHaveBeenCalledWith('genOnOff', target);
expect(endpoint.bind).toHaveBeenCalledWith('genLevelCtrl', target);
expect(endpoint.bind).toHaveBeenCalledWith('genScenes', target);
expect(endpoint.bind).toHaveBeenCalledWith('lightingColorCtrl', target);
expect(target.configureReporting).toHaveBeenCalledTimes(2);
expect(target.configureReporting).toHaveBeenCalledWith('genOnOff', [
{attribute: 'onOff', maximumReportInterval: 3600, minimumReportInterval: 0, reportableChange: 0},
]);
// expect(target.configureReporting).toHaveBeenCalledWith("genLevelCtrl",[{"attribute": "currentLevel", "maximumReportInterval": 3600, "minimumReportInterval": 5, "reportableChange": 1}]);
expect(target.configureReporting).toHaveBeenCalledWith('lightingColorCtrl', [
{attribute: 'colorTemperature', minimumReportInterval: 5, maximumReportInterval: 3600, reportableChange: 1},
{attribute: 'currentX', minimumReportInterval: 5, maximumReportInterval: 3600, reportableChange: 1},
{attribute: 'currentY', minimumReportInterval: 5, maximumReportInterval: 3600, reportableChange: 1},
]);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({
transaction: '1234',
data: {
from: 'remote',
from_endpoint: 'default',
to: 'bulb_color',
clusters: ['genScenes', 'genOnOff', 'genLevelCtrl', 'lightingColorCtrl'],
failed: [],
},
status: 'ok',
}),
{retain: false, qos: 0},
);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bridge/devices', expect.any(String), {retain: true, qos: 0});
// Teardown
target.configuredReportings = originalTargetCR;
target.binds = originalTargetBinds;
device.getEndpoint(1)!.outputClusters = originalDeviceOutputClusters;
});
it('Should bind only specified clusters', async () => {
const device = devices.remote;
const target = devices.bulb_color.getEndpoint(1)!;
const endpoint = device.getEndpoint(1)!;
mockClear(device);
mockMQTTEvents.message('zigbee2mqtt/bridge/request/device/bind', stringify({from: 'remote', to: 'bulb_color', clusters: ['genOnOff']}));
await flushPromises();
expect(endpoint.bind).toHaveBeenCalledTimes(1);
expect(endpoint.bind).toHaveBeenCalledWith('genOnOff', target);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({data: {from: 'remote', from_endpoint: 'default', to: 'bulb_color', clusters: ['genOnOff'], failed: []}, status: 'ok'}),
{retain: false, qos: 0},
);
});
it('Should log error when there is nothing to bind', async () => {
const device = devices.bulb_color;
const endpoint = device.getEndpoint(1)!;
mockClear(device);
mockLogger.error.mockClear();
mockMQTTEvents.message('zigbee2mqtt/bridge/request/device/bind', stringify({from: 'remote', to: 'button'}));
await flushPromises();
expect(endpoint.bind).toHaveBeenCalledTimes(0);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({data: {}, status: 'error', error: 'Nothing to bind'}),
{retain: false, qos: 0},
);
});
it('Should unbind', async () => {
const device = devices.remote;
const target = devices.bulb_color.getEndpoint(1)!;
// setup
target.configureReporting.mockImplementationOnce(() => {
throw new Error('timeout');
});
const originalRemoteBinds = device.getEndpoint(1)!.binds;
device.getEndpoint(1)!.binds = [];
const originalTargetBinds = target.binds;
target.binds = [
{cluster: {name: 'genOnOff'}, target: devices.coordinator.getEndpoint(1)!},
{cluster: {name: 'genLevelCtrl'}, target: devices.coordinator.getEndpoint(1)!},
{cluster: {name: 'lightingColorCtrl'}, target: devices.coordinator.getEndpoint(1)!},
];
const endpoint = device.getEndpoint(1)!;
mockClear(device);
delete devices.bulb_color.meta.configured;
expect(devices.bulb_color.meta.configured).toBe(undefined);
mockMQTTEvents.message('zigbee2mqtt/bridge/request/device/unbind', stringify({from: 'remote', to: 'bulb_color'}));
await flushPromises();
expect(endpoint.unbind).toHaveBeenCalledTimes(3);
expect(endpoint.unbind).toHaveBeenCalledWith('genOnOff', target);
expect(endpoint.unbind).toHaveBeenCalledWith('genLevelCtrl', target);
expect(endpoint.unbind).toHaveBeenCalledWith('genScenes', target);
// Disable reporting
expect(target.configureReporting).toHaveBeenCalledTimes(3);
expect(target.configureReporting).toHaveBeenCalledWith('genOnOff', [
{attribute: 'onOff', maximumReportInterval: 0xffff, minimumReportInterval: 0, reportableChange: 0},
]);
expect(target.configureReporting).toHaveBeenCalledWith('genLevelCtrl', [
{attribute: 'currentLevel', maximumReportInterval: 0xffff, minimumReportInterval: 5, reportableChange: 1},
]);
expect(target.configureReporting).toHaveBeenCalledWith('lightingColorCtrl', [
{attribute: 'colorTemperature', minimumReportInterval: 5, maximumReportInterval: 0xffff, reportableChange: 1},
{attribute: 'currentX', minimumReportInterval: 5, maximumReportInterval: 0xffff, reportableChange: 1},
{attribute: 'currentY', minimumReportInterval: 5, maximumReportInterval: 0xffff, reportableChange: 1},
]);
expect(devices.bulb_color.meta.configured).toBe(332242049);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/unbind',
stringify({
data: {from: 'remote', from_endpoint: 'default', to: 'bulb_color', clusters: ['genScenes', 'genOnOff', 'genLevelCtrl'], failed: []},
status: 'ok',
}),
{retain: false, qos: 0},
);
// Teardown
target.binds = originalTargetBinds;
device.getEndpoint(1)!.binds = originalRemoteBinds;
});
it('Should unbind coordinator', async () => {
const device = devices.remote;
const target = devices.coordinator.getEndpoint(1)!;
const endpoint = device.getEndpoint(1)!;
mockClear(device);
endpoint.unbind.mockClear();
mockMQTTEvents.message('zigbee2mqtt/bridge/request/device/unbind', stringify({from: 'remote', to: 'Coordinator'}));
await flushPromises();
expect(endpoint.unbind).toHaveBeenCalledTimes(3);
expect(endpoint.unbind).toHaveBeenCalledWith('genOnOff', target);
expect(endpoint.unbind).toHaveBeenCalledWith('genLevelCtrl', target);
expect(endpoint.unbind).toHaveBeenCalledWith('genScenes', target);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/unbind',
stringify({
data: {from: 'remote', from_endpoint: 'default', to: 'Coordinator', clusters: ['genScenes', 'genOnOff', 'genLevelCtrl'], failed: []},
status: 'ok',
}),
{retain: false, qos: 0},
);
});
it('Should bind to groups', async () => {
const device = devices.remote;
const target = groups.group_1;
const target1Member = devices.bulb.getEndpoint(1)!;
const endpoint = device.getEndpoint(1)!;
target.members.push(target1Member);
target1Member.configureReporting.mockClear();
mockClear(device);
mockMQTTEvents.message('zigbee2mqtt/bridge/request/device/bind', stringify({from: 'remote', to: 'group_1'}));
await flushPromises();
expect(endpoint.bind).toHaveBeenCalledTimes(3);
expect(endpoint.bind).toHaveBeenCalledWith('genOnOff', target);
expect(endpoint.bind).toHaveBeenCalledWith('genLevelCtrl', target);
expect(endpoint.bind).toHaveBeenCalledWith('genScenes', target);
expect(target1Member.configureReporting).toHaveBeenCalledTimes(2);
expect(target1Member.configureReporting).toHaveBeenCalledWith('genOnOff', [
{attribute: 'onOff', maximumReportInterval: 3600, minimumReportInterval: 0, reportableChange: 0},
]);
expect(target1Member.configureReporting).toHaveBeenCalledWith('genLevelCtrl', [
{attribute: 'currentLevel', maximumReportInterval: 3600, minimumReportInterval: 5, reportableChange: 1},
]);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({
data: {from: 'remote', from_endpoint: 'default', to: 'group_1', clusters: ['genScenes', 'genOnOff', 'genLevelCtrl'], failed: []},
status: 'ok',
}),
{retain: false, qos: 0},
);
// Should configure reporting for device added to group
target1Member.configureReporting.mockClear();
await mockMQTTEvents.message('zigbee2mqtt/bridge/request/group/members/add', stringify({group: 'group_1', device: 'bulb'}));
await flushPromises();
expect(target1Member.configureReporting).toHaveBeenCalledTimes(2);
expect(target1Member.configureReporting).toHaveBeenCalledWith('genOnOff', [
{attribute: 'onOff', maximumReportInterval: 3600, minimumReportInterval: 0, reportableChange: 0},
]);
expect(target1Member.configureReporting).toHaveBeenCalledWith('genLevelCtrl', [
{attribute: 'currentLevel', maximumReportInterval: 3600, minimumReportInterval: 5, reportableChange: 1},
]);
});
it('Should unbind from group', async () => {
const device = devices.remote;
const target = groups.group_1;
const target1Member = devices.bulb.getEndpoint(1)!;
const endpoint = device.getEndpoint(1)!;
target.members.push(target1Member);
target1Member.configureReporting.mockClear();
mockClear(device);
mockMQTTEvents.message('zigbee2mqtt/bridge/request/device/unbind', stringify({from: 'remote', to: 'group_1'}));
await flushPromises();
expect(endpoint.unbind).toHaveBeenCalledTimes(3);
expect(endpoint.unbind).toHaveBeenCalledWith('genOnOff', target);
expect(endpoint.unbind).toHaveBeenCalledWith('genLevelCtrl', target);
expect(endpoint.unbind).toHaveBeenCalledWith('genScenes', target);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/unbind',
stringify({
data: {from: 'remote', from_endpoint: 'default', to: 'group_1', clusters: ['genScenes', 'genOnOff', 'genLevelCtrl'], failed: []},
status: 'ok',
}),
{retain: false, qos: 0},
);
});
it('Should unbind from group with skip_disable_reporting=true', async () => {
const device = devices.remote;
const target = groups.group_1;
const target1Member = devices.bulb_2.getEndpoint(1)!;
const endpoint = device.getEndpoint(1)!;
target.members.push(target1Member);
// The device unbind mock doesn't remove binds, therefore remove them here already otherwise configure reporiting is not disabled.
const originalBinds = endpoint.binds;
endpoint.binds = [];
target1Member.binds = [
{cluster: {name: 'genLevelCtrl'}, target: devices.coordinator.getEndpoint(1)!},
{cluster: {name: 'genOnOff'}, target: devices.coordinator.getEndpoint(1)!},
];
target1Member.configureReporting.mockClear();
mockClear(device);
mockMQTTEvents.message('zigbee2mqtt/bridge/request/device/unbind', stringify({from: 'remote', to: 'group_1', skip_disable_reporting: true}));
await flushPromises();
expect(endpoint.unbind).toHaveBeenCalledTimes(3);
// with skip_disable_reporting set to false, we don't expect it to reconfigure reporting
expect(target1Member.configureReporting).toHaveBeenCalledTimes(0);
endpoint.binds = originalBinds;
});
it('Should unbind from group with skip_disable_reporting=false', async () => {
const device = devices.remote;
const target = groups.group_1;
const target1Member = devices.bulb_2.getEndpoint(1)!;
const endpoint = device.getEndpoint(1)!;
target.members.push(target1Member);
// The device unbind mock doesn't remove binds, therefore remove them here already otherwise configure reporiting is not disabled.
const originalBinds = endpoint.binds;
endpoint.binds = [];
target1Member.binds = [
{cluster: {name: 'genLevelCtrl'}, target: devices.coordinator.getEndpoint(1)!},
{cluster: {name: 'genOnOff'}, target: devices.coordinator.getEndpoint(1)!},
];
target1Member.configureReporting.mockClear();
mockClear(device);
mockMQTTEvents.message('zigbee2mqtt/bridge/request/device/unbind', stringify({from: 'remote', to: 'group_1', skip_disable_reporting: false}));
await flushPromises();
expect(endpoint.unbind).toHaveBeenCalledTimes(3);
// with skip_disable_reporting set, we expect it to reconfigure reporting
expect(target1Member.configureReporting).toHaveBeenCalledTimes(2);
expect(target1Member.configureReporting).toHaveBeenCalledWith('genLevelCtrl', [
{attribute: 'currentLevel', maximumReportInterval: 65535, minimumReportInterval: 5, reportableChange: 1},
]);
expect(target1Member.configureReporting).toHaveBeenCalledWith('genOnOff', [
{attribute: 'onOff', maximumReportInterval: 65535, minimumReportInterval: 0, reportableChange: 0},
]);
endpoint.binds = originalBinds;
});
it('Should bind to group by number', async () => {
const device = devices.remote;
const target = groups.group_1;
const endpoint = device.getEndpoint(1)!;
mockClear(device);
mockMQTTEvents.message('zigbee2mqtt/bridge/request/device/bind', stringify({from: 'remote', to: '1'}));
await flushPromises();
expect(endpoint.bind).toHaveBeenCalledTimes(3);
expect(endpoint.bind).toHaveBeenCalledWith('genOnOff', target);
expect(endpoint.bind).toHaveBeenCalledWith('genLevelCtrl', target);
expect(endpoint.bind).toHaveBeenCalledWith('genScenes', target);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({
data: {from: 'remote', from_endpoint: 'default', to: '1', clusters: ['genScenes', 'genOnOff', 'genLevelCtrl'], failed: []},
status: 'ok',
}),
{retain: false, qos: 0},
);
});
it('Should log when bind fails', async () => {
mockLogger.error.mockClear();
const device = devices.remote;
const endpoint = device.getEndpoint(1)!;
mockClear(device);
endpoint.bind.mockImplementation(() => {
throw new Error('failed');
});
mockMQTTEvents.message('zigbee2mqtt/bridge/request/device/bind', stringify({from: 'remote', to: 'bulb_color'}));
await flushPromises();
expect(endpoint.bind).toHaveBeenCalledTimes(3);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({data: {}, status: 'error', error: 'Failed to bind'}),
{retain: false, qos: 0},
);
});
it('Should bind from non default endpoint names', async () => {
const device = devices.remote;
const target = devices.QBKG03LM.getEndpoint(3)!;
const endpoint = device.getEndpoint(2)!;
mockClear(device);
mockMQTTEvents.message(
'zigbee2mqtt/bridge/request/device/bind',
stringify({from: 'remote', from_endpoint: 'ep2', to: 'wall_switch_double', to_endpoint: 'right'}),
);
await flushPromises();
expect(endpoint.bind).toHaveBeenCalledTimes(1);
expect(endpoint.bind).toHaveBeenCalledWith('genOnOff', target);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({
data: {from: 'remote', from_endpoint: 'ep2', to: 'wall_switch_double', to_endpoint: 'right', clusters: ['genOnOff'], failed: []},
status: 'ok',
}),
{retain: false, qos: 0},
);
});
it('Should bind from non default endpoint IDs', async () => {
const device = devices.remote;
const target = devices.QBKG03LM.getEndpoint(3)!;
const endpoint = device.getEndpoint(2)!;
mockClear(device);
mockMQTTEvents.message(
'zigbee2mqtt/bridge/request/device/bind',
stringify({from: 'remote', from_endpoint: 2, to: 'wall_switch_double', to_endpoint: 3}),
);
await flushPromises();
expect(endpoint.bind).toHaveBeenCalledTimes(1);
expect(endpoint.bind).toHaveBeenCalledWith('genOnOff', target);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({
data: {from: 'remote', from_endpoint: 2, to: 'wall_switch_double', to_endpoint: 3, clusters: ['genOnOff'], failed: []},
status: 'ok',
}),
{retain: false, qos: 0},
);
});
it('Should bind server clusters to client clusters', async () => {
const device = devices.temperature_sensor;
const target = devices.heating_actuator.getEndpoint(1)!;
const endpoint = device.getEndpoint(1)!;
mockClear(device);
mockMQTTEvents.message('zigbee2mqtt/bridge/request/device/bind', stringify({from: 'temperature_sensor', to: 'heating_actuator'}));
await flushPromises();
expect(endpoint.bind).toHaveBeenCalledTimes(1);
expect(endpoint.bind).toHaveBeenCalledWith('msTemperatureMeasurement', target);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({
data: {
from: 'temperature_sensor',
from_endpoint: 'default',
to: 'heating_actuator',
clusters: ['msTemperatureMeasurement'],
failed: [],
},
status: 'ok',
}),
{retain: false, qos: 0},
);
});
it('Should bind to default endpoint returned by endpoints()', async () => {
const device = devices.remote;
const target = devices.QBKG04LM.getEndpoint(2)!;
const endpoint = device.getEndpoint(2)!;
mockClear(device);
mockMQTTEvents.message('zigbee2mqtt/bridge/request/device/bind', stringify({from: 'remote', from_endpoint: 'ep2', to: 'wall_switch'}));
await flushPromises();
expect(endpoint.bind).toHaveBeenCalledTimes(1);
expect(endpoint.bind).toHaveBeenCalledWith('genOnOff', target);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({data: {from: 'remote', from_endpoint: 'ep2', to: 'wall_switch', clusters: ['genOnOff'], failed: []}, status: 'ok'}),
{retain: false, qos: 0},
);
});
it('Should unbind from default_bind_group', async () => {
const device = devices.remote;
const target = 'default_bind_group';
const endpoint = device.getEndpoint(1)!;
mockClear(device);
mockMQTTEvents.message('zigbee2mqtt/bridge/request/device/unbind', stringify({from: 'remote', to: target}));
await flushPromises();
expect(endpoint.unbind).toHaveBeenCalledTimes(3);
expect(endpoint.unbind).toHaveBeenCalledWith('genOnOff', 901);
expect(endpoint.unbind).toHaveBeenCalledWith('genLevelCtrl', 901);
expect(endpoint.unbind).toHaveBeenCalledWith('genScenes', 901);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/unbind',
stringify({
data: {
from: 'remote',
from_endpoint: 'default',
to: 'default_bind_group',
clusters: ['genScenes', 'genOnOff', 'genLevelCtrl'],
failed: [],
},
status: 'ok',
}),
{retain: false, qos: 0},
);
});
it('Error bind fails when source device does not exist', async () => {
const device = devices.remote;
mockClear(device);
mockMQTTEvents.message('zigbee2mqtt/bridge/request/device/bind', stringify({from: 'remote_not_existing', to: 'bulb_color'}));
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({data: {}, status: 'error', error: "Source device 'remote_not_existing' does not exist"}),
{retain: false, qos: 0},
);
});
it("Error bind fails when source device's endpoint does not exist", async () => {
const device = devices.remote;
mockClear(device);
mockMQTTEvents.message(
'zigbee2mqtt/bridge/request/device/bind',
stringify({from: 'remote', from_endpoint: 'not_existing_endpoint', to: 'bulb_color'}),
);
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({data: {}, status: 'error', error: "Source device 'remote' does not have endpoint 'not_existing_endpoint'"}),
{retain: false, qos: 0},
);
});
it('Error bind fails when target device or group does not exist', async () => {
const device = devices.remote;
mockClear(device);
mockMQTTEvents.message('zigbee2mqtt/bridge/request/device/bind', stringify({from: 'remote', to: 'bulb_color_not_existing'}));
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({data: {}, status: 'error', error: "Target device or group 'bulb_color_not_existing' does not exist"}),
{retain: false, qos: 0},
);
});
it("Error bind fails when target device's endpoint does not exist", async () => {
const device = devices.remote;
mockClear(device);
mockMQTTEvents.message(
'zigbee2mqtt/bridge/request/device/bind',
stringify({from: 'remote', to: 'bulb_color', to_endpoint: 'not_existing_endpoint'}),
);
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/bind',
stringify({data: {}, status: 'error', error: "Target device 'bulb_color' does not have endpoint 'not_existing_endpoint'"}),
{retain: false, qos: 0},
);
});
it('Should poll bounded Hue bulb when receiving message from Hue dimmer', async () => {
const remote = devices.remote;
const data = {button: 3, unknown1: 3145728, type: 2, unknown2: 0, time: 1};
const payload = {
data,
cluster: 'manuSpecificPhilips',
device: remote,
endpoint: remote.getEndpoint(2)!,
type: 'commandHueNotification',
linkquality: 10,
groupID: 0,
};
await mockZHEvents.message(payload);
await flushPromises();
expect(mockDebounce).toHaveBeenCalledTimes(1);
expect(devices.bulb_color.getEndpoint(1)!.read).toHaveBeenCalledWith('genLevelCtrl', ['currentLevel']);
});
it('Should poll bounded Hue bulb when receiving message from scene controller', async () => {
const remote = devices.bj_scene_switch;
const data = {action: 'recall_2_row_1'};
devices.bulb_color_2.getEndpoint(1)!.read.mockImplementationOnce(() => {
throw new Error('failed');
});
const payload = {
data,
cluster: 'genScenes',
device: remote,
endpoint: remote.getEndpoint(10)!,
type: 'commandRecall',
linkquality: 10,
groupID: 0,
};
await mockZHEvents.message(payload);
await flushPromises();
// Calls to three clusters are expected in this case
expect(mockDebounce).toHaveBeenCalledTimes(3);
expect(devices.bulb_color_2.getEndpoint(1)!.read).toHaveBeenCalledWith('genOnOff', ['onOff']);
expect(devices.bulb_color_2.getEndpoint(1)!.read).toHaveBeenCalledWith('genLevelCtrl', ['currentLevel']);
expect(devices.bulb_color_2.getEndpoint(1)!.read).toHaveBeenCalledWith('lightingColorCtrl', ['currentX', 'currentY', 'colorTemperature']);
});
it('Should poll grouped Hue bulb when receiving message from TRADFRI remote', async () => {
devices.bulb_color_2.getEndpoint(1)!.read.mockClear();
devices.bulb_2.getEndpoint(1)!.read.mockClear();
const remote = devices.tradfri_remote;
const data = {stepmode: 0, stepsize: 43, transtime: 5};
const payload = {
data,
cluster: 'genLevelCtrl',
device: remote,
endpoint: remote.getEndpoint(1)!,
type: 'commandStepWithOnOff',
linkquality: 10,
groupID: 15071,
};
await mockZHEvents.message(payload);
await flushPromises();
expect(mockDebounce).toHaveBeenCalledTimes(2);
expect(devices.bulb_color_2.getEndpoint(1)!.read).toHaveBeenCalledTimes(2);
expect(devices.bulb_color_2.getEndpoint(1)!.read).toHaveBeenCalledWith('genLevelCtrl', ['currentLevel']);
expect(devices.bulb_color_2.getEndpoint(1)!.read).toHaveBeenCalledWith('genOnOff', ['onOff']);
// Should also only debounce once
await mockZHEvents.message(payload);
await flushPromises();
expect(mockDebounce).toHaveBeenCalledTimes(2);
expect(devices.bulb_color_2.getEndpoint(1)!.read).toHaveBeenCalledTimes(4);
// Should only call Hue bulb, not e.g. tradfri
expect(devices.bulb_2.getEndpoint(1)!.read).toHaveBeenCalledTimes(0);
});
});
File diff suppressed because it is too large Load Diff
@@ -1,53 +1,27 @@
const data = require('./stub/data');
const logger = require('./stub/logger');
const zigbeeHerdsman = require('./stub/zigbeeHerdsman');
const MQTT = require('./stub/mqtt');
const settings = require('../lib/util/settings');
const Controller = require('../lib/controller');
const flushPromises = require('./lib/flushPromises');
const wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const stringify = require('json-stable-stringify-without-jsonify');
import * as data from '../mocks/data';
import {mockLogger} from '../mocks/logger';
import {events as mockMQTTEvents, mockMQTTPublishAsync} from '../mocks/mqtt';
import {flushPromises} from '../mocks/utils';
import {Device, devices, Endpoint, events as mockZHEvents} from '../mocks/zigbeeHerdsman';
const mocksClear = [MQTT.publish, logger.warning, logger.debug];
import stringify from 'json-stable-stringify-without-jsonify';
describe('Configure', () => {
let controller;
let coordinatorEndpoint;
import {Controller} from '../../lib/controller';
import Configure from '../../lib/extension/configure';
import * as settings from '../../lib/util/settings';
const expectRemoteConfigured = () => {
const device = zigbeeHerdsman.devices.remote;
const endpoint1 = device.getEndpoint(1);
expect(endpoint1.bind).toHaveBeenCalledTimes(2);
expect(endpoint1.bind).toHaveBeenCalledWith('genOnOff', coordinatorEndpoint);
expect(endpoint1.bind).toHaveBeenCalledWith('genLevelCtrl', coordinatorEndpoint);
const mocksClear = [mockMQTTPublishAsync, mockLogger.warning, mockLogger.debug];
const endpoint2 = device.getEndpoint(2);
expect(endpoint2.write).toHaveBeenCalledTimes(1);
expect(endpoint2.write).toHaveBeenCalledWith('genBasic', {49: {type: 25, value: 11}}, {disableDefaultResponse: true, manufacturerCode: 4107});
expect(device.meta.configured).toBe(332242049);
describe('Extension: Configure', () => {
let controller: Controller;
let coordinatorEndpoint: Endpoint;
const resetExtension = async (): Promise<void> => {
await controller.removeExtension(controller.getExtension('Configure')!);
await controller.addExtension(new Configure(...controller.extensionArgs));
};
const expectBulbConfigured = () => {
const device = zigbeeHerdsman.devices.bulb;
const endpoint1 = device.getEndpoint(1);
expect(endpoint1.read).toHaveBeenCalledTimes(2);
expect(endpoint1.read).toHaveBeenCalledWith('lightingColorCtrl', ['colorCapabilities']);
expect(endpoint1.read).toHaveBeenCalledWith('lightingColorCtrl', ['colorTempPhysicalMin', 'colorTempPhysicalMax']);
};
const expectBulbNotConfigured = () => {
const device = zigbeeHerdsman.devices.bulb;
const endpoint1 = device.getEndpoint(1);
expect(endpoint1.read).toHaveBeenCalledTimes(0);
};
const expectRemoteNotConfigured = () => {
const device = zigbeeHerdsman.devices.remote;
const endpoint1 = device.getEndpoint(1);
expect(endpoint1.bind).toHaveBeenCalledTimes(0);
};
const mockClear = (device) => {
const mockClear = (device: Device): void => {
for (const endpoint of device.endpoints) {
endpoint.read.mockClear();
endpoint.write.mockClear();
@@ -56,30 +30,61 @@ describe('Configure', () => {
}
};
let resetExtension = async () => {
await controller.enableDisableExtension(false, 'Configure');
await controller.enableDisableExtension(true, 'Configure');
const expectRemoteConfigured = (): void => {
const device = devices.remote;
const endpoint1 = device.getEndpoint(1)!;
expect(endpoint1.bind).toHaveBeenCalledTimes(2);
expect(endpoint1.bind).toHaveBeenCalledWith('genOnOff', coordinatorEndpoint);
expect(endpoint1.bind).toHaveBeenCalledWith('genLevelCtrl', coordinatorEndpoint);
const endpoint2 = device.getEndpoint(2)!;
expect(endpoint2.write).toHaveBeenCalledTimes(1);
expect(endpoint2.write).toHaveBeenCalledWith('genBasic', {49: {type: 25, value: 11}}, {disableDefaultResponse: true, manufacturerCode: 4107});
expect(device.meta.configured).toBe(332242049);
};
const expectBulbConfigured = (): void => {
const device = devices.bulb;
const endpoint1 = device.getEndpoint(1)!;
expect(endpoint1.read).toHaveBeenCalledTimes(2);
expect(endpoint1.read).toHaveBeenCalledWith('lightingColorCtrl', ['colorCapabilities']);
expect(endpoint1.read).toHaveBeenCalledWith('lightingColorCtrl', ['colorTempPhysicalMin', 'colorTempPhysicalMax']);
};
const expectBulbNotConfigured = (): void => {
const device = devices.bulb;
const endpoint1 = device.getEndpoint(1)!;
expect(endpoint1.read).toHaveBeenCalledTimes(0);
};
const expectRemoteNotConfigured = (): void => {
const device = devices.remote;
const endpoint1 = device.getEndpoint(1)!;
expect(endpoint1.bind).toHaveBeenCalledTimes(0);
};
const wait = async (ms: number): Promise<void> => await new Promise((resolve) => setTimeout(resolve, ms));
beforeAll(async () => {
jest.useFakeTimers();
controller = new Controller(jest.fn(), jest.fn());
vi.useFakeTimers();
controller = new Controller(vi.fn(), vi.fn());
await controller.start();
await jest.runOnlyPendingTimers();
await flushPromises();
await vi.runOnlyPendingTimersAsync();
});
beforeEach(async () => {
data.writeDefaultConfiguration();
settings.reRead();
mocksClear.forEach((m) => m.mockClear());
coordinatorEndpoint = zigbeeHerdsman.devices.coordinator.getEndpoint(1);
coordinatorEndpoint = devices.coordinator.getEndpoint(1)!;
await resetExtension();
await jest.runOnlyPendingTimers();
await vi.runOnlyPendingTimersAsync();
});
afterAll(async () => {
jest.useRealTimers();
await controller?.stop();
await flushPromises();
vi.useRealTimers();
});
it('Should configure Router on startup', async () => {
@@ -92,32 +97,33 @@ describe('Configure', () => {
it('Should re-configure when device rejoins', async () => {
expectBulbConfigured();
const device = zigbeeHerdsman.devices.bulb;
const endpoint = device.getEndpoint(1);
const device = devices.bulb;
await flushPromises();
mockClear(device);
const payload = {device};
zigbeeHerdsman.events.deviceJoined(payload);
mockZHEvents.deviceJoined(payload);
await flushPromises();
expectBulbConfigured();
});
it('Should not re-configure disabled devices', async () => {
expectBulbConfigured();
const device = zigbeeHerdsman.devices.bulb;
const device = devices.bulb;
await flushPromises();
mockClear(device);
settings.set(['devices', device.ieeeAddr, 'disabled'], true);
zigbeeHerdsman.events.deviceJoined({device});
mockZHEvents.deviceJoined({device});
await flushPromises();
expectBulbNotConfigured();
});
it('Should reconfigure reporting on reconfigure event', async () => {
expectBulbConfigured();
const device = controller.zigbee.resolveEntity(zigbeeHerdsman.devices.bulb);
// @ts-expect-error private
const device = controller.zigbee.resolveEntity(devices.bulb)!;
mockClear(device.zh);
expectBulbNotConfigured();
// @ts-expect-error private
controller.eventBus.emitReconfigure({device});
await flushPromises();
expectBulbConfigured();
@@ -125,148 +131,134 @@ describe('Configure', () => {
it('Should not configure twice', async () => {
expectBulbConfigured();
const device = zigbeeHerdsman.devices.bulb;
const device = devices.bulb;
mockClear(device);
await zigbeeHerdsman.events.deviceInterview({device});
await mockZHEvents.deviceInterview({device});
await flushPromises();
expectBulbNotConfigured();
});
it('Should configure on zigbee message when not configured yet', async () => {
const device = zigbeeHerdsman.devices.bulb;
const device = devices.bulb;
delete device.meta.configured;
mockClear(device);
await zigbeeHerdsman.events.lastSeenChanged({device});
await mockZHEvents.lastSeenChanged({device});
await flushPromises();
expectBulbConfigured();
});
it('Should allow to configure via MQTT', async () => {
mockClear(zigbeeHerdsman.devices.remote);
mockClear(devices.remote);
expectRemoteNotConfigured();
await MQTT.events.message('zigbee2mqtt/bridge/request/device/configure', 'remote');
await mockMQTTEvents.message('zigbee2mqtt/bridge/request/device/configure', 'remote');
await flushPromises();
expectRemoteConfigured();
expect(MQTT.publish).toHaveBeenCalledWith(
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/configure',
stringify({data: {id: 'remote'}, status: 'ok'}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it('Fail to configure via MQTT when device does not exist', async () => {
await MQTT.events.message('zigbee2mqtt/bridge/request/device/configure', stringify({id: 'not_existing_device'}));
await mockMQTTEvents.message('zigbee2mqtt/bridge/request/device/configure', stringify({id: 'not_existing_device'}));
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/configure',
stringify({data: {id: 'not_existing_device'}, status: 'error', error: "Device 'not_existing_device' does not exist"}),
stringify({data: {}, status: 'error', error: "Device 'not_existing_device' does not exist"}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it('Fail to configure via MQTT when configure fails', async () => {
zigbeeHerdsman.devices.remote.getEndpoint(1).bind.mockImplementationOnce(async () => {
devices.remote.getEndpoint(1)!.bind.mockImplementationOnce(async () => {
throw new Error('Bind timeout after 10s');
});
await MQTT.events.message('zigbee2mqtt/bridge/request/device/configure', stringify({id: 'remote'}));
await mockMQTTEvents.message('zigbee2mqtt/bridge/request/device/configure', stringify({id: 'remote'}));
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/configure',
stringify({data: {id: 'remote'}, status: 'error', error: 'Failed to configure (Bind timeout after 10s)'}),
stringify({data: {}, status: 'error', error: 'Failed to configure (Bind timeout after 10s)'}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it('Fail to configure via MQTT when device has no configure', async () => {
await MQTT.events.message('zigbee2mqtt/bridge/request/device/configure', stringify({id: '0x0017882104a44559', transaction: 20}));
await mockMQTTEvents.message('zigbee2mqtt/bridge/request/device/configure', stringify({id: '0x0017882104a44559', transaction: 20}));
await flushPromises();
expect(MQTT.publish).toHaveBeenCalledWith(
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/configure',
stringify({data: {id: '0x0017882104a44559'}, status: 'error', error: "Device 'TS0601_thermostat' cannot be configured", transaction: 20}),
stringify({data: {}, status: 'error', error: "Device 'TS0601_thermostat' cannot be configured", transaction: 20}),
{retain: false, qos: 0},
expect.any(Function),
);
});
it('Legacy api: Should allow to reconfigure manually', async () => {
mockClear(zigbeeHerdsman.devices.remote);
expectRemoteNotConfigured();
await MQTT.events.message('zigbee2mqtt/bridge/configure', 'remote');
it('Handles invalid payload for configure via MQTT', async () => {
await mockMQTTEvents.message('zigbee2mqtt/bridge/request/device/configure', stringify({idx: '0x0017882104a44559'}));
await flushPromises();
expectRemoteConfigured();
});
it('Legacy api: Shouldnt manually reconfigure when device does not exist', async () => {
await MQTT.events.message('zigbee2mqtt/bridge/configure', 'remote_random_non_existing');
await flushPromises();
expect(logger.error).toHaveBeenCalledWith(`Device 'remote_random_non_existing' does not exist`);
});
it('Legacy api: Should skip reconfigure when device does not require this', async () => {
await MQTT.events.message('zigbee2mqtt/bridge/configure', '0x0017882104a44559');
await flushPromises();
expect(logger.warning).toHaveBeenCalledWith(`Skipping configure of 'TS0601_thermostat', device does not require this.`);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/device/configure',
stringify({data: {}, status: 'error', error: 'Invalid payload'}),
{retain: false, qos: 0},
);
});
it('Should not configure when interview not completed', async () => {
const device = zigbeeHerdsman.devices.remote;
const device = devices.remote;
delete device.meta.configured;
device.interviewCompleted = false;
const endpoint = device.getEndpoint(1);
mockClear(device);
await zigbeeHerdsman.events.lastSeenChanged({device});
await mockZHEvents.lastSeenChanged({device});
await flushPromises();
expectRemoteNotConfigured();
device.interviewCompleted = true;
});
it('Should not configure when already configuring', async () => {
const device = zigbeeHerdsman.devices.remote;
const device = devices.remote;
delete device.meta.configured;
const endpoint = device.getEndpoint(1);
const endpoint = device.getEndpoint(1)!;
endpoint.bind.mockImplementationOnce(async () => await wait(500));
mockClear(device);
await zigbeeHerdsman.events.lastSeenChanged({device});
await mockZHEvents.lastSeenChanged({device});
await flushPromises();
expect(endpoint.bind).toHaveBeenCalledTimes(1);
await zigbeeHerdsman.events.lastSeenChanged({device});
await mockZHEvents.lastSeenChanged({device});
await flushPromises();
expect(endpoint.bind).toHaveBeenCalledTimes(1);
});
it('Should configure max 3 times when fails', async () => {
controller.extensions.find((e) => e.constructor.name === 'Configure').attempts = {};
const device = zigbeeHerdsman.devices.remote;
// @ts-expect-error private
(controller.getExtension('Configure')! as Configure).attempts = {};
const device = devices.remote;
delete device.meta.configured;
const endpoint = device.getEndpoint(1);
const endpoint = device.getEndpoint(1)!;
mockClear(device);
endpoint.bind.mockImplementationOnce(async () => {
throw new Error('BLA');
});
await zigbeeHerdsman.events.lastSeenChanged({device});
await mockZHEvents.lastSeenChanged({device});
await flushPromises();
endpoint.bind.mockImplementationOnce(async () => {
throw new Error('BLA');
});
await zigbeeHerdsman.events.lastSeenChanged({device});
await mockZHEvents.lastSeenChanged({device});
await flushPromises();
endpoint.bind.mockImplementationOnce(async () => {
throw new Error('BLA');
});
await zigbeeHerdsman.events.lastSeenChanged({device});
await mockZHEvents.lastSeenChanged({device});
await flushPromises();
endpoint.bind.mockImplementationOnce(async () => {
throw new Error('BLA');
});
await zigbeeHerdsman.events.lastSeenChanged({device});
await mockZHEvents.lastSeenChanged({device});
await flushPromises();
endpoint.bind.mockImplementationOnce(async () => {
throw new Error('BLA');
});
await zigbeeHerdsman.events.lastSeenChanged({device});
await mockZHEvents.lastSeenChanged({device});
await flushPromises();
expect(endpoint.bind).toHaveBeenCalledTimes(3);
});
+613
View File
@@ -0,0 +1,613 @@
import * as data from '../mocks/data';
import {mockLogger} from '../mocks/logger';
import {mockMQTTEndAsync, mockMQTTPublishAsync} from '../mocks/mqtt';
import {flushPromises} from '../mocks/utils';
import {devices, mockController as mockZHController, returnDevices} from '../mocks/zigbeeHerdsman';
import type Device from '../../lib/model/device';
import fs from 'node:fs';
import path from 'node:path';
import stringify from 'json-stable-stringify-without-jsonify';
import * as zhc from 'zigbee-herdsman-converters';
import {Controller} from '../../lib/controller';
import ExternalConverters from '../../lib/extension/externalConverters';
import * as settings from '../../lib/util/settings';
const BASE_DIR = 'external_converters';
describe('Extension: ExternalConverters', () => {
const mockBasePath = path.join(data.mockDir, BASE_DIR);
let controller: Controller;
const rmSyncSpy = vi.spyOn(fs, 'rmSync');
const writeFileSyncSpy = vi.spyOn(fs, 'writeFileSync');
const zhcAddExternalDefinitionSpy = vi.spyOn(zhc, 'addExternalDefinition');
const zhcRemoveExternalDefinitionsSpy = vi.spyOn(zhc, 'removeExternalDefinitions');
const mocksClear = [
mockMQTTEndAsync,
mockMQTTPublishAsync,
mockLogger.debug,
mockLogger.error,
mockZHController.stop,
devices.bulb.save,
rmSyncSpy,
writeFileSyncSpy,
zhcAddExternalDefinitionSpy,
zhcRemoveExternalDefinitionsSpy,
];
const useAssets = (mtype: 'cjs' | 'mjs'): void => {
fs.cpSync(path.join(__dirname, '..', 'assets', BASE_DIR, mtype), mockBasePath, {recursive: true});
};
const getFileCode = (mtype: 'cjs' | 'mjs', fileName: string): string => {
return fs.readFileSync(path.join(__dirname, '..', 'assets', BASE_DIR, mtype, fileName), 'utf8');
};
const getZ2MDevice = (zhDevice: unknown): Device => {
// @ts-expect-error private
return controller.zigbee.resolveEntity(zhDevice)! as Device;
};
const resetExtension = async (): Promise<void> => {
await controller.removeExtension(controller.getExtension('ExternalConverters')!);
await controller.addExtension(new ExternalConverters(...controller.extensionArgs));
};
beforeAll(async () => {
vi.useFakeTimers();
controller = new Controller(vi.fn(), vi.fn());
await controller.start();
await flushPromises();
});
afterAll(async () => {
vi.useRealTimers();
});
beforeEach(async () => {
zhc.removeExternalDefinitions(); // remove all external converters
// @ts-expect-error private - clear cached
await controller.zigbee.resolveDevicesDefinitions(true);
mocksClear.forEach((m) => m.mockClear());
data.writeDefaultConfiguration();
data.writeDefaultState();
settings.reRead();
returnDevices.push(devices.external_converter_device.ieeeAddr, devices.coordinator.ieeeAddr);
});
afterEach(async () => {
fs.rmSync(mockBasePath, {recursive: true, force: true});
await controller?.stop();
await flushPromises();
});
describe('from folder', () => {
beforeEach(async () => {
controller = new Controller(vi.fn(), vi.fn());
});
it('loads nothing', async () => {
await controller.start();
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bridge/converters', stringify([]), {retain: true, qos: 0});
});
it('CJS: loads converters', async () => {
useAssets('cjs');
await controller.start();
await flushPromises();
expect(getZ2MDevice(devices.external_converter_device).definition).toMatchObject({
description: 'external/converter',
model: 'external_converter_device',
vendor: 'external',
zigbeeModel: ['external_converter_device'],
});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/converters',
stringify([
{name: 'mock-external-converter-multiple.js', code: getFileCode('cjs', 'mock-external-converter-multiple.js')},
{name: 'mock-external-converter.js', code: getFileCode('cjs', 'mock-external-converter.js')},
]),
{retain: true, qos: 0},
);
expect(zhcRemoveExternalDefinitionsSpy).toHaveBeenCalledTimes(2);
expect(zhcRemoveExternalDefinitionsSpy).toHaveBeenNthCalledWith(1, 'mock-external-converter-multiple.js');
expect(zhcRemoveExternalDefinitionsSpy).toHaveBeenNthCalledWith(2, 'mock-external-converter.js');
expect(zhcAddExternalDefinitionSpy).toHaveBeenNthCalledWith(
1,
expect.objectContaining({
mock: 1,
model: 'external_converters_device_1',
zigbeeModel: ['external_converter_device_1'],
vendor: 'external_1',
description: 'external_1',
}),
);
expect(zhcAddExternalDefinitionSpy).toHaveBeenNthCalledWith(
2,
expect.objectContaining({
mock: 2,
model: 'external_converters_device_2',
zigbeeModel: ['external_converter_device_2'],
vendor: 'external_2',
description: 'external_2',
}),
);
expect(zhcAddExternalDefinitionSpy).toHaveBeenNthCalledWith(
3,
expect.objectContaining({
mock: true,
zigbeeModel: ['external_converter_device'],
vendor: 'external',
model: 'external_converter_device',
description: 'external/converter',
}),
);
const bridgeDevices = mockMQTTPublishAsync.mock.calls.filter((c) => c[0] === 'zigbee2mqtt/bridge/devices');
expect(bridgeDevices.length).toBe(1);
expect(JSON.parse(bridgeDevices[0][1])).toEqual(
expect.arrayContaining([
expect.objectContaining({
model_id: 'external_converter_device',
supported: true,
definition: expect.objectContaining({
description: 'external/converter',
model: 'external_converter_device',
}),
}),
]),
);
});
it('MJS: loads converters', async () => {
useAssets('mjs');
await controller.start();
await flushPromises();
expect(getZ2MDevice(devices.external_converter_device).definition).toMatchObject({
description: 'external/converter',
model: 'external_converter_device',
vendor: 'external',
zigbeeModel: ['external_converter_device'],
});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/converters',
stringify([
{name: 'mock-external-converter-multiple.mjs', code: getFileCode('mjs', 'mock-external-converter-multiple.mjs')},
{name: 'mock-external-converter.mjs', code: getFileCode('mjs', 'mock-external-converter.mjs')},
]),
{retain: true, qos: 0},
);
expect(zhcRemoveExternalDefinitionsSpy).toHaveBeenCalledTimes(2);
expect(zhcRemoveExternalDefinitionsSpy).toHaveBeenNthCalledWith(1, 'mock-external-converter-multiple.mjs');
expect(zhcRemoveExternalDefinitionsSpy).toHaveBeenNthCalledWith(2, 'mock-external-converter.mjs');
expect(zhcAddExternalDefinitionSpy).toHaveBeenNthCalledWith(
1,
expect.objectContaining({
mock: 1,
model: 'external_converters_device_1',
zigbeeModel: ['external_converter_device_1'],
vendor: 'external_1',
description: 'external_1',
}),
);
expect(zhcAddExternalDefinitionSpy).toHaveBeenNthCalledWith(
2,
expect.objectContaining({
mock: 2,
model: 'external_converters_device_2',
zigbeeModel: ['external_converter_device_2'],
vendor: 'external_2',
description: 'external_2',
}),
);
expect(zhcAddExternalDefinitionSpy).toHaveBeenNthCalledWith(
3,
expect.objectContaining({
mock: true,
zigbeeModel: ['external_converter_device'],
vendor: 'external',
model: 'external_converter_device',
description: 'external/converter',
}),
);
const bridgeDevices = mockMQTTPublishAsync.mock.calls.filter((c) => c[0] === 'zigbee2mqtt/bridge/devices');
expect(bridgeDevices.length).toBe(1);
expect(JSON.parse(bridgeDevices[0][1])).toEqual(
expect.arrayContaining([
expect.objectContaining({
model_id: 'external_converter_device',
supported: true,
definition: expect.objectContaining({
description: 'external/converter',
model: 'external_converter_device',
}),
}),
]),
);
});
it('updates after edit from MQTT', async () => {
const converterName = 'mock-external-converter.js';
let converterCode = getFileCode('cjs', converterName);
useAssets('cjs');
await controller.start();
await flushPromises();
expect(getZ2MDevice(devices.external_converter_device).definition).toMatchObject({
description: 'external/converter',
model: 'external_converter_device',
vendor: 'external',
zigbeeModel: ['external_converter_device'],
});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/converters',
stringify([
{name: 'mock-external-converter-multiple.js', code: getFileCode('cjs', 'mock-external-converter-multiple.js')},
{name: converterName, code: converterCode},
]),
{retain: true, qos: 0},
);
converterCode = converterCode.replace("posix.join('external', 'converter')", "posix.join('external', 'converter', 'edited')");
await (controller.getExtension('ExternalConverters')! as ExternalConverters).onMQTTMessage({
topic: 'zigbee2mqtt/bridge/request/converter/save',
message: {name: converterName, code: converterCode},
});
expect(getZ2MDevice(devices.external_converter_device).definition).toMatchObject({
description: 'external/converter/edited',
model: 'external_converter_device',
vendor: 'external',
zigbeeModel: ['external_converter_device'],
});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/converters',
stringify([
{name: 'mock-external-converter-multiple.js', code: getFileCode('cjs', 'mock-external-converter-multiple.js')},
{name: 'mock-external-converter.1.js', code: converterCode},
]),
{retain: true, qos: 0},
);
expect(zhcAddExternalDefinitionSpy).toHaveBeenLastCalledWith(
expect.objectContaining({
mock: true,
zigbeeModel: ['external_converter_device'],
vendor: 'external',
model: 'external_converter_device',
description: 'external/converter/edited',
externalConverterName: 'mock-external-converter.1.js',
}),
);
converterCode = converterCode.replace("posix.join('external', 'converter', 'edited')", "posix.join('external', 'converter')");
await (controller.getExtension('ExternalConverters')! as ExternalConverters).onMQTTMessage({
topic: 'zigbee2mqtt/bridge/request/converter/save',
message: {name: 'mock-external-converter.1.js', code: converterCode},
});
expect(getZ2MDevice(devices.external_converter_device).definition).toMatchObject({
description: 'external/converter',
model: 'external_converter_device',
vendor: 'external',
zigbeeModel: ['external_converter_device'],
});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/converters',
stringify([
{name: 'mock-external-converter-multiple.js', code: getFileCode('cjs', 'mock-external-converter-multiple.js')},
{name: 'mock-external-converter.2.js', code: converterCode},
]),
{retain: true, qos: 0},
);
expect(zhcAddExternalDefinitionSpy).toHaveBeenLastCalledWith(
expect.objectContaining({
mock: true,
zigbeeModel: ['external_converter_device'],
vendor: 'external',
model: 'external_converter_device',
description: 'external/converter',
externalConverterName: 'mock-external-converter.2.js',
}),
);
});
it('loads all valid converters, relocates & skips ones with errors', async () => {
useAssets('mjs');
const filepath = path.join(mockBasePath, 'invalid.mjs');
fs.writeFileSync(filepath, 'invalid js', 'utf8');
await controller.start();
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/converters',
stringify([
{name: 'mock-external-converter-multiple.mjs', code: getFileCode('mjs', 'mock-external-converter-multiple.mjs')},
{name: 'mock-external-converter.mjs', code: getFileCode('mjs', 'mock-external-converter.mjs')},
]),
{retain: true, qos: 0},
);
expect(fs.existsSync(filepath)).toStrictEqual(false);
expect(fs.existsSync(path.join(mockBasePath, 'invalid.mjs.invalid'))).toStrictEqual(true);
});
});
describe('from MQTT', () => {
it('CJS: saves and removes', async () => {
const converterName = 'foo.js';
const converterCode = getFileCode('cjs', 'mock-external-converter.js');
await resetExtension();
mocksClear.forEach((m) => m.mockClear());
expect(getZ2MDevice(devices.external_converter_device).definition).toMatchObject({
description: 'Automatically generated definition',
model: 'external_converter_device',
vendor: '',
zigbeeModel: ['external_converter_device'],
});
//-- SAVE
await (controller.getExtension('ExternalConverters')! as ExternalConverters).onMQTTMessage({
topic: 'zigbee2mqtt/bridge/request/converter/save',
message: {name: converterName, code: converterCode},
});
expect(getZ2MDevice(devices.external_converter_device).definition).toMatchObject({
description: 'external/converter',
model: 'external_converter_device',
vendor: 'external',
zigbeeModel: ['external_converter_device'],
});
expect(writeFileSyncSpy).toHaveBeenCalledWith(expect.stringContaining(converterName), converterCode, 'utf8');
expect(zhcRemoveExternalDefinitionsSpy).toHaveBeenCalledTimes(1);
expect(zhcRemoveExternalDefinitionsSpy).toHaveBeenNthCalledWith(1, converterName);
expect(zhcAddExternalDefinitionSpy).toHaveBeenCalledWith(
expect.objectContaining({
mock: true,
zigbeeModel: ['external_converter_device'],
vendor: 'external',
model: 'external_converter_device',
description: 'external/converter',
}),
);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/converters',
stringify([{name: converterName, code: converterCode}]),
{
retain: true,
qos: 0,
},
);
//-- REMOVE
await (controller.getExtension('ExternalConverters')! as ExternalConverters).onMQTTMessage({
topic: 'zigbee2mqtt/bridge/request/converter/remove',
message: {name: converterName},
});
expect(getZ2MDevice(devices.external_converter_device).definition).toMatchObject({
description: 'Automatically generated definition',
model: 'external_converter_device',
vendor: '',
zigbeeModel: ['external_converter_device'],
});
expect(rmSyncSpy).toHaveBeenCalledWith(expect.stringContaining(converterName), {force: true});
expect(zhcRemoveExternalDefinitionsSpy).toHaveBeenCalledTimes(2);
expect(zhcRemoveExternalDefinitionsSpy).toHaveBeenNthCalledWith(2, converterName);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bridge/converters', stringify([]), {retain: true, qos: 0});
});
it('MJS: saves and removes', async () => {
const converterName = 'foo.mjs';
const converterCode = getFileCode('mjs', 'mock-external-converter.mjs');
await resetExtension();
mocksClear.forEach((m) => m.mockClear());
expect(getZ2MDevice(devices.external_converter_device).definition).toMatchObject({
description: 'Automatically generated definition',
model: 'external_converter_device',
vendor: '',
zigbeeModel: ['external_converter_device'],
});
//-- SAVE
await (controller.getExtension('ExternalConverters')! as ExternalConverters).onMQTTMessage({
topic: 'zigbee2mqtt/bridge/request/converter/save',
message: {name: converterName, code: converterCode},
});
expect(getZ2MDevice(devices.external_converter_device).definition).toMatchObject({
description: 'external/converter',
model: 'external_converter_device',
vendor: 'external',
zigbeeModel: ['external_converter_device'],
});
expect(writeFileSyncSpy).toHaveBeenCalledWith(expect.stringContaining(converterName), converterCode, 'utf8');
expect(zhcRemoveExternalDefinitionsSpy).toHaveBeenCalledTimes(1);
expect(zhcRemoveExternalDefinitionsSpy).toHaveBeenNthCalledWith(1, converterName);
expect(zhcAddExternalDefinitionSpy).toHaveBeenCalledWith(
expect.objectContaining({
mock: true,
zigbeeModel: ['external_converter_device'],
vendor: 'external',
model: 'external_converter_device',
description: 'external/converter',
}),
);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/converters',
stringify([{name: converterName, code: converterCode}]),
{
retain: true,
qos: 0,
},
);
//-- REMOVE
await (controller.getExtension('ExternalConverters')! as ExternalConverters).onMQTTMessage({
topic: 'zigbee2mqtt/bridge/request/converter/remove',
message: {name: converterName},
});
expect(getZ2MDevice(devices.external_converter_device).definition).toMatchObject({
description: 'Automatically generated definition',
model: 'external_converter_device',
vendor: '',
zigbeeModel: ['external_converter_device'],
});
expect(rmSyncSpy).toHaveBeenCalledWith(expect.stringContaining(converterName), {force: true});
expect(zhcRemoveExternalDefinitionsSpy).toHaveBeenCalledTimes(2);
expect(zhcRemoveExternalDefinitionsSpy).toHaveBeenNthCalledWith(2, converterName);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bridge/converters', stringify([]), {retain: true, qos: 0});
});
it('returns error on invalid code', async () => {
const converterName = 'foo1.js';
const converterCode = 'definetly not a correct javascript code';
await resetExtension();
mocksClear.forEach((m) => m.mockClear());
await (controller.getExtension('ExternalConverters')! as ExternalConverters).onMQTTMessage({
topic: 'zigbee2mqtt/bridge/request/converter/save',
message: {name: converterName, code: converterCode},
});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/converter/save',
expect.stringContaining(`"error":"${converterName} contains invalid code`),
{retain: false, qos: 0},
);
expect(writeFileSyncSpy).toHaveBeenCalledWith(expect.stringContaining(converterName), converterCode, 'utf8');
expect(rmSyncSpy).toHaveBeenCalledWith(expect.stringContaining(converterName), {force: true});
});
it('returns error on invalid removal', async () => {
const converterName = 'foo2.js';
await resetExtension();
mocksClear.forEach((m) => m.mockClear());
await (controller.getExtension('ExternalConverters')! as ExternalConverters).onMQTTMessage({
topic: 'zigbee2mqtt/bridge/request/converter/remove',
message: {name: converterName},
});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/converter/remove',
expect.stringContaining("doesn't exists"),
{retain: false, qos: 0},
);
expect(rmSyncSpy).not.toHaveBeenCalledWith(expect.stringContaining(converterName), {force: true});
});
it('returns error on invalid definition', async () => {
const converterName = 'foo3.js';
const converterCode = getFileCode('cjs', 'mock-external-converter.js');
await resetExtension();
mocksClear.forEach((m) => m.mockClear());
const errorMsg = `Invalid definition`;
zhcAddExternalDefinitionSpy.mockImplementationOnce(() => {
throw new Error(errorMsg);
});
await (controller.getExtension('ExternalConverters')! as ExternalConverters).onMQTTMessage({
topic: 'zigbee2mqtt/bridge/request/converter/save',
message: {name: converterName, code: converterCode},
});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bridge/response/converter/save', expect.stringContaining(errorMsg), {
retain: false,
qos: 0,
});
expect(writeFileSyncSpy).toHaveBeenCalledWith(expect.stringContaining(converterName), converterCode, 'utf8');
expect(rmSyncSpy).toHaveBeenCalledWith(expect.stringContaining(converterName), {force: true});
});
it('returns error on failed removal', async () => {
const converterName = 'foo4.js';
const converterCode = getFileCode('cjs', 'mock-external-converter.js');
await resetExtension();
mocksClear.forEach((m) => m.mockClear());
//-- SAVE
await (controller.getExtension('ExternalConverters')! as ExternalConverters).onMQTTMessage({
topic: 'zigbee2mqtt/bridge/request/converter/save',
message: {name: converterName, code: converterCode},
});
const errorMsg = `Failed to remove definition`;
zhcRemoveExternalDefinitionsSpy.mockImplementationOnce(() => {
throw new Error(errorMsg);
});
//-- REMOVE
await (controller.getExtension('ExternalConverters')! as ExternalConverters).onMQTTMessage({
topic: 'zigbee2mqtt/bridge/request/converter/remove',
message: {name: converterName},
});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/converter/remove',
stringify({data: {}, status: 'error', error: errorMsg}),
{retain: false, qos: 0},
);
expect(rmSyncSpy).not.toHaveBeenCalledWith(expect.stringContaining(converterName), {force: true});
});
it('handles invalid payloads', async () => {
await resetExtension();
mocksClear.forEach((m) => m.mockClear());
await (controller.getExtension('ExternalConverters')! as ExternalConverters).onMQTTMessage({
topic: 'zigbee2mqtt/bridge/request/converter/save',
message: {name: 'foo5.js', transaction: 1 /* code */},
});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/converter/save',
stringify({data: {}, status: 'error', error: `Invalid payload`, transaction: 1}),
{retain: false, qos: 0},
);
await (controller.getExtension('ExternalConverters')! as ExternalConverters).onMQTTMessage({
topic: 'zigbee2mqtt/bridge/request/converter/remove',
message: {namex: 'foo5.js', transaction: 2},
});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/converter/remove',
stringify({data: {}, status: 'error', error: `Invalid payload`, transaction: 2}),
{retain: false, qos: 0},
);
});
});
});
+350
View File
@@ -0,0 +1,350 @@
import * as data from '../mocks/data';
import {mockLogger} from '../mocks/logger';
import {mockMQTTEndAsync, mockMQTTPublishAsync} from '../mocks/mqtt';
import {flushPromises} from '../mocks/utils';
import {devices, mockController as mockZHController, returnDevices} from '../mocks/zigbeeHerdsman';
import fs from 'node:fs';
import path from 'node:path';
import stringify from 'json-stable-stringify-without-jsonify';
import {Controller} from '../../lib/controller';
import ExternalExtensions from '../../lib/extension/externalExtensions';
import * as settings from '../../lib/util/settings';
const BASE_DIR = 'external_extensions';
describe('Extension: ExternalExtensions', () => {
let controller: Controller;
const mockBasePath = path.join(data.mockDir, BASE_DIR);
const rmSyncSpy = vi.spyOn(fs, 'rmSync');
const writeFileSyncSpy = vi.spyOn(fs, 'writeFileSync');
const mocksClear = [
mockMQTTEndAsync,
mockMQTTPublishAsync,
mockLogger.debug,
mockLogger.error,
mockZHController.stop,
devices.bulb.save,
rmSyncSpy,
writeFileSyncSpy,
];
const useAssets = (mtype: 'cjs' | 'mjs'): void => {
fs.cpSync(path.join(__dirname, '..', 'assets', BASE_DIR, mtype), mockBasePath, {recursive: true});
};
const getFileCode = (mtype: 'cjs' | 'mjs', fileName: string): string => {
return fs.readFileSync(path.join(__dirname, '..', 'assets', BASE_DIR, mtype, fileName), 'utf8');
};
const resetExtension = async (): Promise<void> => {
await controller.removeExtension(controller.getExtension('ExternalExtensions')!);
await controller.addExtension(new ExternalExtensions(...controller.extensionArgs));
};
beforeAll(async () => {
vi.useFakeTimers();
controller = new Controller(vi.fn(), vi.fn());
await controller.start();
await flushPromises();
});
afterAll(async () => {
vi.useRealTimers();
});
beforeEach(async () => {
mocksClear.forEach((m) => m.mockClear());
data.writeDefaultConfiguration();
data.writeDefaultState();
settings.reRead();
returnDevices.splice(0);
});
afterEach(async () => {
fs.rmSync(mockBasePath, {recursive: true, force: true});
await controller?.stop();
await flushPromises();
});
describe('from folder', () => {
beforeEach(async () => {
controller = new Controller(vi.fn(), vi.fn());
});
it('loads nothing', async () => {
await controller.start();
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bridge/extensions', stringify([]), {retain: true, qos: 0});
});
it('CJS: loads extensions', async () => {
useAssets('cjs');
await controller.start();
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/example2/extension', 'call2 from constructor', {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/example2/extension', 'call2 from start', {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/example/extension', 'call from constructor', {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/example/extension', 'call from start', {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/extensions',
stringify([
{name: 'example2Extension.js', code: getFileCode('cjs', 'example2Extension.js')},
{name: 'exampleExtension.js', code: getFileCode('cjs', 'exampleExtension.js')},
]),
{retain: true, qos: 0},
);
});
it('MJS: loads extensions', async () => {
useAssets('mjs');
await controller.start();
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/example2/extension', 'call2 from constructor', {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/example2/extension', 'call2 from start', {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/example/extension', 'call from constructor', {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/example/extension', 'call from start', {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/extensions',
stringify([
{name: 'example2Extension.mjs', code: getFileCode('mjs', 'example2Extension.mjs')},
{name: 'exampleExtension.mjs', code: getFileCode('mjs', 'exampleExtension.mjs')},
]),
{retain: true, qos: 0},
);
});
it('loads all valid extensions, relocates & skips ones with errors', async () => {
useAssets('mjs');
const filepath = path.join(mockBasePath, 'invalid.mjs');
fs.writeFileSync(filepath, 'invalid js', 'utf8');
await controller.start();
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/extensions',
stringify([
{name: 'example2Extension.mjs', code: getFileCode('mjs', 'example2Extension.mjs')},
{name: 'exampleExtension.mjs', code: getFileCode('mjs', 'exampleExtension.mjs')},
]),
{retain: true, qos: 0},
);
expect(fs.existsSync(filepath)).toStrictEqual(false);
expect(fs.existsSync(path.join(mockBasePath, 'invalid.mjs.invalid'))).toStrictEqual(true);
});
it('updates after edit from MQTT', async () => {
const extensionName = 'exampleExtension.js';
let extensionCode = getFileCode('cjs', extensionName);
useAssets('cjs');
await controller.start();
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/example2/extension', 'call2 from constructor', {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/example2/extension', 'call2 from start', {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/example/extension', 'call from constructor', {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/example/extension', 'call from start', {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/extensions',
stringify([
{name: 'example2Extension.js', code: getFileCode('cjs', 'example2Extension.js')},
{name: extensionName, code: extensionCode},
]),
{retain: true, qos: 0},
);
extensionCode = extensionCode.replace("'call from start'", "'call from start - edited'");
mockMQTTPublishAsync.mockClear();
await (controller.getExtension('ExternalExtensions')! as ExternalExtensions).onMQTTMessage({
topic: 'zigbee2mqtt/bridge/request/extension/save',
message: {name: extensionName, code: extensionCode},
});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/example/extension', 'call from start - edited', {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/extensions',
stringify([
{name: 'example2Extension.js', code: getFileCode('cjs', 'example2Extension.js')},
{name: 'exampleExtension.1.js', code: extensionCode},
]),
{retain: true, qos: 0},
);
extensionCode = extensionCode.replace("'call from start - edited'", "'call from start'");
mockMQTTPublishAsync.mockClear();
await (controller.getExtension('ExternalExtensions')! as ExternalExtensions).onMQTTMessage({
topic: 'zigbee2mqtt/bridge/request/extension/save',
message: {name: 'exampleExtension.1.js', code: extensionCode},
});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/example/extension', 'call from start', {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/extensions',
stringify([
{name: 'example2Extension.js', code: getFileCode('cjs', 'example2Extension.js')},
{name: 'exampleExtension.2.js', code: extensionCode},
]),
{retain: true, qos: 0},
);
});
});
describe('from MQTT', () => {
it('CJS: saves and removes', async () => {
const extensionName = 'foo.js';
const extensionCode = getFileCode('cjs', 'exampleExtension.js');
await resetExtension();
mocksClear.forEach((m) => m.mockClear());
//-- SAVE
await (controller.getExtension('ExternalExtensions')! as ExternalExtensions).onMQTTMessage({
topic: 'zigbee2mqtt/bridge/request/extension/save',
message: {name: extensionName, code: extensionCode},
});
expect(writeFileSyncSpy).toHaveBeenCalledWith(expect.stringContaining(extensionName), extensionCode, 'utf8');
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/example/extension', 'call from constructor', {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/example/extension', 'call from start', {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/extensions',
stringify([{name: extensionName, code: extensionCode}]),
{
retain: true,
qos: 0,
},
);
//-- REMOVE
await (controller.getExtension('ExternalExtensions')! as ExternalExtensions).onMQTTMessage({
topic: 'zigbee2mqtt/bridge/request/extension/remove',
message: {name: extensionName},
});
expect(rmSyncSpy).toHaveBeenCalledWith(expect.stringContaining(extensionName), {force: true});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/example/extension', 'call from stop', {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bridge/extensions', stringify([]), {retain: true, qos: 0});
});
it('MJS: saves and removes', async () => {
const extensionName = 'foo.mjs';
const extensionCode = getFileCode('mjs', 'exampleExtension.mjs');
await resetExtension();
mocksClear.forEach((m) => m.mockClear());
//-- SAVE
await (controller.getExtension('ExternalExtensions')! as ExternalExtensions).onMQTTMessage({
topic: 'zigbee2mqtt/bridge/request/extension/save',
message: {name: extensionName, code: extensionCode},
});
expect(writeFileSyncSpy).toHaveBeenCalledWith(expect.stringContaining(extensionName), extensionCode, 'utf8');
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/example/extension', 'call from constructor', {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/example/extension', 'call from start', {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/extensions',
stringify([{name: extensionName, code: extensionCode}]),
{
retain: true,
qos: 0,
},
);
//-- REMOVE
await (controller.getExtension('ExternalExtensions')! as ExternalExtensions).onMQTTMessage({
topic: 'zigbee2mqtt/bridge/request/extension/remove',
message: {name: extensionName},
});
expect(rmSyncSpy).toHaveBeenCalledWith(expect.stringContaining(extensionName), {force: true});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/example/extension', 'call from stop', {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bridge/extensions', stringify([]), {retain: true, qos: 0});
});
it('returns error on invalid code', async () => {
const extensionName = 'foo1.js';
const extensionCode = 'definetly not a correct javascript code';
await resetExtension();
mocksClear.forEach((m) => m.mockClear());
await (controller.getExtension('ExternalExtensions')! as ExternalExtensions).onMQTTMessage({
topic: 'zigbee2mqtt/bridge/request/extension/save',
message: {name: extensionName, code: extensionCode},
});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/extension/save',
expect.stringContaining(`"error":"${extensionName} contains invalid code`),
{retain: false, qos: 0},
);
expect(writeFileSyncSpy).toHaveBeenCalledWith(expect.stringContaining(extensionName), extensionCode, 'utf8');
expect(rmSyncSpy).toHaveBeenCalledWith(expect.stringContaining(extensionName), {force: true});
});
it('returns error on invalid removal', async () => {
const extensionName = 'foo2.js';
await resetExtension();
mocksClear.forEach((m) => m.mockClear());
await (controller.getExtension('ExternalExtensions')! as ExternalExtensions).onMQTTMessage({
topic: 'zigbee2mqtt/bridge/request/extension/remove',
message: {name: extensionName},
});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/extension/remove',
expect.stringContaining("doesn't exists"),
{retain: false, qos: 0},
);
expect(rmSyncSpy).not.toHaveBeenCalledWith(expect.stringContaining(extensionName), {force: true});
});
it('handles invalid payloads', async () => {
await resetExtension();
mocksClear.forEach((m) => m.mockClear());
await (controller.getExtension('ExternalExtensions')! as ExternalExtensions).onMQTTMessage({
topic: 'zigbee2mqtt/bridge/request/extension/save',
message: {name: 'foo3.js', transaction: 1 /* code */},
});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/extension/save',
stringify({data: {}, status: 'error', error: `Invalid payload`, transaction: 1}),
{retain: false, qos: 0},
);
await (controller.getExtension('ExternalExtensions')! as ExternalExtensions).onMQTTMessage({
topic: 'zigbee2mqtt/bridge/request/extension/remove',
message: {namex: 'foo3.js', transaction: 2},
});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/extension/remove',
stringify({data: {}, status: 'error', error: `Invalid payload`, transaction: 2}),
{retain: false, qos: 0},
);
});
});
});
+467
View File
@@ -0,0 +1,467 @@
import * as data from '../mocks/data';
import {mockLogger} from '../mocks/logger';
import {mockMQTTPublishAsync} from '../mocks/mqtt';
import {EventHandler, flushPromises} from '../mocks/utils';
import {devices} from '../mocks/zigbeeHerdsman';
import path from 'node:path';
import stringify from 'json-stable-stringify-without-jsonify';
import {Mock} from 'vitest';
import ws from 'ws';
import {Controller} from '../../lib/controller';
import * as settings from '../../lib/util/settings';
let mockHTTPOnRequest: (request: {url: string}, response: number) => void;
const mockHTTPEvents: Record<string, EventHandler> = {};
const mockHTTP = {
listen: vi.fn(),
on: (event: string, handler: EventHandler): void => {
mockHTTPEvents[event] = handler;
},
close: vi.fn<(cb: (err?: Error) => void) => void>((cb) => cb()),
};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let mockHTTPSOnRequest: (request: {url: string}, response: number) => void;
const mockHTTPSEvents: Record<string, EventHandler> = {};
const mockHTTPS = {
listen: vi.fn(),
on: (event: string, handler: EventHandler): void => {
mockHTTPSEvents[event] = handler;
},
close: vi.fn<(cb: (err?: Error) => void) => void>((cb) => cb()),
};
const mockWSocket = {
close: vi.fn<(cb: (err?: Error) => void) => void>(),
};
const mockWSClientEvents: Record<string, EventHandler> = {};
const mockWSClient = {
on: (event: string, handler: EventHandler): void => {
mockWSClientEvents[event] = handler;
},
send: vi.fn<(data: string) => void>(),
terminate: vi.fn<() => void>(),
readyState: 'close',
};
const mockWSEvents: Record<string, EventHandler> = {};
const mockWSClients: (typeof mockWSClient)[] = [];
const mockWS = {
clients: mockWSClients,
on: (event: string, handler: EventHandler): void => {
mockWSEvents[event] = handler;
},
handleUpgrade: vi.fn().mockImplementation((request, socket, head, cb) => {
cb(mockWSocket);
}),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
emit: vi.fn<(eventName: string, ...args: any[]) => void>(),
close: vi.fn<(code?: number, data?: string | Buffer) => void>(),
};
const frontendPath = 'frontend-path';
const deviceIconsPath = path.join(data.mockDir, 'device_icons');
let mockNodeStatic: {[s: string]: Mock} = {};
const mockFinalHandler = vi.fn();
vi.mock('node:http', () => ({
createServer: vi.fn().mockImplementation((onRequest) => {
mockHTTPOnRequest = onRequest;
return mockHTTP;
}),
Agent: vi.fn(),
}));
vi.mock('node:https', () => ({
createServer: vi.fn().mockImplementation((onRequest) => {
mockHTTPSOnRequest = onRequest;
return mockHTTPS;
}),
Agent: vi.fn(),
}));
vi.mock('express-static-gzip', () => ({
default: vi.fn().mockImplementation((path: string) => {
mockNodeStatic[path] = vi.fn();
return mockNodeStatic[path];
}),
}));
vi.mock('zigbee2mqtt-frontend', () => ({
default: {
getPath: (): string => frontendPath,
},
}));
vi.mock('ws', () => ({
default: {
OPEN: 'open',
Server: vi.fn().mockImplementation(() => {
return mockWS;
}),
},
}));
vi.mock('finalhandler', () => ({
default: vi.fn().mockImplementation(() => {
return mockFinalHandler;
}),
}));
const mocksClear = [
mockHTTP.close,
mockHTTP.listen,
mockHTTPS.close,
mockHTTPS.listen,
mockWSocket.close,
mockWS.close,
mockWS.handleUpgrade,
mockWS.emit,
mockWSClient.send,
mockWSClient.terminate,
mockFinalHandler,
mockMQTTPublishAsync,
mockLogger.error,
];
describe('Extension: Frontend', () => {
let controller: Controller;
beforeAll(async () => {
vi.useFakeTimers();
});
beforeEach(async () => {
mockNodeStatic = {};
mockWS.clients = [];
data.writeDefaultConfiguration();
data.writeDefaultState();
settings.reRead();
settings.set(['frontend'], {enabled: true, port: 8081, host: '127.0.0.1'});
settings.set(['homeassistant'], {enabled: true});
devices.bulb.linkquality = 10;
mocksClear.forEach((m) => m.mockClear());
mockWSClient.readyState = 'close';
});
afterAll(async () => {
vi.useRealTimers();
});
afterEach(async () => {
delete devices.bulb.linkquality;
await controller?.stop();
await flushPromises();
});
it('Start/stop with defaults', async () => {
controller = new Controller(vi.fn(), vi.fn());
await controller.start();
expect(Object.keys(mockNodeStatic)).toStrictEqual([frontendPath, deviceIconsPath]);
expect(mockHTTP.listen).toHaveBeenCalledWith(8081, '127.0.0.1');
mockWS.clients.push(mockWSClient);
await controller.stop();
expect(mockWSClient.terminate).toHaveBeenCalledTimes(1);
expect(mockHTTP.close).toHaveBeenCalledTimes(1);
expect(mockWS.close).toHaveBeenCalledTimes(1);
});
it('Start/stop without host', async () => {
settings.set(['frontend'], {enabled: true, port: 8081});
controller = new Controller(vi.fn(), vi.fn());
await controller.start();
expect(Object.keys(mockNodeStatic)).toStrictEqual([frontendPath, deviceIconsPath]);
expect(mockHTTP.listen).toHaveBeenCalledWith(8081);
mockWS.clients.push(mockWSClient);
await controller.stop();
expect(mockWSClient.terminate).toHaveBeenCalledTimes(1);
expect(mockHTTP.close).toHaveBeenCalledTimes(1);
expect(mockWS.close).toHaveBeenCalledTimes(1);
});
it('Start/stop unix socket', async () => {
settings.set(['frontend', 'host'], '/tmp/zigbee2mqtt.sock');
controller = new Controller(vi.fn(), vi.fn());
await controller.start();
expect(Object.keys(mockNodeStatic)).toStrictEqual([frontendPath, deviceIconsPath]);
expect(mockHTTP.listen).toHaveBeenCalledWith('/tmp/zigbee2mqtt.sock');
mockWS.clients.push(mockWSClient);
await controller.stop();
expect(mockWSClient.terminate).toHaveBeenCalledTimes(1);
expect(mockHTTP.close).toHaveBeenCalledTimes(1);
expect(mockWS.close).toHaveBeenCalledTimes(1);
});
it('Start/stop HTTPS valid', async () => {
settings.set(['frontend', 'ssl_cert'], path.join(__dirname, '..', 'assets', 'certs', 'dummy.crt'));
settings.set(['frontend', 'ssl_key'], path.join(__dirname, '..', 'assets', 'certs', 'dummy.key'));
controller = new Controller(vi.fn(), vi.fn());
await controller.start();
expect(mockHTTP.listen).not.toHaveBeenCalledWith(8081, '127.0.0.1');
expect(mockHTTPS.listen).toHaveBeenCalledWith(8081, '127.0.0.1');
await controller.stop();
});
it('Start/stop HTTPS invalid : missing config', async () => {
settings.set(['frontend', 'ssl_cert'], path.join(__dirname, '..', 'assets', 'certs', 'dummy.crt'));
controller = new Controller(vi.fn(), vi.fn());
await controller.start();
expect(mockHTTP.listen).toHaveBeenCalledWith(8081, '127.0.0.1');
expect(mockHTTPS.listen).not.toHaveBeenCalledWith(8081, '127.0.0.1');
await controller.stop();
});
it('Start/stop HTTPS invalid : missing file', async () => {
settings.set(['frontend', 'ssl_cert'], 'filesNotExists.crt');
settings.set(['frontend', 'ssl_key'], path.join(__dirname, '..', 'assets', 'certs', 'dummy.key'));
controller = new Controller(vi.fn(), vi.fn());
await controller.start();
expect(mockHTTP.listen).toHaveBeenCalledWith(8081, '127.0.0.1');
expect(mockHTTPS.listen).not.toHaveBeenCalledWith(8081, '127.0.0.1');
await controller.stop();
});
it('Websocket interaction', async () => {
controller = new Controller(vi.fn(), vi.fn());
await controller.start();
mockWSClient.readyState = 'open';
mockWS.clients.push(mockWSClient);
await mockWSEvents.connection(mockWSClient);
const allTopics = mockWSClient.send.mock.calls.map(([m]) => JSON.parse(m).topic);
expect(allTopics).toContain('bridge/devices');
expect(allTopics).toContain('bridge/info');
expect(mockWSClient.send).toHaveBeenCalledWith(stringify({topic: 'bridge/state', payload: {state: 'online'}}));
expect(mockWSClient.send).toHaveBeenCalledWith(stringify({topic: 'remote', payload: {brightness: 255}}));
// Message
mockMQTTPublishAsync.mockClear();
mockWSClient.send.mockClear();
mockWSClientEvents.message(stringify({topic: 'bulb_color/set', payload: {state: 'ON'}}), false);
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(1);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bulb_color',
stringify({
state: 'ON',
effect: null,
power_on_behavior: null,
linkquality: null,
update: {state: null, installed_version: -1, latest_version: -1},
}),
{retain: false, qos: 0},
);
mockWSClientEvents.message(undefined, false);
mockWSClientEvents.message('', false);
mockWSClientEvents.message(null, false);
await flushPromises();
// Error
mockWSClientEvents.error(new Error('This is an error'));
expect(mockLogger.error).toHaveBeenCalledWith('WebSocket error: This is an error');
// Received message on socket
expect(mockWSClient.send).toHaveBeenCalledTimes(1);
expect(mockWSClient.send).toHaveBeenCalledWith(
stringify({
topic: 'bulb_color',
payload: {
state: 'ON',
effect: null,
power_on_behavior: null,
linkquality: null,
update: {state: null, installed_version: -1, latest_version: -1},
},
}),
);
// Shouldnt set when not ready
mockWSClient.send.mockClear();
mockWSClient.readyState = 'close';
mockWSClientEvents.message(stringify({topic: 'bulb_color/set', payload: {state: 'ON'}}), false);
expect(mockWSClient.send).toHaveBeenCalledTimes(0);
// Send last seen on connect
mockWSClient.send.mockClear();
mockWSClient.readyState = 'open';
settings.set(['advanced'], {last_seen: 'ISO_8601'});
mockWS.clients.push(mockWSClient);
await mockWSEvents.connection(mockWSClient);
expect(mockWSClient.send).toHaveBeenCalledWith(
stringify({topic: 'remote', payload: {brightness: 255, last_seen: '1970-01-01T00:00:01.000Z'}}),
);
});
it('onRequest/onUpgrade', async () => {
controller = new Controller(vi.fn(), vi.fn());
await controller.start();
const mockSocket = {destroy: vi.fn()};
mockHTTPEvents.upgrade({url: 'http://localhost:8080/api'}, mockSocket, 3);
expect(mockWS.handleUpgrade).toHaveBeenCalledTimes(1);
expect(mockSocket.destroy).toHaveBeenCalledTimes(0);
expect(mockWS.handleUpgrade).toHaveBeenCalledWith({url: 'http://localhost:8080/api'}, mockSocket, 3, expect.any(Function));
mockWS.handleUpgrade.mock.calls[0][3](99);
expect(mockWS.emit).toHaveBeenCalledWith('connection', 99, {url: 'http://localhost:8080/api'});
mockHTTPOnRequest({url: '/file.txt'}, 2);
expect(mockNodeStatic[deviceIconsPath]).toHaveBeenCalledTimes(0);
expect(mockNodeStatic[frontendPath]).toHaveBeenCalledTimes(1);
expect(mockNodeStatic[frontendPath]).toHaveBeenCalledWith(
{originalUrl: '/file.txt', path: '/file.txt', url: '/file.txt'},
2,
expect.any(Function),
);
});
it('Should serve device icons', async () => {
controller = new Controller(vi.fn(), vi.fn());
await controller.start();
mockHTTPOnRequest({url: '/device_icons/my_device.png'}, 2);
expect(mockNodeStatic[frontendPath]).toHaveBeenCalledTimes(0);
expect(mockNodeStatic[deviceIconsPath]).toHaveBeenCalledTimes(1);
expect(mockNodeStatic[deviceIconsPath]).toHaveBeenCalledWith(
{originalUrl: '/device_icons/my_device.png', path: '/my_device.png', url: '/my_device.png'},
2,
expect.any(Function),
);
});
it('Static server', async () => {
controller = new Controller(vi.fn(), vi.fn());
await controller.start();
expect(mockHTTP.listen).toHaveBeenCalledWith(8081, '127.0.0.1');
});
it('Authentification', async () => {
const authToken = 'sample-secure-token';
settings.set(['frontend', 'auth_token'], authToken);
controller = new Controller(vi.fn(), vi.fn());
await controller.start();
const mockSocket = {destroy: vi.fn()};
mockHTTPEvents.upgrade({url: '/api'}, mockSocket, mockWSocket);
expect(mockWS.handleUpgrade).toHaveBeenCalledTimes(1);
expect(mockSocket.destroy).toHaveBeenCalledTimes(0);
expect(mockWS.handleUpgrade).toHaveBeenCalledWith({url: '/api'}, mockSocket, mockWSocket, expect.any(Function));
expect(mockWSocket.close).toHaveBeenCalledWith(4401, 'Unauthorized');
mockWSocket.close.mockClear();
mockWS.emit.mockClear();
const url = `/api?token=${authToken}`;
mockWS.handleUpgrade.mockClear();
mockHTTPEvents.upgrade({url: url}, mockSocket, 3);
expect(mockWS.handleUpgrade).toHaveBeenCalledTimes(1);
expect(mockSocket.destroy).toHaveBeenCalledTimes(0);
expect(mockWS.handleUpgrade).toHaveBeenCalledWith({url}, mockSocket, 3, expect.any(Function));
expect(mockWSocket.close).toHaveBeenCalledTimes(0);
mockWS.handleUpgrade.mock.calls[0][3](mockWSocket);
expect(mockWS.emit).toHaveBeenCalledWith('connection', mockWSocket, {url});
});
it.each(['/z2m/', '/z2m'])('Works with non-default base url %s', async (baseUrl) => {
settings.set(['frontend', 'base_url'], baseUrl);
controller = new Controller(vi.fn(), vi.fn());
await controller.start();
expect(ws.Server).toHaveBeenCalledWith({noServer: true, path: '/z2m/api'});
mockHTTPOnRequest({url: '/z2m'}, 2);
expect(mockNodeStatic[frontendPath]).toHaveBeenCalledTimes(1);
expect(mockNodeStatic[frontendPath]).toHaveBeenCalledWith({originalUrl: '/z2m', path: '/', url: '/'}, 2, expect.any(Function));
expect(mockFinalHandler).not.toHaveBeenCalledWith();
mockNodeStatic[frontendPath].mockReset();
expect(mockFinalHandler).not.toHaveBeenCalledWith();
mockHTTPOnRequest({url: '/z2m/file.txt'}, 2);
expect(mockNodeStatic[frontendPath]).toHaveBeenCalledTimes(1);
expect(mockNodeStatic[frontendPath]).toHaveBeenCalledWith(
{originalUrl: '/z2m/file.txt', path: '/file.txt', url: '/file.txt'},
2,
expect.any(Function),
);
expect(mockFinalHandler).not.toHaveBeenCalledWith();
mockNodeStatic[frontendPath].mockReset();
mockHTTPOnRequest({url: '/z/file.txt'}, 2);
expect(mockNodeStatic[frontendPath]).not.toHaveBeenCalled();
expect(mockFinalHandler).toHaveBeenCalled();
mockHTTPOnRequest({url: '/z2m/device_icons/my-device.png'}, 2);
expect(mockNodeStatic[deviceIconsPath]).toHaveBeenCalledTimes(1);
expect(mockNodeStatic[deviceIconsPath]).toHaveBeenCalledWith(
{originalUrl: '/z2m/device_icons/my-device.png', path: '/my-device.png', url: '/my-device.png'},
2,
expect.any(Function),
);
});
it('Works with non-default complex base url', async () => {
const baseUrl = '/z2m-more++/c0mplex.url/';
settings.set(['frontend', 'base_url'], baseUrl);
controller = new Controller(vi.fn(), vi.fn());
await controller.start();
expect(ws.Server).toHaveBeenCalledWith({noServer: true, path: '/z2m-more++/c0mplex.url/api'});
mockHTTPOnRequest({url: '/z2m-more++/c0mplex.url'}, 2);
expect(mockNodeStatic[frontendPath]).toHaveBeenCalledTimes(1);
expect(mockNodeStatic[frontendPath]).toHaveBeenCalledWith(
{originalUrl: '/z2m-more++/c0mplex.url', path: '/', url: '/'},
2,
expect.any(Function),
);
expect(mockFinalHandler).not.toHaveBeenCalledWith();
mockNodeStatic[frontendPath].mockReset();
expect(mockFinalHandler).not.toHaveBeenCalledWith();
mockHTTPOnRequest({url: '/z2m-more++/c0mplex.url/file.txt'}, 2);
expect(mockNodeStatic[frontendPath]).toHaveBeenCalledTimes(1);
expect(mockNodeStatic[frontendPath]).toHaveBeenCalledWith(
{originalUrl: '/z2m-more++/c0mplex.url/file.txt', path: '/file.txt', url: '/file.txt'},
2,
expect.any(Function),
);
expect(mockFinalHandler).not.toHaveBeenCalledWith();
mockNodeStatic[frontendPath].mockReset();
mockHTTPOnRequest({url: '/z/file.txt'}, 2);
expect(mockNodeStatic[frontendPath]).not.toHaveBeenCalled();
expect(mockFinalHandler).toHaveBeenCalled();
});
it('prevents mismatching setting/extension state', async () => {
settings.set(['frontend', 'enabled'], false);
controller = new Controller(vi.fn(), vi.fn());
await controller.start();
await expect(async () => {
await controller.enableDisableExtension(true, 'Frontend');
}).rejects.toThrow('Tried to enable Frontend extension disabled in settings');
settings.set(['frontend', 'enabled'], true);
await expect(async () => {
await controller.enableDisableExtension(false, 'Frontend');
}).rejects.toThrow('Tried to disable Frontend extension enabled in settings');
await controller.enableDisableExtension(true, 'Frontend');
await expect(async () => {
await controller.enableDisableExtension(true, 'Frontend');
}).rejects.toThrow('Extension with name Frontend already present');
settings.set(['frontend', 'enabled'], false);
await controller.enableDisableExtension(false, 'Frontend');
await vi.waitFor(() => controller.getExtension('Frontend') === undefined);
});
});
+801
View File
@@ -0,0 +1,801 @@
import * as data from '../mocks/data';
import {mockLogger} from '../mocks/logger';
import {events as mockMQTTEvents, mockMQTTPublishAsync} from '../mocks/mqtt';
import {flushPromises} from '../mocks/utils';
import {devices, groups, events as mockZHEvents, resetGroupMembers, returnDevices} from '../mocks/zigbeeHerdsman';
import stringify from 'json-stable-stringify-without-jsonify';
import * as zhcGlobalStore from 'zigbee-herdsman-converters/lib/store';
import {Controller} from '../../lib/controller';
import * as settings from '../../lib/util/settings';
returnDevices.push(
devices.coordinator.ieeeAddr,
devices.bulb_color.ieeeAddr,
devices.bulb.ieeeAddr,
devices.QBKG03LM.ieeeAddr,
devices.bulb_color_2.ieeeAddr,
devices.bulb_2.ieeeAddr,
devices.GLEDOPTO_2ID.ieeeAddr,
devices.InovelliVZM31SN.ieeeAddr,
);
describe('Extension: Groups', () => {
let controller: Controller;
beforeAll(async () => {
vi.useFakeTimers();
controller = new Controller(vi.fn(), vi.fn());
await controller.start();
await flushPromises();
});
afterAll(async () => {
await controller?.stop();
await flushPromises();
vi.useRealTimers();
});
beforeEach(() => {
resetGroupMembers();
data.writeDefaultConfiguration();
settings.reRead();
mockMQTTPublishAsync.mockClear();
groups.gledopto_group.command.mockClear();
zhcGlobalStore.clear();
// @ts-expect-error private
controller.state.state = {};
});
it('Should publish group state change when a device in it changes state', async () => {
const device = devices.bulb_color;
const endpoint = device.getEndpoint(1)!;
const group = groups.group_1;
group.members.push(endpoint);
mockMQTTPublishAsync.mockClear();
const payload = {data: {onOff: 1}, cluster: 'genOnOff', device, endpoint, type: 'attributeReport', linkquality: 10};
await mockZHEvents.message(payload);
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(2);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_color', stringify({state: 'ON'}), {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/group_1', stringify({state: 'ON'}), {retain: false, qos: 0});
});
it('Should not republish identical optimistic group states', async () => {
const device1 = devices.bulb_2;
const device2 = devices.bulb_color_2;
mockMQTTPublishAsync.mockClear();
await mockZHEvents.message({
data: {onOff: 1},
cluster: 'genOnOff',
device: device1,
endpoint: device1.getEndpoint(1),
type: 'attributeReport',
linkquality: 10,
});
await mockZHEvents.message({
data: {onOff: 1},
cluster: 'genOnOff',
device: device2,
endpoint: device2.getEndpoint(1),
type: 'attributeReport',
linkquality: 10,
});
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(6);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/group_tradfri_remote', stringify({state: 'ON'}), {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_2', stringify({state: 'ON'}), {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_color_2', stringify({state: 'ON'}), {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/group_with_tradfri', stringify({state: 'ON'}), {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/ha_discovery_group', stringify({state: 'ON'}), {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/switch_group', stringify({state: 'ON'}), {retain: false, qos: 0});
});
it('Should publish state change of all members when a group changes its state', async () => {
const device = devices.bulb_color;
const endpoint = device.getEndpoint(1)!;
const group = groups.group_1;
group.members.push(endpoint);
mockMQTTPublishAsync.mockClear();
await mockMQTTEvents.message('zigbee2mqtt/group_1/set', stringify({state: 'ON'}));
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(2);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_color', stringify({state: 'ON'}), {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/group_1', stringify({state: 'ON'}), {retain: false, qos: 0});
});
it('Should not publish state change when group changes state and device is disabled', async () => {
const device = devices.bulb_color;
const endpoint = device.getEndpoint(1)!;
const group = groups.group_1;
group.members.push(endpoint);
settings.set(['devices', device.ieeeAddr, 'disabled'], true);
mockMQTTPublishAsync.mockClear();
await mockMQTTEvents.message('zigbee2mqtt/group_1/set', stringify({state: 'ON'}));
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(1);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/group_1', stringify({state: 'ON'}), {retain: false, qos: 0});
});
it('Should publish state change for group when members state change', async () => {
// Created for https://github.com/Koenkk/zigbee2mqtt/issues/5725
const device = devices.bulb_color;
const endpoint = device.getEndpoint(1)!;
const group = groups.group_1;
group.members.push(endpoint);
mockMQTTPublishAsync.mockClear();
await mockMQTTEvents.message('zigbee2mqtt/bulb_color/set', stringify({state: 'ON'}));
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(2);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_color', stringify({state: 'ON'}), {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/group_1', stringify({state: 'ON'}), {retain: false, qos: 0});
mockMQTTPublishAsync.mockClear();
await mockMQTTEvents.message('zigbee2mqtt/group_1/set', stringify({state: 'OFF'}));
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(2);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_color', stringify({state: 'OFF'}), {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/group_1', stringify({state: 'OFF'}), {retain: false, qos: 0});
mockMQTTPublishAsync.mockClear();
await mockMQTTEvents.message('zigbee2mqtt/bulb_color/set', stringify({state: 'ON'}));
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(2);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_color', stringify({state: 'ON'}), {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/group_1', stringify({state: 'ON'}), {retain: false, qos: 0});
});
it('Should publish state of device with endpoint name', async () => {
const group = groups.gledopto_group;
mockMQTTPublishAsync.mockClear();
await mockMQTTEvents.message('zigbee2mqtt/gledopto_group/set', stringify({state: 'ON'}));
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(2);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/GLEDOPTO_2ID', stringify({state_cct: 'ON'}), {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/gledopto_group', stringify({state: 'ON'}), {retain: false, qos: 0});
expect(group.command).toHaveBeenCalledTimes(1);
expect(group.command).toHaveBeenCalledWith('genOnOff', 'on', {}, {});
});
it('Should publish state of group when specific state of specific endpoint is changed', async () => {
const group = groups.gledopto_group;
mockMQTTPublishAsync.mockClear();
await mockMQTTEvents.message('zigbee2mqtt/GLEDOPTO_2ID/set', stringify({state_cct: 'ON'}));
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(2);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/GLEDOPTO_2ID', stringify({state_cct: 'ON'}), {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/gledopto_group', stringify({state: 'ON'}), {retain: false, qos: 0});
expect(group.command).toHaveBeenCalledTimes(0);
});
it('Should publish state change of all members when a group changes its state, filtered', async () => {
const device = devices.bulb_color;
const endpoint = device.getEndpoint(1)!;
const group = groups.group_1;
group.members.push(endpoint);
settings.set(['groups'], {1: {friendly_name: 'group_1', retain: false, filtered_attributes: ['brightness']}});
mockMQTTPublishAsync.mockClear();
await mockMQTTEvents.message('zigbee2mqtt/group_1/set', stringify({state: 'ON', brightness: 100}));
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(2);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_color', stringify({state: 'ON', brightness: 100}), {
retain: false,
qos: 0,
});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/group_1', stringify({state: 'ON'}), {retain: false, qos: 0});
});
it('Shouldnt publish group state change when a group is not optimistic', async () => {
const device = devices.bulb_color;
const endpoint = device.getEndpoint(1)!;
const group = groups.group_1;
group.members.push(endpoint);
settings.set(['groups'], {1: {friendly_name: 'group_1', optimistic: false, retain: false}});
mockMQTTPublishAsync.mockClear();
const payload = {data: {onOff: 1}, cluster: 'genOnOff', device, endpoint, type: 'attributeReport', linkquality: 10};
await mockZHEvents.message(payload);
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(1);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_color', stringify({state: 'ON'}), {retain: false, qos: 0});
});
it('Should publish state change of another group with shared device when a group changes its state', async () => {
const device = devices.bulb_color;
const endpoint = device.getEndpoint(1)!;
groups.group_1.members.push(endpoint);
groups.group_2.members.push(endpoint);
mockMQTTPublishAsync.mockClear();
await mockMQTTEvents.message('zigbee2mqtt/group_1/set', stringify({state: 'ON'}));
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(3);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_color', stringify({state: 'ON'}), {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/group_1', stringify({state: 'ON'}), {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/group_2', stringify({state: 'ON'}), {retain: false, qos: 0});
});
it('Should not publish state change off if any lights within are still on when changed via device', async () => {
const device_1 = devices.bulb_color;
const device_2 = devices.bulb;
const endpoint_1 = device_1.getEndpoint(1)!;
const endpoint_2 = device_2.getEndpoint(1)!;
const group = groups.group_1;
group.members.push(endpoint_1);
group.members.push(endpoint_2);
await mockMQTTEvents.message('zigbee2mqtt/group_1/set', stringify({state: 'ON'}));
await flushPromises();
mockMQTTPublishAsync.mockClear();
await mockMQTTEvents.message('zigbee2mqtt/bulb_color/set', stringify({state: 'OFF'}));
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(1);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_color', stringify({state: 'OFF'}), {retain: false, qos: 0});
});
it('Should publish state change off if any lights within are still on when changed via device when off_state: last_member_state is used', async () => {
const device_1 = devices.bulb_color;
const device_2 = devices.bulb;
const endpoint_1 = device_1.getEndpoint(1)!;
const endpoint_2 = device_2.getEndpoint(1)!;
const group = groups.group_1;
group.members.push(endpoint_1);
group.members.push(endpoint_2);
settings.set(['groups'], {
1: {friendly_name: 'group_1', retain: false, off_state: 'last_member_state'},
});
await mockMQTTEvents.message('zigbee2mqtt/group_1/set', stringify({state: 'ON'}));
await flushPromises();
mockMQTTPublishAsync.mockClear();
await mockMQTTEvents.message('zigbee2mqtt/bulb_color/set', stringify({state: 'OFF'}));
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(2);
expect(mockMQTTPublishAsync).toHaveBeenNthCalledWith(1, 'zigbee2mqtt/group_1', stringify({state: 'OFF'}), {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenNthCalledWith(2, 'zigbee2mqtt/bulb_color', stringify({state: 'OFF'}), {retain: false, qos: 0});
});
it('Should not publish state change off if any lights within with non default-ep are still on when changed via device', async () => {
const device_1 = devices.bulb_color;
const device_2 = devices.QBKG03LM;
const endpoint_1 = device_1.getEndpoint(1)!;
const endpoint_2 = device_2.getEndpoint(2)!;
const group = groups.group_1;
group.members.push(endpoint_1);
group.members.push(endpoint_2);
await mockMQTTEvents.message('zigbee2mqtt/group_1/set', stringify({state: 'ON'}));
await flushPromises();
mockMQTTPublishAsync.mockClear();
await mockMQTTEvents.message('zigbee2mqtt/bulb_color/set', stringify({state: 'OFF'}));
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(1);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_color', stringify({state: 'OFF'}), {retain: false, qos: 0});
});
it('Should not publish state change off if any lights within are still on when changed via device with non default-ep', async () => {
const device_1 = devices.bulb_color;
const device_2 = devices.QBKG03LM;
const endpoint_1 = device_1.getEndpoint(1)!;
const endpoint_2 = device_2.getEndpoint(2)!;
const endpoint_3 = device_2.getEndpoint(3)!;
endpoint_3.removeFromGroup(groups.ha_discovery_group);
const group = groups.group_1;
group.members.push(endpoint_1);
group.members.push(endpoint_2);
group.members.push(endpoint_3);
await mockMQTTEvents.message('zigbee2mqtt/group_1/set', stringify({state: 'ON'}));
await flushPromises();
mockMQTTPublishAsync.mockClear();
await mockMQTTEvents.message('zigbee2mqtt/wall_switch_double/set', stringify({state_left: 'OFF'}));
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(1);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/wall_switch_double', stringify({state_left: 'OFF', state_right: 'ON'}), {
retain: false,
qos: 0,
});
});
it('Should publish state change off if all lights within turn off with non default-ep', async () => {
const device_1 = devices.bulb_color;
const device_2 = devices.QBKG03LM;
const endpoint_1 = device_1.getEndpoint(1)!;
const endpoint_2 = device_2.getEndpoint(2)!;
const group = groups.group_1;
group.members.push(endpoint_1);
group.members.push(endpoint_2);
settings.set(['groups'], {
1: {friendly_name: 'group_1', retain: false},
});
await mockMQTTEvents.message('zigbee2mqtt/group_1/set', stringify({state: 'ON'}));
await flushPromises();
mockMQTTPublishAsync.mockClear();
await mockMQTTEvents.message('zigbee2mqtt/bulb_color/set', stringify({state: 'OFF'}));
await mockMQTTEvents.message('zigbee2mqtt/wall_switch_double/set', stringify({state_left: 'OFF'}));
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(3);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_color', stringify({state: 'OFF'}), {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/wall_switch_double', stringify({state_left: 'OFF'}), {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/group_1', stringify({state: 'OFF'}), {retain: false, qos: 0});
});
it('Should publish state change off if all lights within turn off with non default-ep, but device state does not use them', async () => {
const device_1 = devices.bulb_color;
const device_2 = devices.InovelliVZM31SN;
const endpoint_1 = device_1.getEndpoint(1)!;
const endpoint_2 = device_2.getEndpoint(2)!;
const group = groups.group_1;
group.members.push(endpoint_1);
group.members.push(endpoint_2);
settings.set(['groups'], {
1: {friendly_name: 'group_1', retain: false},
});
await mockMQTTEvents.message('zigbee2mqtt/group_1/set', stringify({state: 'ON'}));
await flushPromises();
mockMQTTPublishAsync.mockClear();
await mockMQTTEvents.message('zigbee2mqtt/bulb_color/set', stringify({state: 'OFF'}));
await mockMQTTEvents.message('zigbee2mqtt/wall_switch_double/set', stringify({state_left: 'OFF'}));
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(3);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_color', stringify({state: 'OFF'}), {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/wall_switch_double', stringify({state_left: 'OFF'}), {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/group_1', stringify({state: 'OFF'}), {retain: false, qos: 0});
});
it('Should not publish state change off if any lights within are still on when changed via shared group', async () => {
const device_1 = devices.bulb_color;
const device_2 = devices.bulb;
const endpoint_1 = device_1.getEndpoint(1)!;
const endpoint_2 = device_2.getEndpoint(1)!;
groups.group_1.members.push(endpoint_1);
groups.group_1.members.push(endpoint_2);
groups.group_2.members.push(endpoint_1);
await mockMQTTEvents.message('zigbee2mqtt/group_1/set', stringify({state: 'ON'}));
await flushPromises();
mockMQTTPublishAsync.mockClear();
await mockMQTTEvents.message('zigbee2mqtt/group_2/set', stringify({state: 'OFF'}));
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(2);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/group_2', stringify({state: 'OFF'}), {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_color', stringify({state: 'OFF'}), {retain: false, qos: 0});
});
it('Should publish state change off if all lights within turn off', async () => {
const device_1 = devices.bulb_color;
const device_2 = devices.bulb;
const endpoint_1 = device_1.getEndpoint(1)!;
const endpoint_2 = device_2.getEndpoint(1)!;
const group = groups.group_1;
group.members.push(endpoint_1);
group.members.push(endpoint_2);
settings.set(['groups'], {
1: {friendly_name: 'group_1', retain: false},
});
await mockMQTTEvents.message('zigbee2mqtt/group_1/set', stringify({state: 'ON'}));
await flushPromises();
mockMQTTPublishAsync.mockClear();
await mockMQTTEvents.message('zigbee2mqtt/bulb_color/set', stringify({state: 'OFF'}));
await mockMQTTEvents.message('zigbee2mqtt/bulb/set', stringify({state: 'OFF'}));
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(3);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_color', stringify({state: 'OFF'}), {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb', stringify({state: 'OFF'}), {retain: true, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/group_1', stringify({state: 'OFF'}), {retain: false, qos: 0});
});
it('Should only update group state with changed properties', async () => {
const device_1 = devices.bulb_color;
const device_2 = devices.bulb;
const endpoint_1 = device_1.getEndpoint(1)!;
const endpoint_2 = device_2.getEndpoint(1)!;
const group = groups.group_1;
group.members.push(endpoint_1);
group.members.push(endpoint_2);
settings.set(['groups'], {
1: {friendly_name: 'group_1', retain: false},
});
mockMQTTPublishAsync.mockClear();
await mockMQTTEvents.message('zigbee2mqtt/bulb_color/set', stringify({state: 'OFF', color_temp: 200}));
await mockMQTTEvents.message('zigbee2mqtt/bulb/set', stringify({state: 'ON', color_temp: 250}));
await flushPromises();
mockMQTTPublishAsync.mockClear();
await mockMQTTEvents.message('zigbee2mqtt/group_1/set', stringify({color_temp: 300}));
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(3);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bulb_color',
stringify({color_mode: 'color_temp', color_temp: 300, state: 'OFF'}),
{retain: false, qos: 0},
);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb', stringify({color_mode: 'color_temp', color_temp: 300, state: 'ON'}), {
retain: true,
qos: 0,
});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/group_1',
stringify({color_mode: 'color_temp', color_temp: 300, state: 'ON'}),
{retain: false, qos: 0},
);
});
it('Should publish state change off even when missing current state', async () => {
const device_1 = devices.bulb_color;
const device_2 = devices.bulb;
const endpoint_1 = device_1.getEndpoint(1)!;
const endpoint_2 = device_2.getEndpoint(1)!;
const group = groups.group_1;
group.members.push(endpoint_1);
group.members.push(endpoint_2);
settings.set(['groups'], {
1: {friendly_name: 'group_1', retain: false},
});
await mockMQTTEvents.message('zigbee2mqtt/group_1/set', stringify({state: 'ON'}));
await flushPromises();
mockMQTTPublishAsync.mockClear();
// @ts-expect-error private
controller.state.state = {};
await mockMQTTEvents.message('zigbee2mqtt/bulb_color/set', stringify({state: 'OFF'}));
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(2);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_color', stringify({state: 'OFF'}), {retain: false, qos: 0});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/group_1', stringify({state: 'OFF'}), {retain: false, qos: 0});
});
it('Add to group via MQTT', async () => {
const device = devices.bulb_color;
const endpoint = device.getEndpoint(1);
const group = groups.group_1;
expect(group.members.length).toBe(0);
mockMQTTPublishAsync.mockClear();
mockMQTTEvents.message(
'zigbee2mqtt/bridge/request/group/members/add',
stringify({transaction: '123', group: 'group_1', device: 'bulb_color'}),
);
await flushPromises();
expect(group.members).toStrictEqual([endpoint]);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bridge/groups', expect.any(String), expect.any(Object));
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/group/members/add',
stringify({data: {device: 'bulb_color', endpoint: 'default', group: 'group_1'}, transaction: '123', status: 'ok'}),
{retain: false, qos: 0},
);
});
it('Add to group via MQTT fails', async () => {
const device = devices.bulb_color;
const endpoint = device.getEndpoint(1)!;
const group = groups.group_1;
expect(group.members.length).toBe(0);
endpoint.addToGroup.mockImplementationOnce(() => {
throw new Error('timeout');
});
await flushPromises();
mockMQTTPublishAsync.mockClear();
mockMQTTEvents.message('zigbee2mqtt/bridge/request/group/members/add', stringify({group: 'group_1', device: 'bulb_color'}));
await flushPromises();
expect(group.members).toStrictEqual([]);
expect(mockMQTTPublishAsync).not.toHaveBeenCalledWith('zigbee2mqtt/bridge/groups', expect.any(String), expect.any(Object));
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/group/members/add',
stringify({data: {}, status: 'error', error: 'Failed to add from group (timeout)'}),
{retain: false, qos: 0},
);
});
it('Add to group with slashes via MQTT', async () => {
const device = devices.bulb_color;
const endpoint = device.getEndpoint(1);
const group = groups['group/with/slashes'];
settings.set(['groups'], {99: {friendly_name: 'group/with/slashes', retain: false}});
expect(group.members.length).toBe(0);
mockMQTTPublishAsync.mockClear();
mockMQTTEvents.message('zigbee2mqtt/bridge/request/group/members/add', stringify({group: 'group/with/slashes', device: 'bulb_color'}));
await flushPromises();
expect(group.members).toStrictEqual([endpoint]);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bridge/groups', expect.any(String), expect.any(Object));
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/group/members/add',
stringify({data: {device: 'bulb_color', endpoint: 'default', group: 'group/with/slashes'}, status: 'ok'}),
{retain: false, qos: 0},
);
});
it('Add to group via MQTT with postfix', async () => {
const device = devices.QBKG03LM;
const endpoint = device.getEndpoint(3)!;
const group = groups.group_1;
expect(group.members.length).toBe(0);
mockMQTTPublishAsync.mockClear();
mockMQTTEvents.message(
'zigbee2mqtt/bridge/request/group/members/add',
stringify({group: 'group_1', device: 'wall_switch_double', endpoint: 'right'}),
);
await flushPromises();
expect(group.members).toStrictEqual([endpoint]);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bridge/groups', expect.any(String), expect.any(Object));
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/group/members/add',
stringify({data: {device: 'wall_switch_double', endpoint: 'right', group: 'group_1'}, status: 'ok'}),
{retain: false, qos: 0},
);
});
it('Add to group via MQTT with postfix shouldnt add it twice', async () => {
const device = devices.QBKG03LM;
const endpoint = device.getEndpoint(3)!;
const group = groups.group_1;
expect(group.members.length).toBe(0);
mockMQTTPublishAsync.mockClear();
mockMQTTEvents.message(
'zigbee2mqtt/bridge/request/group/members/add',
stringify({group: 'group_1', device: 'wall_switch_double', endpoint: 'right'}),
);
await flushPromises();
mockMQTTEvents.message(
'zigbee2mqtt/bridge/request/group/members/add',
stringify({group: 'group_1', device: '0x0017880104e45542', endpoint: '3'}),
);
await flushPromises();
expect(group.members).toStrictEqual([endpoint]);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bridge/groups', expect.any(String), expect.any(Object));
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/group/members/add',
stringify({data: {device: 'wall_switch_double', endpoint: 'right', group: 'group_1'}, status: 'ok'}),
{retain: false, qos: 0},
);
});
it('Remove from group via MQTT', async () => {
const device = devices.bulb_color;
const endpoint = device.getEndpoint(1)!;
const group = groups.group_1;
group.members.push(endpoint);
mockMQTTPublishAsync.mockClear();
mockMQTTEvents.message('zigbee2mqtt/bridge/request/group/members/remove', stringify({group: 'group_1', device: 'bulb_color'}));
await flushPromises();
expect(group.members).toStrictEqual([]);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bridge/groups', expect.any(String), expect.any(Object));
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/group/members/remove',
stringify({data: {device: 'bulb_color', endpoint: 'default', group: 'group_1'}, status: 'ok'}),
{retain: false, qos: 0},
);
});
it('Remove from group via MQTT keeping device reporting', async () => {
const device = devices.bulb_color;
const endpoint = device.getEndpoint(1)!;
const group = groups.group_1;
group.members.push(endpoint);
mockMQTTPublishAsync.mockClear();
mockMQTTEvents.message(
'zigbee2mqtt/bridge/request/group/members/remove',
stringify({group: 'group_1', device: 'bulb_color', skip_disable_reporting: true}),
);
await flushPromises();
expect(group.members).toStrictEqual([]);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bridge/groups', expect.any(String), expect.any(Object));
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/group/members/remove',
stringify({data: {device: 'bulb_color', endpoint: 'default', group: 'group_1'}, status: 'ok'}),
{retain: false, qos: 0},
);
});
it('Remove from group via MQTT with postfix variant 1', async () => {
const device = devices.QBKG03LM;
const endpoint = device.getEndpoint(3)!;
const group = groups.group_1;
group.members.push(endpoint);
mockMQTTPublishAsync.mockClear();
mockMQTTEvents.message(
'zigbee2mqtt/bridge/request/group/members/remove',
stringify({group: 'group_1', device: '0x0017880104e45542', endpoint: '3'}),
);
await flushPromises();
expect(group.members).toStrictEqual([]);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bridge/groups', expect.any(String), expect.any(Object));
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/group/members/remove',
stringify({data: {device: '0x0017880104e45542', endpoint: '3', group: 'group_1'}, status: 'ok'}),
{retain: false, qos: 0},
);
});
it('Remove from group via MQTT with postfix variant 2', async () => {
const device = devices.QBKG03LM;
const endpoint = device.getEndpoint(3)!;
const group = groups.group_1;
group.members.push(endpoint);
mockMQTTPublishAsync.mockClear();
mockMQTTEvents.message(
'zigbee2mqtt/bridge/request/group/members/remove',
stringify({group: 'group_1', device: 'wall_switch_double', endpoint: '3'}),
);
await flushPromises();
expect(group.members).toStrictEqual([]);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bridge/groups', expect.any(String), expect.any(Object));
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/group/members/remove',
stringify({data: {device: 'wall_switch_double', endpoint: '3', group: 'group_1'}, status: 'ok'}),
{retain: false, qos: 0},
);
});
it('Remove from group via MQTT with postfix variant 3', async () => {
const device = devices.QBKG03LM;
const endpoint = device.getEndpoint(3)!;
const group = groups.group_1;
group.members.push(endpoint);
mockMQTTPublishAsync.mockClear();
mockMQTTEvents.message(
'zigbee2mqtt/bridge/request/group/members/remove',
stringify({group: 'group_1', device: '0x0017880104e45542', endpoint: 'right'}),
);
await flushPromises();
expect(group.members).toStrictEqual([]);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bridge/groups', expect.any(String), expect.any(Object));
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/group/members/remove',
stringify({data: {device: '0x0017880104e45542', endpoint: 'right', group: 'group_1'}, status: 'ok'}),
{retain: false, qos: 0},
);
});
it('Remove from group all', async () => {
const group = groups.group_1;
groups.group_1.members.push(devices.QBKG03LM.endpoints[2]);
mockMQTTPublishAsync.mockClear();
mockMQTTEvents.message('zigbee2mqtt/bridge/request/group/members/remove_all', stringify({device: '0x0017880104e45542', endpoint: 'right'}));
await flushPromises();
expect(group.members).toStrictEqual([]);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bridge/groups', expect.any(String), expect.any(Object));
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/group/members/remove_all',
stringify({data: {device: '0x0017880104e45542', endpoint: 'right'}, status: 'ok'}),
{retain: false, qos: 0},
);
});
it('Error when adding to non-existing group', async () => {
mockLogger.error.mockClear();
mockMQTTPublishAsync.mockClear();
mockMQTTEvents.message('zigbee2mqtt/bridge/request/group/members/remove', stringify({group: 'group_1_not_existing', device: 'bulb_color'}));
await flushPromises();
expect(mockMQTTPublishAsync).not.toHaveBeenCalledWith('zigbee2mqtt/bridge/groups', expect.any(String), expect.any(Object));
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/group/members/remove',
stringify({data: {}, status: 'error', error: "Group 'group_1_not_existing' does not exist"}),
{retain: false, qos: 0},
);
});
it('Error when adding a non-existing device', async () => {
mockLogger.error.mockClear();
mockMQTTPublishAsync.mockClear();
mockMQTTEvents.message('zigbee2mqtt/bridge/request/group/members/add', stringify({group: 'group_1', device: 'bulb_color_not_existing'}));
await flushPromises();
expect(mockMQTTPublishAsync).not.toHaveBeenCalledWith('zigbee2mqtt/bridge/groups', expect.any(String), expect.any(Object));
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/group/members/add',
stringify({data: {}, status: 'error', error: "Device 'bulb_color_not_existing' does not exist"}),
{retain: false, qos: 0},
);
});
it('Error when adding a non-existing endpoint', async () => {
mockLogger.error.mockClear();
mockMQTTPublishAsync.mockClear();
mockMQTTEvents.message(
'zigbee2mqtt/bridge/request/group/members/add',
stringify({group: 'group_1', device: 'bulb_color', endpoint: 'not_existing_endpoint'}),
);
await flushPromises();
expect(mockMQTTPublishAsync).not.toHaveBeenCalledWith('zigbee2mqtt/bridge/groups', expect.any(String), expect.any(Object));
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/group/members/add',
stringify({data: {}, status: 'error', error: "Device 'bulb_color' does not have endpoint 'not_existing_endpoint'"}),
{retain: false, qos: 0},
);
});
it('Error when invalid payload', async () => {
mockLogger.error.mockClear();
mockMQTTPublishAsync.mockClear();
mockMQTTEvents.message('zigbee2mqtt/bridge/request/group/members/add', stringify({group: 'group_1', devicez: 'bulb_color'}));
await flushPromises();
expect(mockMQTTPublishAsync).not.toHaveBeenCalledWith('zigbee2mqtt/bridge/groups', expect.any(String), expect.any(Object));
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/group/members/add',
stringify({data: {}, status: 'error', error: 'Invalid payload'}),
{retain: false, qos: 0},
);
});
it('Error when add/remove with invalid payload', async () => {
mockLogger.error.mockClear();
mockMQTTPublishAsync.mockClear();
mockMQTTEvents.message('zigbee2mqtt/bridge/request/group/members/add', stringify({groupz: 'group_1', device: 'bulb_color'}));
await flushPromises();
expect(mockMQTTPublishAsync).not.toHaveBeenCalledWith('zigbee2mqtt/bridge/groups', expect.any(String), expect.any(Object));
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bridge/response/group/members/add',
stringify({data: {}, status: 'error', error: 'Invalid payload'}),
{retain: false, qos: 0},
);
});
it('Should only include relevant properties when publishing member states', async () => {
const bulbColor = devices.bulb_color;
const bulbColorTemp = devices.bulb;
const group = groups.group_1;
group.members.push(bulbColor.getEndpoint(1)!);
group.members.push(bulbColorTemp.getEndpoint(1)!);
mockMQTTPublishAsync.mockClear();
await mockMQTTEvents.message('zigbee2mqtt/group_1/set', stringify({color_temp: 50}));
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(3);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb_color', stringify({color_mode: 'color_temp', color_temp: 50}), {
retain: false,
qos: 0,
});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/group_1', stringify({color_mode: 'color_temp', color_temp: 50}), {
retain: false,
qos: 0,
});
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb', stringify({color_mode: 'color_temp', color_temp: 50}), {
retain: true,
qos: 0,
});
mockMQTTPublishAsync.mockClear();
await mockMQTTEvents.message('zigbee2mqtt/group_1/set', stringify({color: {x: 0.5, y: 0.3}}));
await flushPromises();
expect(mockMQTTPublishAsync).toHaveBeenCalledTimes(3);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/bulb_color',
stringify({color: {x: 0.5, y: 0.3}, color_mode: 'xy', color_temp: 548}),
{retain: false, qos: 0},
);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith(
'zigbee2mqtt/group_1',
stringify({color: {x: 0.5, y: 0.3}, color_mode: 'xy', color_temp: 548}),
{retain: false, qos: 0},
);
expect(mockMQTTPublishAsync).toHaveBeenCalledWith('zigbee2mqtt/bulb', stringify({color_mode: 'color_temp', color_temp: 548}), {
retain: true,
qos: 0,
});
});
});
File diff suppressed because it is too large Load Diff

Some files were not shown because too many files have changed in this diff Show More