]> git.basschouten.com Git - openhab-addons.git/blob
26c95e76d542a70a503c58b7865935ff0e60d0da
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.myq.internal;
14
15 import static org.openhab.binding.myq.internal.MyQBindingConstants.BINDING_ID;
16
17 import java.util.List;
18 import java.util.Set;
19
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;
33
34 /**
35  * The {@link MyQDiscoveryService} is responsible for discovering MyQ things
36  *
37  * @author Dan Cunningham - Initial contribution
38  */
39 @NonNullByDefault
40 public class MyQDiscoveryService extends AbstractDiscoveryService implements DiscoveryService, ThingHandlerService {
41
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;
45
46     public MyQDiscoveryService() {
47         super(SUPPORTED_DISCOVERY_THING_TYPES_UIDS, 1, true);
48     }
49
50     @Override
51     public Set<ThingTypeUID> getSupportedThingTypes() {
52         return SUPPORTED_DISCOVERY_THING_TYPES_UIDS;
53     }
54
55     @Override
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);
71                     }
72                 });
73             }
74         }
75     }
76
77     @Override
78     public void startBackgroundDiscovery() {
79         startScan();
80     }
81
82     @Override
83     public void setThingHandler(ThingHandler handler) {
84         if (handler instanceof MyQAccountHandler) {
85             accountHandler = (MyQAccountHandler) handler;
86         }
87     }
88
89     @Override
90     public @Nullable ThingHandler getThingHandler() {
91         return accountHandler;
92     }
93
94     @Override
95     public void activate() {
96         super.activate(null);
97     }
98
99     @Override
100     public void deactivate() {
101         super.deactivate();
102     }
103 }