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;
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 = Set.of(ThingStatus.ONLINE);
38 protected boolean bridgeValid = false;
39 protected @Nullable BridgeHandlerType bridgeHandler;
41 public ChildThingHandler(Thing thing) {
46 public void initialize() {
47 setStatusBasedOnBridge();
48 if (getBridge() != null) {
53 protected void linkBridge() {
55 BridgeHandler bridgeHandlerUncasted = getBridge().getHandler();
56 bridgeHandler = (BridgeHandlerType) bridgeHandlerUncasted;
57 } catch (ClassCastException e) {
58 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Bridge Type is invalid.");
62 protected void setStatusBasedOnBridge() {
63 setStatusBasedOnBridge(DEFAULT_TOLERATED_BRIDGE_STATUSES);
67 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
68 this.setStatusBasedOnBridge();
71 protected void setStatusBasedOnBridge(Collection<ThingStatus> toleratedBridgeStatuses) {
72 if (getBridge() != null) {
73 if (toleratedBridgeStatuses.contains(getBridge().getStatus())) {
77 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE,
78 "Bridge in unsupported status: " + getBridge().getStatus());
82 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED, "Bridge missing.");