Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion .copilot-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,19 @@ npm run build # Rebuild templates
4. Watch for changes: `npm run watch`
5. Test CLI locally: `npm link` then `create-adapter`

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.
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.

## Pull Request Requirements

When making a PR to this repository, ensure the following points are addressed:

- **Meaningful PR description**: Provide a clear description to this PR or mention which issues this fixes.
- **Project builds**: Ensure the project builds successfully with `npm run build`
- **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?).
- **Test suite passes**: Run the test suite with `npm test` and ensure all tests pass
- **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
- **Template creation updates**: If you added a required option, also add it to the template creation (`.github/create_templates.ts`)
- **Migration documentation**: Add a detailed migration description to `docs/updates` explaining what the user needs to do when manually updating an existing project
- **CHANGELOG.md updated**: Add your changes to CHANGELOG.md (referencing the migration description and this PR or the issue you fixed)

These requirements help maintain code quality, ensure proper testing coverage, and provide clear documentation for users upgrading existing projects.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
## __WORK IN PROGRESS__
(at the beginning of a new line)
-->
## __WORK IN PROGRESS__
* (copilot) Replace deprecated `setStateAsync` with `setState` in adapter templates (#1148, [Migration guide](./docs/updates/20250831_setstate_sync.md))

## 2.6.5 (2024-09-13)
* (AlCalzone) Update required versions of `js-controller` and `admin` to the current stable versions (#1116)
* (AlCalzone) Remove deprecated `main` and `title` fields from `io-package.json` (#1115)
Expand Down
34 changes: 34 additions & 0 deletions docs/updates/20250831_setstate_sync.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Replace deprecated setStateAsync with setState

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.

## What changed

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

## Manual migration for existing adapters

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

```diff
// Before (deprecated)
- await this.setStateAsync("testVariable", true);
- await this.setStateAsync("testVariable", { val: true, ack: true });
- await this.setStateAsync("testVariable", { val: true, ack: true, expire: 30 });

// After (current)
+ await this.setState("testVariable", true);
+ await this.setState("testVariable", { val: true, ack: true });
+ await this.setState("testVariable", { val: true, ack: true, expire: 30 });
```

## Why this change

The `setStateAsync` method was deprecated in favor of the simpler `setState` method:
- `setState` when called without a callback returns a Promise and should be awaited
- `setState` when called with a callback is synchronous
- Using `setState` aligns with current ioBroker adapter development best practices

## No functional changes

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.
6 changes: 3 additions & 3 deletions templates/main.js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ ${adapterSettings.map(s => `\t\tthis.log.info("config ${s.key}: " + this.config.
you will notice that each setState will cause the stateChange event to fire (because of above subscribeStates cmd)
*/
// the variable testVariable is set to true as command (ack=false)
await this.setStateAsync("testVariable", true);
await this.setState("testVariable", true);

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

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

// examples for the checkPassword/checkGroup functions
const pwdResult = await this.checkPasswordAsync("admin", "iobroker");
Expand Down
6 changes: 3 additions & 3 deletions templates/src/main.ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ ${adapterSettings.map(s => `\t\tthis.log.info("config ${s.key}: " + this.config.
you will notice that each setState will cause the stateChange event to fire (because of above subscribeStates cmd)
*/
// the variable testVariable is set to true as command (ack=false)
await this.setStateAsync("testVariable", true);
await this.setState("testVariable", true);

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

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

// examples for the checkPassword/checkGroup functions
const pwdResult = await this.checkPasswordAsync("admin", "iobroker");
Expand Down
6 changes: 3 additions & 3 deletions test/baselines/TS_SingleQuotes/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ class TestAdapter extends utils.Adapter {
you will notice that each setState will cause the stateChange event to fire (because of above subscribeStates cmd)
*/
// the variable testVariable is set to true as command (ack=false)
await this.setStateAsync('testVariable', true);
await this.setState('testVariable', true);

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

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

// examples for the checkPassword/checkGroup functions
const pwdResult = await this.checkPasswordAsync('admin', 'iobroker');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ class TestAdapter extends utils.Adapter {
you will notice that each setState will cause the stateChange event to fire (because of above subscribeStates cmd)
*/
// the variable testVariable is set to true as command (ack=false)
await this.setStateAsync('testVariable', true);
await this.setState('testVariable', true);

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

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

// examples for the checkPassword/checkGroup functions
const pwdResult = await this.checkPasswordAsync('admin', 'iobroker');
Expand Down
6 changes: 3 additions & 3 deletions test/baselines/adapter_JS_React/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ class TestAdapter extends utils.Adapter {
you will notice that each setState will cause the stateChange event to fire (because of above subscribeStates cmd)
*/
// the variable testVariable is set to true as command (ack=false)
await this.setStateAsync("testVariable", true);
await this.setState("testVariable", true);

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

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

// examples for the checkPassword/checkGroup functions
const pwdResult = await this.checkPasswordAsync("admin", "iobroker");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ class TestAdapter extends utils.Adapter {
you will notice that each setState will cause the stateChange event to fire (because of above subscribeStates cmd)
*/
// the variable testVariable is set to true as command (ack=false)
await this.setStateAsync("testVariable", true);
await this.setState("testVariable", true);

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

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

// examples for the checkPassword/checkGroup functions
const pwdResult = await this.checkPasswordAsync("admin", "iobroker");
Expand Down
6 changes: 3 additions & 3 deletions test/baselines/adapter_TS_React/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ class TestAdapter extends utils.Adapter {
you will notice that each setState will cause the stateChange event to fire (because of above subscribeStates cmd)
*/
// the variable testVariable is set to true as command (ack=false)
await this.setStateAsync("testVariable", true);
await this.setState("testVariable", true);

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

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

// examples for the checkPassword/checkGroup functions
const pwdResult = await this.checkPasswordAsync("admin", "iobroker");
Expand Down
6 changes: 3 additions & 3 deletions test/baselines/connectionIndicator_yes/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ class TestAdapter extends utils.Adapter {
you will notice that each setState will cause the stateChange event to fire (because of above subscribeStates cmd)
*/
// the variable testVariable is set to true as command (ack=false)
await this.setStateAsync("testVariable", true);
await this.setState("testVariable", true);

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

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

// examples for the checkPassword/checkGroup functions
const pwdResult = await this.checkPasswordAsync("admin", "iobroker");
Expand Down
6 changes: 3 additions & 3 deletions test/baselines/customAdapterSettings/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ class TestAdapter extends utils.Adapter {
you will notice that each setState will cause the stateChange event to fire (because of above subscribeStates cmd)
*/
// the variable testVariable is set to true as command (ack=false)
await this.setStateAsync("testVariable", true);
await this.setState("testVariable", true);

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

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

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