]> git.basschouten.com Git - openhab-addons.git/blob
2f575fdf64c894032ad4d5c40d88f4e1c868be58
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.tesla.internal.discovery;
14
15 import java.util.Map;
16
17 import org.openhab.binding.tesla.internal.TeslaBindingConstants;
18 import org.openhab.binding.tesla.internal.TeslaHandlerFactory;
19 import org.openhab.binding.tesla.internal.handler.TeslaAccountHandler;
20 import org.openhab.binding.tesla.internal.handler.VehicleListener;
21 import org.openhab.binding.tesla.internal.protocol.Vehicle;
22 import org.openhab.binding.tesla.internal.protocol.VehicleConfig;
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.ThingTypeUID;
28 import org.openhab.core.thing.ThingUID;
29 import org.openhab.core.thing.binding.ThingHandler;
30 import org.openhab.core.thing.binding.ThingHandlerService;
31 import org.osgi.service.component.annotations.Component;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * This service is used by {@link TeslaAccountHandler} instances in order to
37  * automatically provide vehicle information from the account.
38  *
39  * @author Kai Kreuzer - Initial contribution
40  *
41  */
42 @Component(service = ThingHandlerService.class)
43 public class TeslaVehicleDiscoveryService extends AbstractDiscoveryService
44         implements DiscoveryService, VehicleListener, ThingHandlerService {
45     private final Logger logger = LoggerFactory.getLogger(TeslaVehicleDiscoveryService.class);
46
47     public TeslaVehicleDiscoveryService() throws IllegalArgumentException {
48         super(TeslaHandlerFactory.SUPPORTED_THING_TYPES_UIDS, 10, true);
49     }
50
51     private TeslaAccountHandler handler;
52
53     @Override
54     public void setThingHandler(ThingHandler handler) {
55         this.handler = (TeslaAccountHandler) handler;
56         this.handler.addVehicleListener(this);
57     }
58
59     @Override
60     public ThingHandler getThingHandler() {
61         return handler;
62     }
63
64     @Override
65     protected void startScan() {
66         handler.scanForVehicles();
67     }
68
69     @Override
70     public void activate(Map<String, Object> configProperties) {
71         super.activate(configProperties);
72     }
73
74     @Override
75     public void deactivate() {
76         super.deactivate();
77         if (handler != null) {
78             handler.removeVehicleListener(this);
79         }
80     }
81
82     @Override
83     public void vehicleFound(Vehicle vehicle, VehicleConfig vehicleConfig) {
84         ThingTypeUID type = vehicleConfig == null ? TeslaBindingConstants.THING_TYPE_VEHICLE
85                 : vehicleConfig.identifyModel();
86         if (type != null) {
87             logger.debug("Found a {} vehicle", type.getId());
88             ThingUID thingUID = new ThingUID(type, handler.getThing().getUID(), vehicle.vin);
89             DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withLabel(vehicle.display_name)
90                     .withBridge(handler.getThing().getUID()).withProperty(TeslaBindingConstants.VIN, vehicle.vin)
91                     .build();
92             thingDiscovered(discoveryResult);
93         }
94     }
95 }