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.homeconnect.internal.discovery;
15 import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.*;
17 import java.util.List;
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;
39 * The {@link HomeConnectDiscoveryService} is responsible for discovering new devices.
41 * @author Jonas BrĂ¼stel - Initial contribution
44 public class HomeConnectDiscoveryService extends AbstractDiscoveryService
45 implements DiscoveryService, ThingHandlerService {
47 private static final int SEARCH_TIME_SEC = 20;
49 private final Logger logger = LoggerFactory.getLogger(HomeConnectDiscoveryService.class);
51 private @Nullable HomeConnectBridgeHandler bridgeHandler;
54 * Construct a {@link HomeConnectDiscoveryService}.
57 public HomeConnectDiscoveryService() {
58 super(DISCOVERABLE_DEVICE_THING_TYPES_UIDS, SEARCH_TIME_SEC, true);
62 public void setThingHandler(ThingHandler handler) {
63 if (handler instanceof HomeConnectBridgeHandler) {
64 this.bridgeHandler = (HomeConnectBridgeHandler) handler;
69 public @Nullable ThingHandler getThingHandler() {
74 protected void startScan() {
75 logger.debug("Starting device scan.");
77 var bridgeHandler = this.bridgeHandler;
78 if (bridgeHandler != null) {
79 HomeConnectApiClient apiClient = bridgeHandler.getApiClient();
82 List<HomeAppliance> appliances = apiClient.getHomeAppliances();
83 logger.debug("Scan found {} devices.", appliances.size());
86 for (HomeAppliance appliance : appliances) {
88 ThingTypeUID thingTypeUID = getThingTypeUID(appliance);
90 if (thingTypeUID != null) {
91 logger.debug("Found {} ({}).", appliance.getHaId(), appliance.getType().toUpperCase());
93 Map<String, Object> properties = Map.of(HA_ID, appliance.getHaId());
94 String name = appliance.getBrand() + " " + appliance.getName() + " (" + appliance.getHaId()
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);
105 logger.debug("Ignoring unsupported device {} of type {}.", appliance.getHaId(),
106 appliance.getType());
109 } catch (CommunicationException | AuthorizationException e) {
110 logger.debug("Exception during scan.", e);
113 logger.debug("Finished device scan.");
117 public void deactivate() {
119 var bridgeHandler = this.bridgeHandler;
120 if (bridgeHandler != null) {
121 removeOlderResults(System.currentTimeMillis(), bridgeHandler.getThing().getUID());
126 protected synchronized void stopScan() {
128 var bridgeHandler = this.bridgeHandler;
129 if (bridgeHandler != null) {
130 removeOlderResults(getTimestampOfLastScan(), bridgeHandler.getThing().getUID());
134 private @Nullable ThingTypeUID getThingTypeUID(HomeAppliance appliance) {
136 ThingTypeUID thingTypeUID = null;
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;