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

[yeelight] Add basic support for yeelight desklamp #4453

Merged
merged 2 commits into from
Jan 28, 2019
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<thing:thing-descriptions bindingId="yeelight" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:thing="http://eclipse.org/smarthome/schemas/thing-description/v1.0.0"
xsi:schemaLocation="http://eclipse.org/smarthome/schemas/thing-description/v1.0.0 http://eclipse.org/smarthome/schemas/thing-description-1.0.0.xsd">

<thing-type id="desklamp">
<label>Yeelight MI LED Desk Lamp</label>
<description>Yeelight MI LED Desk Lamp</description>

<channels>
<channel id="brightness" typeId="brightness" />
<channel id="colorTemperature" typeId="colorTemperature" />
</channels>

<config-description>
<parameter name="deviceId" type="text" required="true">
<label>Device ID</label>
<description>Id of the Yeelight device to connect with.</description>
</parameter>
</config-description>
</thing-type>
</thing:thing-descriptions>
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class YeelightBindingConstants {
public static final ThingTypeUID THING_TYPE_CTBULB = new ThingTypeUID(BINDING_ID, "ct_bulb");
public static final ThingTypeUID THING_TYPE_WONDER = new ThingTypeUID(BINDING_ID, "wonder");
public static final ThingTypeUID THING_TYPE_STRIPE = new ThingTypeUID(BINDING_ID, "stripe");
public static final ThingTypeUID THING_TYPE_DESKLAMP = new ThingTypeUID(BINDING_ID, "desklamp");

// List of thing Parameters names
public static final String PARAMETER_DEVICE_ID = "deviceId";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class YeelightHandlerFactory extends BaseThingHandlerFactory {
SUPPORTED_THING_TYPES_UIDS.add(THING_TYPE_CTBULB);
SUPPORTED_THING_TYPES_UIDS.add(THING_TYPE_WONDER);
SUPPORTED_THING_TYPES_UIDS.add(THING_TYPE_STRIPE);
SUPPORTED_THING_TYPES_UIDS.add(THING_TYPE_DESKLAMP);
}

@Override
Expand All @@ -62,7 +63,7 @@ protected ThingHandler createHandler(Thing thing) {
} else if (thingTypeUID.equals(THING_TYPE_STRIPE)) {
return new YeelightStripeHandler(thing);
} else if (thingTypeUID.equals(THING_TYPE_CEILING) || thingTypeUID.equals(THING_TYPE_CEILING1)
|| thingTypeUID.equals(THING_TYPE_CEILING3)) {
|| thingTypeUID.equals(THING_TYPE_CEILING3) || thingTypeUID.equals(THING_TYPE_DESKLAMP)) {
return new YeelightCeilingHandler(thing);
} else {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ private ThingUID getThingUID(DeviceBase device) {
return new ThingUID(YeelightBindingConstants.THING_TYPE_CTBULB, device.getDeviceId());
case stripe:
return new ThingUID(YeelightBindingConstants.THING_TYPE_STRIPE, device.getDeviceId());
case desklamp:
return new ThingUID(YeelightBindingConstants.THING_TYPE_DESKLAMP, device.getDeviceId());
default:
return null;
}
Expand All @@ -119,6 +121,8 @@ private ThingTypeUID getThingTypeUID(DeviceBase device) {
return YeelightBindingConstants.THING_TYPE_CTBULB;
case stripe:
return YeelightBindingConstants.THING_TYPE_STRIPE;
case desklamp:
return YeelightBindingConstants.THING_TYPE_DESKLAMP;
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ private DeviceType getDeviceModel(ThingTypeUID typeUID) {
return DeviceType.ct_bulb;
} else if (typeUID.equals(YeelightBindingConstants.THING_TYPE_STRIPE)) {
return DeviceType.stripe;
} else if (typeUID.equals(YeelightBindingConstants.THING_TYPE_DESKLAMP)) {
return DeviceType.desklamp;
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Copyright (c) 2010-2019 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.yeelight.internal.lib.device;

import org.openhab.binding.yeelight.internal.lib.device.connection.WifiConnection;
import org.openhab.binding.yeelight.internal.lib.enums.DeviceType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

/**
* The {@link DesklampDevice} contains methods for handling the desklamp device.
*
* @author Sebastian Rakel - Initial contribution
*/
public class DesklampDevice extends DeviceBase {
private final Logger logger = LoggerFactory.getLogger(DesklampDevice.class);

public DesklampDevice(String id) {
super(id);
mDeviceType = DeviceType.desklamp;
mConnection = new WifiConnection(this);
mMinCt = 2700;
mMaxCt = 6500;
}

@Override
public void onNotify(String msg) {
JsonObject result = new JsonParser().parse(msg).getAsJsonObject();
try {
String id = "-1";
if (result.has("id")) {
id = result.get("id").getAsString();
// for cmd transaction.

if (mQueryList.contains(id)) {
mQueryList.remove(id);
// DeviceMethod(MethodAction.PROP, new Object[] { "power", "name", "bright", "ct" });
JsonArray status = result.get("result").getAsJsonArray();

// power:
if (status.get(0).toString().equals("\"off\"")) {
mDeviceStatus.setPowerOff(true);
} else if (status.get(0).toString().equals("\"on\"")) {
mDeviceStatus.setPowerOff(false);
}

// name:
mDeviceStatus.setName(status.get(1).getAsString());

// brightness:
mDeviceStatus.setBrightness(status.get(2).getAsInt());

// ct:
mDeviceStatus.setCt(status.get(3).getAsInt());
}
}
} catch (Exception e) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know if we can catch a more specific exception type?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, i only copied a other light and changed the needed lines.

I will have a look into that at the weekend

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how to catch this exception more specific, normaly this exception should not be thrown. I can remove this exception but i don't know if that is a better solution

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind. It seems to be the way how this binding handles it. Let's do not touch it though.

logger.debug("Problem setting values: {}", e);
}

super.onNotify(msg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public static DeviceBase build(String model, String id) {
return new CtBulbDevice(id);
case stripe:
return new PitayaDevice(id);
case desklamp:
return new DesklampDevice(id);
default:
return null;
}
Expand Down Expand Up @@ -76,6 +78,9 @@ public static DeviceBase build(Map<String, String> bulbInfo) {
case stripe:
device = new PitayaDevice(bulbInfo.get("id"));
break;
case desklamp:
device = new DesklampDevice(bulbInfo.get("id"));
break;
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ public static DeviceMethod buildScnene(String type, int value, int brightness) {

/**
* @param type scene type {@link DeviceMethod#SCENE_TYPE_COLOR}
* {@link DeviceMethod#SCENE_TYPE_CT}
* {@link DeviceMethod#SCENE_TYPE_DELAY}
* {@link DeviceMethod#SCENE_TYPE_HSV}
* {@link DeviceMethod#SCENE_TYPE_CF}
* {@link DeviceMethod#SCENE_TYPE_CT}
* {@link DeviceMethod#SCENE_TYPE_DELAY}
* {@link DeviceMethod#SCENE_TYPE_HSV}
* {@link DeviceMethod#SCENE_TYPE_CF}
*
*/
public static DeviceMethod buildScene(String type, int value, int brightness, int count, int endAction,
Expand Down Expand Up @@ -166,6 +166,8 @@ public static DeviceMethod buildQuery(DeviceBase device) {
case stripe:
return new DeviceMethod(MethodAction.PROP,
new Object[] { "power", "name", "bright", "ct", "rgb", "hue", "sat" });
case desklamp:
return new DeviceMethod(MethodAction.PROP, new Object[] { "power", "name", "bright", "ct" });
default:
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ public enum DeviceType {
ceiling,
ceiling1,
ceiling3,
stripe
stripe,
desklamp
}
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ public static String getDefaultName(DeviceBase device) {
return "Yeelight White LED Bulb v2";
case stripe:
return "Yeelight Color LED Stripe";
case desklamp:
return "Yeelight Mi LED Desk Lamp";
default:
return "";
}
Expand Down