2 * Copyright (c) 2010-2022 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.FullyQualifiedApplianceIdentifier;
24 import org.openhab.binding.miele.internal.handler.ApplianceStatusListener;
25 import org.openhab.binding.miele.internal.handler.MieleApplianceHandler;
26 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler;
27 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.DeviceClassObject;
28 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.DeviceProperty;
29 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.HomeDevice;
30 import org.openhab.core.config.discovery.AbstractDiscoveryService;
31 import org.openhab.core.config.discovery.DiscoveryResult;
32 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.thing.ThingUID;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * The {@link MieleApplianceDiscoveryService} tracks appliances that are
40 * associated with the Miele@Home gateway
42 * @author Karel Goderis - Initial contribution
43 * @author Martin Lepsy - Added protocol information in order so support WiFi devices
44 * @author Jacob Laursen - Fixed multicast and protocol support (ZigBee/LAN)
46 public class MieleApplianceDiscoveryService extends AbstractDiscoveryService implements ApplianceStatusListener {
48 private final Logger logger = LoggerFactory.getLogger(MieleApplianceDiscoveryService.class);
50 private static final int SEARCH_TIME = 60;
52 private MieleBridgeHandler mieleBridgeHandler;
54 public MieleApplianceDiscoveryService(MieleBridgeHandler mieleBridgeHandler) {
55 super(MieleApplianceHandler.SUPPORTED_THING_TYPES, SEARCH_TIME, false);
56 this.mieleBridgeHandler = mieleBridgeHandler;
59 public void activate() {
60 mieleBridgeHandler.registerApplianceStatusListener(this);
64 public void deactivate() {
65 removeOlderResults(new Date().getTime());
66 mieleBridgeHandler.unregisterApplianceStatusListener(this);
70 public Set<ThingTypeUID> getSupportedThingTypes() {
71 return MieleApplianceHandler.SUPPORTED_THING_TYPES;
75 public void startScan() {
76 List<HomeDevice> appliances = mieleBridgeHandler.getHomeDevices();
77 if (appliances != null) {
78 for (HomeDevice l : appliances) {
79 onApplianceAddedInternal(l);
85 protected synchronized void stopScan() {
87 removeOlderResults(getTimestampOfLastScan());
91 public void onApplianceAdded(HomeDevice appliance) {
92 onApplianceAddedInternal(appliance);
95 private void onApplianceAddedInternal(HomeDevice appliance) {
96 ThingUID thingUID = getThingUID(appliance);
97 if (thingUID != null) {
98 ThingUID bridgeUID = mieleBridgeHandler.getThing().getUID();
99 Map<String, Object> properties = new HashMap<>(2);
101 FullyQualifiedApplianceIdentifier applianceIdentifier = appliance.getApplianceIdentifier();
102 properties.put(MODEL_PROPERTY_NAME, appliance.getApplianceModel());
103 String deviceClass = appliance.getDeviceClass();
104 if (deviceClass != null) {
105 properties.put(DEVICE_CLASS, deviceClass);
107 properties.put(PROTOCOL_ADAPTER_PROPERTY_NAME, appliance.ProtocolAdapterName);
108 properties.put(APPLIANCE_ID, applianceIdentifier.getApplianceId());
109 properties.put(SERIAL_NUMBER_PROPERTY_NAME, appliance.getSerialNumber());
110 String connectionType = appliance.getConnectionType();
111 if (connectionType != null) {
112 properties.put(CONNECTION_TYPE_PROPERTY_NAME, connectionType);
115 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
116 .withBridge(bridgeUID).withLabel((String) properties.get(DEVICE_CLASS))
117 .withRepresentationProperty(APPLIANCE_ID).build();
119 thingDiscovered(discoveryResult);
121 logger.debug("Discovered an unsupported appliance of vendor '{}' with id {}", appliance.Vendor,
127 public void onApplianceRemoved(HomeDevice appliance) {
128 ThingUID thingUID = getThingUID(appliance);
130 if (thingUID != null) {
131 thingRemoved(thingUID);
136 public void onApplianceStateChanged(FullyQualifiedApplianceIdentifier applianceIdentifier, DeviceClassObject dco) {
141 public void onAppliancePropertyChanged(FullyQualifiedApplianceIdentifier applianceIdentifier, DeviceProperty dp) {
145 private ThingUID getThingUID(HomeDevice appliance) {
146 ThingUID bridgeUID = mieleBridgeHandler.getThing().getUID();
147 String modelId = appliance.getDeviceClass();
149 if (modelId != null) {
150 ThingTypeUID thingTypeUID = getThingTypeUidFromModelId(modelId);
152 if (getSupportedThingTypes().contains(thingTypeUID)) {
153 ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, appliance.getApplianceIdentifier().getId());
163 private ThingTypeUID getThingTypeUidFromModelId(String modelId) {
165 * Coffee machine CVA 6805 is reported as CoffeeSystem, but thing type is
166 * coffeemachine. At least until it is known if any models are actually reported
167 * as CoffeeMachine, we need this special mapping.
169 if (MIELE_DEVICE_CLASS_COFFEE_SYSTEM.equals(modelId)) {
170 return THING_TYPE_COFFEEMACHINE;
173 String thingTypeId = modelId.replaceAll("[^a-zA-Z0-9_]", "_").toLowerCase();
175 return new ThingTypeUID(BINDING_ID, thingTypeId);