]> git.basschouten.com Git - openhab-addons.git/blob
88f47cab72041df4510367e59e31502d4802998d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.tado.internal.handler;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.tado.internal.api.client.HomeApi;
18 import org.openhab.core.thing.Bridge;
19 import org.openhab.core.thing.Thing;
20 import org.openhab.core.thing.ThingStatus;
21 import org.openhab.core.thing.binding.BaseThingHandler;
22 import org.openhab.core.thing.binding.BridgeHandler;
23
24 /**
25  * Common base class for home-based thing-handler.
26  *
27  * @author Dennis Frommknecht - Initial contribution
28  */
29 @NonNullByDefault
30 public abstract class BaseHomeThingHandler extends BaseThingHandler {
31
32     public BaseHomeThingHandler(Thing thing) {
33         super(thing);
34     }
35
36     public @Nullable Long getHomeId() {
37         TadoHomeHandler handler = getHomeHandler();
38         return handler.getHomeId();
39     }
40
41     protected TadoHomeHandler getHomeHandler() {
42         Bridge bridge = getBridge();
43         if (bridge == null) {
44             throw new IllegalStateException("Bridge not initialized");
45         }
46         BridgeHandler handler = bridge.getHandler();
47         if (!(handler instanceof TadoHomeHandler)) {
48             throw new IllegalStateException("Handler not initialized");
49         }
50         return (TadoHomeHandler) handler;
51     }
52
53     protected HomeApi getApi() {
54         TadoHomeHandler handler = getHomeHandler();
55         return handler.getApi();
56     }
57
58     protected void onSuccessfulOperation() {
59         // update without error -> we're back online
60         if (getThing().getStatus() == ThingStatus.OFFLINE) {
61             updateStatus(ThingStatus.ONLINE);
62         }
63     }
64 }