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.groupepsa.internal.discovery;
15 import static org.openhab.binding.groupepsa.internal.GroupePSABindingConstants.THING_TYPE_VEHICLE;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.List;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.groupepsa.internal.GroupePSABindingConstants;
25 import org.openhab.binding.groupepsa.internal.bridge.GroupePSABridgeHandler;
26 import org.openhab.binding.groupepsa.internal.rest.api.dto.Vehicle;
27 import org.openhab.binding.groupepsa.internal.rest.exceptions.GroupePSACommunicationException;
28 import org.openhab.core.config.discovery.AbstractDiscoveryService;
29 import org.openhab.core.config.discovery.DiscoveryResult;
30 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.openhab.core.thing.binding.ThingHandler;
34 import org.openhab.core.thing.binding.ThingHandlerService;
35 import org.osgi.service.component.annotations.Component;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * The {@link GroupePSADiscoveryService} is responsible for discovering new
41 * vehicles available for the configured app key.
43 * @author Arjan Mels - Initial contribution
45 @Component(service = ThingHandlerService.class)
47 public class GroupePSADiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
48 private final Logger logger = LoggerFactory.getLogger(GroupePSADiscoveryService.class);
50 private @Nullable GroupePSABridgeHandler bridgeHandler;
52 public GroupePSADiscoveryService() {
53 super(Collections.singleton(THING_TYPE_VEHICLE), 10, false);
57 public void setThingHandler(@Nullable ThingHandler handler) {
58 if (handler instanceof GroupePSABridgeHandler) {
59 bridgeHandler = (GroupePSABridgeHandler) handler;
64 public @Nullable ThingHandler getThingHandler() {
69 public void deactivate() {
74 protected void startScan() {
76 GroupePSABridgeHandler localBridgeHandler = bridgeHandler;
77 if (localBridgeHandler == null) {
80 List<Vehicle> vehicles = localBridgeHandler.getVehicles();
81 if (vehicles == null || vehicles.isEmpty()) {
82 logger.warn("No vehicles found");
85 for (Vehicle vehicle : vehicles) {
86 ThingUID bridgeUID = localBridgeHandler.getThing().getUID();
87 ThingTypeUID thingTypeUID = THING_TYPE_VEHICLE;
88 String id = vehicle.getId();
90 ThingUID vehicleThingUid = new ThingUID(THING_TYPE_VEHICLE, bridgeUID, id);
92 Map<String, Object> properties = new HashMap<>();
93 putProperty(properties, GroupePSABindingConstants.VEHICLE_ID, id);
94 putProperty(properties, GroupePSABindingConstants.VEHICLE_VIN, vehicle.getVin());
95 putProperty(properties, GroupePSABindingConstants.VEHICLE_VENDOR, vehicle.getBrand());
96 putProperty(properties, GroupePSABindingConstants.VEHICLE_MODEL, vehicle.getLabel());
98 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(vehicleThingUid)
99 .withThingType(thingTypeUID).withProperties(properties).withBridge(bridgeUID)
100 .withRepresentationProperty(GroupePSABindingConstants.VEHICLE_VIN)
101 .withLabel(vehicle.getBrand() + " (" + vehicle.getVin() + ")").build();
103 thingDiscovered(discoveryResult);
106 } catch (GroupePSACommunicationException e) {
107 logger.warn("No vehicles found", e);
112 private void putProperty(Map<String, Object> properties, String key, @Nullable String value) {
116 properties.put(key, value);