2 * Copyright (c) 2010-2021 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
13 package org.openhab.binding.evohome.internal.handler;
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;
25 * Base class for an evohome handler
27 * @author Jasper van Zuijlen - Initial contribution
29 public abstract class BaseEvohomeHandler extends BaseThingHandler {
30 private EvohomeThingConfiguration configuration;
32 public BaseEvohomeHandler(Thing thing) {
37 public void initialize() {
38 configuration = getConfigAs(EvohomeThingConfiguration.class);
43 public void dispose() {
47 public String getId() {
48 if (configuration != null) {
49 return configuration.id;
55 * Returns the configuration of the Thing
57 * @return The parsed configuration or null
59 protected EvohomeThingConfiguration getEvohomeThingConfig() {
64 * Retrieves the bridge
66 * @return The evohome brdige
68 protected EvohomeAccountBridgeHandler getEvohomeBridge() {
69 Bridge bridge = getBridge();
71 return (EvohomeAccountBridgeHandler) bridge.getHandler();
78 * Retrieves the evohome configuration from the bridge
80 * @return The current evohome configuration
82 protected Locations getEvohomeConfig() {
83 EvohomeAccountBridgeHandler bridge = getEvohomeBridge();
85 return bridge.getEvohomeConfig();
92 * Retrieves the evohome configuration from the bridge
94 * @return The current evohome configuration
96 protected void requestUpdate() {
97 Bridge bridge = getBridge();
99 ((EvohomeAccountBridgeHandler) bridge).getEvohomeConfig();
104 * Updates the status of the evohome thing when it changes
106 * @param newStatus The new status to update to
108 protected void updateEvohomeThingStatus(ThingStatus newStatus) {
109 updateEvohomeThingStatus(newStatus, ThingStatusDetail.NONE, null);
113 * Updates the status of the evohome thing when it changes
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
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);
127 * Checks the configuration for validity, result is reflected in the status of the Thing
129 * @param configuration The configuration to check
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");