]> git.basschouten.com Git - openhab-addons.git/blob
11048291cde0e9c9c2c8cb206dd518fd15783e04
[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.evohome.internal.handler;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.evohome.internal.api.models.v2.dto.response.Locations;
18 import org.openhab.binding.evohome.internal.configuration.EvohomeThingConfiguration;
19 import org.openhab.core.thing.Bridge;
20 import org.openhab.core.thing.Thing;
21 import org.openhab.core.thing.ThingStatus;
22 import org.openhab.core.thing.ThingStatusDetail;
23 import org.openhab.core.thing.binding.BaseThingHandler;
24
25 /**
26  * Base class for an evohome handler
27  *
28  * @author Jasper van Zuijlen - Initial contribution
29  */
30 @NonNullByDefault
31 public abstract class BaseEvohomeHandler extends BaseThingHandler {
32     private EvohomeThingConfiguration configuration = new EvohomeThingConfiguration();
33
34     public BaseEvohomeHandler(Thing thing) {
35         super(thing);
36     }
37
38     @Override
39     public void initialize() {
40         configuration = getConfigAs(EvohomeThingConfiguration.class);
41         checkConfig();
42     }
43
44     @Override
45     public void dispose() {
46     }
47
48     public String getId() {
49         return configuration.id;
50     }
51
52     /**
53      * Returns the configuration of the Thing
54      *
55      * @return The parsed configuration or null
56      */
57     protected EvohomeThingConfiguration getEvohomeThingConfig() {
58         return configuration;
59     }
60
61     /**
62      * Retrieves the bridge
63      *
64      * @return The evohome brdige
65      */
66     protected @Nullable EvohomeAccountBridgeHandler getEvohomeBridge() {
67         Bridge bridge = getBridge();
68         if (bridge != null) {
69             return (EvohomeAccountBridgeHandler) bridge.getHandler();
70         }
71
72         return null;
73     }
74
75     /**
76      * Retrieves the evohome configuration from the bridge
77      *
78      * @return The current evohome configuration
79      */
80     protected @Nullable Locations getEvohomeConfig() {
81         EvohomeAccountBridgeHandler bridgeAccountHandler = getEvohomeBridge();
82         if (bridgeAccountHandler != null) {
83             return bridgeAccountHandler.getEvohomeConfig();
84         }
85
86         return null;
87     }
88
89     /**
90      * Retrieves the evohome configuration from the bridge
91      *
92      * @return The current evohome configuration
93      */
94     protected void requestUpdate() {
95         Bridge bridge = getBridge();
96         if (bridge != null) {
97             ((EvohomeAccountBridgeHandler) bridge).getEvohomeConfig();
98         }
99     }
100
101     /**
102      * Updates the status of the evohome thing when it changes
103      *
104      * @param newStatus The new status to update to
105      */
106     protected void updateEvohomeThingStatus(ThingStatus newStatus) {
107         updateEvohomeThingStatus(newStatus, ThingStatusDetail.NONE, null);
108     }
109
110     /**
111      * Updates the status of the evohome thing when it changes
112      *
113      * @param newStatus The new status to update to
114      * @param detail The status detail value
115      * @param message The message to show with the status
116      */
117     protected void updateEvohomeThingStatus(ThingStatus newStatus, ThingStatusDetail detail, @Nullable String message) {
118         // Prevent spamming the log file
119         if (!newStatus.equals(getThing().getStatus())) {
120             updateStatus(newStatus, detail, message);
121         }
122     }
123
124     /**
125      * Checks the configuration for validity, result is reflected in the status of the Thing
126      *
127      * @param configuration The configuration to check
128      */
129     private void checkConfig() {
130         if (configuration.id.isEmpty()) {
131             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Id not configured");
132         }
133     }
134 }