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.dwdpollenflug.internal.handler;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.dwdpollenflug.internal.config.DWDPollenflugRegionConfiguration;
18 import org.openhab.binding.dwdpollenflug.internal.dto.DWDPollenflug;
19 import org.openhab.binding.dwdpollenflug.internal.dto.DWDRegion;
20 import org.openhab.core.thing.Bridge;
21 import org.openhab.core.thing.ChannelUID;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingStatus;
24 import org.openhab.core.thing.ThingStatusDetail;
25 import org.openhab.core.thing.binding.BaseThingHandler;
26 import org.openhab.core.thing.binding.ThingHandler;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.types.RefreshType;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * The {@link DWDPollenflugRegionHandler} is the handler for bridge thing
35 * @author Johannes Ott - Initial contribution
38 public class DWDPollenflugRegionHandler extends BaseThingHandler {
40 private final Logger logger = LoggerFactory.getLogger(DWDPollenflugRegionHandler.class);
42 private DWDPollenflugRegionConfiguration thingConfig = new DWDPollenflugRegionConfiguration();
44 public DWDPollenflugRegionHandler(Thing thing) {
49 public void initialize() {
50 logger.debug("Initializing DWD Pollenflug region handler");
51 thingConfig = getConfigAs(DWDPollenflugRegionConfiguration.class);
53 if (thingConfig.isValid()) {
54 DWDPollenflugBridgeHandler handler = getBridgeHandler();
55 if (handler == null) {
56 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Bridge handler missing");
58 updateStatus(ThingStatus.ONLINE);
61 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No valid region id given.");
65 private @Nullable DWDPollenflugBridgeHandler getBridgeHandler() {
66 Bridge bridge = getBridge();
68 ThingHandler handler = bridge.getHandler();
69 if (handler instanceof DWDPollenflugBridgeHandler bridgeHandler) {
78 public void dispose() {
79 logger.debug("DWDPollenflug region handler disposes.");
83 public void handleCommand(ChannelUID channelUID, Command command) {
84 if (command instanceof RefreshType) {
89 private void refresh() {
90 DWDPollenflugBridgeHandler handler = getBridgeHandler();
91 if (handler != null) {
92 DWDPollenflug pollenflug = handler.getPollenflug();
93 if (pollenflug != null) {
94 notifyOnUpdate(pollenflug);
99 public void notifyOnUpdate(DWDPollenflug pollenflug) {
100 DWDRegion region = pollenflug.getRegion(thingConfig.regionID);
101 if (region == null) {
102 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Region not found");
106 updateStatus(ThingStatus.ONLINE);
107 updateProperties(region.getProperties());
109 region.getChannelsStateMap().forEach(this::updateState);