]> git.basschouten.com Git - openhab-addons.git/blob
251e60aa34d35b4cc1686653d43001bf46d43c46
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.groheondus.internal.discovery;
14
15 import static org.openhab.binding.groheondus.internal.GroheOndusBindingConstants.*;
16
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;
22 import java.util.Map;
23 import java.util.stream.Collectors;
24 import java.util.stream.Stream;
25
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;
36
37 /**
38  * @author Florian Schmidt - Initial contribution
39  */
40 @NonNullByDefault
41 public class GroheOndusDiscoveryService extends AbstractDiscoveryService {
42     private static final String PROPERTY_APPLIANCE_ID = "applianceId";
43
44     private static final String PROPERTY_ROOM_ID = "roomId";
45
46     private static final String PROPERTY_LOCATION_ID = "locationId";
47
48     private final Logger logger = LoggerFactory.getLogger(GroheOndusDiscoveryService.class);
49
50     private final GroheOndusAccountHandler bridgeHandler;
51
52     public GroheOndusDiscoveryService(GroheOndusAccountHandler bridgeHandler) {
53         super(Collections
54                 .unmodifiableSet(Stream.of(THING_TYPE_SENSE, THING_TYPE_SENSEGUARD).collect(Collectors.toSet())), 30);
55         logger.debug("initialize discovery service");
56         this.bridgeHandler = bridgeHandler;
57         this.activate(null);
58     }
59
60     @Override
61     protected void startScan() {
62         OndusService service;
63         try {
64             service = bridgeHandler.getService();
65         } catch (IllegalStateException e) {
66             logger.debug("No instance of OndusService given.", e);
67             return;
68         }
69         List<BaseAppliance> discoveredAppliances = new ArrayList<>();
70         try {
71             discoveredAppliances = service.appliances();
72         } catch (IOException e) {
73             logger.debug("Could not discover appliances.", e);
74             return;
75         }
76
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());
83                     break;
84                 case org.grohe.ondus.api.model.sense.Appliance.TYPE:
85                     thingUID = new ThingUID(THING_TYPE_SENSE, bridgeUID, appliance.getApplianceId());
86                     break;
87                 default:
88                     return;
89             }
90
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());
95
96             DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
97                     .withBridge(bridgeUID).withLabel(appliance.getName())
98                     .withRepresentationProperty(PROPERTY_APPLIANCE_ID).build();
99
100             thingDiscovered(discoveryResult);
101         });
102     }
103 }