homebridge-tuya-easy 0.2.0-beta.1 → 0.2.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +20 -0
- package/README.ar.md +23 -0
- package/README.md +14 -2
- package/homebridge-ui/public/index.html +7 -4
- package/homebridge-ui/server.js +1 -0
- package/package.json +1 -1
- package/src/homekit/deviceAccessory.js +1 -0
- package/src/platform.js +223 -80
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,26 @@
|
|
|
2
2
|
|
|
3
3
|
Versions follow [Semantic Versioning](https://semver.org/). Releases are published under the npm tag `latest`; pre-releases under `beta`.
|
|
4
4
|
|
|
5
|
+
## 0.2.0-beta.2
|
|
6
|
+
|
|
7
|
+
Settings take effect without restarting the bridge.
|
|
8
|
+
|
|
9
|
+
- **What you change in the settings reaches Apple Home by itself within a few seconds**: a name, a part ticked or unticked, a word for a part, readings, a device added or removed. The page says so after saving instead of asking for a restart.
|
|
10
|
+
- **Devices you did not touch keep their connection.** Only the device whose options changed is opened again. Changing how devices are reached (local, fallback, cloud) opens all of them again, still without a restart.
|
|
11
|
+
- **Devices brought again from the account** are picked up the same way.
|
|
12
|
+
- A warning about a device (no key yet, needs the cloud) is written to the log once, not at every change.
|
|
13
|
+
- The page shows where the User Code and the scan button are in the Tuya app (README).
|
|
14
|
+
|
|
15
|
+
**بالعربية**
|
|
16
|
+
|
|
17
|
+
الإعدادات تُطبَّق دون إعادة تشغيل الجسر.
|
|
18
|
+
|
|
19
|
+
- **ما تغيّره في الإعدادات يصل إلى تطبيق المنزل من نفسه خلال ثوانٍ**: اسم، أو جزء تحدّده أو تلغيه، أو كلمة لجزء، أو القراءات، أو جهاز تضيفه أو تحذفه. وتذكر الصفحة ذلك بعد الحفظ بدل طلب إعادة التشغيل.
|
|
20
|
+
- **الأجهزة التي لم تغيّرها تبقى على اتصالها.** يُعاد فتح الجهاز الذي تغيّرت خياراته وحده. وتغيير طريقة الوصول (محلي، بديل، سحابي) يعيد فتح الأجهزة كلها، ودون إعادة تشغيل أيضًا.
|
|
21
|
+
- **الأجهزة التي تجلبها من الحساب مرة أخرى** تُلتقط بالطريقة نفسها.
|
|
22
|
+
- التحذير عن جهاز (ينقصه المفتاح، أو يحتاج السحابة) يُكتب في السجل مرة واحدة، وليس مع كل تغيير.
|
|
23
|
+
- صفحة الإضافة تعرض مكان User Code وزر المسح في تطبيق Tuya.
|
|
24
|
+
|
|
5
25
|
## 0.2.0-beta.1
|
|
6
26
|
|
|
7
27
|
Every common kind of Tuya device, as a trial, and Tuya's cloud as an optional fallback. Your devices and settings are kept; nothing changes for plugs and dimmers unless you choose the fallback.
|
package/README.ar.md
CHANGED
|
@@ -4,12 +4,33 @@
|
|
|
4
4
|
|
|
5
5
|
# homebridge-tuya-easy
|
|
6
6
|
|
|
7
|
+
</div>
|
|
8
|
+
|
|
9
|
+
<p align="center">
|
|
10
|
+
<a href="https://www.npmjs.com/package/homebridge-tuya-easy"><img alt="npm version" src="https://badgen.net/npm/v/homebridge-tuya-easy/beta"></a>
|
|
11
|
+
<a href="https://github.com/Abdulazizruh/homebridge-tuya-easy/actions/workflows/ci.yml"><img alt="tests" src="https://github.com/Abdulazizruh/homebridge-tuya-easy/actions/workflows/ci.yml/badge.svg"></a>
|
|
12
|
+
<br>
|
|
13
|
+
<a href="https://www.npmjs.com/package/homebridge-tuya-easy"><img alt="downloads a month" src="https://badgen.net/npm/dm/homebridge-tuya-easy"></a>
|
|
14
|
+
<a href="https://www.npmjs.com/package/homebridge-tuya-easy"><img alt="total downloads" src="https://badgen.net/npm/dt/homebridge-tuya-easy"></a>
|
|
15
|
+
<br>
|
|
16
|
+
<a href="https://github.com/Abdulazizruh/homebridge-tuya-easy/stargazers"><img alt="stars" src="https://badgen.net/github/stars/Abdulazizruh/homebridge-tuya-easy"></a>
|
|
17
|
+
<a href="https://github.com/Abdulazizruh/homebridge-tuya-easy/issues"><img alt="open issues" src="https://badgen.net/github/open-issues/Abdulazizruh/homebridge-tuya-easy"></a>
|
|
18
|
+
<a href="LICENSE"><img alt="license" src="https://badgen.net/npm/license/homebridge-tuya-easy"></a>
|
|
19
|
+
<br>
|
|
20
|
+
<a href="https://github.com/Abdulazizruh/homebridge-tuya-easy/commits/main"><img alt="last commit" src="https://badgen.net/github/last-commit/Abdulazizruh/homebridge-tuya-easy"></a>
|
|
21
|
+
<br>
|
|
22
|
+
<a href="https://nodejs.org"><img alt="node" src="https://badgen.net/npm/node/homebridge-tuya-easy"></a>
|
|
23
|
+
</p>
|
|
24
|
+
|
|
25
|
+
<div dir="rtl">
|
|
26
|
+
|
|
7
27
|
أجهزة Tuya و Smart Life في تطبيق المنزل من Apple، بتحكم محلي على شبكة بيتك، مع سحابة Tuya بديلًا اختياريًا. [English](README.md)
|
|
8
28
|
|
|
9
29
|
| | |
|
|
10
30
|
|---|---|
|
|
11
31
|
| **بدون حساب مطور** | **بدون مشروع في منصة Tuya IoT وبدون Access ID وبدون فترة تجريبية تُجدَّد.** تربط حسابك في Tuya أو Smart Life مرة واحدة بمسح رمز من التطبيق |
|
|
12
32
|
| **بدون إدخال لكل جهاز** | الإضافة تجلب أجهزتك بنفسها: الأسماء والغرف والأنواع ونقاط البيانات والمفتاح المحلي لكل جهاز |
|
|
33
|
+
| **المفاتيح المحلية من نفسها** | المفتاح الذي يحتاجه كل جهاز للتحكم المحلي يأتي من حسابك نفسه عند الربط. **بدون حساب مطور، وبدون أي إضافة Tuya أخرى، وبدون أدوات استخراج المفاتيح، وبدون كتابة مفتاح بيدك** |
|
|
13
34
|
| **تحكم محلي** | بعد الربط يذهب كل أمر من تطبيق المنزل إلى Homebridge ثم إلى الجهاز مباشرة داخل شبكة بيتك، ويستمر العمل عند انقطاع الإنترنت |
|
|
14
35
|
| **السحابة بديلًا إذا اخترتها** | الجهاز الذي يتعذر الوصول إليه في البيت (بعيد عن الشبكة، أو خلف بوابة Zigbee أو Bluetooth، أو يعمل ببطارية) يمر عبر سحابة Tuya بدل «لا يستجيب»، ويعود إلى الشبكة المحلية بنفسه |
|
|
15
36
|
| **خارج البيت** | عبر مركز منزلي من Apple في البيت (Apple TV أو HomePod)، مثل أي ملحق في تطبيق المنزل |
|
|
@@ -55,6 +76,8 @@
|
|
|
55
76
|
3. **جلب أجهزتي**: تقرأ الإضافة أجهزتك من الحساب ثم تبحث عن كل جهاز على شبكة بيتك. حدّد ما تريد وعدّل الأسماء إن أحببت، ثم اضغط **أضف الأجهزة المحددة**.
|
|
56
77
|
3. أعد تشغيل الجسر الفرعي وأضفه إلى تطبيق المنزل برمزه الخاص.
|
|
57
78
|
|
|
79
|
+
بعد هذا التشغيل الأول، ما تغيّره في الإعدادات (اسم، أو جزء، أو جهاز تضيفه أو تحذفه، أو طريقة الوصول) يصل إلى تطبيق المنزل من نفسه خلال ثوانٍ. الجسر يبقى يعمل دون إعادة تشغيل، والأجهزة التي لم تغيّرها تبقى على اتصالها.
|
|
80
|
+
|
|
58
81
|
مكان الاثنين في تطبيق Tuya أو Smart Life: **User Code** في أسفل صفحة *Account and Security*، وزر المسح في أعلى تبويب *Me*.
|
|
59
82
|
<p align="center"><img src="assets/screenshots/user-code.png" width="260" alt="سطر User Code في أسفل صفحة Account and Security في تطبيق Tuya"></p>
|
|
60
83
|
<p align="center"><img src="assets/screenshots/scan-button.png" width="320" alt="زر المسح في أعلى تبويب Me في تطبيق Tuya"></p>
|
package/README.md
CHANGED
|
@@ -5,8 +5,17 @@
|
|
|
5
5
|
<p align="center">
|
|
6
6
|
<a href="https://www.npmjs.com/package/homebridge-tuya-easy"><img alt="npm version" src="https://badgen.net/npm/v/homebridge-tuya-easy/beta"></a>
|
|
7
7
|
<a href="https://github.com/Abdulazizruh/homebridge-tuya-easy/actions/workflows/ci.yml"><img alt="tests" src="https://github.com/Abdulazizruh/homebridge-tuya-easy/actions/workflows/ci.yml/badge.svg"></a>
|
|
8
|
-
<
|
|
9
|
-
<a href="https://
|
|
8
|
+
<br>
|
|
9
|
+
<a href="https://www.npmjs.com/package/homebridge-tuya-easy"><img alt="downloads a month" src="https://badgen.net/npm/dm/homebridge-tuya-easy"></a>
|
|
10
|
+
<a href="https://www.npmjs.com/package/homebridge-tuya-easy"><img alt="total downloads" src="https://badgen.net/npm/dt/homebridge-tuya-easy"></a>
|
|
11
|
+
<br>
|
|
12
|
+
<a href="https://github.com/Abdulazizruh/homebridge-tuya-easy/stargazers"><img alt="stars" src="https://badgen.net/github/stars/Abdulazizruh/homebridge-tuya-easy"></a>
|
|
13
|
+
<a href="https://github.com/Abdulazizruh/homebridge-tuya-easy/issues"><img alt="open issues" src="https://badgen.net/github/open-issues/Abdulazizruh/homebridge-tuya-easy"></a>
|
|
14
|
+
<a href="LICENSE"><img alt="license" src="https://badgen.net/npm/license/homebridge-tuya-easy"></a>
|
|
15
|
+
<br>
|
|
16
|
+
<a href="https://github.com/Abdulazizruh/homebridge-tuya-easy/commits/main"><img alt="last commit" src="https://badgen.net/github/last-commit/Abdulazizruh/homebridge-tuya-easy"></a>
|
|
17
|
+
<br>
|
|
18
|
+
<a href="https://nodejs.org"><img alt="node" src="https://badgen.net/npm/node/homebridge-tuya-easy"></a>
|
|
10
19
|
</p>
|
|
11
20
|
|
|
12
21
|
Tuya and Smart Life devices in Apple Home, controlled locally over your home network, with Tuya's cloud as an optional fallback. [بالعربية](README.ar.md)
|
|
@@ -15,6 +24,7 @@ Tuya and Smart Life devices in Apple Home, controlled locally over your home net
|
|
|
15
24
|
|---|---|
|
|
16
25
|
| **No developer account** | **No Tuya IoT project, no Access ID, no trial to renew.** You link your own Tuya or Smart Life account once, by scanning a code with the app |
|
|
17
26
|
| **Nothing typed per device** | The plugin brings your devices by itself: names, rooms, kinds, data points and the local key each one needs |
|
|
27
|
+
| **Local keys by themselves** | The key each device needs for local control comes from your own account when you link it. **No developer account, no other Tuya plugin, no key-extraction tool and no key typed by hand** |
|
|
18
28
|
| **Local control** | After the link, every command goes Apple Home → Homebridge → the device, directly on your home network. It keeps working when the internet is down |
|
|
19
29
|
| **Cloud as a fallback, if you choose it** | A device that cannot be reached at home (out of reach, behind a Zigbee or Bluetooth hub, or battery powered) goes through Tuya's cloud instead of showing "No Response", and comes back to the home network by itself |
|
|
20
30
|
| **Away from home** | With an Apple home hub at home (Apple TV, HomePod), like any Apple Home accessory |
|
|
@@ -60,6 +70,8 @@ Battery sensors and devices behind a Zigbee or Bluetooth hub cannot be reached o
|
|
|
60
70
|
3. **Bring my devices**: the plugin reads your devices from the account, then listens for each on your home network. Tick the ones you want, rename them if you like, and press **Add the ticked devices**.
|
|
61
71
|
3. Restart the child bridge and add it to Apple Home with its own code.
|
|
62
72
|
|
|
73
|
+
After that first start, what you change in the settings (a name, a part, a device added or removed, how devices are reached) reaches Apple Home by itself within a few seconds. The bridge is not restarted, and the devices you did not touch keep their connection.
|
|
74
|
+
|
|
63
75
|
Where the two things are in the Tuya or Smart Life app: the **User Code** at the bottom of *Account and Security*, and the scan button at the top of the *Me* tab.
|
|
64
76
|
<p align="center"><img src="assets/screenshots/user-code.png" width="260" alt="The User Code line at the bottom of Account and Security in the Tuya app"></p>
|
|
65
77
|
<p align="center"><img src="assets/screenshots/scan-button.png" width="320" alt="The scan button at the top of the Me tab in the Tuya app"></p>
|
|
@@ -110,7 +110,7 @@
|
|
|
110
110
|
</details>
|
|
111
111
|
</div>
|
|
112
112
|
|
|
113
|
-
<div class="te-note" id="te-restart" style="display:none"><span
|
|
113
|
+
<div class="te-note" id="te-restart" style="display:none"><span id="te-restart-text"></span></div>
|
|
114
114
|
</div>
|
|
115
115
|
|
|
116
116
|
<script>
|
|
@@ -139,7 +139,7 @@
|
|
|
139
139
|
stNoKey: 'No local key', trialKind: 'Trial', trialKindNote: 'This kind of device follows Tuya\'s standard functions and has not been confirmed on a real one yet. If something is wrong, report it on GitHub.', kFan: 'Fans', kCover: 'Curtains and doors', kClimate: 'Heating and cooling', kAir: 'Air', kSensor: 'Sensors', kValve: 'Valves', viaCloud: 'Connected · cloud', reversed: 'Open and closed are the wrong way round: reverse them', searchAgain: 'Search the network again', searching: 'Listening on the network…',
|
|
140
140
|
missingNote: 'A device that is switched off can still be added; it connects by itself when it is back.',
|
|
141
141
|
mineTitle: 'My devices', kOutlet: 'Outlets', kLight: 'Lights', kSwitch: 'Switches', kOther: 'Other',
|
|
142
|
-
connected: 'Connected', notReach: 'Not reachable', atRestart: 'At the next restart', bridgeOff: 'Bridge not running',
|
|
142
|
+
connected: 'Connected', notReach: 'Not reachable', atRestart: 'At the next restart', applying: 'Being applied', bridgeOff: 'Bridge not running',
|
|
143
143
|
options: 'Options', remove: 'Remove', removeAsk: 'Remove this device from Apple Home?', yesRemove: 'Yes, remove', cancel: 'Cancel',
|
|
144
144
|
name: 'Name', shown: 'Shown in HomeKit', whole: 'The device', apply: 'Save', advanced: 'Advanced: fixed address',
|
|
145
145
|
advNote: 'Only when the device is on another network and cannot be heard. Otherwise leave empty.', ip: 'IP address', proto: 'Protocol',
|
|
@@ -148,6 +148,7 @@
|
|
|
148
148
|
importTitle: 'From another Tuya plugin on this Homebridge', importNote: 'Nothing is read from another plugin unless you press this button, and nothing is ever written to it.',
|
|
149
149
|
importBtn: 'Read its devices', importNone: 'No other local Tuya plugin with devices was found.', importDone: 'Read: {a}. Left for the account to describe: {b}.',
|
|
150
150
|
wipeTitle: 'Delete this plugin\'s data', wipeNote: 'Removes the account link, the keys and the names kept in this plugin\'s folder. No other plugin is touched. Type DELETE to confirm.', wipeBtn: 'Delete', wiped: 'Deleted.',
|
|
151
|
+
appliedNote: 'Saved. The change reaches Apple Home by itself within a few seconds; the bridge is not restarted.',
|
|
151
152
|
restartNote: 'Saved. Restart this plugin\'s child bridge for the change to show in Apple Home.',
|
|
152
153
|
saved: 'Saved', partSwitch: 'Switch', partLight: 'Light', partInching: 'Auto off (Inching in the app): on or off', trial: 'Trial', inchNote: 'The time stays as set in the Tuya app.', labelPh: 'name alone', partReadings: 'Power readings', readHas: 'This device reports:', rk_power: 'watts', rk_voltage: 'volts', rk_current: 'amps', rk_energy: 'energy used (kWh)', hasReadings: 'power readings',
|
|
153
154
|
readNote: 'Shown in the Eve app and other HomeKit apps that read them; Apple\'s Home app does not display power figures. Energy used is counted from when this is switched on.',
|
|
@@ -178,7 +179,7 @@
|
|
|
178
179
|
stNoKey: 'بدون مفتاح محلي', trialKind: 'تجريبي', trialKindNote: 'هذا النوع من الأجهزة مبني على وظائف Tuya القياسية ولم يُؤكَّد بعد على جهاز حقيقي. إذا ظهر خطأ فأبلغ عنه على GitHub.', kFan: 'المراوح', kCover: 'الستائر والأبواب', kClimate: 'التدفئة والتبريد', kAir: 'الهواء', kSensor: 'الحساسات', kValve: 'الصمامات', viaCloud: 'متصل · السحابة', reversed: 'الفتح والإغلاق معكوسان: اعكسهما', searchAgain: 'ابحث في الشبكة من جديد', searching: 'جارٍ الاستماع على الشبكة…',
|
|
179
180
|
missingNote: 'يمكن إضافة الجهاز المطفأ، ويتصل بنفسه عند عودته.',
|
|
180
181
|
mineTitle: 'أجهزتي', kOutlet: 'المقابس', kLight: 'الإضاءة', kSwitch: 'المفاتيح', kOther: 'أخرى',
|
|
181
|
-
connected: 'متصل', notReach: 'يتعذر الوصول إليه', atRestart: 'عند إعادة التشغيل', bridgeOff: 'الجسر متوقف',
|
|
182
|
+
connected: 'متصل', notReach: 'يتعذر الوصول إليه', atRestart: 'عند إعادة التشغيل', applying: 'يُطبَّق الآن', bridgeOff: 'الجسر متوقف',
|
|
182
183
|
options: 'خيارات', remove: 'إزالة', removeAsk: 'هل تزيل هذا الجهاز من تطبيق المنزل؟', yesRemove: 'نعم، أزله', cancel: 'إلغاء',
|
|
183
184
|
name: 'الاسم', shown: 'يظهر في HomeKit', whole: 'الجهاز', apply: 'حفظ', advanced: 'متقدم: عنوان ثابت',
|
|
184
185
|
advNote: 'فقط إذا كان الجهاز على شبكة أخرى ويتعذر سماعه. اتركه فارغًا في غير ذلك.', ip: 'عنوان IP', proto: 'البروتوكول',
|
|
@@ -187,6 +188,7 @@
|
|
|
187
188
|
importTitle: 'من إضافة Tuya أخرى على هذا الـ Homebridge', importNote: 'تبقى بيانات الإضافات الأخرى دون قراءة حتى تضغط هذا الزر، وتبقى دون أي كتابة فيها دائمًا.',
|
|
188
189
|
importBtn: 'اقرأ أجهزتها', importNone: 'لم يُعثر على إضافة Tuya محلية أخرى فيها أجهزة.', importDone: 'قُرئ: {a}. تُرك ليصفه الحساب: {b}.',
|
|
189
190
|
wipeTitle: 'حذف بيانات هذه الإضافة', wipeNote: 'يحذف ربط الحساب والمفاتيح والأسماء المحفوظة في مجلد هذه الإضافة. تبقى الإضافات الأخرى كما هي. اكتب DELETE للتأكيد.', wipeBtn: 'حذف', wiped: 'تم الحذف.',
|
|
191
|
+
appliedNote: 'تم الحفظ. يصل التغيير إلى تطبيق المنزل من نفسه خلال ثوانٍ دون إعادة تشغيل الجسر.',
|
|
190
192
|
restartNote: 'تم الحفظ. أعد تشغيل الجسر الفرعي لهذه الإضافة ليظهر التغيير في تطبيق المنزل.',
|
|
191
193
|
saved: 'تم الحفظ', partSwitch: 'مفتاح', partLight: 'إضاءة', partInching: 'الإيقاف التلقائي (Inching في التطبيق): تفعيل أو إيقاف', trial: 'تجريبي', inchNote: 'تبقى المدة كما ضُبطت في تطبيق Tuya.', labelPh: 'الاسم وحده', partReadings: 'قراءات الكهرباء', readHas: 'يرسل هذا الجهاز:', rk_power: 'الواط', rk_voltage: 'الفولت', rk_current: 'الأمبير', rk_energy: 'الاستهلاك (kWh)', hasReadings: 'قراءات الكهرباء',
|
|
192
194
|
readNote: 'تظهر في تطبيق Eve وتطبيقات HomeKit الأخرى التي تقرؤها؛ تطبيق المنزل من Apple يخلو من عرض أرقام الكهرباء. يبدأ عدّ الاستهلاك من لحظة تفعيل هذا الخيار.',
|
|
@@ -393,7 +395,7 @@
|
|
|
393
395
|
html += list.map(({ entry, d }) => {
|
|
394
396
|
const live = status.online.includes(entry.id);
|
|
395
397
|
const pill = !status.bridgeRunning ? `<span class="te-pill">${t('bridgeOff')}</span>` : live ? `<span class="te-pill te-ok">${t(status.cloud?.includes(entry.id) ? 'viaCloud' : 'connected')}</span>`
|
|
396
|
-
: changed ? `<span class="te-pill te-warn">${t('atRestart')}</span>` : `<span class="te-pill te-bad">${t('notReach')}</span>`;
|
|
398
|
+
: changed ? `<span class="te-pill te-warn">${t(status.live ? 'applying' : 'atRestart')}</span>` : `<span class="te-pill te-bad">${t('notReach')}</span>`;
|
|
397
399
|
const prob = !live && status.bridgeRunning ? status.problems?.[entry.id] : null;
|
|
398
400
|
const why = !prob ? '' : `<div class="te-alert-warn mt-2">${prob.kind === 'unheard' ? t('whyUnheard') : (prob.kind === 'refused' ? t('whyRefused') : t('whyDropped')).replace('{ip}', `<span dir="ltr">${esc(prob.ip)}</span>`).replace('{v}', `<span dir="ltr">${esc(prob.version)}</span>`)}${prob.message ? `<div class="te-muted" dir="ltr">${esc(prob.message)}</div>` : ''}</div>`;
|
|
399
401
|
const open = openDevice === entry.id;
|
|
@@ -479,6 +481,7 @@
|
|
|
479
481
|
$('te-m-type').innerHTML = `<option value="outlet">${t('tOutlet')}</option><option value="dimmer">${t('tDimmer')}</option>`;
|
|
480
482
|
$('te-after-mode').style.display = config.mode ? '' : 'none';
|
|
481
483
|
$('te-restart').style.display = changed ? '' : 'none';
|
|
484
|
+
$('te-restart-text').textContent = t(status.bridgeRunning && status.live ? 'appliedNote' : 'restartNote');
|
|
482
485
|
renderLink();
|
|
483
486
|
renderBring();
|
|
484
487
|
renderMine();
|
package/homebridge-ui/server.js
CHANGED
|
@@ -123,6 +123,7 @@ class UiServer extends HomebridgePluginUiServer {
|
|
|
123
123
|
devices: Object.values(saved.devices ?? {}).map((r) => this.#summary(r, addresses, names)),
|
|
124
124
|
broughtAt: saved.savedAt ?? null,
|
|
125
125
|
bridgeRunning: bridgeRunning(this.paths),
|
|
126
|
+
live: running?.live === true,
|
|
126
127
|
online: Array.isArray(running?.online) ? running.online : [],
|
|
127
128
|
cloud: Array.isArray(running?.cloud) ? running.cloud : [],
|
|
128
129
|
problems: running?.problems && typeof running.problems === 'object' ? running.problems : {},
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "homebridge-tuya-easy",
|
|
3
3
|
"displayName": "Tuya Easy",
|
|
4
|
-
"version": "0.2.0-beta.
|
|
4
|
+
"version": "0.2.0-beta.2",
|
|
5
5
|
"description": "No Tuya developer account, no keys typed by hand. Tuya and Smart Life devices in Apple Home, controlled locally over your home network, with Tuya's cloud as an optional fallback. Link your account once by scanning a code in the Tuya or Smart Life app; the plugin brings your devices, their names and their local keys by itself.",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "index.js",
|
|
@@ -160,6 +160,7 @@ export class DeviceAccessory {
|
|
|
160
160
|
|
|
161
161
|
#name(service, shown) {
|
|
162
162
|
const { Characteristic } = this;
|
|
163
|
+
service.displayName = shown;
|
|
163
164
|
service.setCharacteristic(Characteristic.Name, shown);
|
|
164
165
|
if (Characteristic.ConfiguredName) {
|
|
165
166
|
if (!service.testCharacteristic(Characteristic.ConfiguredName)) {
|
package/src/platform.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
|
|
1
3
|
import { describeDevice } from './capabilities/index.js';
|
|
2
4
|
import { DeviceChannel } from './channel.js';
|
|
3
5
|
import { CloudHub } from './cloudHub.js';
|
|
@@ -23,7 +25,7 @@ export class TuyaEasyPlatform {
|
|
|
23
25
|
* @param {import('homebridge').Logging} log
|
|
24
26
|
* @param {import('homebridge').PlatformConfig} config
|
|
25
27
|
* @param {import('homebridge').API} api
|
|
26
|
-
* @param {{createScanner?: Function, createDevice?: Function, createHub?: Function, graceMs?: number}} [overrides] test seam
|
|
28
|
+
* @param {{createScanner?: Function, createDevice?: Function, createHub?: Function, graceMs?: number, configPath?: string, watch?: boolean}} [overrides] test seam
|
|
27
29
|
*/
|
|
28
30
|
constructor(log, config, api, overrides = {}) {
|
|
29
31
|
this.log = log;
|
|
@@ -37,6 +39,10 @@ export class TuyaEasyPlatform {
|
|
|
37
39
|
this.scanner = null;
|
|
38
40
|
this.hub = null;
|
|
39
41
|
this.stopping = false;
|
|
42
|
+
this.said = new Set();
|
|
43
|
+
this.mode = 'local';
|
|
44
|
+
this.addresses = {};
|
|
45
|
+
this.watched = [];
|
|
40
46
|
|
|
41
47
|
api.on('didFinishLaunching', () => {
|
|
42
48
|
this.start().catch((err) => this.log.error(`Startup failed: ${err.message}`));
|
|
@@ -53,120 +59,254 @@ export class TuyaEasyPlatform {
|
|
|
53
59
|
|
|
54
60
|
async start() {
|
|
55
61
|
ensureDir(this.paths.root);
|
|
62
|
+
this.#begin(this.#plan());
|
|
63
|
+
this.#beat();
|
|
64
|
+
const beat = setInterval(() => this.#beat(), HEARTBEAT_MS);
|
|
65
|
+
beat.unref?.();
|
|
66
|
+
this.timers.add(beat);
|
|
67
|
+
this.#watch();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** What the settings ask for now: how the devices are reached, and what each configured device is. */
|
|
71
|
+
#plan() {
|
|
56
72
|
const entries = (Array.isArray(this.config.devices) ? this.config.devices : []).filter((d) => d && d.id && d.enabled !== false);
|
|
57
73
|
const records = readJson(this.paths.devices)?.devices ?? {};
|
|
58
|
-
const addresses = readJson(this.paths.addresses) ?? {};
|
|
59
74
|
try { rememberNames(this.paths.names, entries); } catch { /* names are best effort */ }
|
|
60
75
|
|
|
61
76
|
if (!this.config.mode) {
|
|
62
77
|
this.log.warn('Setup is not finished: open the plugin settings and choose how the devices are reached.');
|
|
63
78
|
}
|
|
64
79
|
let mode = MODES.includes(this.config.mode) ? this.config.mode : 'local';
|
|
80
|
+
let account = null;
|
|
81
|
+
if (mode !== 'local') {
|
|
82
|
+
account = readJson(this.paths.account);
|
|
83
|
+
if (!account && !this.overrides.createHub) {
|
|
84
|
+
this.log.warn('The cloud was chosen as a fallback but the account is not linked: open the plugin settings and link it. Control is local until then.');
|
|
85
|
+
mode = 'local';
|
|
86
|
+
}
|
|
87
|
+
}
|
|
65
88
|
|
|
66
|
-
|
|
67
|
-
const plans = [];
|
|
89
|
+
const plans = new Map();
|
|
68
90
|
for (const entry of entries) {
|
|
69
91
|
const record = records[entry.id];
|
|
70
92
|
const name = entry.name || record?.name || shortId(entry.id);
|
|
71
93
|
if (!record) {
|
|
72
|
-
this
|
|
94
|
+
this.#say(`[${name}] is not known yet: open the plugin settings and bring your devices from the account.`);
|
|
73
95
|
continue;
|
|
74
96
|
}
|
|
75
97
|
const described = describeDevice(record);
|
|
76
98
|
if (!described.parts.length) {
|
|
77
|
-
this
|
|
99
|
+
this.#say(`[${name}] is of a kind this version does not show in Apple Home yet.`);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
const reachable = Boolean(record.localKey) && !record.sub && Object.values(described.dps).some((dp) => Number.isFinite(dp));
|
|
103
|
+
if (mode === 'local' && (!reachable || described.cloudOnly)) {
|
|
104
|
+
this.#say(!record.localKey
|
|
105
|
+
? `[${name}] has no local key yet: open the plugin settings and bring your devices from the account.`
|
|
106
|
+
: `[${name}] cannot be reached on the home network alone (it works through a hub or the cloud): choose "Local first, cloud as a fallback" in the settings to use it.`);
|
|
78
107
|
continue;
|
|
79
108
|
}
|
|
80
|
-
|
|
109
|
+
// What the device was when it was brought, without the values it had at that moment.
|
|
110
|
+
const { status, ...lasting } = record;
|
|
111
|
+
plans.set(entry.id, { entry, record, name, described, reachable, mark: JSON.stringify([entry, lasting]) });
|
|
112
|
+
}
|
|
113
|
+
return { mode, account, plans };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** A warning about a device is said once, not at every change of the settings. */
|
|
117
|
+
#say(text) {
|
|
118
|
+
if (!this.said.has(text)) {
|
|
119
|
+
this.said.add(text);
|
|
120
|
+
this.log.warn(text);
|
|
81
121
|
}
|
|
122
|
+
}
|
|
82
123
|
|
|
124
|
+
/** Opens everything a plan asks for: the cloud when chosen, the listener, then each device. */
|
|
125
|
+
#begin({ mode, account, plans }) {
|
|
126
|
+
this.mode = mode;
|
|
127
|
+
this.addresses = readJson(this.paths.addresses) ?? {};
|
|
83
128
|
// The cloud, only when the owner chose it: as a fallback, or alone.
|
|
84
129
|
if (mode !== 'local') {
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
devices: wanted,
|
|
94
|
-
log: this.log,
|
|
95
|
-
});
|
|
96
|
-
this.hub.start().catch((err) => this.log.warn(`The cloud could not be reached (${err.message}); it is tried again by itself. Devices on the home network are not affected.`));
|
|
97
|
-
}
|
|
130
|
+
const wanted = [...plans.values()].map((p) => ({ id: p.entry.id, local: Object.keys(p.record.dpMap ?? {}).length > 0 && Object.values(p.record.dpMap).every((d) => d.local !== false) }));
|
|
131
|
+
this.hubMark = [...plans.keys()].sort().join();
|
|
132
|
+
this.hub = this.overrides.createHub?.(wanted) ?? new CloudHub({
|
|
133
|
+
cloud: new TuyaCloud(account, { onSession: (session) => { try { writeJson(this.paths.account, session); } catch { /* kept in memory */ } } }),
|
|
134
|
+
devices: wanted,
|
|
135
|
+
log: this.log,
|
|
136
|
+
});
|
|
137
|
+
this.hub.start().catch((err) => this.log.warn(`The cloud could not be reached (${err.message}); it is tried again by itself. Devices on the home network are not affected.`));
|
|
98
138
|
}
|
|
99
|
-
|
|
100
139
|
if (mode !== 'cloud') {
|
|
101
140
|
this.scanner = this.overrides.createScanner?.() ?? new LanScanner({ log: this.log });
|
|
102
141
|
this.scanner.start();
|
|
103
142
|
}
|
|
104
|
-
|
|
105
|
-
const wantedUuids = new Set();
|
|
106
143
|
let gap = 0;
|
|
107
|
-
for (const
|
|
108
|
-
const reachable = Boolean(record.localKey) && !record.sub && Object.values(described.dps).some((dp) => Number.isFinite(dp));
|
|
109
|
-
if (mode === 'local' && (!reachable || described.cloudOnly)) {
|
|
110
|
-
this.log.warn(!record.localKey
|
|
111
|
-
? `[${name}] has no local key yet: open the plugin settings and bring your devices from the account.`
|
|
112
|
-
: `[${name}] cannot be reached on the home network alone (it works through a hub or the cloud): choose "Local first, cloud as a fallback" in the settings to use it.`);
|
|
113
|
-
continue;
|
|
114
|
-
}
|
|
115
|
-
const uuid = this.api.hap.uuid.generate(UUID_NAMESPACE + entry.id);
|
|
116
|
-
wantedUuids.add(uuid);
|
|
117
|
-
let accessory = this.cached.get(uuid);
|
|
118
|
-
const fresh = !accessory;
|
|
119
|
-
if (fresh) {
|
|
120
|
-
accessory = new this.api.platformAccessory(name, uuid, this.#category(described.kind));
|
|
121
|
-
}
|
|
122
|
-
const last = addresses[entry.id] ?? {};
|
|
123
|
-
const local = mode !== 'cloud' && reachable ? new DeviceLink({
|
|
124
|
-
id: entry.id,
|
|
125
|
-
key: record.localKey,
|
|
126
|
-
host: entry.host || last.ip || null,
|
|
127
|
-
version: entry.version || last.version || null,
|
|
128
|
-
name,
|
|
129
|
-
scanner: this.scanner,
|
|
130
|
-
log: this.log,
|
|
131
|
-
createDevice: this.overrides.createDevice,
|
|
132
|
-
refresh: entry.features?.readings !== false && described.parts.some((p) => p.type === 'readings'),
|
|
133
|
-
}) : null;
|
|
134
|
-
const link = new DeviceChannel({ id: entry.id, name, mode, dps: described.dps, local, hub: this.hub });
|
|
135
|
-
const handler = new DeviceAccessory({ api: this.api, log: this.log, accessory, entry: { ...entry, name }, record, described, link, graceMs: this.overrides.graceMs });
|
|
136
|
-
local?.on('address', (address) => this.#keepAddress(entry.id, address));
|
|
137
|
-
local?.on('fault', (err) => this.log.error(`[${name}] ${err.message}`));
|
|
138
|
-
// The settings page shows who is connected: told at once, not at the next minute.
|
|
139
|
-
link.on('online', () => this.#beat());
|
|
140
|
-
link.on('via', (via) => {
|
|
141
|
-
this.log.info(`[${name}] now reached ${via === 'cloud' ? 'through the cloud' : 'on the home network'}.`);
|
|
142
|
-
this.#beat();
|
|
143
|
-
});
|
|
144
|
-
if (fresh) {
|
|
145
|
-
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
|
|
146
|
-
} else {
|
|
147
|
-
this.api.updatePlatformAccessories([accessory]);
|
|
148
|
-
}
|
|
149
|
-
this.devices.set(entry.id, { link, handler, accessory, entry });
|
|
144
|
+
for (const plan of plans.values()) {
|
|
150
145
|
// Opened one after another, so a small computer is not asked for everything at once.
|
|
151
|
-
|
|
152
|
-
timer.unref?.();
|
|
153
|
-
this.timers.add(timer);
|
|
146
|
+
this.#open(plan, gap);
|
|
154
147
|
gap += START_GAP_MS;
|
|
155
148
|
}
|
|
149
|
+
this.#prune();
|
|
150
|
+
const how = mode === 'local' ? 'on the home network; control is local' : mode === 'hybrid' ? 'on the home network first, the cloud as a fallback' : 'through the cloud';
|
|
151
|
+
this.log.info(`${this.devices.size} device${this.devices.size === 1 ? '' : 's'} ${how}.`);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** One device: its accessory, its link, and the two paired. */
|
|
155
|
+
#open({ entry, record, name, described, reachable, mark }, gap = 0) {
|
|
156
|
+
const mode = this.mode;
|
|
157
|
+
const uuid = this.api.hap.uuid.generate(UUID_NAMESPACE + entry.id);
|
|
158
|
+
let accessory = this.cached.get(uuid);
|
|
159
|
+
const fresh = !accessory;
|
|
160
|
+
if (fresh) {
|
|
161
|
+
accessory = new this.api.platformAccessory(name, uuid, this.#category(described.kind));
|
|
162
|
+
this.cached.set(uuid, accessory);
|
|
163
|
+
}
|
|
164
|
+
const last = this.addresses[entry.id] ?? {};
|
|
165
|
+
const local = mode !== 'cloud' && reachable ? new DeviceLink({
|
|
166
|
+
id: entry.id,
|
|
167
|
+
key: record.localKey,
|
|
168
|
+
host: entry.host || last.ip || null,
|
|
169
|
+
version: entry.version || last.version || null,
|
|
170
|
+
name,
|
|
171
|
+
scanner: this.scanner,
|
|
172
|
+
log: this.log,
|
|
173
|
+
createDevice: this.overrides.createDevice,
|
|
174
|
+
refresh: entry.features?.readings !== false && described.parts.some((p) => p.type === 'readings'),
|
|
175
|
+
}) : null;
|
|
176
|
+
const link = new DeviceChannel({ id: entry.id, name, mode, dps: described.dps, local, hub: this.hub });
|
|
177
|
+
const handler = new DeviceAccessory({ api: this.api, log: this.log, accessory, entry: { ...entry, name }, record, described, link, graceMs: this.overrides.graceMs });
|
|
178
|
+
local?.on('address', (address) => this.#keepAddress(entry.id, address));
|
|
179
|
+
local?.on('fault', (err) => this.log.error(`[${name}] ${err.message}`));
|
|
180
|
+
// The settings page shows who is connected: told at once, not at the next minute.
|
|
181
|
+
link.on('online', () => this.#beat());
|
|
182
|
+
link.on('via', (via) => {
|
|
183
|
+
this.log.info(`[${name}] now reached ${via === 'cloud' ? 'through the cloud' : 'on the home network'}.`);
|
|
184
|
+
this.#beat();
|
|
185
|
+
});
|
|
186
|
+
if (fresh) {
|
|
187
|
+
this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
|
|
188
|
+
} else {
|
|
189
|
+
this.api.updatePlatformAccessories([accessory]);
|
|
190
|
+
}
|
|
191
|
+
const timer = setTimeout(() => {
|
|
192
|
+
this.timers.delete(timer);
|
|
193
|
+
link.start();
|
|
194
|
+
}, gap);
|
|
195
|
+
timer.unref?.();
|
|
196
|
+
this.timers.add(timer);
|
|
197
|
+
this.devices.set(entry.id, { link, handler, accessory, entry, mark, timer });
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
#close(id) {
|
|
201
|
+
const device = this.devices.get(id);
|
|
202
|
+
if (!device) {
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
clearTimeout(device.timer);
|
|
206
|
+
this.timers.delete(device.timer);
|
|
207
|
+
device.handler.stop();
|
|
208
|
+
device.link.removeAllListeners();
|
|
209
|
+
device.link.stop();
|
|
210
|
+
this.devices.delete(id);
|
|
211
|
+
}
|
|
156
212
|
|
|
157
|
-
|
|
158
|
-
|
|
213
|
+
/** Accessories of devices that are no longer in the settings leave Apple Home. */
|
|
214
|
+
#prune() {
|
|
215
|
+
const kept = new Set([...this.devices.values()].map((d) => d.accessory.UUID));
|
|
216
|
+
const stale = [...this.cached.values()].filter((a) => !kept.has(a.UUID));
|
|
159
217
|
if (stale.length) {
|
|
160
218
|
this.api.unregisterPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, stale);
|
|
161
219
|
stale.forEach((a) => this.cached.delete(a.UUID));
|
|
162
220
|
}
|
|
221
|
+
}
|
|
163
222
|
|
|
164
|
-
|
|
165
|
-
|
|
223
|
+
/**
|
|
224
|
+
* The settings changed while the bridge runs: only what changed is opened again. A device whose
|
|
225
|
+
* options are as before keeps its connection; a change of how devices are reached opens all anew.
|
|
226
|
+
* @param {object} config this platform's block as it is now in config.json
|
|
227
|
+
*/
|
|
228
|
+
apply(config) {
|
|
229
|
+
if (this.stopping) {
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
this.config = config ?? {};
|
|
233
|
+
const next = this.#plan();
|
|
234
|
+
const hubMark = [...next.plans.keys()].sort().join();
|
|
235
|
+
if (next.mode !== this.mode || (next.mode !== 'local' && hubMark !== this.hubMark)) {
|
|
236
|
+
this.log.info('The settings changed how the devices are reached: every device is opened again.');
|
|
237
|
+
for (const id of [...this.devices.keys()]) {
|
|
238
|
+
this.#close(id);
|
|
239
|
+
}
|
|
240
|
+
this.scanner?.stop();
|
|
241
|
+
this.hub?.stop();
|
|
242
|
+
this.scanner = null;
|
|
243
|
+
this.hub = null;
|
|
244
|
+
this.#begin(next);
|
|
245
|
+
this.#beat();
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
let changed = 0;
|
|
249
|
+
for (const id of [...this.devices.keys()]) {
|
|
250
|
+
if (!next.plans.has(id)) {
|
|
251
|
+
this.#close(id);
|
|
252
|
+
changed += 1;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
this.addresses = readJson(this.paths.addresses) ?? {};
|
|
256
|
+
for (const [id, plan] of next.plans) {
|
|
257
|
+
if (this.devices.get(id)?.mark !== plan.mark) {
|
|
258
|
+
this.#close(id);
|
|
259
|
+
this.#open(plan, changed * START_GAP_MS);
|
|
260
|
+
changed += 1;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
this.#prune();
|
|
264
|
+
if (changed) {
|
|
265
|
+
this.log.info(`The settings changed: ${changed} device${changed === 1 ? '' : 's'} updated without a restart.`);
|
|
266
|
+
}
|
|
166
267
|
this.#beat();
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** Looks at config.json and at what was brought from the account; a change is applied by itself. */
|
|
271
|
+
#watch() {
|
|
272
|
+
const configPath = this.overrides.configPath ?? this.api.user.configPath?.();
|
|
273
|
+
if (!configPath || this.overrides.watch === false) {
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
const mine = () => {
|
|
277
|
+
const all = readJson(configPath)?.platforms;
|
|
278
|
+
const blocks = (Array.isArray(all) ? all : []).filter((p) => p?.platform === PLATFORM_NAME);
|
|
279
|
+
return blocks.find((p) => p.name === this.config.name) ?? blocks[0] ?? null;
|
|
280
|
+
};
|
|
281
|
+
let seen = JSON.stringify(mine());
|
|
282
|
+
let wait = null;
|
|
283
|
+
this.onFile = () => {
|
|
284
|
+
clearTimeout(wait);
|
|
285
|
+
// A moment, so the page's two writes (the devices, then the settings) are read together.
|
|
286
|
+
wait = setTimeout(() => {
|
|
287
|
+
this.timers.delete(wait);
|
|
288
|
+
try {
|
|
289
|
+
const block = mine();
|
|
290
|
+
if (!block) {
|
|
291
|
+
return; // removed from the settings: Homebridge stops the plugin at its next start
|
|
292
|
+
}
|
|
293
|
+
seen = JSON.stringify(block);
|
|
294
|
+
this.apply(block);
|
|
295
|
+
} catch (err) {
|
|
296
|
+
this.log.warn(`The changed settings could not be applied (${err.message}); they are applied at the next restart.`);
|
|
297
|
+
}
|
|
298
|
+
}, 1500);
|
|
299
|
+
wait.unref?.();
|
|
300
|
+
this.timers.add(wait);
|
|
301
|
+
};
|
|
302
|
+
this.watched = [configPath, this.paths.devices];
|
|
303
|
+
for (const file of this.watched) {
|
|
304
|
+
fs.watchFile(file, { interval: 2000, persistent: false }, (now, before) => {
|
|
305
|
+
if (now.mtimeMs !== before.mtimeMs && (file !== configPath || JSON.stringify(mine()) !== seen)) {
|
|
306
|
+
this.onFile();
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
}
|
|
170
310
|
}
|
|
171
311
|
|
|
172
312
|
#category(kind) {
|
|
@@ -196,19 +336,22 @@ export class TuyaEasyPlatform {
|
|
|
196
336
|
const online = all.filter(([, d]) => d.link.online).map(([id]) => id);
|
|
197
337
|
const cloud = all.filter(([, d]) => d.link.online && d.link.via === 'cloud').map(([id]) => id);
|
|
198
338
|
const problems = Object.fromEntries(all.filter(([, d]) => !d.link.online && d.link.problem).map(([id, d]) => [id, d.link.problem]));
|
|
199
|
-
|
|
339
|
+
// "live" tells the settings page that this bridge applies changed settings by itself.
|
|
340
|
+
writeJson(this.paths.running, { at: Date.now(), live: true, online, cloud, problems });
|
|
200
341
|
} catch { /* best effort */ }
|
|
201
342
|
}
|
|
202
343
|
|
|
203
344
|
stop() {
|
|
204
345
|
this.stopping = true;
|
|
346
|
+
for (const file of this.watched) {
|
|
347
|
+
fs.unwatchFile(file);
|
|
348
|
+
}
|
|
205
349
|
for (const timer of this.timers) {
|
|
206
350
|
clearTimeout(timer);
|
|
207
351
|
clearInterval(timer);
|
|
208
352
|
}
|
|
209
|
-
for (const
|
|
210
|
-
|
|
211
|
-
link.stop();
|
|
353
|
+
for (const id of [...this.devices.keys()]) {
|
|
354
|
+
this.#close(id);
|
|
212
355
|
}
|
|
213
356
|
this.scanner?.stop();
|
|
214
357
|
this.hub?.stop();
|