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.meater.internal.discovery;
15 import static org.openhab.binding.meater.internal.MeaterBindingConstants.*;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.meater.internal.MeaterConfiguration;
22 import org.openhab.binding.meater.internal.handler.MeaterBridgeHandler;
23 import org.openhab.core.config.discovery.AbstractDiscoveryService;
24 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
25 import org.openhab.core.thing.ThingUID;
26 import org.openhab.core.thing.binding.ThingHandler;
27 import org.openhab.core.thing.binding.ThingHandlerService;
30 * The {@link MeaterDiscoveryService} searches for available
31 * Meater probes discoverable through MEATER REST API.
33 * @author Jan Gustafsson - Initial contribution
36 public class MeaterDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
37 private static final int SEARCH_TIME = 2;
38 private @Nullable MeaterBridgeHandler handler;
40 public MeaterDiscoveryService() {
41 super(SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME);
45 public void setThingHandler(@Nullable ThingHandler handler) {
46 if (handler instanceof MeaterBridgeHandler) {
47 this.handler = (MeaterBridgeHandler) handler;
48 i18nProvider = ((MeaterBridgeHandler) handler).getI18nProvider();
49 localeProvider = ((MeaterBridgeHandler) handler).getLocaleProvider();
54 public @Nullable ThingHandler getThingHandler() {
59 public void activate(@Nullable Map<String, Object> configProperties) {
60 super.activate(configProperties);
64 public void deactivate() {
69 protected void startScan() {
70 MeaterBridgeHandler bridgeHandler = this.handler;
71 if (bridgeHandler != null) {
72 ThingUID bridgeUID = bridgeHandler.getThing().getUID();
73 bridgeHandler.getMeaterThings().entrySet().stream().forEach(thing -> {
75 DiscoveryResultBuilder.create(new ThingUID(THING_TYPE_MEATER_PROBE, bridgeUID, thing.getKey()))
76 .withLabel("@text/discovery.probe.label").withBridge(bridgeUID)
77 .withProperty(MeaterConfiguration.DEVICE_ID_LABEL, thing.getKey())
78 .withRepresentationProperty(MeaterConfiguration.DEVICE_ID_LABEL).build());