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.groheondus.internal.discovery;
15 import static org.openhab.binding.groheondus.internal.GroheOndusBindingConstants.*;
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.HashMap;
21 import java.util.List;
23 import java.util.stream.Collectors;
24 import java.util.stream.Stream;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.grohe.ondus.api.OndusService;
28 import org.grohe.ondus.api.model.BaseAppliance;
29 import org.openhab.binding.groheondus.internal.handler.GroheOndusAccountHandler;
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.ThingUID;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * @author Florian Schmidt - Initial contribution
41 public class GroheOndusDiscoveryService extends AbstractDiscoveryService {
42 private static final String PROPERTY_APPLIANCE_ID = "applianceId";
44 private static final String PROPERTY_ROOM_ID = "roomId";
46 private static final String PROPERTY_LOCATION_ID = "locationId";
48 private final Logger logger = LoggerFactory.getLogger(GroheOndusDiscoveryService.class);
50 private final GroheOndusAccountHandler bridgeHandler;
52 public GroheOndusDiscoveryService(GroheOndusAccountHandler bridgeHandler) {
54 .unmodifiableSet(Stream.of(THING_TYPE_SENSE, THING_TYPE_SENSEGUARD).collect(Collectors.toSet())), 30);
55 logger.debug("initialize discovery service");
56 this.bridgeHandler = bridgeHandler;
61 protected void startScan() {
64 service = bridgeHandler.getService();
65 } catch (IllegalStateException e) {
66 logger.debug("No instance of OndusService given.", e);
69 List<BaseAppliance> discoveredAppliances = new ArrayList<>();
71 discoveredAppliances = service.appliances();
72 } catch (IOException e) {
73 logger.debug("Could not discover appliances.", e);
77 discoveredAppliances.forEach(appliance -> {
78 ThingUID bridgeUID = bridgeHandler.getThing().getUID();
79 ThingUID thingUID = null;
80 switch (appliance.getType()) {
81 case org.grohe.ondus.api.model.guard.Appliance.TYPE:
82 thingUID = new ThingUID(THING_TYPE_SENSEGUARD, bridgeUID, appliance.getApplianceId());
84 case org.grohe.ondus.api.model.sense.Appliance.TYPE:
85 thingUID = new ThingUID(THING_TYPE_SENSE, bridgeUID, appliance.getApplianceId());
91 Map<String, Object> properties = new HashMap<>();
92 properties.put(PROPERTY_LOCATION_ID, appliance.getRoom().getLocation().getId());
93 properties.put(PROPERTY_ROOM_ID, appliance.getRoom().getId());
94 properties.put(PROPERTY_APPLIANCE_ID, appliance.getApplianceId());
96 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
97 .withBridge(bridgeUID).withLabel(appliance.getName())
98 .withRepresentationProperty(PROPERTY_APPLIANCE_ID).build();
100 thingDiscovered(discoveryResult);