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.plugwiseha.internal.handler;
15 import static org.openhab.binding.plugwiseha.internal.PlugwiseHABindingConstants.*;
16 import static org.openhab.core.thing.ThingStatus.*;
17 import static org.openhab.core.thing.ThingStatusDetail.BRIDGE_OFFLINE;
18 import static org.openhab.core.thing.ThingStatusDetail.COMMUNICATION_ERROR;
19 import static org.openhab.core.thing.ThingStatusDetail.CONFIGURATION_ERROR;
22 import java.util.Optional;
24 import javax.measure.Unit;
25 import javax.measure.quantity.Temperature;
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.openhab.binding.plugwiseha.internal.PlugwiseHABindingConstants;
30 import org.openhab.binding.plugwiseha.internal.api.exception.PlugwiseHAException;
31 import org.openhab.binding.plugwiseha.internal.api.model.PlugwiseHAController;
32 import org.openhab.binding.plugwiseha.internal.api.model.dto.Location;
33 import org.openhab.binding.plugwiseha.internal.config.PlugwiseHAThingConfig;
34 import org.openhab.core.library.types.OnOffType;
35 import org.openhab.core.library.types.QuantityType;
36 import org.openhab.core.library.types.StringType;
37 import org.openhab.core.library.unit.ImperialUnits;
38 import org.openhab.core.library.unit.SIUnits;
39 import org.openhab.core.thing.ChannelUID;
40 import org.openhab.core.thing.Thing;
41 import org.openhab.core.thing.ThingTypeUID;
42 import org.openhab.core.types.Command;
43 import org.openhab.core.types.State;
44 import org.openhab.core.types.UnDefType;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * The {@link PlugwiseHAZoneHandler} class is responsible for handling commands
50 * and status updates for the Plugwise Home Automation zones/locations.
51 * Extends @{link PlugwiseHABaseHandler}
53 * @author Bas van Wetten - Initial contribution
54 * @author Leo Siepel - finish initial contribution
59 public class PlugwiseHAZoneHandler extends PlugwiseHABaseHandler<Location, PlugwiseHAThingConfig> {
61 private @Nullable Location location;
62 private final Logger logger = LoggerFactory.getLogger(PlugwiseHAZoneHandler.class);
66 public PlugwiseHAZoneHandler(Thing thing) {
70 public static boolean supportsThingType(ThingTypeUID thingTypeUID) {
71 return PlugwiseHABindingConstants.THING_TYPE_ZONE.equals(thingTypeUID);
77 protected synchronized void initialize(PlugwiseHAThingConfig config, PlugwiseHABridgeHandler bridgeHandler) {
78 if (thing.getStatus() == INITIALIZING) {
79 logger.debug("Initializing Plugwise Home Automation zone handler with config = {}", config);
80 if (!config.isValid()) {
81 updateStatus(OFFLINE, CONFIGURATION_ERROR,
82 "Invalid configuration for Plugwise Home Automation zone handler.");
87 PlugwiseHAController controller = bridgeHandler.getController();
88 if (controller != null) {
89 this.location = getEntity(controller);
90 if (this.location != null) {
91 setLocationProperties();
94 updateStatus(OFFLINE);
97 updateStatus(OFFLINE, BRIDGE_OFFLINE);
99 } catch (PlugwiseHAException e) {
100 updateStatus(OFFLINE, COMMUNICATION_ERROR, e.getMessage());
106 protected @Nullable Location getEntity(PlugwiseHAController controller) throws PlugwiseHAException {
107 PlugwiseHAThingConfig config = getPlugwiseThingConfig();
108 return controller.getLocation(config.getId());
112 protected void handleCommand(Location entity, ChannelUID channelUID, Command command) throws PlugwiseHAException {
113 String channelID = channelUID.getIdWithoutGroup();
114 PlugwiseHABridgeHandler bridge = this.getPlugwiseHABridge();
115 if (bridge != null) {
116 PlugwiseHAController controller = bridge.getController();
117 if (controller != null) {
119 case ZONE_COOLING_CHANNEL:
120 if (command instanceof OnOffType onOffCommand) {
122 controller.setAllowCooling(entity, command == OnOffType.ON);
123 } catch (PlugwiseHAException e) {
124 logger.warn("Unable to switch allow cooling {} for zone '{}'", onOffCommand,
129 case ZONE_SETPOINT_CHANNEL:
130 if (command instanceof QuantityType quantityCommand) {
131 Unit<Temperature> unit = entity.getSetpointTemperatureUnit().orElse(UNIT_CELSIUS)
132 .equals(UNIT_CELSIUS) ? SIUnits.CELSIUS : ImperialUnits.FAHRENHEIT;
133 QuantityType<?> state = quantityCommand.toUnit(unit);
136 controller.setLocationThermostat(entity, state.doubleValue());
137 } catch (PlugwiseHAException e) {
138 logger.warn("Unable to update setpoint for zone '{}': {} -> {}", entity.getName(),
139 entity.getSetpointTemperature().orElse(null), state.doubleValue());
144 case ZONE_PREHEAT_CHANNEL:
145 if (command instanceof OnOffType onOffCommand) {
147 controller.setPreHeating(entity, command == OnOffType.ON);
148 } catch (PlugwiseHAException e) {
149 logger.warn("Unable to switch zone pre heating {} for zone '{}'", onOffCommand,
154 case ZONE_REGULATION_CHANNEL:
155 if (command instanceof StringType stringCommand) {
157 controller.setRegulationControl(entity, command.toString());
158 } catch (PlugwiseHAException e) {
159 logger.warn("Unable to switch regulation control {} for zone '{}'", stringCommand,
164 case ZONE_PRESETSCENE_CHANNEL:
165 if (command instanceof StringType stringCommand) {
167 controller.setPresetScene(entity, command.toString());
168 } catch (PlugwiseHAException e) {
169 logger.warn("Unable to switch preset scene {} for zone '{}'", stringCommand,
175 logger.warn("Ignoring unsupported command = {} for channel = {}", command, channelUID);
181 private State getDefaultState(String channelID) {
182 State state = UnDefType.NULL;
184 case ZONE_COOLING_CHANNEL:
185 case ZONE_PREHEAT_CHANNEL:
186 case ZONE_PRESETSCENE_CHANNEL:
187 case ZONE_REGULATION_CHANNEL:
188 case ZONE_SETPOINT_CHANNEL:
189 case ZONE_TEMPERATURE_CHANNEL:
190 state = UnDefType.NULL;
197 protected void refreshChannel(Location entity, ChannelUID channelUID) {
198 String channelID = channelUID.getIdWithoutGroup();
199 State state = getDefaultState(channelID);
202 case ZONE_COOLING_CHANNEL:
203 Optional<Boolean> allowCoolingState = entity.getCoolingAllowed();
204 if (allowCoolingState.isPresent()) {
205 state = OnOffType.from(allowCoolingState.get());
208 case ZONE_PREHEAT_CHANNEL:
209 Optional<Boolean> preHeatState = entity.getPreHeatState();
210 if (preHeatState.isPresent()) {
211 state = OnOffType.from(preHeatState.get());
214 case ZONE_PRESETSCENE_CHANNEL:
215 state = new StringType(entity.getPreset());
217 case ZONE_SETPOINT_CHANNEL:
218 if (entity.getSetpointTemperature().isPresent()) {
219 Unit<Temperature> unit = entity.getSetpointTemperatureUnit().orElse(UNIT_CELSIUS)
220 .equals(UNIT_CELSIUS) ? SIUnits.CELSIUS : ImperialUnits.FAHRENHEIT;
221 state = new QuantityType<Temperature>(entity.getSetpointTemperature().get(), unit);
224 case ZONE_REGULATION_CHANNEL:
225 String value = entity.getRegulationControl();
227 state = new StringType(entity.getRegulationControl());
230 case ZONE_TEMPERATURE_CHANNEL:
231 if (entity.getTemperature().isPresent()) {
232 Unit<Temperature> unit = entity.getTemperatureUnit().orElse(UNIT_CELSIUS).equals(UNIT_CELSIUS)
234 : ImperialUnits.FAHRENHEIT;
235 state = new QuantityType<Temperature>(entity.getTemperature().get(), unit);
242 if (state != UnDefType.NULL) {
243 updateState(channelID, state);
247 protected void setLocationProperties() {
248 if (this.location != null) {
249 Map<String, String> properties = editProperties();
251 Location localLocation = this.location;
252 if (localLocation != null) {
253 properties.put(PlugwiseHABindingConstants.LOCATION_PROPERTY_DESCRIPTION,
254 localLocation.getDescription());
255 properties.put(PlugwiseHABindingConstants.LOCATION_PROPERTY_TYPE, localLocation.getType());
256 properties.put(PlugwiseHABindingConstants.LOCATION_PROPERTY_FUNCTIONALITIES,
257 String.join(", ", localLocation.getActuatorFunctionalities().keySet()));
260 updateProperties(properties);