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.avmfritz.internal.discovery;
15 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.*;
16 import static org.openhab.core.thing.Thing.*;
18 import java.util.HashMap;
21 import java.util.stream.Collectors;
22 import java.util.stream.Stream;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.openhab.binding.avmfritz.internal.dto.AVMFritzBaseModel;
27 import org.openhab.binding.avmfritz.internal.dto.GroupModel;
28 import org.openhab.binding.avmfritz.internal.handler.AVMFritzBaseBridgeHandler;
29 import org.openhab.binding.avmfritz.internal.hardware.FritzAhaStatusListener;
30 import org.openhab.core.config.discovery.AbstractDiscoveryService;
31 import org.openhab.core.config.discovery.DiscoveryResult;
32 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
33 import org.openhab.core.config.discovery.DiscoveryService;
34 import org.openhab.core.thing.ThingTypeUID;
35 import org.openhab.core.thing.ThingUID;
36 import org.openhab.core.thing.binding.ThingHandler;
37 import org.openhab.core.thing.binding.ThingHandlerService;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * Discover all AHA (AVM Home Automation) devices connected to a FRITZ!Box device.
44 * @author Robert Bausdorf - Initial contribution
45 * @author Christoph Weitkamp - Added support for groups
48 public class AVMFritzDiscoveryService extends AbstractDiscoveryService
49 implements FritzAhaStatusListener, DiscoveryService, ThingHandlerService {
51 private final Logger logger = LoggerFactory.getLogger(AVMFritzDiscoveryService.class);
53 * Handler of the bridge of which devices have to be discovered.
55 private @NonNullByDefault({}) AVMFritzBaseBridgeHandler bridgeHandler;
57 public AVMFritzDiscoveryService() {
59 .of(SUPPORTED_LIGHTING_THING_TYPES, SUPPORTED_BUTTON_THING_TYPES_UIDS, SUPPORTED_HEATING_THING_TYPES,
60 SUPPORTED_DEVICE_THING_TYPES_UIDS, SUPPORTED_GROUP_THING_TYPES_UIDS)
61 .flatMap(Set::stream).collect(Collectors.toUnmodifiableSet()), 30);
65 public void activate() {
67 bridgeHandler.registerStatusListener(this);
71 public void deactivate() {
72 bridgeHandler.unregisterStatusListener(this);
77 public void startScan() {
78 logger.debug("Start manual scan on bridge {}", bridgeHandler.getThing().getUID());
79 bridgeHandler.handleRefreshCommand();
83 protected synchronized void stopScan() {
84 logger.debug("Stop manual scan on bridge {}", bridgeHandler.getThing().getUID());
89 public void setThingHandler(@NonNullByDefault({}) ThingHandler handler) {
90 if (handler instanceof AVMFritzBaseBridgeHandler) {
91 bridgeHandler = (AVMFritzBaseBridgeHandler) handler;
96 public @Nullable ThingHandler getThingHandler() {
101 public void onDeviceAdded(AVMFritzBaseModel device) {
102 String id = bridgeHandler.getThingTypeId(device);
103 ThingTypeUID thingTypeUID = id.isEmpty() ? null : new ThingTypeUID(BINDING_ID, id);
104 if (thingTypeUID != null && getSupportedThingTypes().contains(thingTypeUID)) {
105 ThingUID thingUID = new ThingUID(thingTypeUID, bridgeHandler.getThing().getUID(),
106 bridgeHandler.getThingName(device));
107 onDeviceAddedInternal(thingUID, device);
109 logger.debug("Discovered unsupported device: {}", device);
114 public void onDeviceUpdated(ThingUID thingUID, AVMFritzBaseModel device) {
115 onDeviceAddedInternal(thingUID, device);
119 public void onDeviceGone(ThingUID thingUID) {
123 private void onDeviceAddedInternal(ThingUID thingUID, AVMFritzBaseModel device) {
124 if (device.getPresent() == 1) {
125 Map<String, Object> properties = new HashMap<>();
126 properties.put(CONFIG_AIN, device.getIdentifier());
127 properties.put(PROPERTY_VENDOR, device.getManufacturer());
128 properties.put(PRODUCT_NAME, device.getProductName());
129 properties.put(PROPERTY_SERIAL_NUMBER, device.getIdentifier());
130 properties.put(PROPERTY_FIRMWARE_VERSION, device.getFirmwareVersion());
131 if (device instanceof GroupModel && ((GroupModel) device).getGroupinfo() != null) {
132 properties.put(PROPERTY_MASTER, ((GroupModel) device).getGroupinfo().getMasterdeviceid());
133 properties.put(PROPERTY_MEMBERS, ((GroupModel) device).getGroupinfo().getMembers());
136 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
137 .withRepresentationProperty(CONFIG_AIN).withBridge(bridgeHandler.getThing().getUID())
138 .withLabel(device.getName()).build();
140 thingDiscovered(discoveryResult);
142 thingRemoved(thingUID);