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.yeelight.internal.lib.device;
15 import org.openhab.binding.yeelight.internal.lib.enums.ActiveMode;
16 import org.openhab.binding.yeelight.internal.lib.enums.DeviceType;
17 import org.openhab.binding.yeelight.internal.lib.enums.MethodAction;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 import com.google.gson.JsonArray;
22 import com.google.gson.JsonObject;
23 import com.google.gson.JsonParser;
26 * The {@link CeilingDeviceWithAmbientDevice} contains methods for handling the ceiling device with ambient light.
28 * @author Viktor Koop - Initial contribution
30 public class CeilingDeviceWithAmbientDevice extends CeilingDevice
31 implements DeviceWithAmbientLight, DeviceWithNightlight {
32 private final Logger logger = LoggerFactory.getLogger(CeilingDeviceWithAmbientDevice.class);
34 public CeilingDeviceWithAmbientDevice(String id) {
37 mDeviceType = DeviceType.ceiling4;
41 public void onNotify(String msg) {
42 logger.debug("Got state: {}", msg);
44 JsonObject result = JsonParser.parseString(msg).getAsJsonObject();
46 if (result.has("id")) {
47 String id = result.get("id").getAsString();
48 // for cmd transaction.
50 if (mQueryList.contains(id)) {
51 JsonArray status = result.get("result").getAsJsonArray();
53 final String backgroundPowerState = status.get(4).toString();
54 if ("\"off\"".equals(backgroundPowerState)) {
55 mDeviceStatus.setBackgroundIsPowerOff(true);
56 } else if ("\"on\"".equals(backgroundPowerState)) {
57 mDeviceStatus.setBackgroundIsPowerOff(false);
60 final int backgroundBrightness = status.get(5).getAsInt();
61 mDeviceStatus.setBackgroundBrightness(backgroundBrightness);
63 final int backgroundHue = status.get(6).getAsInt();
64 mDeviceStatus.setBackgroundHue(backgroundHue);
66 final int backgroundSaturation = status.get(7).getAsInt();
67 mDeviceStatus.setBackgroundSat(backgroundSaturation);
69 final int activeMode = status.get(8).getAsInt();
70 mDeviceStatus.setActiveMode(ActiveMode.values()[activeMode]);
78 public void setBackgroundColor(int hue, int saturation, int duration) {
80 .invoke(MethodFactory.buildBackgroundHSVMethod(hue, saturation, DeviceMethod.EFFECT_SMOOTH, duration));
84 public void setBackgroundBrightness(int brightness, int duration) {
86 .invoke(MethodFactory.buildBackgroundBrightnessMethd(brightness, DeviceMethod.EFFECT_SMOOTH, duration));
90 public void setBackgroundPower(boolean on, int duration) {
91 mConnection.invoke(new DeviceMethod(MethodAction.BG_SWITCH,
92 new Object[] { on ? "on" : "off", DeviceMethod.EFFECT_SMOOTH, duration }));
96 public void toggleNightlightMode(boolean turnOn) {
99 new DeviceMethod(MethodAction.SCENE, new Object[] { "nightlight", mDeviceStatus.getBrightness() }));
101 mConnection.invoke(MethodFactory.buildCTMethod(mDeviceStatus.getCt(), DeviceMethod.EFFECT_SMOOTH, 500));