2 * Copyright (c) 2010-2020 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.ojelectronics.internal;
15 import java.time.ZoneId;
16 import java.time.ZonedDateTime;
17 import java.util.HashMap;
19 import java.util.function.Consumer;
21 import javax.measure.quantity.Temperature;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.ojelectronics.internal.config.OJElectronicsThermostatConfiguration;
26 import org.openhab.binding.ojelectronics.internal.models.groups.Thermostat;
27 import org.openhab.core.library.types.DateTimeType;
28 import org.openhab.core.library.types.DecimalType;
29 import org.openhab.core.library.types.OpenClosedType;
30 import org.openhab.core.library.types.QuantityType;
31 import org.openhab.core.library.types.StringType;
32 import org.openhab.core.library.unit.SIUnits;
33 import org.openhab.core.thing.ChannelUID;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.thing.ThingStatus;
36 import org.openhab.core.thing.binding.BaseThingHandler;
37 import org.openhab.core.types.Command;
38 import org.openhab.core.types.RefreshType;
41 * The {@link ThermostatHandler} is responsible for handling commands, which are
42 * sent to one of the channels.
44 * @author Christian Kittel - Initial contribution
47 public class ThermostatHandler extends BaseThingHandler {
49 private final String serialNumber;
50 private @Nullable Thermostat currentThermostat;
51 private static final Map<Integer, String> REGULATION_MODES = createRegulationMap();
52 private final Map<String, Consumer<Thermostat>> channelrefreshActions = createChannelRefreshActionMap();
55 * Creates a new instance of {@link ThermostatHandler}
59 public ThermostatHandler(Thing thing) {
61 serialNumber = getConfigAs(OJElectronicsThermostatConfiguration.class).serialNumber;
65 * Gets the thing's serial number.
67 * @return serial number
69 public String getSerialNumber() {
74 * Handles commands to this thing.
77 public void handleCommand(ChannelUID channelUID, Command command) {
78 if (command instanceof RefreshType) {
79 final Thermostat thermostat = currentThermostat;
80 if (thermostat != null && channelrefreshActions.containsKey(channelUID.getId())) {
81 channelrefreshActions.get(channelUID.getId()).accept(thermostat);
87 * Initializes the thing handler.
90 public void initialize() {
91 updateStatus(ThingStatus.ONLINE);
95 * Sets the values after refreshing the thermostats values
97 * @param thermostat thermostat values
99 public void handleThermostatRefresh(Thermostat thermostat) {
100 currentThermostat = thermostat;
101 channelrefreshActions.forEach((channelUID, action) -> action.accept(thermostat));
104 private void updateManualSetpoint(Thermostat thermostat) {
105 updateState(BindingConstants.CHANNEL_OWD5_MANUALSETPOINT,
106 new QuantityType<Temperature>(thermostat.manualModeSetpoint / (double) 100, SIUnits.CELSIUS));
109 private void updateBoostEndTime(Thermostat thermostat) {
110 updateState(BindingConstants.CHANNEL_OWD5_BOOSTENDTIME,
111 new DateTimeType(ZonedDateTime.ofInstant(thermostat.boostEndTime.toInstant(), ZoneId.systemDefault())));
114 private void updateComfortEndTime(Thermostat thermostat) {
115 updateState(BindingConstants.CHANNEL_OWD5_COMFORTENDTIME, new DateTimeType(
116 ZonedDateTime.ofInstant(thermostat.comfortEndTime.toInstant(), ZoneId.systemDefault())));
119 private void updateComfortSetpoint(Thermostat thermostat) {
120 updateState(BindingConstants.CHANNEL_OWD5_COMFORTSETPOINT,
121 new QuantityType<Temperature>(thermostat.comfortSetpoint / (double) 100, SIUnits.CELSIUS));
124 private void updateRegulationMode(Thermostat thermostat) {
125 updateState(BindingConstants.CHANNEL_OWD5_REGULATIONMODE,
126 StringType.valueOf(getRegulationMode(thermostat.regulationMode)));
129 private void updateThermostatName(Thermostat thermostat) {
130 updateState(BindingConstants.CHANNEL_OWD5_THERMOSTATNAME, StringType.valueOf(thermostat.thermostatName));
133 private void updateFloorTemperature(Thermostat thermostat) {
134 updateState(BindingConstants.CHANNEL_OWD5_FLOORTEMPERATURE,
135 new QuantityType<Temperature>(thermostat.floorTemperature / (double) 100, SIUnits.CELSIUS));
138 private void updateRoomTemperature(Thermostat thermostat) {
139 updateState(BindingConstants.CHANNEL_OWD5_ROOMTEMPERATURE,
140 new QuantityType<Temperature>(thermostat.roomTemperature / (double) 100, SIUnits.CELSIUS));
143 private void updateHeating(Thermostat thermostat) {
144 updateState(BindingConstants.CHANNEL_OWD5_HEATING,
145 thermostat.heating ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
148 private void updateOnline(Thermostat thermostat) {
149 updateState(BindingConstants.CHANNEL_OWD5_ONLINE,
150 thermostat.online ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
153 private void updateGroupId(Thermostat thermostat) {
154 updateState(BindingConstants.CHANNEL_OWD5_GROUPID, new DecimalType(thermostat.groupId));
157 private void updateGroupName(Thermostat thermostat) {
158 updateState(BindingConstants.CHANNEL_OWD5_GROUPNAME, StringType.valueOf(thermostat.groupName));
161 private @Nullable String getRegulationMode(int regulationMode) {
162 return REGULATION_MODES.get(regulationMode);
165 private static Map<Integer, String> createRegulationMap() {
166 HashMap<Integer, String> map = new HashMap<>();
168 map.put(2, "comfort");
169 map.put(3, "manual");
170 map.put(4, "vacation");
171 map.put(6, "frostProtection");
177 private Map<String, Consumer<Thermostat>> createChannelRefreshActionMap() {
178 HashMap<String, Consumer<Thermostat>> map = new HashMap<>();
179 map.put(BindingConstants.CHANNEL_OWD5_GROUPNAME, this::updateGroupName);
180 map.put(BindingConstants.CHANNEL_OWD5_GROUPID, this::updateGroupId);
181 map.put(BindingConstants.CHANNEL_OWD5_ONLINE, this::updateOnline);
182 map.put(BindingConstants.CHANNEL_OWD5_HEATING, this::updateHeating);
183 map.put(BindingConstants.CHANNEL_OWD5_ROOMTEMPERATURE, this::updateRoomTemperature);
184 map.put(BindingConstants.CHANNEL_OWD5_FLOORTEMPERATURE, this::updateFloorTemperature);
185 map.put(BindingConstants.CHANNEL_OWD5_THERMOSTATNAME, this::updateThermostatName);
186 map.put(BindingConstants.CHANNEL_OWD5_REGULATIONMODE, this::updateRegulationMode);
187 map.put(BindingConstants.CHANNEL_OWD5_COMFORTSETPOINT, this::updateComfortSetpoint);
188 map.put(BindingConstants.CHANNEL_OWD5_COMFORTENDTIME, this::updateComfortEndTime);
189 map.put(BindingConstants.CHANNEL_OWD5_BOOSTENDTIME, this::updateBoostEndTime);
190 map.put(BindingConstants.CHANNEL_OWD5_MANUALSETPOINT, this::updateManualSetpoint);