2 * Copyright (c) 2010-2021 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.miele.internal.discovery;
15 import static org.openhab.binding.miele.internal.MieleBindingConstants.*;
17 import java.util.Date;
18 import java.util.HashMap;
19 import java.util.List;
23 import org.openhab.binding.miele.internal.handler.ApplianceStatusListener;
24 import org.openhab.binding.miele.internal.handler.MieleApplianceHandler;
25 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler;
26 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.DeviceClassObject;
27 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.DeviceProperty;
28 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.HomeDevice;
29 import org.openhab.core.config.discovery.AbstractDiscoveryService;
30 import org.openhab.core.config.discovery.DiscoveryResult;
31 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 import com.google.gson.JsonElement;
40 * The {@link MieleApplianceDiscoveryService} tracks appliances that are
41 * associated with the Miele@Home gateway
43 * @author Karel Goderis - Initial contribution
44 * @author Martin Lepsy - Added protocol information in order so support WiFi devices
46 public class MieleApplianceDiscoveryService extends AbstractDiscoveryService implements ApplianceStatusListener {
48 private static final String MIELE_APPLIANCE_CLASS = "com.miele.xgw3000.gateway.hdm.deviceclasses.MieleAppliance";
49 private static final String MIELE_CLASS = "com.miele.xgw3000.gateway.hdm.deviceclasses.Miele";
51 private final Logger logger = LoggerFactory.getLogger(MieleApplianceDiscoveryService.class);
53 private static final int SEARCH_TIME = 60;
55 private MieleBridgeHandler mieleBridgeHandler;
57 public MieleApplianceDiscoveryService(MieleBridgeHandler mieleBridgeHandler) {
58 super(MieleApplianceHandler.SUPPORTED_THING_TYPES, SEARCH_TIME, false);
59 this.mieleBridgeHandler = mieleBridgeHandler;
62 public void activate() {
63 mieleBridgeHandler.registerApplianceStatusListener(this);
67 public void deactivate() {
68 removeOlderResults(new Date().getTime());
69 mieleBridgeHandler.unregisterApplianceStatusListener(this);
73 public Set<ThingTypeUID> getSupportedThingTypes() {
74 return MieleApplianceHandler.SUPPORTED_THING_TYPES;
78 public void startScan() {
79 List<HomeDevice> appliances = mieleBridgeHandler.getHomeDevices();
80 if (appliances != null) {
81 for (HomeDevice l : appliances) {
82 onApplianceAddedInternal(l);
88 protected synchronized void stopScan() {
90 removeOlderResults(getTimestampOfLastScan());
94 public void onApplianceAdded(HomeDevice appliance) {
95 onApplianceAddedInternal(appliance);
98 private void onApplianceAddedInternal(HomeDevice appliance) {
99 ThingUID thingUID = getThingUID(appliance);
100 if (thingUID != null) {
101 ThingUID bridgeUID = mieleBridgeHandler.getThing().getUID();
102 Map<String, Object> properties = new HashMap<>(2);
104 properties.put(PROTOCOL_PROPERTY_NAME, appliance.getProtocol());
105 properties.put(APPLIANCE_ID, appliance.getApplianceId());
107 for (JsonElement dc : appliance.DeviceClasses) {
108 String dcStr = dc.getAsString();
109 if (dcStr.contains(MIELE_CLASS) && !dcStr.equals(MIELE_APPLIANCE_CLASS)) {
110 properties.put(DEVICE_CLASS, dcStr.substring(MIELE_CLASS.length()));
115 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
116 .withBridge(bridgeUID).withLabel((String) properties.get(DEVICE_CLASS)).build();
118 thingDiscovered(discoveryResult);
120 logger.debug("Discovered an unsupported appliance of vendor '{}' with id {}", appliance.Vendor,
126 public void onApplianceRemoved(HomeDevice appliance) {
127 ThingUID thingUID = getThingUID(appliance);
129 if (thingUID != null) {
130 thingRemoved(thingUID);
135 public void onApplianceStateChanged(String uid, DeviceClassObject dco) {
140 public void onAppliancePropertyChanged(String uid, DeviceProperty dp) {
144 private ThingUID getThingUID(HomeDevice appliance) {
145 ThingUID bridgeUID = mieleBridgeHandler.getThing().getUID();
146 String modelID = null;
148 for (JsonElement dc : appliance.DeviceClasses) {
149 String dcStr = dc.getAsString();
150 if (dcStr.contains(MIELE_CLASS) && !dcStr.equals(MIELE_APPLIANCE_CLASS)) {
151 modelID = dcStr.substring(MIELE_CLASS.length());
156 if (modelID != null) {
157 ThingTypeUID thingTypeUID = new ThingTypeUID(BINDING_ID,
158 modelID.replaceAll("[^a-zA-Z0-9_]", "_").toLowerCase());
160 if (getSupportedThingTypes().contains(thingTypeUID)) {
161 ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, appliance.getId());