]> git.basschouten.com Git - openhab-addons.git/blob
f6c320eed92eb8b95a15995f01b6bfbee9951b1e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.homeconnect.internal.discovery;
14
15 import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.*;
16
17 import java.util.List;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.homeconnect.internal.client.HomeConnectApiClient;
23 import org.openhab.binding.homeconnect.internal.client.exception.AuthorizationException;
24 import org.openhab.binding.homeconnect.internal.client.exception.CommunicationException;
25 import org.openhab.binding.homeconnect.internal.client.model.HomeAppliance;
26 import org.openhab.binding.homeconnect.internal.handler.HomeConnectBridgeHandler;
27 import org.openhab.core.config.discovery.AbstractDiscoveryService;
28 import org.openhab.core.config.discovery.DiscoveryResult;
29 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
30 import org.openhab.core.config.discovery.DiscoveryService;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.openhab.core.thing.binding.ThingHandlerService;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * The {@link HomeConnectDiscoveryService} is responsible for discovering new devices.
40  *
41  * @author Jonas BrĂ¼stel - Initial contribution
42  */
43 @NonNullByDefault
44 public class HomeConnectDiscoveryService extends AbstractDiscoveryService
45         implements DiscoveryService, ThingHandlerService {
46
47     private static final int SEARCH_TIME_SEC = 20;
48
49     private final Logger logger = LoggerFactory.getLogger(HomeConnectDiscoveryService.class);
50
51     private @Nullable HomeConnectBridgeHandler bridgeHandler;
52
53     /**
54      * Construct a {@link HomeConnectDiscoveryService}.
55      *
56      */
57     public HomeConnectDiscoveryService() {
58         super(DISCOVERABLE_DEVICE_THING_TYPES_UIDS, SEARCH_TIME_SEC, true);
59     }
60
61     @Override
62     public void setThingHandler(ThingHandler handler) {
63         if (handler instanceof HomeConnectBridgeHandler homeConnectBridgeHandler) {
64             this.bridgeHandler = homeConnectBridgeHandler;
65         }
66     }
67
68     @Override
69     public @Nullable ThingHandler getThingHandler() {
70         return bridgeHandler;
71     }
72
73     @Override
74     protected void startScan() {
75         logger.debug("Starting device scan.");
76
77         var bridgeHandler = this.bridgeHandler;
78         if (bridgeHandler != null) {
79             HomeConnectApiClient apiClient = bridgeHandler.getApiClient();
80
81             try {
82                 List<HomeAppliance> appliances = apiClient.getHomeAppliances();
83                 logger.debug("Scan found {} devices.", appliances.size());
84
85                 // add found devices
86                 for (HomeAppliance appliance : appliances) {
87                     @Nullable
88                     ThingTypeUID thingTypeUID = getThingTypeUID(appliance);
89
90                     if (thingTypeUID != null) {
91                         logger.debug("Found {} ({}).", appliance.getHaId(), appliance.getType().toUpperCase());
92
93                         Map<String, Object> properties = Map.of(HA_ID, appliance.getHaId());
94                         String name = appliance.getBrand() + " " + appliance.getName() + " (" + appliance.getHaId()
95                                 + ")";
96
97                         DiscoveryResult discoveryResult = DiscoveryResultBuilder
98                                 .create(new ThingUID(BINDING_ID, appliance.getType(),
99                                         bridgeHandler.getThing().getUID().getId(), appliance.getHaId()))
100                                 .withThingType(thingTypeUID).withProperties(properties)
101                                 .withRepresentationProperty(HA_ID).withBridge(bridgeHandler.getThing().getUID())
102                                 .withLabel(name).build();
103                         thingDiscovered(discoveryResult);
104                     } else {
105                         logger.debug("Ignoring unsupported device {} of type {}.", appliance.getHaId(),
106                                 appliance.getType());
107                     }
108                 }
109             } catch (CommunicationException | AuthorizationException e) {
110                 logger.debug("Exception during scan.", e);
111             }
112         }
113         logger.debug("Finished device scan.");
114     }
115
116     @Override
117     public void deactivate() {
118         super.deactivate();
119         var bridgeHandler = this.bridgeHandler;
120         if (bridgeHandler != null) {
121             removeOlderResults(System.currentTimeMillis(), bridgeHandler.getThing().getUID());
122         }
123     }
124
125     @Override
126     protected synchronized void stopScan() {
127         super.stopScan();
128         var bridgeHandler = this.bridgeHandler;
129         if (bridgeHandler != null) {
130             removeOlderResults(getTimestampOfLastScan(), bridgeHandler.getThing().getUID());
131         }
132     }
133
134     private @Nullable ThingTypeUID getThingTypeUID(HomeAppliance appliance) {
135         @Nullable
136         ThingTypeUID thingTypeUID = null;
137
138         if (THING_TYPE_DISHWASHER.getId().equalsIgnoreCase(appliance.getType())) {
139             thingTypeUID = THING_TYPE_DISHWASHER;
140         } else if (THING_TYPE_OVEN.getId().equalsIgnoreCase(appliance.getType())) {
141             thingTypeUID = THING_TYPE_OVEN;
142         } else if (THING_TYPE_FRIDGE_FREEZER.getId().equalsIgnoreCase(appliance.getType())) {
143             thingTypeUID = THING_TYPE_FRIDGE_FREEZER;
144         } else if (THING_TYPE_DRYER.getId().equalsIgnoreCase(appliance.getType())) {
145             thingTypeUID = THING_TYPE_DRYER;
146         } else if (THING_TYPE_COFFEE_MAKER.getId().equalsIgnoreCase(appliance.getType())) {
147             thingTypeUID = THING_TYPE_COFFEE_MAKER;
148         } else if (THING_TYPE_HOOD.getId().equalsIgnoreCase(appliance.getType())) {
149             thingTypeUID = THING_TYPE_HOOD;
150         } else if (THING_TYPE_WASHER_DRYER.getId().equalsIgnoreCase(appliance.getType())) {
151             thingTypeUID = THING_TYPE_WASHER_DRYER;
152         } else if (THING_TYPE_COOKTOP.getId().equalsIgnoreCase(appliance.getType())) {
153             thingTypeUID = THING_TYPE_COOKTOP;
154         } else if (THING_TYPE_WASHER.getId().equalsIgnoreCase(appliance.getType())) {
155             thingTypeUID = THING_TYPE_WASHER;
156         }
157
158         return thingTypeUID;
159     }
160 }