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.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 if (bridgeStatusInfo.getStatus() != ThingStatus.ONLINE) {
69 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE, ERROR_OFFLINE_CONTROLLER_OFFLINE);
73 Bridge bridge = getBridge();
75 TeleinfoAbstractControllerHandler controllerHandler = (TeleinfoAbstractControllerHandler) bridge
77 if (controllerHandler != null) {
78 controllerHandler.addListener(this);
79 updateStatus(ThingStatus.ONLINE);
85 public void handleCommand(ChannelUID channelUID, Command command) {
86 // no commands supported
89 protected void updateStatesForBaseFrameOption(FrameBaseOption frameBaseOption) {
90 updateState(CHANNEL_BASE_FRAME_BASE, QuantityType.valueOf(frameBaseOption.getBase(), Units.WATT_HOUR));
93 protected void updateStatesForHcFrameOption(FrameHcOption frameHcOption) {
94 updateState(CHANNEL_HC_FRAME_HCHC, QuantityType.valueOf(frameHcOption.getHchc(), Units.WATT_HOUR));
95 updateState(CHANNEL_HC_FRAME_HCHP, QuantityType.valueOf(frameHcOption.getHchp(), Units.WATT_HOUR));
96 updateState(CHANNEL_HC_FRAME_HHPHC, new StringType(frameHcOption.getHhphc().name()));
99 protected void updateStatesForTempoFrameOption(FrameTempoOption frameTempoOption) {
100 updateState(CHANNEL_TEMPO_FRAME_BBRHPJR, QuantityType.valueOf(frameTempoOption.getBbrhpjr(), Units.WATT_HOUR));
101 updateState(CHANNEL_TEMPO_FRAME_BBRHCJR, QuantityType.valueOf(frameTempoOption.getBbrhcjr(), Units.WATT_HOUR));
102 updateState(CHANNEL_TEMPO_FRAME_BBRHPJW, QuantityType.valueOf(frameTempoOption.getBbrhpjw(), Units.WATT_HOUR));
103 updateState(CHANNEL_TEMPO_FRAME_BBRHCJW, QuantityType.valueOf(frameTempoOption.getBbrhcjw(), Units.WATT_HOUR));
104 updateState(CHANNEL_TEMPO_FRAME_BBRHPJB, QuantityType.valueOf(frameTempoOption.getBbrhpjb(), Units.WATT_HOUR));
105 updateState(CHANNEL_TEMPO_FRAME_BBRHCJB, QuantityType.valueOf(frameTempoOption.getBbrhcjb(), Units.WATT_HOUR));
106 updateState(CHANNEL_TEMPO_FRAME_HHPHC, new StringType(frameTempoOption.getHhphc().name()));
107 updateState(CHANNEL_TEMPO_FRAME_PROGRAMME_CIRCUIT_1,
108 new StringType(frameTempoOption.getProgrammeCircuit1().name()));
109 updateState(CHANNEL_TEMPO_FRAME_PROGRAMME_CIRCUIT_2,
110 new StringType(frameTempoOption.getProgrammeCircuit2().name()));
112 if (frameTempoOption.getDemain() == null) {
113 updateState(CHANNEL_TEMPO_FRAME_DEMAIN, UnDefType.NULL);
115 updateState(CHANNEL_TEMPO_FRAME_DEMAIN, new StringType(frameTempoOption.getDemain().name()));
119 protected void updateStatesForEjpFrameOption(FrameEjpOption frameEjpOption) {
120 updateState(CHANNEL_EJP_FRAME_EJPHN, QuantityType.valueOf(frameEjpOption.getEjphn(), Units.WATT_HOUR));
121 updateState(CHANNEL_EJP_FRAME_EJPHPM, QuantityType.valueOf(frameEjpOption.getEjphpm(), Units.WATT_HOUR));
123 if (frameEjpOption.getPejp() == null) {
124 updateState(CHANNEL_EJP_FRAME_PEJP, UnDefType.NULL);
126 updateState(CHANNEL_EJP_FRAME_PEJP, QuantityType.valueOf(frameEjpOption.getPejp(), Units.MINUTE));
131 protected void updateStatus(ThingStatus status, ThingStatusDetail statusDetail, @Nullable String description) {
132 super.updateStatus(status, statusDetail, description);
134 if (!(ThingStatus.ONLINE.equals(status))) {
135 for (Channel channel : getThing().getChannels()) {
136 if (!CHANNEL_LAST_UPDATE.equals(channel.getUID().getId())) {
137 updateState(channel.getUID(), UnDefType.UNDEF);
144 protected void updateStatus(ThingStatus status, ThingStatusDetail statusDetail) {
145 this.updateStatus(status, statusDetail, null);
149 protected void updateStatus(ThingStatus status) {
150 this.updateStatus(status, ThingStatusDetail.NONE, null);