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 protected void requestUpdate() {
93 Bridge bridge = getBridge();
95 ((EvohomeAccountBridgeHandler) bridge).getEvohomeConfig();
100 * Updates the status of the evohome thing when it changes
102 * @param newStatus The new status to update to
104 protected void updateEvohomeThingStatus(ThingStatus newStatus) {
105 updateEvohomeThingStatus(newStatus, ThingStatusDetail.NONE, null);
109 * Updates the status of the evohome thing when it changes
111 * @param newStatus The new status to update to
112 * @param detail The status detail value
113 * @param message The message to show with the status
115 protected void updateEvohomeThingStatus(ThingStatus newStatus, ThingStatusDetail detail, @Nullable String message) {
116 // Prevent spamming the log file
117 if (!newStatus.equals(getThing().getStatus())) {
118 updateStatus(newStatus, detail, message);
123 * Checks the configuration for validity, result is reflected in the status of the Thing
125 * @param configuration The configuration to check
127 private void checkConfig() {
128 if (configuration.id.isEmpty()) {
129 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Id not configured");