2 * Copyright (c) 2010-2024 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.hydrawise.internal.discovery;
15 import java.time.Instant;
16 import java.util.List;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.hydrawise.internal.HydrawiseBindingConstants;
21 import org.openhab.binding.hydrawise.internal.HydrawiseControllerListener;
22 import org.openhab.binding.hydrawise.internal.api.graphql.dto.Controller;
23 import org.openhab.binding.hydrawise.internal.api.graphql.dto.Customer;
24 import org.openhab.binding.hydrawise.internal.handler.HydrawiseAccountHandler;
25 import org.openhab.core.config.discovery.AbstractThingHandlerDiscoveryService;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.thing.ThingUID;
28 import org.openhab.core.thing.binding.ThingHandlerService;
29 import org.osgi.service.component.annotations.Component;
30 import org.osgi.service.component.annotations.ServiceScope;
34 * @author Dan Cunningham - Initial contribution
39 @Component(scope = ServiceScope.PROTOTYPE, service = ThingHandlerService.class)
40 public class HydrawiseCloudControllerDiscoveryService
41 extends AbstractThingHandlerDiscoveryService<HydrawiseAccountHandler> implements HydrawiseControllerListener {
42 private static final int TIMEOUT = 5;
44 public HydrawiseCloudControllerDiscoveryService() {
45 super(HydrawiseAccountHandler.class, Set.of(HydrawiseBindingConstants.THING_TYPE_CONTROLLER), TIMEOUT, true);
49 protected void startScan() {
50 Customer data = thingHandler.lastData();
52 data.controllers.forEach(controller -> addDiscoveryResults(controller));
57 public void dispose() {
59 removeOlderResults(Instant.now().toEpochMilli(), thingHandler.getThing().getUID());
63 protected synchronized void stopScan() {
65 removeOlderResults(getTimestampOfLastScan(), thingHandler.getThing().getUID());
69 public void onData(List<Controller> controllers) {
70 controllers.forEach(controller -> addDiscoveryResults(controller));
74 public void initialize() {
75 thingHandler.addControllerListeners(this);
79 private void addDiscoveryResults(Controller controller) {
80 String label = String.format("Hydrawise Controller %s", controller.name);
81 int id = controller.id;
82 ThingUID bridgeUID = thingHandler.getThing().getUID();
83 ThingUID thingUID = new ThingUID(HydrawiseBindingConstants.THING_TYPE_CONTROLLER, bridgeUID,
85 thingDiscovered(DiscoveryResultBuilder.create(thingUID).withLabel(label).withBridge(bridgeUID)
86 .withProperty(HydrawiseBindingConstants.CONFIG_CONTROLLER_ID, id)
87 .withRepresentationProperty(HydrawiseBindingConstants.CONFIG_CONTROLLER_ID).build());