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.groheondus.internal.discovery;
15 import static org.openhab.binding.groheondus.internal.GroheOndusBindingConstants.THING_TYPE_SENSE;
16 import static org.openhab.binding.groheondus.internal.GroheOndusBindingConstants.THING_TYPE_SENSEGUARD;
18 import java.io.IOException;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.List;
24 import java.util.stream.Collectors;
25 import java.util.stream.Stream;
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.openhab.binding.groheondus.internal.handler.GroheOndusAccountHandler;
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.ThingUID;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import io.github.floriansw.ondus.api.OndusService;
37 import io.github.floriansw.ondus.api.model.BaseAppliance;
40 * @author Florian Schmidt - Initial contribution
43 public class GroheOndusDiscoveryService extends AbstractDiscoveryService {
44 private static final String PROPERTY_APPLIANCE_ID = "applianceId";
46 private static final String PROPERTY_ROOM_ID = "roomId";
48 private static final String PROPERTY_LOCATION_ID = "locationId";
50 private final Logger logger = LoggerFactory.getLogger(GroheOndusDiscoveryService.class);
52 private final GroheOndusAccountHandler bridgeHandler;
54 public GroheOndusDiscoveryService(GroheOndusAccountHandler bridgeHandler) {
56 .unmodifiableSet(Stream.of(THING_TYPE_SENSE, THING_TYPE_SENSEGUARD).collect(Collectors.toSet())), 30);
57 logger.debug("initialize discovery service");
58 this.bridgeHandler = bridgeHandler;
63 protected void startScan() {
64 // Remove old results - or they will stay there forever
65 removeOlderResults(getTimestampOfLastScan(), null, bridgeHandler.getThing().getUID());
69 service = bridgeHandler.getService();
70 } catch (IllegalStateException e) {
71 logger.debug("No instance of OndusService given.", e);
74 List<BaseAppliance> discoveredAppliances = new ArrayList<>();
76 discoveredAppliances = service.appliances();
77 } catch (IOException e) {
78 logger.debug("Could not discover appliances.", e);
81 discoveredAppliances.forEach(appliance -> {
82 ThingUID bridgeUID = bridgeHandler.getThing().getUID();
83 ThingUID thingUID = null;
84 switch (appliance.getType()) {
85 case io.github.floriansw.ondus.api.model.guard.Appliance.TYPE:
86 thingUID = new ThingUID(THING_TYPE_SENSEGUARD, bridgeUID, appliance.getApplianceId());
88 case io.github.floriansw.ondus.api.model.sense.Appliance.TYPE:
89 thingUID = new ThingUID(THING_TYPE_SENSE, bridgeUID, appliance.getApplianceId());
95 Map<String, Object> properties = new HashMap<>();
96 properties.put(PROPERTY_LOCATION_ID, appliance.getRoom().getLocation().getId());
97 properties.put(PROPERTY_ROOM_ID, appliance.getRoom().getId());
98 properties.put(PROPERTY_APPLIANCE_ID, appliance.getApplianceId());
100 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
101 .withBridge(bridgeUID).withLabel(appliance.getName())
102 .withRepresentationProperty(PROPERTY_APPLIANCE_ID).build();
104 thingDiscovered(discoveryResult);