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