2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.wled.internal.api;
15 import static org.openhab.binding.wled.internal.WLedBindingConstants.*;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.Map.Entry;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jetty.client.HttpClient;
24 import org.openhab.binding.wled.internal.WledState.PresetState;
25 import org.openhab.binding.wled.internal.handlers.WLedBridgeHandler;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.types.StateOption;
29 import com.google.gson.JsonElement;
30 import com.google.gson.JsonObject;
31 import com.google.gson.JsonSyntaxException;
34 * The {@link WledApiV0110} is the json Api methods for firmware version 0.11.0 and newer
35 * as newer firmwares come out with breaking changes, extend this class into a newer firmware version class.
37 * @author Matthew Skinner - Initial contribution
40 public class WledApiV0110 extends WledApiV084 {
42 public WledApiV0110(WLedBridgeHandler handler, HttpClient httpClient) {
43 super(handler, httpClient);
47 public void initialize() throws ApiException {
52 protected void getPresets() throws JsonSyntaxException, ApiException {
53 List<StateOption> presetsOptions = new ArrayList<>();
54 List<StateOption> playlistsOptions = new ArrayList<>();
55 JsonObject obj = gson.fromJson(sendGetRequest("/presets.json"), JsonObject.class);
59 Set<Entry<String, JsonElement>> set = obj.entrySet();
61 for (Entry<String, JsonElement> presetEntry : set) {
62 logger.trace("Preset:{} json:{}", presetEntry.getKey(), presetEntry.getValue());
63 PresetState preset = gson.fromJson(presetEntry.getValue(), PresetState.class);
64 if (preset != null && counter > 0) {
65 if (presetEntry.getValue().toString().contains("playlist")) {
66 playlistsOptions.add(new StateOption(presetEntry.getKey(), preset.n));
68 presetsOptions.add(new StateOption(presetEntry.getKey(), preset.n));
73 handler.stateDescriptionProvider.setStateOptions(new ChannelUID(handler.getThing().getUID(), CHANNEL_PRESETS),
75 handler.stateDescriptionProvider.setStateOptions(new ChannelUID(handler.getThing().getUID(), CHANNEL_PLAYLISTS),
80 public void savePreset(int position, String presetName) throws ApiException {
82 logger.warn("Preset position {} is not supported in this firmware version", position);
86 String name = presetName;
88 name = "Preset " + position;
90 postState("{\"psave\":" + position + ",\"n\":\"" + name + "\",\"ib\":true,\"sb\":true}");
94 public void setSleepMode(String value) throws ApiException {
95 postState("{\"nl\":{\"mode\":" + value + "}}");