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.automower.internal.discovery;
15 import static org.openhab.binding.automower.internal.AutomowerBindingConstants.THING_TYPE_AUTOMOWER;
17 import java.util.Collections;
18 import java.util.HashMap;
20 import java.util.Optional;
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;
34 * The {@link AutomowerDiscoveryService} is responsible for discovering new mowers available for the
37 * @author Markus Pfleger - Initial contribution
40 public class AutomowerDiscoveryService extends AbstractDiscoveryService {
42 private final AutomowerBridgeHandler bridgeHandler;
44 public AutomowerDiscoveryService(AutomowerBridgeHandler bridgeHandler) {
45 super(Collections.singleton(THING_TYPE_AUTOMOWER), 10, false);
46 this.bridgeHandler = bridgeHandler;
50 protected void startScan() {
51 Optional<MowerListResult> registeredMowers = bridgeHandler.getAutomowers();
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());
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());
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() + ")")
73 thingDiscovered(discoveryResult);