2 * Copyright (c) 2010-2021 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;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.myq.internal.dto.DevicesDTO;
22 import org.openhab.binding.myq.internal.handler.MyQAccountHandler;
23 import org.openhab.core.config.discovery.AbstractDiscoveryService;
24 import org.openhab.core.config.discovery.DiscoveryResult;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.config.discovery.DiscoveryService;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingTypeUID;
29 import org.openhab.core.thing.ThingUID;
30 import org.openhab.core.thing.binding.ThingHandler;
31 import org.openhab.core.thing.binding.ThingHandlerService;
34 * The {@link MyQDiscoveryService} is responsible for discovering MyQ things
36 * @author Dan Cunningham - Initial contribution
39 public class MyQDiscoveryService extends AbstractDiscoveryService implements DiscoveryService, ThingHandlerService {
41 private static final Set<ThingTypeUID> SUPPORTED_DISCOVERY_THING_TYPES_UIDS = Set
42 .of(MyQBindingConstants.THING_TYPE_GARAGEDOOR, MyQBindingConstants.THING_TYPE_LAMP);
43 private @Nullable MyQAccountHandler accountHandler;
45 public MyQDiscoveryService() {
46 super(SUPPORTED_DISCOVERY_THING_TYPES_UIDS, 1, true);
50 public Set<ThingTypeUID> getSupportedThingTypes() {
51 return SUPPORTED_DISCOVERY_THING_TYPES_UIDS;
55 public void startScan() {
56 MyQAccountHandler accountHandler = this.accountHandler;
57 if (accountHandler != null) {
58 DevicesDTO devices = accountHandler.devicesCache();
59 if (devices != null) {
60 devices.items.forEach(device -> {
61 ThingTypeUID thingTypeUID = new ThingTypeUID(BINDING_ID, device.deviceFamily);
62 if (SUPPORTED_DISCOVERY_THING_TYPES_UIDS.contains(thingTypeUID)) {
63 ThingUID thingUID = new ThingUID(thingTypeUID, accountHandler.getThing().getUID(),
64 device.serialNumber.toLowerCase());
65 DiscoveryResult result = DiscoveryResultBuilder.create(thingUID).withLabel("MyQ " + device.name)
66 .withProperty(Thing.PROPERTY_SERIAL_NUMBER, thingUID.getId())
67 .withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER)
68 .withBridge(accountHandler.getThing().getUID()).build();
69 thingDiscovered(result);
77 public void setThingHandler(ThingHandler handler) {
78 if (handler instanceof MyQAccountHandler) {
79 accountHandler = (MyQAccountHandler) handler;
84 public @Nullable ThingHandler getThingHandler() {
85 return accountHandler;
89 public void activate() {
94 public void deactivate() {