]> git.basschouten.com Git - openhab-addons.git/blob
883d2988cf7369ec6b79f93ccc6401470f921c78
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.Set;
18
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;
32
33 /**
34  * The {@link MyQDiscoveryService} is responsible for discovering MyQ things
35  *
36  * @author Dan Cunningham - Initial contribution
37  */
38 @NonNullByDefault
39 public class MyQDiscoveryService extends AbstractDiscoveryService implements DiscoveryService, ThingHandlerService {
40
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;
44
45     public MyQDiscoveryService() {
46         super(SUPPORTED_DISCOVERY_THING_TYPES_UIDS, 1, true);
47     }
48
49     @Override
50     public Set<ThingTypeUID> getSupportedThingTypes() {
51         return SUPPORTED_DISCOVERY_THING_TYPES_UIDS;
52     }
53
54     @Override
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);
70                     }
71                 });
72             }
73         }
74     }
75
76     @Override
77     public void setThingHandler(ThingHandler handler) {
78         if (handler instanceof MyQAccountHandler) {
79             accountHandler = (MyQAccountHandler) handler;
80         }
81     }
82
83     @Override
84     public @Nullable ThingHandler getThingHandler() {
85         return accountHandler;
86     }
87
88     @Override
89     public void activate() {
90         super.activate(null);
91     }
92
93     @Override
94     public void deactivate() {
95         super.deactivate();
96     }
97 }