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