Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow sending of raw json commands to wled #716

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ When the adapter crashes or another Code error happens, this error message that
Placeholder for the next version (at the beginning of the line):
### __WORK IN PROGRESS__
-->
### __WORK IN PROGRESS__
* (ticaki) allow sending of raw json from state

### 0.7.3 (2024-10-26)
* (HaggardFFM) allow write on segments, solves #688 #706
* (DutchmanNL) Fixed error when a device is not available Solves #698
Expand Down
8 changes: 7 additions & 1 deletion lib/stateAttr.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ const state_attrb = {
name: 'Unknown',
type: 'number',
},
'm12': {
'm12': {
name: 'Unknown',
type: 'number',
//Setting of segment field 'Expand 1D FX'. (0: Pixels, 1: Bar, 2: Arc, 3: Corner)
Expand Down Expand Up @@ -484,6 +484,12 @@ const state_attrb = {
//Bitfield for broadcast receive groups 1-8
type: 'number',
},
'action': {
name: 'Command Json',
type: 'string',
role: 'json',
write: true,
}
};

module.exports = state_attrb;
18 changes: 14 additions & 4 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,19 @@ class Wled extends utils.Adapter {
// Build send command for state changes
if (deviceId[4] === undefined) {
this.log.debug('Send state');
values = {
[deviceId[3]]: state.val
};
if (deviceId[3] === 'action') {
try {
if (typeof state.val !== 'string') throw new Error('State value is not a string');
values = JSON.parse(state.val);
} catch (e) {
this.log.error(`State ${id} is not a valid JSON string: ${state.val}`);
return;
}
} else {
values = {
[deviceId[3]]: state.val
};
}
this.log.debug('values 4 ' + JSON.stringify(values));

} else {
Expand Down Expand Up @@ -529,7 +539,7 @@ class Wled extends utils.Adapter {
await this.create_state(device_id + '.psave', 'psave', '');
await this.create_state(device_id + '.udpn.nn', 'nn', '');
await this.create_state(device_id + '.time', 'time', null);
await this.create_state(device_id + '.time', 'time', null);
await this.create_state(device_id + '.action', 'action', '');

// Create structure for all states
await this.handleStates(deviceData, deviceIP);
Expand Down