2 * Copyright (c) 2010-2020 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.Collections;
19 import java.util.HashMap;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.avmfritz.internal.dto.AVMFritzBaseModel;
28 import org.openhab.binding.avmfritz.internal.dto.GroupModel;
29 import org.openhab.binding.avmfritz.internal.handler.AVMFritzBaseBridgeHandler;
30 import org.openhab.binding.avmfritz.internal.hardware.FritzAhaStatusListener;
31 import org.openhab.core.config.discovery.AbstractDiscoveryService;
32 import org.openhab.core.config.discovery.DiscoveryResult;
33 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
34 import org.openhab.core.config.discovery.DiscoveryService;
35 import org.openhab.core.thing.ThingTypeUID;
36 import org.openhab.core.thing.ThingUID;
37 import org.openhab.core.thing.binding.ThingHandler;
38 import org.openhab.core.thing.binding.ThingHandlerService;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Discover all AHA (AVM Home Automation) devices connected to a FRITZ!Box device.
45 * @author Robert Bausdorf - Initial contribution
46 * @author Christoph Weitkamp - Added support for groups
49 public class AVMFritzDiscoveryService extends AbstractDiscoveryService
50 implements FritzAhaStatusListener, DiscoveryService, ThingHandlerService {
52 private final Logger logger = LoggerFactory.getLogger(AVMFritzDiscoveryService.class);
54 * Handler of the bridge of which devices have to be discovered.
56 private @NonNullByDefault({}) AVMFritzBaseBridgeHandler bridgeHandler;
58 public AVMFritzDiscoveryService() {
60 .unmodifiableSet(Stream
61 .of(SUPPORTED_BUTTON_THING_TYPES_UIDS, SUPPORTED_HEATING_THING_TYPES,
62 SUPPORTED_DEVICE_THING_TYPES_UIDS, SUPPORTED_GROUP_THING_TYPES_UIDS)
63 .flatMap(Set::stream).collect(Collectors.toSet())),
68 public void activate() {
70 bridgeHandler.registerStatusListener(this);
74 public void deactivate() {
75 bridgeHandler.unregisterStatusListener(this);
80 public void startScan() {
81 logger.debug("Start manual scan on bridge {}", bridgeHandler.getThing().getUID());
82 bridgeHandler.handleRefreshCommand();
86 protected synchronized void stopScan() {
87 logger.debug("Stop manual scan on bridge {}", bridgeHandler.getThing().getUID());
92 public void setThingHandler(@NonNullByDefault({}) ThingHandler handler) {
93 if (handler instanceof AVMFritzBaseBridgeHandler) {
94 bridgeHandler = (AVMFritzBaseBridgeHandler) handler;
99 public @Nullable ThingHandler getThingHandler() {
100 return bridgeHandler;
104 public void onDeviceAdded(AVMFritzBaseModel device) {
105 ThingTypeUID thingTypeUID = new ThingTypeUID(BINDING_ID, bridgeHandler.getThingTypeId(device));
106 if (getSupportedThingTypes().contains(thingTypeUID)) {
107 ThingUID thingUID = new ThingUID(thingTypeUID, bridgeHandler.getThing().getUID(),
108 bridgeHandler.getThingName(device));
109 onDeviceAddedInternal(thingUID, device);
111 logger.debug("Discovered unsupported device: {}", device);
116 public void onDeviceUpdated(ThingUID thingUID, AVMFritzBaseModel device) {
117 onDeviceAddedInternal(thingUID, device);
121 public void onDeviceGone(ThingUID thingUID) {
125 private void onDeviceAddedInternal(ThingUID thingUID, AVMFritzBaseModel device) {
126 if (device.getPresent() == 1) {
127 Map<String, Object> properties = new HashMap<>();
128 properties.put(CONFIG_AIN, device.getIdentifier());
129 properties.put(PROPERTY_VENDOR, device.getManufacturer());
130 properties.put(PROPERTY_MODEL_ID, device.getDeviceId());
131 properties.put(PROPERTY_SERIAL_NUMBER, device.getIdentifier());
132 properties.put(PROPERTY_FIRMWARE_VERSION, device.getFirmwareVersion());
133 if (device instanceof GroupModel && ((GroupModel) device).getGroupinfo() != null) {
134 properties.put(PROPERTY_MASTER, ((GroupModel) device).getGroupinfo().getMasterdeviceid());
135 properties.put(PROPERTY_MEMBERS, ((GroupModel) device).getGroupinfo().getMembers());
138 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
139 .withRepresentationProperty(CONFIG_AIN).withBridge(bridgeHandler.getThing().getUID())
140 .withLabel(device.getName()).build();
142 thingDiscovered(discoveryResult);
144 thingRemoved(thingUID);