2 * Copyright (c) 2010-2023 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.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;
24 * Base class for an evohome handler
26 * @author Jasper van Zuijlen - Initial contribution
28 public abstract class BaseEvohomeHandler extends BaseThingHandler {
29 private EvohomeThingConfiguration configuration;
31 public BaseEvohomeHandler(Thing thing) {
36 public void initialize() {
37 configuration = getConfigAs(EvohomeThingConfiguration.class);
42 public void dispose() {
46 public String getId() {
47 if (configuration != null) {
48 return configuration.id;
54 * Returns the configuration of the Thing
56 * @return The parsed configuration or null
58 protected EvohomeThingConfiguration getEvohomeThingConfig() {
63 * Retrieves the bridge
65 * @return The evohome brdige
67 protected EvohomeAccountBridgeHandler getEvohomeBridge() {
68 Bridge bridge = getBridge();
70 return (EvohomeAccountBridgeHandler) bridge.getHandler();
77 * Retrieves the evohome configuration from the bridge
79 * @return The current evohome configuration
81 protected Locations getEvohomeConfig() {
82 EvohomeAccountBridgeHandler bridge = getEvohomeBridge();
84 return bridge.getEvohomeConfig();
91 * Retrieves the evohome configuration from the bridge
93 * @return The current evohome configuration
95 protected void requestUpdate() {
96 Bridge bridge = getBridge();
98 ((EvohomeAccountBridgeHandler) bridge).getEvohomeConfig();
103 * Updates the status of the evohome thing when it changes
105 * @param newStatus The new status to update to
107 protected void updateEvohomeThingStatus(ThingStatus newStatus) {
108 updateEvohomeThingStatus(newStatus, ThingStatusDetail.NONE, null);
112 * Updates the status of the evohome thing when it changes
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
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);
126 * Checks the configuration for validity, result is reflected in the status of the Thing
128 * @param configuration The configuration to check
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");