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.mynice.internal.discovery;
15 import static org.openhab.binding.mynice.internal.MyNiceBindingConstants.*;
17 import java.util.List;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.mynice.internal.handler.It4WifiHandler;
23 import org.openhab.binding.mynice.internal.handler.MyNiceDataListener;
24 import org.openhab.binding.mynice.internal.xml.dto.CommandType;
25 import org.openhab.binding.mynice.internal.xml.dto.Device;
26 import org.openhab.core.config.discovery.AbstractDiscoveryService;
27 import org.openhab.core.config.discovery.DiscoveryResult;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.thing.ThingUID;
30 import org.openhab.core.thing.binding.ThingHandler;
31 import org.openhab.core.thing.binding.ThingHandlerService;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * The {@link MyNiceDiscoveryService} is responsible for discovering all things
37 * except the It4Wifi bridge itself
39 * @author Gaƫl L'hopital - Initial contribution
42 public class MyNiceDiscoveryService extends AbstractDiscoveryService
43 implements MyNiceDataListener, ThingHandlerService {
45 private static final int SEARCH_TIME = 5;
46 private final Logger logger = LoggerFactory.getLogger(MyNiceDiscoveryService.class);
48 private @Nullable It4WifiHandler bridgeHandler;
51 * Creates a MyNiceDiscoveryService with background discovery disabled.
53 public MyNiceDiscoveryService() {
54 super(Set.of(THING_TYPE_SWING), SEARCH_TIME, false);
58 public void setThingHandler(ThingHandler handler) {
59 if (handler instanceof It4WifiHandler it4Handler) {
60 bridgeHandler = it4Handler;
65 public @Nullable ThingHandler getThingHandler() {
70 public void activate() {
72 It4WifiHandler handler = bridgeHandler;
73 if (handler != null) {
74 handler.registerDataListener(this);
79 public void deactivate() {
80 It4WifiHandler handler = bridgeHandler;
81 if (handler != null) {
82 handler.unregisterDataListener(this);
88 public void onDataFetched(List<Device> devices) {
89 It4WifiHandler handler = bridgeHandler;
90 if (handler != null) {
91 ThingUID bridgeUID = handler.getThing().getUID();
92 devices.stream().filter(device -> device.type != null).forEach(device -> {
93 ThingUID thingUID = switch (device.type) {
94 case SWING -> new ThingUID(THING_TYPE_SWING, bridgeUID, device.id);
95 case SLIDING -> new ThingUID(THING_TYPE_SLIDING, bridgeUID, device.id);
99 if (thingUID != null) {
100 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withBridge(bridgeUID)
101 .withLabel(String.format("%s %s", device.manuf, device.prod))
102 .withRepresentationProperty(DEVICE_ID).withProperty(DEVICE_ID, device.id).build();
103 thingDiscovered(discoveryResult);
105 logger.info("`{}` type of device is not yet supported", device.type);
112 protected void startScan() {
113 It4WifiHandler handler = bridgeHandler;
114 if (handler != null) {
115 handler.sendCommand(CommandType.INFO);