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.myq.internal;
15 import static org.openhab.binding.myq.internal.MyQBindingConstants.BINDING_ID;
17 import java.util.List;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.myq.internal.dto.DeviceDTO;
23 import org.openhab.binding.myq.internal.handler.MyQAccountHandler;
24 import org.openhab.core.config.discovery.AbstractDiscoveryService;
25 import org.openhab.core.config.discovery.DiscoveryResult;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.config.discovery.DiscoveryService;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.thing.ThingUID;
31 import org.openhab.core.thing.binding.ThingHandler;
32 import org.openhab.core.thing.binding.ThingHandlerService;
35 * The {@link MyQDiscoveryService} is responsible for discovering MyQ things
37 * @author Dan Cunningham - Initial contribution
40 public class MyQDiscoveryService extends AbstractDiscoveryService implements DiscoveryService, ThingHandlerService {
42 private static final Set<ThingTypeUID> SUPPORTED_DISCOVERY_THING_TYPES_UIDS = Set
43 .of(MyQBindingConstants.THING_TYPE_GARAGEDOOR, MyQBindingConstants.THING_TYPE_LAMP);
44 private @Nullable MyQAccountHandler accountHandler;
46 public MyQDiscoveryService() {
47 super(SUPPORTED_DISCOVERY_THING_TYPES_UIDS, 1, true);
51 public Set<ThingTypeUID> getSupportedThingTypes() {
52 return SUPPORTED_DISCOVERY_THING_TYPES_UIDS;
56 public void startScan() {
57 MyQAccountHandler accountHandler = this.accountHandler;
58 if (accountHandler != null) {
59 List<DeviceDTO> devices = accountHandler.devicesCache();
60 if (devices != null) {
61 devices.forEach(device -> {
62 ThingTypeUID thingTypeUID = new ThingTypeUID(BINDING_ID, device.deviceFamily);
63 if (SUPPORTED_DISCOVERY_THING_TYPES_UIDS.contains(thingTypeUID)) {
64 ThingUID thingUID = new ThingUID(thingTypeUID, accountHandler.getThing().getUID(),
65 device.serialNumber.toLowerCase());
66 DiscoveryResult result = DiscoveryResultBuilder.create(thingUID).withLabel("MyQ " + device.name)
67 .withProperty(Thing.PROPERTY_SERIAL_NUMBER, thingUID.getId())
68 .withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER)
69 .withBridge(accountHandler.getThing().getUID()).build();
70 thingDiscovered(result);
78 public void startBackgroundDiscovery() {
83 public void setThingHandler(ThingHandler handler) {
84 if (handler instanceof MyQAccountHandler myqAccountHandler) {
85 accountHandler = myqAccountHandler;
90 public @Nullable ThingHandler getThingHandler() {
91 return accountHandler;
95 public void activate() {
100 public void deactivate() {