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.mielecloud.internal.discovery;
15 import java.util.HashMap;
17 import java.util.Optional;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.mielecloud.internal.MieleCloudBindingConstants;
21 import org.openhab.binding.mielecloud.internal.webservice.api.DeviceState;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingTypeUID;
26 * Helper class extracting information related to things from {@link DeviceState}s received from the Miele cloud.
28 * @author Björn Lange - Initial contribution
31 public final class ThingInformationExtractor {
32 private ThingInformationExtractor() {
33 throw new IllegalStateException(getClass().getName() + " cannot be instantiated");
37 * Extracts thing properties from a {@link DeviceState}.
39 * The returned properties always contain {@link Thing#PROPERTY_SERIAL_NUMBER} and {@link Thing#PROPERTY_MODEL_ID}.
40 * More might be present depending on the type of device.
42 * @param thingTypeUid {@link ThingTypeUID} of the thing to extract properties for.
43 * @param deviceState {@link DeviceState} received from the Miele cloud.
44 * @return A {@link Map} holding the properties as key-value pairs.
46 public static Map<String, String> extractProperties(ThingTypeUID thingTypeUid, DeviceState deviceState) {
47 var propertyMap = new HashMap<String, String>();
48 propertyMap.put(Thing.PROPERTY_SERIAL_NUMBER, getSerialNumber(deviceState));
49 propertyMap.put(Thing.PROPERTY_MODEL_ID, getModelId(deviceState));
50 propertyMap.put(MieleCloudBindingConstants.CONFIG_PARAM_DEVICE_IDENTIFIER, deviceState.getDeviceIdentifier());
52 if (MieleCloudBindingConstants.THING_TYPE_HOB.equals(thingTypeUid)) {
53 deviceState.getPlateStepCount().ifPresent(plateCount -> propertyMap
54 .put(MieleCloudBindingConstants.PROPERTY_PLATE_COUNT, plateCount.toString()));
60 private static String getSerialNumber(DeviceState deviceState) {
61 return deviceState.getFabNumber().orElse(deviceState.getDeviceIdentifier());
64 private static String getModelId(DeviceState deviceState) {
65 return getDeviceAndTechType(deviceState).orElse("Unknown");
69 * Formats device type and tech type from the given {@link DeviceState} for the purpose of displaying then to the
72 * If either of device or tech type is missing then it will be omitted. If both are missing then an empty
73 * {@link Optional} will be returned.
75 * @param deviceState {@link DeviceState} obtained from the Miele cloud.
76 * @return An {@link Optional} holding the formatted value or an empty {@link Optional} if neither device type nor
77 * tech type were present.
79 static Optional<String> getDeviceAndTechType(DeviceState deviceState) {
80 Optional<String> deviceType = deviceState.getType();
81 Optional<String> techType = deviceState.getTechType();
82 if (deviceType.isPresent() && techType.isPresent()) {
83 return Optional.of(deviceType.get() + " " + techType.get());
85 if (deviceType.isEmpty() && techType.isPresent()) {
88 if (deviceType.isPresent() && techType.isEmpty()) {
91 return Optional.empty();