Add a settings page
Add a setting to your plugin and let users change it in the Volumio web UI. This guide adds a switch to the Hello Volumio plugin from Build your first plugin. When the switch is on, the plugin writes its greeting in capital letters.
A setting lives in four places:
| File | Holds |
|---|---|
config.json | The setting's type and default value |
UIConfig.json | The field on the settings page |
i18n/strings_en.json | The text of the field, in English |
index.js | The code that shows the saved value and stores a new one |
Prerequisites
- A plugin made with
volumio-plugin init. Its settings page already has one field,greeting.
1. Add the default value
In config.json, add the setting with its type and default:
{
"greeting": { "type": "string", "value": "Hello from Volumio" },
"shout": { "type": "boolean", "value": false }
}
Volumio copies config.json to the device the first time the plugin runs. After that, the plugin reads and writes the copy, so users keep their values when the plugin is updated.
2. Add the field
In UIConfig.json, add a field to the section's content, and add its id to saveButton.data:
{
"page": { "label": "TRANSLATE.PLUGIN_NAME" },
"sections": [
{
"id": "settings",
"element": "section",
"label": "TRANSLATE.SETTINGS",
"icon": "fa-cog",
"onSave": { "type": "controller", "endpoint": "miscellanea/hello_volumio", "method": "saveSettings" },
"saveButton": { "label": "TRANSLATE.SAVE", "data": ["greeting", "shout"] },
"content": [
{
"id": "greeting",
"element": "input",
"type": "text",
"label": "TRANSLATE.GREETING",
"doc": "TRANSLATE.GREETING_DOC",
"value": ""
},
{
"id": "shout",
"element": "switch",
"label": "TRANSLATE.SHOUT",
"doc": "TRANSLATE.SHOUT_DOC",
"value": false
}
]
}
]
}
When the user selects Save, Volumio sends the values of the fields in saveButton.data to the method in onSave. A field missing from saveButton.data is not sent.
3. Add the text
Text the user reads is never written in UIConfig.json itself. Each label is a TRANSLATE. key, looked up in i18n/strings_<language>.json. Add the two keys to i18n/strings_en.json:
{
"PLUGIN_NAME": "Hello Volumio",
"SETTINGS": "Settings",
"SAVE": "Save",
"GREETING": "Greeting",
"GREETING_DOC": "Written to the log when the plugin starts",
"SHOUT": "Capital letters",
"SHOUT_DOC": "Writes the greeting in capital letters",
"SAVED": "Settings saved"
}
For another language, add the same keys to a file such as i18n/strings_it.json. Volumio uses English for any key a language file does not have.
4. Show the saved value
getUIConfig builds the page each time the user opens it. It translates UIConfig.json, then fills in the saved values. Add the new field:
getUIConfig() {
const lang = this.commandRouter.sharedVars.get('language_code');
return this.commandRouter
.i18nJson(this.file(`i18n/strings_${lang}.json`), this.file('i18n/strings_en.json'), this.file('UIConfig.json'))
.then((uiconf) => {
uiconf.sections[0].content[0].value = this.config.get('greeting');
uiconf.sections[0].content[1].value = this.config.get('shout');
return uiconf;
});
}
5. Store the new value
The method named in onSave gets an object with one entry per field id. Store the new value with this.config.set, which also saves it to disk:
saveSettings(data) {
this.config.set('greeting', data.greeting);
this.config.set('shout', data.shout);
this.commandRouter.pushToastMessage('success', this.strings.PLUGIN_NAME, this.strings.SAVED);
return libQ.resolve();
}
The method returns a kew promise, as every method Volumio calls. See How a plugin runs.
6. Use the setting
Read the value with this.config.get where the plugin needs it:
onStart() {
this.strings = this.loadStrings();
const greeting = this.config.get('greeting');
this.logger.info(`hello_volumio: ${this.config.get('shout') ? greeting.toUpperCase() : greeting}`);
return libQ.resolve();
}
7. Check that it works
- Run
npm testandnpx --package=@volumio/plugin-sdk volumio-plugin check .. Both must pass. - Install the plugin with
npx --package=@volumio/plugin-sdk volumio-plugin deploy 192.168.1.20 .. - In the Volumio web UI, go to Plugins › Installed Plugins and open the settings of Hello Volumio. The Capital letters switch is there.
- Turn it on and select Save. The toast Settings saved appears.
Next steps
- UIConfig.json lists every field type: text, password, number, switch, select, button, equalizer.
- Show toasts and modals to tell the user what happened.