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.miele.internal.discovery;
15 import java.io.IOException;
16 import java.net.InetAddress;
17 import java.net.Socket;
18 import java.util.HashMap;
22 import javax.jmdns.ServiceInfo;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.miele.internal.MieleBindingConstants;
27 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler;
28 import org.openhab.core.config.discovery.DiscoveryResult;
29 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
30 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.osgi.service.component.annotations.Activate;
34 import org.osgi.service.component.annotations.Component;
35 import org.osgi.service.component.annotations.Modified;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * The {@link MieleMDNSDiscoveryParticipant} is responsible for discovering Miele XGW3000 Gateways. It uses the central
41 * {@link org.openhab.core.config.discovery.mdns.internal.MDNSDiscoveryService}.
43 * @author Karel Goderis - Initial contribution
44 * @author Martin Lepsy - Added check for Miele gateway for cleaner discovery
45 * @author Jacob Laursen - Fixed multicast and protocol support (ZigBee/LAN)
48 @Component(configurationPid = "discovery.miele")
49 public class MieleMDNSDiscoveryParticipant implements MDNSDiscoveryParticipant {
51 private static final String SERVICE_TYPE = "_mieleathome._tcp.local.";
52 private static final String PATH_TO_CHECK_FOR_XGW3000 = "/rest/";
53 private static final String SERVICE_NAME = "mieleathome";
54 private static final String PATH_PROPERTY_NAME = "path";
56 private final Logger logger = LoggerFactory.getLogger(MieleMDNSDiscoveryParticipant.class);
58 private long removalGracePeriodSeconds = 15;
61 public void activate(@Nullable Map<String, Object> configProperties) {
62 if (configProperties == null) {
65 updateRemovalGracePeriod(configProperties);
69 public void modified(@Nullable Map<String, Object> configProperties) {
70 if (configProperties == null) {
73 updateRemovalGracePeriod(configProperties);
77 * Update the removalGracePeriodSeconds when the component is activates or modified.
79 * @param configProperties the passed configuration parameters.
81 private void updateRemovalGracePeriod(Map<String, Object> configProperties) {
82 Object value = configProperties.get(MieleBindingConstants.REMOVAL_GRACE_PERIOD);
85 removalGracePeriodSeconds = Integer.parseInt(value.toString());
86 } catch (NumberFormatException e) {
87 logger.warn("Configuration property '{}' has invalid value: {}",
88 MieleBindingConstants.REMOVAL_GRACE_PERIOD, value);
94 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
95 return MieleBridgeHandler.SUPPORTED_THING_TYPES;
99 public String getServiceType() {
104 public @Nullable DiscoveryResult createResult(ServiceInfo service) {
105 if (!isMieleGateway(service)) {
109 ThingUID uid = getThingUID(service);
114 InetAddress[] addresses = service.getInetAddresses();
115 if (addresses.length <= 0 || addresses[0] == null) {
119 String ipAddress = addresses[0].getHostAddress();
120 Map<String, Object> properties = new HashMap<>(2);
121 properties.put(MieleBindingConstants.HOST, ipAddress);
123 Socket socket = null;
125 socket = new Socket(addresses[0], 80);
126 InetAddress ourAddress = socket.getLocalAddress();
127 String interfaceIpAddress = ourAddress.getHostAddress();
130 properties.put(MieleBindingConstants.INTERFACE, interfaceIpAddress);
131 logger.debug("Discovered Miele@home gateway with IP address {} and interface IP address {}", ipAddress,
133 } catch (IOException e) {
134 logger.warn("An exception occurred while connecting to the Miele Gateway: '{}'", e.getMessage());
138 return DiscoveryResultBuilder.create(uid).withProperties(properties)
139 .withRepresentationProperty(MieleBindingConstants.HOST).withLabel("@text/discovery.xgw3000.label")
144 public @Nullable ThingUID getThingUID(ServiceInfo service) {
145 if (SERVICE_TYPE.equals(service.getType())) {
146 return new ThingUID(MieleBindingConstants.THING_TYPE_XGW3000, service.getName().replace(" ", "_"));
153 * Checks if service is a Miele XGW 3000 Gateway
155 * application must be mieleathome
156 * must contain path with value /rest/
158 * @param service the service to check
159 * @return true, if the discovered service is a Miele XGW3000 Gateway
161 private boolean isMieleGateway(ServiceInfo serviceInfo) {
162 return serviceInfo.getApplication().contains(SERVICE_NAME)
163 && PATH_TO_CHECK_FOR_XGW3000.equalsIgnoreCase(serviceInfo.getPropertyString(PATH_PROPERTY_NAME));
167 * Miele devices are sometimes a few seconds late in updating their mDNS announcements, which means that they are
168 * repeatedly removed from, and (re)added to, the Inbox. To prevent this, we override this method to specify an
169 * additional delay period (grace period) to wait before the device is removed from the Inbox.
172 public long getRemovalGracePeriodSeconds(ServiceInfo serviceInfo) {
173 return removalGracePeriodSeconds;