Skip to content

Commit

Permalink
* added boilerplate for all other supported devices
Browse files Browse the repository at this point in the history
* added channels and commands for the Bot
* cleanup / generalisation of api proxy and response
  • Loading branch information
alamers committed Sep 5, 2021
1 parent 050bb96 commit 9603282
Show file tree
Hide file tree
Showing 28 changed files with 1,222 additions and 380 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@ public class SwitchbotBindingConstants {
// List of all Thing Type UIDs
public static final ThingTypeUID BRIDGE_TYPE_SWITCHBOT_ACCOUNT = new ThingTypeUID(BINDING_ID, "switchbotAccount");
public static final ThingTypeUID THING_TYPE_CURTAIN = new ThingTypeUID(BINDING_ID, "curtain");
public static final ThingTypeUID THING_TYPE_HUB_MINI = new ThingTypeUID(BINDING_ID, "hubmini");
public static final ThingTypeUID THING_TYPE_HUB = new ThingTypeUID(BINDING_ID, "hub");
public static final ThingTypeUID THING_TYPE_BOT = new ThingTypeUID(BINDING_ID, "bot");
public static final ThingTypeUID THING_TYPE_PLUG = new ThingTypeUID(BINDING_ID, "plug");
public static final ThingTypeUID THING_TYPE_METER = new ThingTypeUID(BINDING_ID, "meter");
public static final ThingTypeUID THING_TYPE_HUMIDIFIER = new ThingTypeUID(BINDING_ID, "humidifier");
public static final ThingTypeUID THING_TYPE_SMARTFAN = new ThingTypeUID(BINDING_ID, "smartfan");

public static final String COMMAND = "command";
public static final String COMMAND_TURN_ON = "turnOn";
public static final String COMMAND_TURN_OFF = "turnOff";
public static final String COMMAND_OPEN = "open";
public static final String COMMAND_CLOSE = "close";
public static final String COMMAND_PRESS = "press";

public static final String CONFIG_DEVICE_ID = "deviceId";
public static final String CONFIG_GROUP = "group";
Expand All @@ -45,4 +51,7 @@ public class SwitchbotBindingConstants {
public static final String CHANNEL_MOVING = "moving";
public static final String CHANNEL_GROUP = "group";
public static final String CHANNEL_SLIDE_POSITION = "slide-position";

// bot channels
public static final String CHANNEL_POWER = "power";
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.switchbot.internal.discovery.SwitchbotAccountDiscoveryService;
import org.openhab.binding.switchbot.internal.handler.BotHandler;
import org.openhab.binding.switchbot.internal.handler.CurtainHandler;
import org.openhab.binding.switchbot.internal.handler.HubMiniHandler;
import org.openhab.binding.switchbot.internal.handler.HubHandler;
import org.openhab.binding.switchbot.internal.handler.HumidifierHandler;
import org.openhab.binding.switchbot.internal.handler.MeterHandler;
import org.openhab.binding.switchbot.internal.handler.PlugHandler;
import org.openhab.binding.switchbot.internal.handler.SmartfanHandler;
import org.openhab.binding.switchbot.internal.handler.SwitchbotAccountHandler;
import org.openhab.core.config.discovery.DiscoveryService;
import org.openhab.core.thing.Bridge;
Expand All @@ -49,11 +54,15 @@
@Component(configurationPid = "binding.switchbot", service = ThingHandlerFactory.class)
public class SwitchbotHandlerFactory extends BaseThingHandlerFactory {

public static final Set<ThingTypeUID> SUPPORTED_THING_TYPE_UIDS = Collections.unmodifiableSet(Stream
.of(BRIDGE_TYPE_SWITCHBOT_ACCOUNT, THING_TYPE_CURTAIN, THING_TYPE_HUB_MINI).collect(Collectors.toSet()));
public static final Set<ThingTypeUID> SUPPORTED_THING_TYPE_UIDS = Collections
.unmodifiableSet(Stream
.of(BRIDGE_TYPE_SWITCHBOT_ACCOUNT, THING_TYPE_CURTAIN, THING_TYPE_HUB, THING_TYPE_BOT,
THING_TYPE_PLUG, THING_TYPE_METER, THING_TYPE_HUMIDIFIER, THING_TYPE_SMARTFAN)
.collect(Collectors.toSet()));

public static final Set<ThingTypeUID> DISCOVERABLE_THING_TYPE_UIDS = Collections
.unmodifiableSet(Stream.of(THING_TYPE_CURTAIN, THING_TYPE_HUB_MINI).collect(Collectors.toSet()));
.unmodifiableSet(Stream.of(THING_TYPE_CURTAIN, THING_TYPE_HUB, THING_TYPE_BOT, THING_TYPE_PLUG,
THING_TYPE_METER, THING_TYPE_HUMIDIFIER, THING_TYPE_SMARTFAN).collect(Collectors.toSet()));

private Map<ThingUID, ServiceRegistration<DiscoveryService>> discoveryServiceRegistrations = new HashMap<>();

Expand All @@ -69,8 +78,18 @@ protected ThingHandler createHandler(Thing thing) {

if (thingTypeUID.equals(THING_TYPE_CURTAIN)) {
return new CurtainHandler(thing);
} else if (thingTypeUID.equals(THING_TYPE_HUB_MINI)) {
return new HubMiniHandler(thing);
} else if (thingTypeUID.equals(THING_TYPE_HUB)) {
return new HubHandler(thing);
} else if (thingTypeUID.equals(THING_TYPE_BOT)) {
return new BotHandler(thing);
} else if (thingTypeUID.equals(THING_TYPE_PLUG)) {
return new PlugHandler(thing);
} else if (thingTypeUID.equals(THING_TYPE_METER)) {
return new MeterHandler(thing);
} else if (thingTypeUID.equals(THING_TYPE_HUMIDIFIER)) {
return new HumidifierHandler(thing);
} else if (thingTypeUID.equals(THING_TYPE_SMARTFAN)) {
return new SmartfanHandler(thing);
} else if (thingTypeUID.equals(BRIDGE_TYPE_SWITCHBOT_ACCOUNT)) {
SwitchbotAccountHandler handler = new SwitchbotAccountHandler((Bridge) thing);
registerAccountDiscoveryService(handler);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.openhab.binding.switchbot.internal.config;

public class BotConfig {
private int refreshInterval;
private String deviceId;

public String getDeviceId() {
return deviceId;
}

public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}

public int getRefreshInterval() {
return refreshInterval;
}

public void setRefreshInterval(int refreshInterval) {
this.refreshInterval = refreshInterval;
}

@Override
public String toString() {
return "BotConfig [refreshInterval=" + refreshInterval + ", deviceId=" + deviceId + "]";
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.openhab.binding.switchbot.internal.config;

public class HubMiniConfig {
public class HubConfig {
private String deviceId;

public String getDeviceId() {
Expand All @@ -13,6 +13,6 @@ public void setDeviceId(String deviceId) {

@Override
public String toString() {
return "HubMiniConfig [deviceId=" + deviceId + "]";
return "HubConfig [deviceId=" + deviceId + "]";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.openhab.binding.switchbot.internal.config;

public class HumidifierConfig {
private String deviceId;
private int refreshInterval;

public String getDeviceId() {
return deviceId;
}

public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}

public int getRefreshInterval() {
return refreshInterval;
}

public void setRefreshInterval(int refreshInterval) {
this.refreshInterval = refreshInterval;
}

@Override
public String toString() {
return "HumidifierConfig [deviceId=" + deviceId + ", refreshInterval=" + refreshInterval + "]";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.openhab.binding.switchbot.internal.config;

public class MeterConfig {
private String deviceId;
private int refreshInterval;

public String getDeviceId() {
return deviceId;
}

public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}

public int getRefreshInterval() {
return refreshInterval;
}

public void setRefreshInterval(int refreshInterval) {
this.refreshInterval = refreshInterval;
}

@Override
public String toString() {
return "MeterConfig [deviceId=" + deviceId + ", refreshInterval=" + refreshInterval + "]";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.openhab.binding.switchbot.internal.config;

public class PlugConfig {
private String deviceId;
private int refreshInterval;

public String getDeviceId() {
return deviceId;
}

public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}

public int getRefreshInterval() {
return refreshInterval;
}

public void setRefreshInterval(int refreshInterval) {
this.refreshInterval = refreshInterval;
}

@Override
public String toString() {
return "MeterConfig [deviceId=" + deviceId + ", refreshInterval=" + refreshInterval + "]";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.openhab.binding.switchbot.internal.config;

public class SmartfanConfig {
private String deviceId;
private int refreshInterval;

public String getDeviceId() {
return deviceId;
}

public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}

public int getRefreshInterval() {
return refreshInterval;
}

public void setRefreshInterval(int refreshInterval) {
this.refreshInterval = refreshInterval;
}

@Override
public String toString() {
return "MeterConfig [deviceId=" + deviceId + ", refreshInterval=" + refreshInterval + "]";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.openhab.binding.switchbot.internal.discovery;

/**
* Represents a discovered Bot device.
*
* @author Arjan Lamers - Initial contribution
*/
public class BotDevice extends SwitchbotDevice {

public BotDevice(String name, String deviceId) {
super(name, deviceId, DeviceType.BOT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
*
* @author Arjan Lamers - Initial contribution
*/
public class HubMiniDevice extends SwitchbotDevice {
public class HubDevice extends SwitchbotDevice {

public HubMiniDevice(String name, String deviceId) {
super(name, deviceId, DeviceType.HUB_MINI);
public HubDevice(String name, String deviceId) {
super(name, deviceId, DeviceType.HUB);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ public class SwitchbotDevice {

public enum DeviceType {
CURTAIN(THING_TYPE_CURTAIN),
HUB_MINI(THING_TYPE_HUB_MINI);
BOT(THING_TYPE_BOT),
PLUG(THING_TYPE_PLUG),
METER(THING_TYPE_METER),
HUMIDIFIER(THING_TYPE_HUMIDIFIER),
SMARTFAN(THING_TYPE_SMARTFAN),
HUB(THING_TYPE_HUB);

private ThingTypeUID thingType;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Copyright (c) 2010-2021 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.switchbot.internal.handler;

import static org.openhab.binding.switchbot.internal.SwitchbotBindingConstants.CHANNEL_POWER;

import org.openhab.binding.switchbot.internal.config.BotConfig;
import org.openhab.core.library.types.OnOffType;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingStatus;
import org.openhab.core.thing.ThingStatusDetail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The {@link BotHandler} is responsible for handling commands, which are
* sent to one of the channels. It maps the OpenHAB world to the Switchbot world.
*
* @author Arjan Lamers - Initial contribution
*/
public class BotHandler extends SwitchbotHandler {

private Logger logger = LoggerFactory.getLogger(BotHandler.class);

public BotHandler(Thing thing) {
super(thing);
}

@Override
public void initialize() {
updateStatus(ThingStatus.UNKNOWN);
logger.debug("Will boot up Switchbot Bot binding");

BotConfig config = getThing().getConfiguration().as(BotConfig.class);

logger.debug("Bot Config: {}", config);

refreshTime = config.getRefreshInterval();
if (refreshTime < 30) {
logger.warn(
"Refresh time [{}] is not valid. Refresh time must be at least 30 seconds. Setting to minimum of 30 sec",
refreshTime);
config.setRefreshInterval(30);
}

apiProxy = new SwitchbotApiProxy(config.getDeviceId(), authorizationOpenToken);
startAutomaticRefresh();
}

@Override
protected void updateState(SwitchbotApiStatusModel state) {
if (state != null) {
updateStatus(ThingStatus.ONLINE);
publishChannels(state);
} else {
logger.warn("Bot {} not cloud-enabled, check app settings", apiProxy.getDeviceId());
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Bot not cloud-enabled, check app settings");
}
}

private void publishChannels(SwitchbotApiStatusModel state) {
if (state == null) {
updateState(CHANNEL_POWER, OnOffType.OFF);
return;
}

boolean power = state.getBody().getPower() == null ? false : state.getBody().getPower().equalsIgnoreCase("on");
updateState(CHANNEL_POWER, power ? OnOffType.ON : OnOffType.OFF);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class CommandModel {

public static CommandModel TURN_OFF = new CommandModel("turnOff", "default", "command");
public static CommandModel TURN_ON = new CommandModel("turnOn", "default", "command");
public static CommandModel PRESS = new CommandModel("press", "default", "command");

public CommandModel(String command, String parameter, String commandType) {
this.command = command;
Expand Down
Loading

0 comments on commit 9603282

Please sign in to comment.