Skip to content

Commit

Permalink
[miele] Add null annotations and improve error handling robustness (#…
Browse files Browse the repository at this point in the history
…12497)

* Add null annotations and improve error handling robustness
* Fix compliancy with rule ConstantNameCheck

Fixes #12496

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
  • Loading branch information
jlaur authored Mar 21, 2022
1 parent 7c29e4d commit f21bbc5
Show file tree
Hide file tree
Showing 35 changed files with 993 additions and 668 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import java.nio.charset.StandardCharsets;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.miele.internal.api.dto.DeviceMetaData;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.library.unit.SIUnits;
Expand All @@ -29,13 +32,14 @@
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
public class DeviceUtil {
private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes(StandardCharsets.US_ASCII);
private static final String TEMPERATURE_UNDEFINED = "32768";
private static final String TEMPERATURE_COLD = "-32760";
private static final String TEXT_PREFIX = "miele.";

private static final Map<String, String> states = Map.ofEntries(Map.entry("1", "off"), Map.entry("2", "stand-by"),
private static final Map<String, String> STATES = Map.ofEntries(Map.entry("1", "off"), Map.entry("2", "stand-by"),
Map.entry("3", "programmed"), Map.entry("4", "waiting-to-start"), Map.entry("5", "running"),
Map.entry("6", "paused"), Map.entry("7", "end"), Map.entry("8", "failure"), Map.entry("9", "abort"),
Map.entry("10", "idle"), Map.entry("11", "rinse-hold"), Map.entry("12", "service"),
Expand Down Expand Up @@ -84,8 +88,9 @@ public static State getTemperatureState(String s) throws NumberFormatException {
* Get state text for provided string taking into consideration {@link DeviceMetaData}
* as well as built-in/translated strings.
*/
public static State getStateTextState(String s, DeviceMetaData dmd, MieleTranslationProvider translationProvider) {
return getTextState(s, dmd, translationProvider, states, MISSING_STATE_TEXT_PREFIX, "");
public static State getStateTextState(String s, @Nullable DeviceMetaData dmd,
@Nullable MieleTranslationProvider translationProvider) {
return getTextState(s, dmd, translationProvider, STATES, MISSING_STATE_TEXT_PREFIX, "");
}

/**
Expand All @@ -100,8 +105,9 @@ public static State getStateTextState(String s, DeviceMetaData dmd, MieleTransla
* @param appliancePrefix Appliance prefix appended to text key (including dot)
* @return Text string as State
*/
public static State getTextState(String s, DeviceMetaData dmd, MieleTranslationProvider translationProvider,
Map<String, String> valueMap, String propertyPrefix, String appliancePrefix) {
public static State getTextState(String s, @Nullable DeviceMetaData dmd,
@Nullable MieleTranslationProvider translationProvider, Map<String, String> valueMap, String propertyPrefix,
String appliancePrefix) {
if ("0".equals(s)) {
return UnDefType.UNDEF;
}
Expand All @@ -116,7 +122,7 @@ public static State getTextState(String s, DeviceMetaData dmd, MieleTranslationP
}

String value = valueMap.get(s);
if (value != null) {
if (value != null && translationProvider != null) {
String key = TEXT_PREFIX + propertyPrefix + appliancePrefix + value;
return new StringType(
translationProvider.getText(key, gatewayText != null ? gatewayText : propertyPrefix + s));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
*/
package org.openhab.binding.miele.internal;

import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* The {@link FullyQualifiedApplianceIdentifier} class represents a fully qualified appliance identifier.
* Example: "hdm:ZigBee:0123456789abcdef#210"
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
public class FullyQualifiedApplianceIdentifier {
private String uid;
private String protocol;
Expand Down Expand Up @@ -56,7 +59,7 @@ public String getId() {
}

/**
* @return Protocol prefix of fully qualified appliance identifier (e.g. "hdmi:ZigBee:"")
* @return Protocol prefix of fully qualified appliance identifier (e.g. "hdmi:ZigBee:")
*/
public String getProtocol() {
return this.protocol;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class MieleBindingConstants {

public static final String BINDING_ID = "miele";
public static final String APPLIANCE_ID = "uid";
public static final String MIELE_CLASS = "com.miele.xgw3000.gateway.hdm.deviceclasses.Miele";

// Properties
public static final String PROPERTY_DEVICE_CLASS = "deviceClass";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.miele.internal.discovery.MieleApplianceDiscoveryService;
import org.openhab.binding.miele.internal.handler.CoffeeMachineHandler;
import org.openhab.binding.miele.internal.handler.DishWasherHandler;
Expand Down Expand Up @@ -56,6 +58,7 @@
*
* @author Karel Goderis - Initial contribution
*/
@NonNullByDefault
@Component(service = ThingHandlerFactory.class, configurationPid = "binding.miele")
public class MieleHandlerFactory extends BaseThingHandlerFactory {

Expand All @@ -82,8 +85,8 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
}

@Override
public Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration, ThingUID thingUID,
ThingUID bridgeUID) {
public @Nullable Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
@Nullable ThingUID thingUID, @Nullable ThingUID bridgeUID) {
if (MieleBridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) {
ThingUID mieleBridgeUID = getBridgeThingUID(thingTypeUID, thingUID, configuration);
return super.createThing(thingTypeUID, configuration, mieleBridgeUID, null);
Expand All @@ -97,7 +100,7 @@ public Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration,
}

@Override
protected ThingHandler createHandler(Thing thing) {
protected @Nullable ThingHandler createHandler(Thing thing) {
if (MieleBridgeHandler.SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())) {
MieleBridgeHandler handler = new MieleBridgeHandler((Bridge) thing);
registerApplianceDiscoveryService(handler);
Expand Down Expand Up @@ -135,20 +138,25 @@ protected ThingHandler createHandler(Thing thing) {
return null;
}

private ThingUID getBridgeThingUID(ThingTypeUID thingTypeUID, ThingUID thingUID, Configuration configuration) {
private ThingUID getBridgeThingUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
Configuration configuration) {
if (thingUID == null) {
String hostID = (String) configuration.get(HOST);
thingUID = new ThingUID(thingTypeUID, hostID);
}
return thingUID;
}

private ThingUID getApplianceUID(ThingTypeUID thingTypeUID, ThingUID thingUID, Configuration configuration,
ThingUID bridgeUID) {
private ThingUID getApplianceUID(ThingTypeUID thingTypeUID, @Nullable ThingUID thingUID,
Configuration configuration, @Nullable ThingUID bridgeUID) {
String applianceId = (String) configuration.get(APPLIANCE_ID);

if (thingUID == null) {
thingUID = new ThingUID(thingTypeUID, applianceId, bridgeUID.getId());
if (bridgeUID == null) {
thingUID = new ThingUID(thingTypeUID, applianceId);
} else {
thingUID = new ThingUID(thingTypeUID, bridgeUID, applianceId);
}
}
return thingUID;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright (c) 2010-2022 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.miele.internal.api.dto;

import com.google.gson.JsonArray;

/**
* The {@link DeviceClassObject} class represents the DeviceClassObject node in the response JSON.
*
* @author Jacob Laursen - Initial contribution
**/
public class DeviceClassObject {
public String DeviceClassType;
public JsonArray Operations;
public String DeviceClass;
public JsonArray Properties;

public DeviceClassObject() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.binding.miele.internal;
package org.openhab.binding.miele.internal.api.dto;

import java.util.Map.Entry;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Copyright (c) 2010-2022 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.miele.internal.api.dto;

import com.google.gson.JsonObject;

/**
* The {@link DeviceProperty} class represents the DeviceProperty node in the response JSON.
*
* @author Jacob Laursen - Initial contribution
**/
public class DeviceProperty {
public String Name;
public String Value;
public int Polling;
public JsonObject Metadata;

public DeviceProperty() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* Copyright (c) 2010-2022 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.miele.internal.api.dto;

import org.openhab.binding.miele.internal.FullyQualifiedApplianceIdentifier;
import org.openhab.binding.miele.internal.MieleBindingConstants;

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

/**
* The {@link HomeDevice} class represents the HomeDevice node in the response JSON.
*
* @author Jacob Laursen - Initial contribution
**/
public class HomeDevice {

private static final String MIELE_APPLIANCE_CLASS = "com.miele.xgw3000.gateway.hdm.deviceclasses.MieleAppliance";

public String Name;
public String Status;
public String ParentUID;
public String ProtocolAdapterName;
public String Vendor;
public String UID;
public String Type;
public JsonArray DeviceClasses;
public String Version;
public String TimestampAdded;
public JsonObject Error;
public JsonObject Properties;

public HomeDevice() {
}

public FullyQualifiedApplianceIdentifier getApplianceIdentifier() {
return new FullyQualifiedApplianceIdentifier(this.UID);
}

public String getSerialNumber() {
return Properties.get("serial.number").getAsString();
}

public String getFirmwareVersion() {
return Properties.get("firmware.version").getAsString();
}

public String getRemoteUid() {
JsonElement remoteUid = Properties.get("remote.uid");
if (remoteUid == null) {
// remote.uid and serial.number seems to be the same. If remote.uid
// is missing for some reason, it makes sense to provide fallback
// to serial number.
return getSerialNumber();
}
return remoteUid.getAsString();
}

public String getConnectionType() {
JsonElement connectionType = Properties.get("connection.type");
if (connectionType == null) {
return null;
}
return connectionType.getAsString();
}

public String getConnectionBaudRate() {
JsonElement baudRate = Properties.get("connection.baud.rate");
if (baudRate == null) {
return null;
}
return baudRate.getAsString();
}

public String getApplianceModel() {
JsonElement model = Properties.get("miele.model");
if (model == null) {
return "";
}
return model.getAsString();
}

public String getDeviceClass() {
for (JsonElement dc : DeviceClasses) {
String dcStr = dc.getAsString();
if (dcStr.contains(MieleBindingConstants.MIELE_CLASS) && !dcStr.equals(MIELE_APPLIANCE_CLASS)) {
return dcStr.substring(MieleBindingConstants.MIELE_CLASS.length());
}
}
return null;
}
}
Loading

0 comments on commit f21bbc5

Please sign in to comment.