]> git.basschouten.com Git - openhab-addons.git/blob
832e0f76c3f40fc2ad3d5a7db4c04027d1a2ef01
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.neohub.internal;
14
15 import static org.openhab.binding.neohub.internal.NeoHubBindingConstants.*;
16
17 import javax.measure.Unit;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.library.types.QuantityType;
22 import org.openhab.core.library.types.StringType;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.types.Command;
25
26 /**
27  * The {@link NeoStatHandler} is the openHAB Handler for NeoStat devices Note:
28  * inherits almost all the functionality of a {@link NeoBaseHandler}
29  *
30  * @author Andrew Fiddian-Green - Initial contribution
31  *
32  */
33 @NonNullByDefault
34 public class NeoStatHandler extends NeoBaseHandler {
35
36     public NeoStatHandler(Thing thing) {
37         super(thing);
38     }
39
40     @Override
41     protected String toNeoHubBuildCommandString(String channelId, Command command) {
42         NeoBaseConfiguration config = this.config;
43         if (config != null) {
44             if (command instanceof QuantityType<?> && channelId.equals(CHAN_TARGET_TEMP)) {
45                 Command doCommand = command;
46                 QuantityType<?> temp = ((QuantityType<?>) command).toUnit(getTemperatureUnit());
47                 if (temp != null) {
48                     doCommand = temp;
49                 }
50                 return String.format(CMD_CODE_TEMP, ((QuantityType<?>) doCommand).toBigDecimal().toString(),
51                         config.deviceNameInHub);
52             }
53
54             if (command instanceof OnOffType && channelId.equals(CHAN_OCC_MODE_PRESENT)) {
55                 return String.format(CMD_CODE_AWAY, invert((OnOffType) command).toString(), config.deviceNameInHub);
56             }
57         }
58         return "";
59     }
60
61     @Override
62     protected void toOpenHabSendChannelValues(NeoHubAbstractDeviceData.AbstractRecord deviceRecord) {
63         Unit<?> unit = getTemperatureUnit();
64         boolean offline = deviceRecord.offline();
65
66         toOpenHabSendValueDebounced(CHAN_TARGET_TEMP, new QuantityType<>(deviceRecord.getTargetTemperature(), unit),
67                 offline);
68
69         toOpenHabSendValueDebounced(CHAN_ROOM_TEMP, new QuantityType<>(deviceRecord.getActualTemperature(), unit),
70                 offline);
71
72         toOpenHabSendValueDebounced(CHAN_FLOOR_TEMP, new QuantityType<>(deviceRecord.getFloorTemperature(), unit),
73                 offline);
74
75         toOpenHabSendValueDebounced(CHAN_OCC_MODE_PRESENT, OnOffType.from(!deviceRecord.isStandby()), offline);
76
77         toOpenHabSendValueDebounced(CHAN_STAT_OUTPUT_STATE,
78                 (deviceRecord.isHeating() || deviceRecord.isPreHeating() ? new StringType(VAL_HEATING)
79                         : new StringType(VAL_OFF)),
80                 offline);
81
82         toOpenHabSendValueDebounced(CHAN_BATTERY_LOW_ALARM, OnOffType.from(deviceRecord.isBatteryLow()), offline);
83     }
84 }