]> git.basschouten.com Git - openhab-addons.git/blob
4f3c13d13282a11273c7e94e7dc2effded0838f5
[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.automower.internal.discovery;
14
15 import static org.openhab.binding.automower.internal.AutomowerBindingConstants.THING_TYPE_AUTOMOWER;
16
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Optional;
20 import java.util.Set;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.automower.internal.AutomowerBindingConstants;
24 import org.openhab.binding.automower.internal.bridge.AutomowerBridgeHandler;
25 import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.Mower;
26 import org.openhab.binding.automower.internal.rest.api.automowerconnect.dto.MowerListResult;
27 import org.openhab.core.config.discovery.AbstractDiscoveryService;
28 import org.openhab.core.config.discovery.DiscoveryResult;
29 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32
33 /**
34  * The {@link AutomowerDiscoveryService} is responsible for discovering new mowers available for the
35  * configured app key.
36  *
37  * @author Markus Pfleger - Initial contribution
38  */
39 @NonNullByDefault
40 public class AutomowerDiscoveryService extends AbstractDiscoveryService {
41
42     private final AutomowerBridgeHandler bridgeHandler;
43
44     public AutomowerDiscoveryService(AutomowerBridgeHandler bridgeHandler) {
45         super(Set.of(THING_TYPE_AUTOMOWER), 10, false);
46         this.bridgeHandler = bridgeHandler;
47     }
48
49     @Override
50     protected void startScan() {
51         Optional<MowerListResult> registeredMowers = bridgeHandler.getAutomowers();
52
53         registeredMowers.ifPresent(mowers -> {
54             for (Mower mower : mowers.getData()) {
55                 ThingUID bridgeUID = bridgeHandler.getThing().getUID();
56                 ThingTypeUID thingTypeUID = THING_TYPE_AUTOMOWER;
57                 ThingUID mowerThingUid = new ThingUID(THING_TYPE_AUTOMOWER, bridgeUID, mower.getId());
58
59                 Map<String, Object> properties = new HashMap<>();
60                 properties.put(AutomowerBindingConstants.AUTOMOWER_ID, mower.getId());
61                 properties.put(AutomowerBindingConstants.AUTOMOWER_SERIAL_NUMBER,
62                         mower.getAttributes().getSystem().getSerialNumber());
63                 properties.put(AutomowerBindingConstants.AUTOMOWER_MODEL, mower.getAttributes().getSystem().getModel());
64                 properties.put(AutomowerBindingConstants.AUTOMOWER_NAME, mower.getAttributes().getSystem().getName());
65
66                 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(mowerThingUid)
67                         .withThingType(thingTypeUID).withProperties(properties).withBridge(bridgeUID)
68                         .withRepresentationProperty(AutomowerBindingConstants.AUTOMOWER_ID)
69                         .withLabel(mower.getAttributes().getSystem().getName() + " (Automower "
70                                 + mower.getAttributes().getSystem().getModel() + ")")
71                         .build();
72
73                 thingDiscovered(discoveryResult);
74             }
75         });
76     }
77 }