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.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;
26 * Base class for an evohome handler
28 * @author Jasper van Zuijlen - Initial contribution
31 public abstract class BaseEvohomeHandler extends BaseThingHandler {
32 private EvohomeThingConfiguration configuration = new EvohomeThingConfiguration();
34 public BaseEvohomeHandler(Thing thing) {
39 public void initialize() {
40 configuration = getConfigAs(EvohomeThingConfiguration.class);
45 public void dispose() {
48 public String getId() {
49 return configuration.id;
53 * Returns the configuration of the Thing
55 * @return The parsed configuration or null
57 protected EvohomeThingConfiguration getEvohomeThingConfig() {
62 * Retrieves the bridge
64 * @return The evohome brdige
66 protected @Nullable EvohomeAccountBridgeHandler getEvohomeBridge() {
67 Bridge bridge = getBridge();
69 return (EvohomeAccountBridgeHandler) bridge.getHandler();
76 * Retrieves the evohome configuration from the bridge
78 * @return The current evohome configuration
80 protected @Nullable Locations getEvohomeConfig() {
81 EvohomeAccountBridgeHandler bridgeAccountHandler = getEvohomeBridge();
82 if (bridgeAccountHandler != null) {
83 return bridgeAccountHandler.getEvohomeConfig();
90 * Retrieves the evohome configuration from the bridge
92 * @return The current evohome configuration
94 protected void requestUpdate() {
95 Bridge bridge = getBridge();
97 ((EvohomeAccountBridgeHandler) bridge).getEvohomeConfig();
102 * Updates the status of the evohome thing when it changes
104 * @param newStatus The new status to update to
106 protected void updateEvohomeThingStatus(ThingStatus newStatus) {
107 updateEvohomeThingStatus(newStatus, ThingStatusDetail.NONE, null);
111 * Updates the status of the evohome thing when it changes
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
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);
125 * Checks the configuration for validity, result is reflected in the status of the Thing
127 * @param configuration The configuration to check
129 private void checkConfig() {
130 if (configuration.id.isEmpty()) {
131 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Id not configured");