Skip to content

Commit 4bba48c

Browse files
CopilotApollon77
andauthored
Replace deprecated setStateAsync with setState in adapter templates and update copilot instructions (#1152)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Apollon77 <11976694+Apollon77@users.noreply.github.com> Co-authored-by: Ingo Fischer <github@fischer-ka.de>
1 parent ec8eaf8 commit 4bba48c

12 files changed

Lines changed: 80 additions & 28 deletions

File tree

.copilot-instructions.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,19 @@ npm run build # Rebuild templates
144144
4. Watch for changes: `npm run watch`
145145
5. Test CLI locally: `npm link` then `create-adapter`
146146

147-
This tool is actively maintained and used by the ioBroker community to create hundreds of adapters. Changes should be thoroughly tested and maintain backward compatibility.
147+
This tool is actively maintained and used by the ioBroker community to create hundreds of adapters. Changes should be thoroughly tested and maintain backward compatibility.
148+
149+
## Pull Request Requirements
150+
151+
When making a PR to this repository, ensure the following points are addressed:
152+
153+
- **Meaningful PR description**: Provide a clear description to this PR or mention which issues this fixes.
154+
- **Project builds**: Ensure the project builds successfully with `npm run build`
155+
- **Tests included**: Add tests for your change. This includes negative tests (i.e. inputs that need to fail) as well as baseline tests (i.e. how should the directory structure look like?).
156+
- **Test suite passes**: Run the test suite with `npm test` and ensure all tests pass
157+
- **Baseline changes**: If there are baseline changes, review them and make a separate commit for them with the comment "accept baselines" if they are desired changes
158+
- **Template creation updates**: If you added a required option, also add it to the template creation (`.github/create_templates.ts`)
159+
- **Migration documentation**: Add a detailed migration description to `docs/updates` explaining what the user needs to do when manually updating an existing project
160+
- **CHANGELOG.md updated**: Add your changes to CHANGELOG.md (referencing the migration description and this PR or the issue you fixed)
161+
162+
These requirements help maintain code quality, ensure proper testing coverage, and provide clear documentation for users upgrading existing projects.

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
## __WORK IN PROGRESS__
66
(at the beginning of a new line)
77
-->
8+
## __WORK IN PROGRESS__
9+
* (copilot) Replace deprecated `setStateAsync` with `setState` in adapter templates (#1148, [Migration guide](./docs/updates/20250831_setstate_sync.md))
10+
811
## 2.6.5 (2024-09-13)
912
* (AlCalzone) Update required versions of `js-controller` and `admin` to the current stable versions (#1116)
1013
* (AlCalzone) Remove deprecated `main` and `title` fields from `io-package.json` (#1115)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Replace deprecated setStateAsync with setState
2+
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.
4+
5+
## What changed
6+
7+
The adapter template generation was updated to use `await this.setState()` instead of `await this.setStateAsync()` for setting states.
8+
9+
## Manual migration for existing adapters
10+
11+
If you have an existing adapter that uses `setStateAsync`, you should replace all occurrences with the `setState` method:
12+
13+
```diff
14+
// Before (deprecated)
15+
- await this.setStateAsync("testVariable", true);
16+
- await this.setStateAsync("testVariable", { val: true, ack: true });
17+
- await this.setStateAsync("testVariable", { val: true, ack: true, expire: 30 });
18+
19+
// After (current)
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 });
23+
```
24+
25+
## Why this change
26+
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
31+
32+
## No functional changes
33+
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-
await this.setStateAsync("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-
await this.setStateAsync("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-
await this.setStateAsync("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-
await this.setStateAsync("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-
await this.setStateAsync("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-
await this.setStateAsync("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-
await this.setStateAsync('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-
await this.setStateAsync('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-
await this.setStateAsync('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-
await this.setStateAsync('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-
await this.setStateAsync('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-
await this.setStateAsync('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-
await this.setStateAsync("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-
await this.setStateAsync("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-
await this.setStateAsync("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-
await this.setStateAsync("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-
await this.setStateAsync("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-
await this.setStateAsync("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-
await this.setStateAsync("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-
await this.setStateAsync("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-
await this.setStateAsync("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)