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
14 package org.openhab.binding.flicbutton.internal.handler;
16 import java.util.Collection;
17 import java.util.Collections;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.thing.Thing;
22 import org.openhab.core.thing.ThingStatus;
23 import org.openhab.core.thing.ThingStatusDetail;
24 import org.openhab.core.thing.ThingStatusInfo;
25 import org.openhab.core.thing.binding.BaseThingHandler;
26 import org.openhab.core.thing.binding.BridgeHandler;
29 * The {@link ChildThingHandler} class is an abstract class for handlers that are dependent from a parent
30 * {@link BridgeHandler}.
32 * @author Patrick Fink - Initial contribution
33 * @param <BridgeHandlerType> The bridge type this child handler depends on
36 public abstract class ChildThingHandler<BridgeHandlerType extends BridgeHandler> extends BaseThingHandler {
37 private static final Collection<ThingStatus> DEFAULT_TOLERATED_BRIDGE_STATUSES = Collections
38 .singleton(ThingStatus.ONLINE);
39 protected boolean bridgeValid = false;
40 protected @Nullable BridgeHandlerType bridgeHandler;
42 public ChildThingHandler(Thing thing) {
47 public void initialize() {
48 setStatusBasedOnBridge();
49 if (getBridge() != null) {
54 protected void linkBridge() {
56 BridgeHandler bridgeHandlerUncasted = getBridge().getHandler();
57 bridgeHandler = (BridgeHandlerType) bridgeHandlerUncasted;
58 } catch (ClassCastException e) {
59 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Bridge Type is invalid.");
63 protected void setStatusBasedOnBridge() {
64 setStatusBasedOnBridge(DEFAULT_TOLERATED_BRIDGE_STATUSES);
68 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
69 this.setStatusBasedOnBridge();
72 protected void setStatusBasedOnBridge(Collection<ThingStatus> toleratedBridgeStatuses) {
73 if (getBridge() != null) {
74 if (toleratedBridgeStatuses.contains(getBridge().getStatus())) {
78 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE,
79 "Bridge in unsupported status: " + getBridge().getStatus());
83 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED, "Bridge missing.");