From 8d567e1682c634a1aaf2d4838f614b1dc619071f Mon Sep 17 00:00:00 2001 From: Johannes Ott Date: Fri, 17 Apr 2020 00:17:06 +0200 Subject: [PATCH] [DWDPollenflug] Small code improvments (#7395) * [DWDPollenflug] Small refactoring and improvements * [DWDPollenflug] Added DiscoveryService Signed-off-by: Johannes DerOetzi Ott --- .../README.md | 9 ++- .../DWDPollenflugBindingConstants.java | 15 +++++ .../internal/DWDPollenflugHandlerFactory.java | 9 --- .../DWDPollenflugDiscoveryService.java | 66 +++++++++++++++++++ .../handler/DWDPollenflugBridgeHandler.java | 2 +- .../handler/DWDPollenflugRegionHandler.java | 2 +- .../ESH-INF/i18n/dwdpollenflug_de.properties | 4 +- .../main/resources/ESH-INF/thing/bridge.xml | 2 +- .../main/resources/ESH-INF/thing/region.xml | 2 +- 9 files changed, 94 insertions(+), 17 deletions(-) create mode 100644 bundles/org.openhab.binding.dwdpollenflug/src/main/java/org/openhab/binding/dwdpollenflug/internal/discovery/DWDPollenflugDiscoveryService.java diff --git a/bundles/org.openhab.binding.dwdpollenflug/README.md b/bundles/org.openhab.binding.dwdpollenflug/README.md index af20a6a7c2580..6d23af4f8ebc0 100644 --- a/bundles/org.openhab.binding.dwdpollenflug/README.md +++ b/bundles/org.openhab.binding.dwdpollenflug/README.md @@ -11,17 +11,22 @@ https://isabel.dwd.de/DE/leistungen/gefahrenindizespollen/gefahrenindexpollen.ht This binding supports a bridge thing (`bridge`), which polls the dataset for Germany in an adjustable interval. And it supports a region thing (`region`), representing the data for all pollen types of a region or partregion. +## Discovery + +This binding adds a default `bridge` thing to the Inbox. +This can be used for `region` things you may add manually. + ## Thing Configuration ### Bridge -| Property | Default | Required | Description | +| Parameter | Default | Required | Description | | --------- | :-----: | :------: | ------------------------------------------------------------------------------------ | | `refresh` | 30 | no | Define the interval for polling the data from DWD in minutes. Minimum is 15 minutes. | ### Region -| Property | Default | Required | Description | +| Parameter | Default | Required | Description | | ---------- | :-----: | :------: | -------------------------------------------------------------------------------------------------------- | | `regionID` | - | yes | In PaperUI just select the region you want to display data for. For files-configuration see table below. | diff --git a/bundles/org.openhab.binding.dwdpollenflug/src/main/java/org/openhab/binding/dwdpollenflug/internal/DWDPollenflugBindingConstants.java b/bundles/org.openhab.binding.dwdpollenflug/src/main/java/org/openhab/binding/dwdpollenflug/internal/DWDPollenflugBindingConstants.java index 83498093978af..9f820c3ea481d 100644 --- a/bundles/org.openhab.binding.dwdpollenflug/src/main/java/org/openhab/binding/dwdpollenflug/internal/DWDPollenflugBindingConstants.java +++ b/bundles/org.openhab.binding.dwdpollenflug/src/main/java/org/openhab/binding/dwdpollenflug/internal/DWDPollenflugBindingConstants.java @@ -12,6 +12,11 @@ */ package org.openhab.binding.dwdpollenflug.internal; +import java.util.Collections; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.smarthome.core.thing.ThingTypeUID; @@ -28,10 +33,20 @@ public class DWDPollenflugBindingConstants { // bridge public static final ThingTypeUID THING_TYPE_BRIDGE = new ThingTypeUID(BINDING_ID, "bridge"); + public static final String DWD = "dwd"; + public static final String BRIDGE_LABEL = "DWD Pollen Count Index (Bridge)"; // List of all Thing Type UIDs public static final ThingTypeUID THING_TYPE_REGION = new ThingTypeUID(BINDING_ID, "region"); + // @formatter:off + public static final Set SUPPORTED_THING_TYPES_UIDS = + Collections.unmodifiableSet(Stream + .of(THING_TYPE_BRIDGE, THING_TYPE_REGION) + .collect(Collectors.toSet()) + ); + // @formatter:on + // Channels of pollen groups public static final String CHANNEL_TODAY = "today"; public static final String CHANNEL_TOMORROW = "tomorrow"; diff --git a/bundles/org.openhab.binding.dwdpollenflug/src/main/java/org/openhab/binding/dwdpollenflug/internal/DWDPollenflugHandlerFactory.java b/bundles/org.openhab.binding.dwdpollenflug/src/main/java/org/openhab/binding/dwdpollenflug/internal/DWDPollenflugHandlerFactory.java index 4e236775e30de..9d181c52ce4bd 100644 --- a/bundles/org.openhab.binding.dwdpollenflug/src/main/java/org/openhab/binding/dwdpollenflug/internal/DWDPollenflugHandlerFactory.java +++ b/bundles/org.openhab.binding.dwdpollenflug/src/main/java/org/openhab/binding/dwdpollenflug/internal/DWDPollenflugHandlerFactory.java @@ -14,11 +14,6 @@ import static org.openhab.binding.dwdpollenflug.internal.DWDPollenflugBindingConstants.*; -import java.util.Collections; -import java.util.Set; -import java.util.stream.Collectors; -import java.util.stream.Stream; - import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jetty.client.HttpClient; @@ -44,10 +39,6 @@ @NonNullByDefault @Component(configurationPid = "binding.dwdpollenflug", service = ThingHandlerFactory.class) public class DWDPollenflugHandlerFactory extends BaseThingHandlerFactory { - - private static final Set SUPPORTED_THING_TYPES_UIDS = Collections - .unmodifiableSet(Stream.of(THING_TYPE_BRIDGE, THING_TYPE_REGION).collect(Collectors.toSet())); - private final HttpClient httpClient; @Activate diff --git a/bundles/org.openhab.binding.dwdpollenflug/src/main/java/org/openhab/binding/dwdpollenflug/internal/discovery/DWDPollenflugDiscoveryService.java b/bundles/org.openhab.binding.dwdpollenflug/src/main/java/org/openhab/binding/dwdpollenflug/internal/discovery/DWDPollenflugDiscoveryService.java new file mode 100644 index 0000000000000..02b1276df62bf --- /dev/null +++ b/bundles/org.openhab.binding.dwdpollenflug/src/main/java/org/openhab/binding/dwdpollenflug/internal/discovery/DWDPollenflugDiscoveryService.java @@ -0,0 +1,66 @@ +/** + * Copyright (c) 2010-2020 Contributors to the openHAB project + * + * See the NOTICE file(s) distributed with this work for additional + * information. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0 + * + * SPDX-License-Identifier: EPL-2.0 + */ +package org.openhab.binding.dwdpollenflug.internal.discovery; + +import static org.openhab.binding.dwdpollenflug.internal.DWDPollenflugBindingConstants.*; + +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.smarthome.config.discovery.AbstractDiscoveryService; +import org.eclipse.smarthome.config.discovery.DiscoveryResult; +import org.eclipse.smarthome.config.discovery.DiscoveryResultBuilder; +import org.eclipse.smarthome.config.discovery.DiscoveryService; +import org.eclipse.smarthome.core.thing.ThingUID; +import org.osgi.service.component.annotations.Component; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The {@link DWDPollenflugDiscoveryService} create a default bridge thing. + * + * @author Johannes Ott - Initial contribution + */ +@NonNullByDefault +@Component(service = DiscoveryService.class, immediate = true, configurationPid = "discovery.dwdpollenflug") +public class DWDPollenflugDiscoveryService extends AbstractDiscoveryService { + private static final ThingUID BRIDGE_THING_UID = new ThingUID(THING_TYPE_BRIDGE, DWD); + private static final int DISCOVER_TIMEOUT_SECONDS = 2; + + private final Logger logger = LoggerFactory.getLogger(DWDPollenflugDiscoveryService.class); + + public DWDPollenflugDiscoveryService() throws IllegalArgumentException { + super(SUPPORTED_THING_TYPES_UIDS, DISCOVER_TIMEOUT_SECONDS, true); + } + + @Override + protected void startScan() { + logger.debug("Manual DWDPollenflug discovery scan."); + addBridge(); + } + + @Override + protected void startBackgroundDiscovery() { + logger.debug("Background DWDPollenflug discovery scan."); + addBridge(); + } + + private void addBridge() { + // @formatter:off + DiscoveryResult bridge = DiscoveryResultBuilder + .create(BRIDGE_THING_UID) + .withLabel(BRIDGE_LABEL) + .build(); + // @formatter:on + + thingDiscovered(bridge); + } +} diff --git a/bundles/org.openhab.binding.dwdpollenflug/src/main/java/org/openhab/binding/dwdpollenflug/internal/handler/DWDPollenflugBridgeHandler.java b/bundles/org.openhab.binding.dwdpollenflug/src/main/java/org/openhab/binding/dwdpollenflug/internal/handler/DWDPollenflugBridgeHandler.java index a34bcbb159122..225e86343d0a3 100644 --- a/bundles/org.openhab.binding.dwdpollenflug/src/main/java/org/openhab/binding/dwdpollenflug/internal/handler/DWDPollenflugBridgeHandler.java +++ b/bundles/org.openhab.binding.dwdpollenflug/src/main/java/org/openhab/binding/dwdpollenflug/internal/handler/DWDPollenflugBridgeHandler.java @@ -86,8 +86,8 @@ public void initialize() { bridgeConfig = getConfigAs(DWDPollenflugBridgeConfiguration.class); if (bridgeConfig.isValid()) { + updateStatus(ThingStatus.UNKNOWN); startPolling(); - updateStatus(ThingStatus.ONLINE); } else { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Refresh interval has to be at least 15 minutes."); diff --git a/bundles/org.openhab.binding.dwdpollenflug/src/main/java/org/openhab/binding/dwdpollenflug/internal/handler/DWDPollenflugRegionHandler.java b/bundles/org.openhab.binding.dwdpollenflug/src/main/java/org/openhab/binding/dwdpollenflug/internal/handler/DWDPollenflugRegionHandler.java index a4482e7527d02..1b1949f0eb5c5 100644 --- a/bundles/org.openhab.binding.dwdpollenflug/src/main/java/org/openhab/binding/dwdpollenflug/internal/handler/DWDPollenflugRegionHandler.java +++ b/bundles/org.openhab.binding.dwdpollenflug/src/main/java/org/openhab/binding/dwdpollenflug/internal/handler/DWDPollenflugRegionHandler.java @@ -62,7 +62,7 @@ public void initialize() { } } - private synchronized @Nullable DWDPollenflugBridgeHandler getBridgeHandler() { + private @Nullable DWDPollenflugBridgeHandler getBridgeHandler() { Bridge bridge = getBridge(); if (bridge != null) { ThingHandler handler = bridge.getHandler(); diff --git a/bundles/org.openhab.binding.dwdpollenflug/src/main/resources/ESH-INF/i18n/dwdpollenflug_de.properties b/bundles/org.openhab.binding.dwdpollenflug/src/main/resources/ESH-INF/i18n/dwdpollenflug_de.properties index 4c23588250dc8..16efc6c5ef21b 100644 --- a/bundles/org.openhab.binding.dwdpollenflug/src/main/resources/ESH-INF/i18n/dwdpollenflug_de.properties +++ b/bundles/org.openhab.binding.dwdpollenflug/src/main/resources/ESH-INF/i18n/dwdpollenflug_de.properties @@ -3,7 +3,7 @@ binding.dwdpollenflug.name = DWD Pollenflugindex binding.dwdpollenflug.description = Binding um den Pollenflugindex des deutschen Wetterdienstes anzuzeigen #bridge -thing-type.dwdpollenflug.bridge.label = DWD Pollenflugindex - Bridge +thing-type.dwdpollenflug.bridge.label = DWD Pollenflugindex (Bridge) thing-type.dwdpollenflug.bridge.description = Bridge um den Pollenflugindex des DWDs abzurufen. #bridge config @@ -11,7 +11,7 @@ thing-type.config.dwdpollenflug.bridge.refresh.label = Aktualisierungsinterval thing-type.config.dwdpollenflug.bridge.refresh.description = Zeit zwischen zwei API-Anfragen in Minuten. Minimum 15 Minuten. #thing types -thing-type.dwdpollenflug.region.label = DWD Pollenflugindex Region +thing-type.dwdpollenflug.region.label = DWD Pollenflugindex (Region) thing-type.dwdpollenflug.region.description = Pollenflugindex f\u00FCr eine bestimmte Region oder Teilregion thing-type.dwdpollenflug.region.group.updates.label = Aktualisierung thing-type.dwdpollenflug.region.group.updates.description = Informationen zur Aktualisierung diff --git a/bundles/org.openhab.binding.dwdpollenflug/src/main/resources/ESH-INF/thing/bridge.xml b/bundles/org.openhab.binding.dwdpollenflug/src/main/resources/ESH-INF/thing/bridge.xml index d103e0ac2aa67..68fb42028e654 100644 --- a/bundles/org.openhab.binding.dwdpollenflug/src/main/resources/ESH-INF/thing/bridge.xml +++ b/bundles/org.openhab.binding.dwdpollenflug/src/main/resources/ESH-INF/thing/bridge.xml @@ -5,7 +5,7 @@ xsi:schemaLocation="https://openhab.org/schemas/thing-description/v1.0.0 https://openhab.org/schemas/thing-description-1.0.0.xsd"> - + Bridge for accessing pollen count index data of the DWD diff --git a/bundles/org.openhab.binding.dwdpollenflug/src/main/resources/ESH-INF/thing/region.xml b/bundles/org.openhab.binding.dwdpollenflug/src/main/resources/ESH-INF/thing/region.xml index eaf5b7968a3fd..7c6fdba3d0e8f 100644 --- a/bundles/org.openhab.binding.dwdpollenflug/src/main/resources/ESH-INF/thing/region.xml +++ b/bundles/org.openhab.binding.dwdpollenflug/src/main/resources/ESH-INF/thing/region.xml @@ -9,7 +9,7 @@ - + Pollen count index for a region or partregion