2 * Copyright (c) 2010-2021 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.teleinfo.internal.handler;
15 import static org.openhab.binding.teleinfo.internal.TeleinfoBindingConstants.*;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.teleinfo.internal.dto.common.FrameBaseOption;
20 import org.openhab.binding.teleinfo.internal.dto.common.FrameEjpOption;
21 import org.openhab.binding.teleinfo.internal.dto.common.FrameHcOption;
22 import org.openhab.binding.teleinfo.internal.dto.common.FrameTempoOption;
23 import org.openhab.core.library.types.QuantityType;
24 import org.openhab.core.library.types.StringType;
25 import org.openhab.core.library.unit.Units;
26 import org.openhab.core.thing.Bridge;
27 import org.openhab.core.thing.Channel;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.ThingStatusDetail;
32 import org.openhab.core.thing.ThingStatusInfo;
33 import org.openhab.core.thing.binding.BaseThingHandler;
34 import org.openhab.core.types.Command;
35 import org.openhab.core.types.UnDefType;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * The {@link TeleinfoAbstractElectricityMeterHandler} class defines a skeleton for Electricity Meters handlers.
42 * @author Nicolas SIBERIL - Initial contribution
45 public abstract class TeleinfoAbstractElectricityMeterHandler extends BaseThingHandler
46 implements TeleinfoControllerHandlerListener {
47 private final Logger logger = LoggerFactory.getLogger(TeleinfoAbstractElectricityMeterHandler.class);
48 protected TeleinfoElectricityMeterConfiguration configuration = new TeleinfoElectricityMeterConfiguration();
50 public TeleinfoAbstractElectricityMeterHandler(Thing thing) {
55 public void initialize() {
56 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, ERROR_OFFLINE_CONTROLLER_OFFLINE);
58 Bridge bridge = getBridge();
59 logger.debug("bridge = {}", bridge);
61 bridgeStatusChanged(bridge.getStatusInfo());
63 configuration = getConfigAs(TeleinfoElectricityMeterConfiguration.class);
67 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
68 TeleinfoAbstractControllerHandler controllerHandler = getControllerHandler();
69 if (bridgeStatusInfo.getStatus() != ThingStatus.ONLINE) {
70 if (controllerHandler != null) {
71 controllerHandler.removeListener(this);
73 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, ERROR_OFFLINE_CONTROLLER_OFFLINE);
77 if (controllerHandler != null) {
78 controllerHandler.addListener(this);
79 updateStatus(ThingStatus.ONLINE);
84 public void dispose() {
85 TeleinfoAbstractControllerHandler controllerHandler = getControllerHandler();
86 if (controllerHandler != null) {
87 controllerHandler.removeListener(this);
92 private @Nullable TeleinfoAbstractControllerHandler getControllerHandler() {
93 Bridge bridge = getBridge();
94 return bridge != null ? (TeleinfoAbstractControllerHandler) bridge.getHandler() : null;
98 public void handleCommand(ChannelUID channelUID, Command command) {
99 // no commands supported
102 protected void updateStatesForBaseFrameOption(FrameBaseOption frameBaseOption) {
103 updateState(CHANNEL_BASE_FRAME_BASE, QuantityType.valueOf(frameBaseOption.getBase(), Units.WATT_HOUR));
106 protected void updateStatesForHcFrameOption(FrameHcOption frameHcOption) {
107 updateState(CHANNEL_HC_FRAME_HCHC, QuantityType.valueOf(frameHcOption.getHchc(), Units.WATT_HOUR));
108 updateState(CHANNEL_HC_FRAME_HCHP, QuantityType.valueOf(frameHcOption.getHchp(), Units.WATT_HOUR));
109 updateState(CHANNEL_HC_FRAME_HHPHC, new StringType(frameHcOption.getHhphc().name()));
112 protected void updateStatesForTempoFrameOption(FrameTempoOption frameTempoOption) {
113 updateState(CHANNEL_TEMPO_FRAME_BBRHPJR, QuantityType.valueOf(frameTempoOption.getBbrhpjr(), Units.WATT_HOUR));
114 updateState(CHANNEL_TEMPO_FRAME_BBRHCJR, QuantityType.valueOf(frameTempoOption.getBbrhcjr(), Units.WATT_HOUR));
115 updateState(CHANNEL_TEMPO_FRAME_BBRHPJW, QuantityType.valueOf(frameTempoOption.getBbrhpjw(), Units.WATT_HOUR));
116 updateState(CHANNEL_TEMPO_FRAME_BBRHCJW, QuantityType.valueOf(frameTempoOption.getBbrhcjw(), Units.WATT_HOUR));
117 updateState(CHANNEL_TEMPO_FRAME_BBRHPJB, QuantityType.valueOf(frameTempoOption.getBbrhpjb(), Units.WATT_HOUR));
118 updateState(CHANNEL_TEMPO_FRAME_BBRHCJB, QuantityType.valueOf(frameTempoOption.getBbrhcjb(), Units.WATT_HOUR));
119 updateState(CHANNEL_TEMPO_FRAME_HHPHC, new StringType(frameTempoOption.getHhphc().name()));
120 updateState(CHANNEL_TEMPO_FRAME_PROGRAMME_CIRCUIT_1,
121 new StringType(frameTempoOption.getProgrammeCircuit1().name()));
122 updateState(CHANNEL_TEMPO_FRAME_PROGRAMME_CIRCUIT_2,
123 new StringType(frameTempoOption.getProgrammeCircuit2().name()));
125 if (frameTempoOption.getDemain() == null) {
126 updateState(CHANNEL_TEMPO_FRAME_DEMAIN, UnDefType.NULL);
128 updateState(CHANNEL_TEMPO_FRAME_DEMAIN, new StringType(frameTempoOption.getDemain().name()));
132 protected void updateStatesForEjpFrameOption(FrameEjpOption frameEjpOption) {
133 updateState(CHANNEL_EJP_FRAME_EJPHN, QuantityType.valueOf(frameEjpOption.getEjphn(), Units.WATT_HOUR));
134 updateState(CHANNEL_EJP_FRAME_EJPHPM, QuantityType.valueOf(frameEjpOption.getEjphpm(), Units.WATT_HOUR));
136 if (frameEjpOption.getPejp() == null) {
137 updateState(CHANNEL_EJP_FRAME_PEJP, UnDefType.NULL);
139 updateState(CHANNEL_EJP_FRAME_PEJP, QuantityType.valueOf(frameEjpOption.getPejp(), Units.MINUTE));
144 protected void updateStatus(ThingStatus status, ThingStatusDetail statusDetail, @Nullable String description) {
145 super.updateStatus(status, statusDetail, description);
147 if (!(ThingStatus.ONLINE.equals(status))) {
148 for (Channel channel : getThing().getChannels()) {
149 if (!CHANNEL_LAST_UPDATE.equals(channel.getUID().getId())) {
150 updateState(channel.getUID(), UnDefType.UNDEF);
157 protected void updateStatus(ThingStatus status, ThingStatusDetail statusDetail) {
158 this.updateStatus(status, statusDetail, null);
162 protected void updateStatus(ThingStatus status) {
163 this.updateStatus(status, ThingStatusDetail.NONE, null);