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.freebox.internal.discovery;
15 import java.util.HashMap;
16 import java.util.List;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.freebox.internal.FreeboxBindingConstants;
22 import org.openhab.binding.freebox.internal.FreeboxDataListener;
23 import org.openhab.binding.freebox.internal.api.FreeboxException;
24 import org.openhab.binding.freebox.internal.api.model.FreeboxAirMediaReceiver;
25 import org.openhab.binding.freebox.internal.api.model.FreeboxLanHost;
26 import org.openhab.binding.freebox.internal.api.model.FreeboxLanHostL3Connectivity;
27 import org.openhab.binding.freebox.internal.config.FreeboxAirPlayDeviceConfiguration;
28 import org.openhab.binding.freebox.internal.config.FreeboxNetDeviceConfiguration;
29 import org.openhab.binding.freebox.internal.config.FreeboxNetInterfaceConfiguration;
30 import org.openhab.binding.freebox.internal.config.FreeboxServerConfiguration;
31 import org.openhab.binding.freebox.internal.handler.FreeboxHandler;
32 import org.openhab.core.config.core.Configuration;
33 import org.openhab.core.config.discovery.AbstractDiscoveryService;
34 import org.openhab.core.config.discovery.DiscoveryResult;
35 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
36 import org.openhab.core.thing.Thing;
37 import org.openhab.core.thing.ThingStatus;
38 import org.openhab.core.thing.ThingUID;
39 import org.openhab.core.thing.binding.ThingHandler;
40 import org.openhab.core.thing.binding.ThingHandlerService;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * The {@link FreeboxDiscoveryService} is responsible for discovering all things
46 * except the Freebox Server thing itself
48 * @author Laurent Garnier - Initial contribution
49 * @author Laurent Garnier - add discovery settings
50 * @author Laurent Garnier - use new internal API manager
51 * @author Laurent Garnier - use ThingHandlerService
54 public class FreeboxDiscoveryService extends AbstractDiscoveryService
55 implements FreeboxDataListener, ThingHandlerService {
57 private final Logger logger = LoggerFactory.getLogger(FreeboxDiscoveryService.class);
59 private static final int SEARCH_TIME = 10;
61 private static final String PHONE_ID = "wired";
63 private @Nullable FreeboxHandler bridgeHandler;
64 private boolean discoverPhone;
65 private boolean discoverNetDevice;
66 private boolean discoverNetInterface;
67 private boolean discoverAirPlayReceiver;
70 * Creates a FreeboxDiscoveryService with background discovery disabled.
72 public FreeboxDiscoveryService() {
73 super(FreeboxBindingConstants.SUPPORTED_THING_TYPES_UIDS, SEARCH_TIME, false);
74 this.discoverPhone = true;
75 this.discoverNetDevice = true;
76 this.discoverNetInterface = true;
77 this.discoverAirPlayReceiver = true;
81 public void setThingHandler(ThingHandler handler) {
82 if (handler instanceof FreeboxHandler freeboxHandler) {
83 bridgeHandler = freeboxHandler;
88 public @Nullable ThingHandler getThingHandler() {
93 public void activate() {
95 FreeboxHandler handler = bridgeHandler;
96 if (handler != null) {
97 Configuration config = handler.getThing().getConfiguration();
98 Object property = config.get(FreeboxServerConfiguration.DISCOVER_PHONE);
99 discoverPhone = property != null ? ((Boolean) property).booleanValue() : true;
100 property = config.get(FreeboxServerConfiguration.DISCOVER_NET_DEVICE);
101 discoverNetDevice = property != null ? ((Boolean) property).booleanValue() : true;
102 property = config.get(FreeboxServerConfiguration.DISCOVER_NET_INTERFACE);
103 discoverNetInterface = property != null ? ((Boolean) property).booleanValue() : true;
104 property = config.get(FreeboxServerConfiguration.DISCOVER_AIRPLAY_RECEIVER);
105 discoverAirPlayReceiver = property != null ? ((Boolean) property).booleanValue() : true;
106 logger.debug("Freebox discovery - discoverPhone : {}", discoverPhone);
107 logger.debug("Freebox discovery - discoverNetDevice : {}", discoverNetDevice);
108 logger.debug("Freebox discovery - discoverNetInterface : {}", discoverNetInterface);
109 logger.debug("Freebox discovery - discoverAirPlayReceiver : {}", discoverAirPlayReceiver);
111 handler.registerDataListener(this);
116 public void deactivate() {
117 FreeboxHandler handler = bridgeHandler;
118 if (handler != null) {
119 handler.unregisterDataListener(this);
125 protected void startScan() {
126 logger.debug("Starting Freebox discovery scan");
127 FreeboxHandler handler = bridgeHandler;
128 if (handler != null && handler.getThing().getStatus() == ThingStatus.ONLINE) {
130 List<FreeboxLanHost> lanHosts = handler.getApiManager().getLanHosts();
131 List<FreeboxAirMediaReceiver> airPlayDevices = handler.getApiManager().getAirMediaReceivers();
132 onDataFetched(handler.getThing().getUID(), lanHosts, airPlayDevices);
133 } catch (FreeboxException e) {
134 logger.warn("Error while requesting data for things discovery", e);
140 public void onDataFetched(ThingUID bridge, @Nullable List<FreeboxLanHost> lanHosts,
141 @Nullable List<FreeboxAirMediaReceiver> airPlayDevices) {
143 DiscoveryResult discoveryResult;
147 thingUID = new ThingUID(FreeboxBindingConstants.FREEBOX_THING_TYPE_PHONE, bridge, PHONE_ID);
148 logger.trace("Adding new Freebox Phone {} to inbox", thingUID);
149 discoveryResult = DiscoveryResultBuilder.create(thingUID).withBridge(bridge).withLabel("Wired phone")
151 thingDiscovered(discoveryResult);
154 if (lanHosts != null && (discoverNetDevice || discoverNetInterface)) {
156 for (FreeboxLanHost host : lanHosts) {
157 String mac = host.getMAC();
158 String primaryName = host.getPrimaryName();
159 String vendorName = host.getVendorName();
160 if (mac != null && !mac.isEmpty()) {
161 if (discoverNetDevice) {
162 String uid = mac.replaceAll("[^A-Za-z0-9_]", "_");
163 thingUID = new ThingUID(FreeboxBindingConstants.FREEBOX_THING_TYPE_NET_DEVICE, bridge, uid);
164 String name = (primaryName == null || primaryName.isEmpty()) ? ("Freebox Network Device " + mac)
166 logger.trace("Adding new Freebox Network Device {} to inbox", thingUID);
167 Map<String, Object> properties = new HashMap<>(1);
168 if (vendorName != null && !vendorName.isEmpty()) {
169 properties.put(Thing.PROPERTY_VENDOR, vendorName);
171 properties.put(FreeboxNetDeviceConfiguration.MAC_ADDRESS, mac);
172 discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
173 .withBridge(bridge).withLabel(name).build();
174 thingDiscovered(discoveryResult);
177 // Network interfaces
178 if (host.getL3Connectivities() != null && discoverNetInterface) {
179 for (FreeboxLanHostL3Connectivity l3 : host.getL3Connectivities()) {
180 String addr = l3.getAddr();
181 if (addr != null && !addr.isEmpty()) {
182 String uid = addr.replaceAll("[^A-Za-z0-9_]", "_");
183 thingUID = new ThingUID(FreeboxBindingConstants.FREEBOX_THING_TYPE_NET_INTERFACE,
186 if (primaryName != null && !primaryName.isEmpty()) {
187 name += " (" + (primaryName + ")");
189 logger.trace("Adding new Freebox Network Interface {} to inbox", thingUID);
190 Map<String, Object> properties = new HashMap<>(1);
191 if (vendorName != null && !vendorName.isEmpty()) {
192 properties.put(Thing.PROPERTY_VENDOR, vendorName);
194 properties.put(FreeboxNetInterfaceConfiguration.IP_ADDRESS, addr);
195 discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
196 .withBridge(bridge).withLabel(name).build();
197 thingDiscovered(discoveryResult);
205 if (airPlayDevices != null && discoverAirPlayReceiver) {
207 for (FreeboxAirMediaReceiver device : airPlayDevices) {
208 String name = device.getName();
209 boolean videoCapable = device.isVideoCapable();
210 logger.debug("AirPlay Device name {} video capable {}", name, videoCapable);
211 // The Freebox API allows pushing media only to receivers with photo or video capabilities
212 // but not to receivers with only audio capability; so receivers without video capability
213 // are ignored by the discovery
214 if (name != null && !name.isEmpty() && videoCapable) {
215 String uid = name.replaceAll("[^A-Za-z0-9_]", "_");
216 thingUID = new ThingUID(FreeboxBindingConstants.FREEBOX_THING_TYPE_AIRPLAY, bridge, uid);
217 logger.trace("Adding new Freebox AirPlay Device {} to inbox", thingUID);
218 Map<String, Object> properties = new HashMap<>(1);
219 properties.put(FreeboxAirPlayDeviceConfiguration.NAME, name);
220 discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
221 .withBridge(bridge).withLabel(name + " (AirPlay)").build();
222 thingDiscovered(discoveryResult);