2 * Copyright (c) 2010-2024 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 javax.measure.quantity.Temperature;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.evohome.internal.EvohomeBindingConstants;
20 import org.openhab.binding.evohome.internal.api.models.v2.dto.response.ZoneStatus;
21 import org.openhab.core.library.types.QuantityType;
22 import org.openhab.core.library.types.StringType;
23 import org.openhab.core.library.unit.SIUnits;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.thing.ThingStatusDetail;
28 import org.openhab.core.types.Command;
29 import org.openhab.core.types.RefreshType;
32 * The {@link EvohomeHeatingZoneHandler} is responsible for handling commands, which are
33 * sent to one of the channels.
35 * @author Jasper van Zuijlen - Initial contribution
36 * @author Neil Renaud - Working implementation
37 * @author Jasper van Zuijlen - Refactor + Permanent Zone temperature setting
38 * @author Leo Siepel - Add UoM
41 public class EvohomeHeatingZoneHandler extends BaseEvohomeHandler {
43 private static final int CANCEL_SET_POINT_OVERRIDE = 0;
44 private @Nullable ThingStatus tcsStatus;
45 private @Nullable ZoneStatus zoneStatus;
47 public EvohomeHeatingZoneHandler(Thing thing) {
52 public void initialize() {
56 public void update(@Nullable ThingStatus tcsStatus, @Nullable ZoneStatus zoneStatus) {
57 this.tcsStatus = tcsStatus;
58 this.zoneStatus = zoneStatus;
60 // Make the zone offline when the related display is offline
61 // If the related display is not a thing, ignore this
62 if (tcsStatus != null && tcsStatus.equals(ThingStatus.OFFLINE)) {
63 updateEvohomeThingStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
64 "Display Controller offline");
65 } else if (zoneStatus == null) {
66 updateEvohomeThingStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
67 "Status not found, check the zone id");
68 } else if (!handleActiveFaults(zoneStatus)) {
69 updateEvohomeThingStatus(ThingStatus.ONLINE);
71 updateState(EvohomeBindingConstants.ZONE_TEMPERATURE_CHANNEL,
72 new QuantityType<Temperature>(zoneStatus.getTemperature().getTemperature(), SIUnits.CELSIUS));
73 updateState(EvohomeBindingConstants.ZONE_SET_POINT_STATUS_CHANNEL,
74 new StringType(zoneStatus.getHeatSetpoint().getSetpointMode()));
75 updateState(EvohomeBindingConstants.ZONE_SET_POINT_CHANNEL, new QuantityType<Temperature>(
76 zoneStatus.getHeatSetpoint().getTargetTemperature(), SIUnits.CELSIUS));
81 public void handleCommand(ChannelUID channelUID, Command command) {
82 if (command == RefreshType.REFRESH) {
83 update(tcsStatus, zoneStatus);
85 EvohomeAccountBridgeHandler bridge = getEvohomeBridge();
87 String channelId = channelUID.getId();
88 if (EvohomeBindingConstants.ZONE_SET_POINT_CHANNEL.equals(channelId)) {
89 if (command instanceof QuantityType quantityCommand) {
90 QuantityType<?> state = quantityCommand.toUnit(SIUnits.CELSIUS);
91 double newTempInCelsius = state.doubleValue();
93 if (newTempInCelsius == CANCEL_SET_POINT_OVERRIDE) {
94 bridge.cancelSetPointOverride(getEvohomeThingConfig().id);
95 } else if (newTempInCelsius < 5) {
98 if (newTempInCelsius >= 5 && newTempInCelsius <= 35) {
99 bridge.setPermanentSetPoint(getEvohomeThingConfig().id, newTempInCelsius);
107 private boolean handleActiveFaults(ZoneStatus zoneStatus) {
108 if (zoneStatus.hasActiveFaults()) {
109 updateEvohomeThingStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
110 zoneStatus.getActiveFault(0).getFaultType());