Skip to content

Commit 8cb4225

Browse files
CopilotApollon77
andcommitted
Fix setState calls to be properly awaited - setState returns Promise when called without callback
Co-authored-by: Apollon77 <11976694+Apollon77@users.noreply.github.com>
1 parent 126a843 commit 8cb4225

10 files changed

Lines changed: 38 additions & 38 deletions

File tree

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Replace deprecated setStateAsync with setState
22

3-
The `setStateAsync` method has been deprecated in favor of the synchronous `setState` method. Newly created adapters will now use the current recommended API. If you have an existing adapter using `setStateAsync`, you should update it to use `setState` instead.
3+
The `setStateAsync` method has been deprecated in favor of the `setState` method. Newly created adapters will now use the current recommended API. If you have an existing adapter using `setStateAsync`, you should update it to use `setState` instead.
44

55
## What changed
66

7-
The adapter template generation was updated to use `this.setState()` instead of `await this.setStateAsync()` for setting states.
7+
The adapter template generation was updated to use `await this.setState()` instead of `await this.setStateAsync()` for setting states.
88

99
## Manual migration for existing adapters
1010

11-
If you have an existing adapter that uses `setStateAsync`, you should replace all occurrences with the synchronous `setState` method:
11+
If you have an existing adapter that uses `setStateAsync`, you should replace all occurrences with the `setState` method:
1212

1313
```diff
1414
// Before (deprecated)
@@ -17,18 +17,18 @@ If you have an existing adapter that uses `setStateAsync`, you should replace al
1717
- await this.setStateAsync("testVariable", { val: true, ack: true, expire: 30 });
1818

1919
// After (current)
20-
+ this.setState("testVariable", true);
21-
+ this.setState("testVariable", { val: true, ack: true });
22-
+ this.setState("testVariable", { val: true, ack: true, expire: 30 });
20+
+ await this.setState("testVariable", true);
21+
+ await this.setState("testVariable", { val: true, ack: true });
22+
+ await this.setState("testVariable", { val: true, ack: true, expire: 30 });
2323
```
2424

2525
## Why this change
2626

27-
The `setStateAsync` method was deprecated because:
28-
- `setState` is now synchronous and doesn't return a Promise
29-
- Using the synchronous version is simpler and more efficient
30-
- It aligns with current ioBroker adapter development best practices
27+
The `setStateAsync` method was deprecated in favor of the simpler `setState` method:
28+
- `setState` when called without a callback returns a Promise and should be awaited
29+
- `setState` when called with a callback is synchronous
30+
- Using `setState` aligns with current ioBroker adapter development best practices
3131

3232
## No functional changes
3333

34-
This change does not affect the functionality of your adapter - both methods perform the same operation. The only difference is that `setState` is synchronous and doesn't need to be awaited.
34+
This change does not affect the functionality of your adapter - both methods perform the same operation. The only difference is using the current recommended API instead of the deprecated one.

templates/main.js.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,14 @@ ${adapterSettings.map(s => `\t\tthis.log.info("config ${s.key}: " + this.config.
8787
you will notice that each setState will cause the stateChange event to fire (because of above subscribeStates cmd)
8888
*/
8989
// the variable testVariable is set to true as command (ack=false)
90-
this.setState("testVariable", true);
90+
await this.setState("testVariable", true);
9191
9292
// same thing, but the value is flagged "ack"
9393
// ack should be always set to true if the value is received from or acknowledged from the target system
94-
this.setState("testVariable", { val: true, ack: true });
94+
await this.setState("testVariable", { val: true, ack: true });
9595
9696
// same thing, but the state is deleted after 30s (getState will return null afterwards)
97-
this.setState("testVariable", { val: true, ack: true, expire: 30 });
97+
await this.setState("testVariable", { val: true, ack: true, expire: 30 });
9898
9999
// examples for the checkPassword/checkGroup functions
100100
const pwdResult = await this.checkPasswordAsync("admin", "iobroker");

templates/src/main.ts.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ ${adapterSettings.map(s => `\t\tthis.log.info("config ${s.key}: " + this.config.
8383
you will notice that each setState will cause the stateChange event to fire (because of above subscribeStates cmd)
8484
*/
8585
// the variable testVariable is set to true as command (ack=false)
86-
this.setState("testVariable", true);
86+
await this.setState("testVariable", true);
8787
8888
// same thing, but the value is flagged "ack"
8989
// ack should be always set to true if the value is received from or acknowledged from the target system
90-
this.setState("testVariable", { val: true, ack: true });
90+
await this.setState("testVariable", { val: true, ack: true });
9191
9292
// same thing, but the state is deleted after 30s (getState will return null afterwards)
93-
this.setState("testVariable", { val: true, ack: true, expire: 30 });
93+
await this.setState("testVariable", { val: true, ack: true, expire: 30 });
9494
9595
// examples for the checkPassword/checkGroup functions
9696
const pwdResult = await this.checkPasswordAsync("admin", "iobroker");

test/baselines/TS_SingleQuotes/src/main.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ class TestAdapter extends utils.Adapter {
6363
you will notice that each setState will cause the stateChange event to fire (because of above subscribeStates cmd)
6464
*/
6565
// the variable testVariable is set to true as command (ack=false)
66-
this.setState('testVariable', true);
66+
await this.setState('testVariable', true);
6767

6868
// same thing, but the value is flagged "ack"
6969
// ack should be always set to true if the value is received from or acknowledged from the target system
70-
this.setState('testVariable', { val: true, ack: true });
70+
await this.setState('testVariable', { val: true, ack: true });
7171

7272
// same thing, but the state is deleted after 30s (getState will return null afterwards)
73-
this.setState('testVariable', { val: true, ack: true, expire: 30 });
73+
await this.setState('testVariable', { val: true, ack: true, expire: 30 });
7474

7575
// examples for the checkPassword/checkGroup functions
7676
const pwdResult = await this.checkPasswordAsync('admin', 'iobroker');

test/baselines/adapter_JS_JsonUI_ESLint_TypeChecking_Spaces_SingleQuotes_Apache-2.0/main.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ class TestAdapter extends utils.Adapter {
6868
you will notice that each setState will cause the stateChange event to fire (because of above subscribeStates cmd)
6969
*/
7070
// the variable testVariable is set to true as command (ack=false)
71-
this.setState('testVariable', true);
71+
await this.setState('testVariable', true);
7272

7373
// same thing, but the value is flagged "ack"
7474
// ack should be always set to true if the value is received from or acknowledged from the target system
75-
this.setState('testVariable', { val: true, ack: true });
75+
await this.setState('testVariable', { val: true, ack: true });
7676

7777
// same thing, but the state is deleted after 30s (getState will return null afterwards)
78-
this.setState('testVariable', { val: true, ack: true, expire: 30 });
78+
await this.setState('testVariable', { val: true, ack: true, expire: 30 });
7979

8080
// examples for the checkPassword/checkGroup functions
8181
const pwdResult = await this.checkPasswordAsync('admin', 'iobroker');

test/baselines/adapter_JS_React/main.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ class TestAdapter extends utils.Adapter {
6868
you will notice that each setState will cause the stateChange event to fire (because of above subscribeStates cmd)
6969
*/
7070
// the variable testVariable is set to true as command (ack=false)
71-
this.setState("testVariable", true);
71+
await this.setState("testVariable", true);
7272

7373
// same thing, but the value is flagged "ack"
7474
// ack should be always set to true if the value is received from or acknowledged from the target system
75-
this.setState("testVariable", { val: true, ack: true });
75+
await this.setState("testVariable", { val: true, ack: true });
7676

7777
// same thing, but the state is deleted after 30s (getState will return null afterwards)
78-
this.setState("testVariable", { val: true, ack: true, expire: 30 });
78+
await this.setState("testVariable", { val: true, ack: true, expire: 30 });
7979

8080
// examples for the checkPassword/checkGroup functions
8181
const pwdResult = await this.checkPasswordAsync("admin", "iobroker");

test/baselines/adapter_TS_ESLint_Tabs_DoubleQuotes_MIT/src/main.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ class TestAdapter extends utils.Adapter {
6363
you will notice that each setState will cause the stateChange event to fire (because of above subscribeStates cmd)
6464
*/
6565
// the variable testVariable is set to true as command (ack=false)
66-
this.setState("testVariable", true);
66+
await this.setState("testVariable", true);
6767

6868
// same thing, but the value is flagged "ack"
6969
// ack should be always set to true if the value is received from or acknowledged from the target system
70-
this.setState("testVariable", { val: true, ack: true });
70+
await this.setState("testVariable", { val: true, ack: true });
7171

7272
// same thing, but the state is deleted after 30s (getState will return null afterwards)
73-
this.setState("testVariable", { val: true, ack: true, expire: 30 });
73+
await this.setState("testVariable", { val: true, ack: true, expire: 30 });
7474

7575
// examples for the checkPassword/checkGroup functions
7676
const pwdResult = await this.checkPasswordAsync("admin", "iobroker");

test/baselines/adapter_TS_React/src/main.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ class TestAdapter extends utils.Adapter {
6363
you will notice that each setState will cause the stateChange event to fire (because of above subscribeStates cmd)
6464
*/
6565
// the variable testVariable is set to true as command (ack=false)
66-
this.setState("testVariable", true);
66+
await this.setState("testVariable", true);
6767

6868
// same thing, but the value is flagged "ack"
6969
// ack should be always set to true if the value is received from or acknowledged from the target system
70-
this.setState("testVariable", { val: true, ack: true });
70+
await this.setState("testVariable", { val: true, ack: true });
7171

7272
// same thing, but the state is deleted after 30s (getState will return null afterwards)
73-
this.setState("testVariable", { val: true, ack: true, expire: 30 });
73+
await this.setState("testVariable", { val: true, ack: true, expire: 30 });
7474

7575
// examples for the checkPassword/checkGroup functions
7676
const pwdResult = await this.checkPasswordAsync("admin", "iobroker");

test/baselines/connectionIndicator_yes/src/main.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ class TestAdapter extends utils.Adapter {
6666
you will notice that each setState will cause the stateChange event to fire (because of above subscribeStates cmd)
6767
*/
6868
// the variable testVariable is set to true as command (ack=false)
69-
this.setState("testVariable", true);
69+
await this.setState("testVariable", true);
7070

7171
// same thing, but the value is flagged "ack"
7272
// ack should be always set to true if the value is received from or acknowledged from the target system
73-
this.setState("testVariable", { val: true, ack: true });
73+
await this.setState("testVariable", { val: true, ack: true });
7474

7575
// same thing, but the state is deleted after 30s (getState will return null afterwards)
76-
this.setState("testVariable", { val: true, ack: true, expire: 30 });
76+
await this.setState("testVariable", { val: true, ack: true, expire: 30 });
7777

7878
// examples for the checkPassword/checkGroup functions
7979
const pwdResult = await this.checkPasswordAsync("admin", "iobroker");

test/baselines/customAdapterSettings/src/main.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ class TestAdapter extends utils.Adapter {
6363
you will notice that each setState will cause the stateChange event to fire (because of above subscribeStates cmd)
6464
*/
6565
// the variable testVariable is set to true as command (ack=false)
66-
this.setState("testVariable", true);
66+
await this.setState("testVariable", true);
6767

6868
// same thing, but the value is flagged "ack"
6969
// ack should be always set to true if the value is received from or acknowledged from the target system
70-
this.setState("testVariable", { val: true, ack: true });
70+
await this.setState("testVariable", { val: true, ack: true });
7171

7272
// same thing, but the state is deleted after 30s (getState will return null afterwards)
73-
this.setState("testVariable", { val: true, ack: true, expire: 30 });
73+
await this.setState("testVariable", { val: true, ack: true, expire: 30 });
7474

7575
// examples for the checkPassword/checkGroup functions
7676
const pwdResult = await this.checkPasswordAsync("admin", "iobroker");

0 commit comments

Comments
 (0)