]> git.basschouten.com Git - openhab-addons.git/blob
9bc44eb67464b5ff641624828f55b62e65798084
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.loxone.internal.controls;
14
15 import static org.openhab.binding.loxone.internal.LxBindingConstants.*;
16
17 import java.io.IOException;
18
19 import org.openhab.binding.loxone.internal.types.LxUuid;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.thing.ChannelUID;
22 import org.openhab.core.thing.type.ChannelTypeUID;
23 import org.openhab.core.types.Command;
24 import org.openhab.core.types.StateDescriptionFragmentBuilder;
25
26 /**
27  * A meter type of control on Loxone Miniserver.
28  * According to Loxone API documentation, a meter control covers Utility Meter functional block in Loxone Config.
29  *
30  * @author Pawel Pieczul - initial contribution
31  *
32  */
33 class LxControlMeter extends LxControl {
34
35     static class Factory extends LxControlInstance {
36         @Override
37         LxControl create(LxUuid uuid) {
38             return new LxControlMeter(uuid);
39         }
40
41         @Override
42         String getType() {
43             return "meter";
44         }
45     }
46
47     /**
48      * Value for actual consumption
49      */
50     private static final String STATE_ACTUAL = "actual";
51
52     /**
53      * Value for total consumption
54      */
55     private static final String STATE_TOTAL = "total";
56
57     /**
58      * Command string used to reset the meter
59      */
60     private static final String CMD_RESET = "reset";
61
62     LxControlMeter(LxUuid uuid) {
63         super(uuid);
64     }
65
66     @Override
67     public void initialize(LxControlConfig config) {
68         super.initialize(config);
69         ChannelUID cid = addChannel("Number", new ChannelTypeUID(BINDING_ID, MINISERVER_CHANNEL_TYPE_RO_NUMBER),
70                 defaultChannelLabel + " / Current", "Current meter value", tags, null,
71                 () -> getStateDecimalValue(STATE_ACTUAL));
72         String format;
73         if (details != null && details.actualFormat != null) {
74             format = details.actualFormat;
75         } else {
76             format = "%.3f"; // Loxone default for this format
77         }
78         addChannelStateDescriptionFragment(cid,
79                 StateDescriptionFragmentBuilder.create().withPattern(format).withReadOnly(true).build());
80
81         cid = addChannel("Number", new ChannelTypeUID(BINDING_ID, MINISERVER_CHANNEL_TYPE_RO_NUMBER),
82                 defaultChannelLabel + " / Total", "Total meter consumption", tags, null,
83                 () -> getStateDecimalValue(STATE_TOTAL));
84         if (details != null && details.totalFormat != null) {
85             format = details.totalFormat;
86         } else {
87             format = "%.1f"; // Loxone default for this format
88         }
89         addChannelStateDescriptionFragment(cid,
90                 StateDescriptionFragmentBuilder.create().withPattern(format).withReadOnly(true).build());
91
92         addChannel("Switch", new ChannelTypeUID(BINDING_ID, MINISERVER_CHANNEL_TYPE_SWITCH),
93                 defaultChannelLabel + " / Reset", "Reset meter", tags, this::handleResetCommands, () -> OnOffType.OFF);
94     }
95
96     private void handleResetCommands(Command command) throws IOException {
97         if (command instanceof OnOffType && (OnOffType) command == OnOffType.ON) {
98             sendAction(CMD_RESET);
99         }
100     }
101 }