iobroker.sonoff 2.5.3 → 3.0.3

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/lib/tools.js CHANGED
@@ -1,99 +1,99 @@
1
- const axios = require('axios').default;
2
-
3
- /**
4
- * Tests whether the given variable is a real object and not an Array
5
- * @param {any} it The variable to test
6
- * @returns {it is Record<string, any>}
7
- */
8
- function isObject(it) {
9
- // This is necessary because:
10
- // typeof null === 'object'
11
- // typeof [] === 'object'
12
- // [] instanceof Object === true
13
- return Object.prototype.toString.call(it) === '[object Object]';
14
- }
15
-
16
- /**
17
- * Tests whether the given variable is really an Array
18
- * @param {any} it The variable to test
19
- * @returns {it is any[]}
20
- */
21
- function isArray(it) {
22
- if (typeof Array.isArray === 'function') return Array.isArray(it);
23
- return Object.prototype.toString.call(it) === '[object Array]';
24
- }
25
-
26
- /**
27
- * Translates text to the target language. Automatically chooses the right translation API.
28
- * @param {string} text The text to translate
29
- * @param {string} targetLang The target languate
30
- * @param {string} [yandexApiKey] The yandex API key. You can create one for free at https://translate.yandex.com/developers
31
- * @returns {Promise<string>}
32
- */
33
- async function translateText(text, targetLang, yandexApiKey) {
34
- if (targetLang === 'en') {
35
- return text;
36
- } else if (!text) {
37
- return '';
38
- }
39
- if (yandexApiKey) {
40
- return translateYandex(text, targetLang, yandexApiKey);
41
- } else {
42
- return translateGoogle(text, targetLang);
43
- }
44
- }
45
-
46
- /**
47
- * Translates text with Yandex API
48
- * @param {string} text The text to translate
49
- * @param {string} targetLang The target languate
50
- * @param {string} apiKey The yandex API key. You can create one for free at https://translate.yandex.com/developers
51
- * @returns {Promise<string>}
52
- */
53
- async function translateYandex(text, targetLang, apiKey) {
54
- if (targetLang === 'zh-cn') {
55
- targetLang = 'zh';
56
- }
57
- try {
58
- const url = `https://translate.yandex.net/api/v1.5/tr.json/translate?key=${apiKey}&text=${encodeURIComponent(text)}&lang=en-${targetLang}`;
59
- const response = await axios({url, timeout: 15000});
60
- if (response.data && response.data.text && isArray(response.data.text)) {
61
- return response.data.text[0];
62
- }
63
- throw new Error('Invalid response for translate request');
64
- } catch (e) {
65
- throw new Error(`Could not translate to "${targetLang}": ${e}`);
66
- }
67
- }
68
-
69
- /**
70
- * Translates text with Google API
71
- * @param {string} text The text to translate
72
- * @param {string} targetLang The target languate
73
- * @returns {Promise<string>}
74
- */
75
- async function translateGoogle(text, targetLang) {
76
- try {
77
- const url = `http://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=${targetLang}&dt=t&q=${encodeURIComponent(text)}&ie=UTF-8&oe=UTF-8`;
78
- const response = await axios({url, timeout: 15000});
79
- if (isArray(response.data)) {
80
- // we got a valid response
81
- return response.data[0][0][0];
82
- }
83
- throw new Error('Invalid response for translate request');
84
- } catch (e) {
85
- if (e.response && e.response.status === 429) {
86
- throw new Error(
87
- `Could not translate to "${targetLang}": Rate-limited by Google Translate`
88
- );
89
- } else {
90
- throw new Error(`Could not translate to "${targetLang}": ${e}`);
91
- }
92
- }
93
- }
94
-
95
- module.exports = {
96
- isArray,
97
- isObject,
98
- translateText
99
- };
1
+ const axios = require('axios').default;
2
+
3
+ /**
4
+ * Tests whether the given variable is a real object and not an Array
5
+ * @param {any} it The variable to test
6
+ * @returns {it is Record<string, any>}
7
+ */
8
+ function isObject(it) {
9
+ // This is necessary because:
10
+ // typeof null === 'object'
11
+ // typeof [] === 'object'
12
+ // [] instanceof Object === true
13
+ return Object.prototype.toString.call(it) === '[object Object]';
14
+ }
15
+
16
+ /**
17
+ * Tests whether the given variable is really an Array
18
+ * @param {any} it The variable to test
19
+ * @returns {it is any[]}
20
+ */
21
+ function isArray(it) {
22
+ if (typeof Array.isArray === 'function') return Array.isArray(it);
23
+ return Object.prototype.toString.call(it) === '[object Array]';
24
+ }
25
+
26
+ /**
27
+ * Translates text to the target language. Automatically chooses the right translation API.
28
+ * @param {string} text The text to translate
29
+ * @param {string} targetLang The target languate
30
+ * @param {string} [yandexApiKey] The yandex API key. You can create one for free at https://translate.yandex.com/developers
31
+ * @returns {Promise<string>}
32
+ */
33
+ async function translateText(text, targetLang, yandexApiKey) {
34
+ if (targetLang === 'en') {
35
+ return text;
36
+ } else if (!text) {
37
+ return '';
38
+ }
39
+ if (yandexApiKey) {
40
+ return translateYandex(text, targetLang, yandexApiKey);
41
+ } else {
42
+ return translateGoogle(text, targetLang);
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Translates text with Yandex API
48
+ * @param {string} text The text to translate
49
+ * @param {string} targetLang The target languate
50
+ * @param {string} apiKey The yandex API key. You can create one for free at https://translate.yandex.com/developers
51
+ * @returns {Promise<string>}
52
+ */
53
+ async function translateYandex(text, targetLang, apiKey) {
54
+ if (targetLang === 'zh-cn') {
55
+ targetLang = 'zh';
56
+ }
57
+ try {
58
+ const url = `https://translate.yandex.net/api/v1.5/tr.json/translate?key=${apiKey}&text=${encodeURIComponent(text)}&lang=en-${targetLang}`;
59
+ const response = await axios({url, timeout: 15000});
60
+ if (response.data && response.data.text && isArray(response.data.text)) {
61
+ return response.data.text[0];
62
+ }
63
+ throw new Error('Invalid response for translate request');
64
+ } catch (e) {
65
+ throw new Error(`Could not translate to "${targetLang}": ${e}`);
66
+ }
67
+ }
68
+
69
+ /**
70
+ * Translates text with Google API
71
+ * @param {string} text The text to translate
72
+ * @param {string} targetLang The target languate
73
+ * @returns {Promise<string>}
74
+ */
75
+ async function translateGoogle(text, targetLang) {
76
+ try {
77
+ const url = `http://translate.googleapis.com/translate_a/single?client=gtx&sl=en&tl=${targetLang}&dt=t&q=${encodeURIComponent(text)}&ie=UTF-8&oe=UTF-8`;
78
+ const response = await axios({url, timeout: 15000});
79
+ if (isArray(response.data)) {
80
+ // we got a valid response
81
+ return response.data[0][0][0];
82
+ }
83
+ throw new Error('Invalid response for translate request');
84
+ } catch (e) {
85
+ if (e.response && e.response.status === 429) {
86
+ throw new Error(
87
+ `Could not translate to "${targetLang}": Rate-limited by Google Translate`
88
+ );
89
+ } else {
90
+ throw new Error(`Could not translate to "${targetLang}": ${e}`);
91
+ }
92
+ }
93
+ }
94
+
95
+ module.exports = {
96
+ isArray,
97
+ isObject,
98
+ translateText
99
+ };
package/main.js CHANGED
@@ -1,93 +1,101 @@
1
- /* jshint -W097 */
2
- /* jshint strict:true */
3
- /* jslint node: true */
4
- /* jslint esversion: 6 */
5
-
6
- /**
7
- *
8
- * ioBroker sonoff Adapter
9
- *
10
- * (c) 2017-2023 bluefox
11
- *
12
- * MIT License
13
- *
14
- */
15
- 'use strict';
16
-
17
- const utils = require('@iobroker/adapter-core'); // Get common adapter utils
18
- const adapterName = require('./package.json').name.split('.').pop();
19
- const Server = require('./lib/server');
20
- let server = null;
21
- let adapter;
22
-
23
- function decrypt(key, value) {
24
- let result = '';
25
- for (let i = 0; i < value.length; ++i) {
26
- result += String.fromCharCode(key[i % key.length].charCodeAt(0) ^ value.charCodeAt(i));
27
- }
28
- return result;
29
- }
30
-
31
- function startAdapter(options) {
32
- options = options || {};
33
- Object.assign(options, {name: adapterName});
34
-
35
- adapter = new utils.Adapter(options);
36
-
37
- adapter.on('ready', () => {
38
- // it must be like this
39
-
40
- adapter.getForeignObject('system.config', (err, obj) => {
41
- if (obj && obj.native && obj.native.secret) {
42
- //noinspection JSUnresolvedVariable
43
- adapter.config.pass = decrypt(obj.native.secret, adapter.config.pass);
44
- } else {
45
- //noinspection JSUnresolvedVariable
46
- adapter.config.pass = decrypt('Zgfr56gFe87jJOM', adapter.config.pass);
47
- }
48
- main();
49
- });
50
- });
51
-
52
- adapter.on('unload', cb => {
53
- if (server) {
54
- server.destroy(cb);
55
- server = null;
56
- } else if (typeof cb === 'function') {
57
- cb();
58
- }
59
- });
60
-
61
- // is called if a subscribed state changes
62
- adapter.on('stateChange', (id, state) => {
63
- adapter.log.debug(`stateChange ${id}: ${JSON.stringify(state)}`);
64
- // you can use the ack flag to detect if state is desired or acknowledged
65
- state && !state.ack && server && server.onStateChange(id, state);
66
- });
67
- return adapter;
68
- }
69
-
70
- function main() {
71
- adapter.config.TELE_SENSOR = adapter.config.TELE_SENSOR === true || adapter.config.TELE_SENSOR === 'true';
72
- adapter.config.TELE_STATE = adapter.config.TELE_STATE === true || adapter.config.TELE_STATE === 'true';
73
- adapter.config.STAT_RESULT = adapter.config.STAT_RESULT === true || adapter.config.STAT_RESULT === 'true';
74
- adapter.config.OBJ_TREE = adapter.config.OBJ_TREE === true || adapter.config.OBJ_TREE === 'true';
75
-
76
- // subscribe for all own variables
77
- adapter.subscribeStates('*');
78
-
79
- // read all states and set alive to false
80
- adapter.getStatesOf('', '', (err, states) =>
81
- states && states.length && states.forEach(state =>
82
- state._id.match(/\.alive$/) && adapter.setForeignState(state._id, false, true)));
83
-
84
- server = new Server(adapter);
85
- }
86
-
87
- // If started as allInOne/compact mode => return function to create instance
88
- if (module && module.parent) {
89
- module.exports = startAdapter;
90
- } else {
91
- // or start the instance directly
92
- startAdapter();
93
- }
1
+ /**
2
+ *
3
+ * ioBroker sonoff Adapter
4
+ *
5
+ * (c) 2017-2023 bluefox
6
+ *
7
+ * MIT License
8
+ *
9
+ */
10
+ 'use strict';
11
+
12
+ const utils = require('@iobroker/adapter-core'); // Get common adapter utils
13
+ const adapterName = require('./package.json').name.split('.').pop();
14
+ const Server = require('./lib/server');
15
+ let server = null;
16
+ let adapter;
17
+
18
+ function decrypt(key, value) {
19
+ let result = '';
20
+ for (let i = 0; i < value.length; ++i) {
21
+ result += String.fromCharCode(key[i % key.length].charCodeAt(0) ^ value.charCodeAt(i));
22
+ }
23
+ return result;
24
+ }
25
+
26
+ function startAdapter(options) {
27
+ options = options || {};
28
+ Object.assign(options, { name: adapterName });
29
+
30
+ adapter = new utils.Adapter(options);
31
+
32
+ adapter.on('ready', () => {
33
+ // delete pass if a password set
34
+ if (adapter.config.password && adapter.config.pass) {
35
+ // delete adapter.config.pass
36
+ adapter.getForeignObject(`system.adapter.${adapter.namespace}`, (err, config) => {
37
+ delete config.native.pass;
38
+ adapter.setForeignObject(config._id, config);
39
+ });
40
+ } else if (!adapter.config.password && adapter.config.pass) {
41
+ // migrate
42
+ adapter.getForeignObject('system.config', (err, systemConfig) => {
43
+ adapter.getForeignObject(`system.adapter.${adapter.namespace}`, (err, config) => {
44
+ if (systemConfig && systemConfig.native && systemConfig.native.secret) {
45
+ // noinspection JSUnresolvedVariable
46
+ config.native.password = decrypt(systemConfig.native.secret, adapter.config.pass);
47
+ } else {
48
+ // noinspection JSUnresolvedVariable
49
+ config.native.password = decrypt('Zgfr56gFe87jJOM', adapter.config.pass);
50
+ }
51
+ delete config.native.pass;
52
+ adapter.setForeignObject(config._id, config);
53
+ });
54
+ });
55
+ } else {
56
+ main();
57
+ }
58
+ });
59
+
60
+ adapter.on('unload', cb => {
61
+ if (server) {
62
+ server.destroy(cb);
63
+ server = null;
64
+ } else if (typeof cb === 'function') {
65
+ cb();
66
+ }
67
+ });
68
+
69
+ // called if the subscribed state changes itself
70
+ adapter.on('stateChange', (id, state) => {
71
+ adapter.log.debug(`stateChange ${id}: ${JSON.stringify(state)}`);
72
+ // you can use the ack flag to detect if state is desired or acknowledged
73
+ state && !state.ack && server && server.onStateChange(id, state);
74
+ });
75
+ return adapter;
76
+ }
77
+
78
+ function main() {
79
+ adapter.config.TELE_SENSOR = adapter.config.TELE_SENSOR === true || adapter.config.TELE_SENSOR === 'true';
80
+ adapter.config.TELE_STATE = adapter.config.TELE_STATE === true || adapter.config.TELE_STATE === 'true';
81
+ adapter.config.STAT_RESULT = adapter.config.STAT_RESULT === true || adapter.config.STAT_RESULT === 'true';
82
+ adapter.config.OBJ_TREE = adapter.config.OBJ_TREE === true || adapter.config.OBJ_TREE === 'true';
83
+
84
+ // subscribe for all own variables
85
+ adapter.subscribeStates('*');
86
+
87
+ // read all states and set alive to false
88
+ adapter.getStatesOf('', '', (err, states) =>
89
+ states && states.length && states.forEach(state =>
90
+ state._id.match(/\.alive$/) && adapter.setForeignState(state._id, false, true)));
91
+
92
+ server = new Server(adapter);
93
+ }
94
+
95
+ // If started as allInOne/compact mode => return function to create instance
96
+ if (module && module.parent) {
97
+ module.exports = startAdapter;
98
+ } else {
99
+ // or start the instance directly
100
+ startAdapter();
101
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "iobroker.sonoff",
3
3
  "description": "The adapter allows to send and receive MQTT messages from ioBroker and to be a broker.",
4
- "version": "2.5.3",
4
+ "version": "3.0.3",
5
5
  "author": "bluefox <dogafox@gmail.com>",
6
6
  "homepage": "https://github.com/ioBroker/ioBroker.sonoff",
7
7
  "repository": {
@@ -17,18 +17,18 @@
17
17
  ],
18
18
  "dependencies": {
19
19
  "mqtt-connection": "^4.1.0",
20
- "@iobroker/adapter-core": "^2.6.8"
20
+ "@iobroker/adapter-core": "^3.0.3"
21
21
  },
22
22
  "devDependencies": {
23
- "@alcalzone/release-script": "^3.5.9",
24
- "@alcalzone/release-script-plugin-iobroker": "^3.5.9",
23
+ "@alcalzone/release-script": "^3.6.0",
24
+ "@alcalzone/release-script-plugin-iobroker": "^3.6.0",
25
25
  "@alcalzone/release-script-plugin-license": "^3.5.9",
26
- "@iobroker/legacy-testing": "^0.1.1",
27
- "axios": "^1.3.4",
26
+ "@iobroker/legacy-testing": "^1.0.0",
27
+ "axios": "^1.5.0",
28
28
  "mqtt": "^4.3.7",
29
29
  "gulp": "^4.0.2",
30
30
  "mocha": "^10.2.0",
31
- "chai": "^4.3.7"
31
+ "chai": "^4.3.8"
32
32
  },
33
33
  "bugs": {
34
34
  "url": "https://github.com/ioBroker/ioBroker.sonoff/issues"
@@ -46,7 +46,8 @@
46
46
  "release": "release-script",
47
47
  "release-patch": "release-script patch --yes",
48
48
  "release-minor": "release-script minor --yes",
49
- "release-major": "release-script major --yes"
49
+ "release-major": "release-script major --yes",
50
+ "update-packages": "ncu --upgrade"
50
51
  },
51
52
  "engines": {
52
53
  "node": ">=4.0.0"