2 * Copyright (c) 2010-2023 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.loxone.internal.controls;
15 import static org.openhab.binding.loxone.internal.LxBindingConstants.*;
17 import java.io.IOException;
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;
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.
30 * @author Pawel Pieczul - initial contribution
33 class LxControlMeter extends LxControl {
35 static class Factory extends LxControlInstance {
37 LxControl create(LxUuid uuid) {
38 return new LxControlMeter(uuid);
48 * Value for actual consumption
50 private static final String STATE_ACTUAL = "actual";
53 * Value for total consumption
55 private static final String STATE_TOTAL = "total";
58 * Command string used to reset the meter
60 private static final String CMD_RESET = "reset";
62 LxControlMeter(LxUuid uuid) {
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));
73 if (details != null && details.actualFormat != null) {
74 format = details.actualFormat;
76 format = "%.3f"; // Loxone default for this format
78 addChannelStateDescriptionFragment(cid,
79 StateDescriptionFragmentBuilder.create().withPattern(format).withReadOnly(true).build());
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;
87 format = "%.1f"; // Loxone default for this format
89 addChannelStateDescriptionFragment(cid,
90 StateDescriptionFragmentBuilder.create().withPattern(format).withReadOnly(true).build());
92 addChannel("Switch", new ChannelTypeUID(BINDING_ID, MINISERVER_CHANNEL_TYPE_SWITCH),
93 defaultChannelLabel + " / Reset", "Reset meter", tags, this::handleResetCommands, () -> OnOffType.OFF);
96 private void handleResetCommands(Command command) throws IOException {
97 if (command instanceof OnOffType && (OnOffType) command == OnOffType.ON) {
98 sendAction(CMD_RESET);