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.SmartHomeUnits;
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(), SmartHomeUnits.WATT_HOUR));
93 protected void updateStatesForHcFrameOption(FrameHcOption frameHcOption) {
94 updateState(CHANNEL_HC_FRAME_HCHC, QuantityType.valueOf(frameHcOption.getHchc(), SmartHomeUnits.WATT_HOUR));
95 updateState(CHANNEL_HC_FRAME_HCHP, QuantityType.valueOf(frameHcOption.getHchp(), SmartHomeUnits.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,
101 QuantityType.valueOf(frameTempoOption.getBbrhpjr(), SmartHomeUnits.WATT_HOUR));
102 updateState(CHANNEL_TEMPO_FRAME_BBRHCJR,
103 QuantityType.valueOf(frameTempoOption.getBbrhcjr(), SmartHomeUnits.WATT_HOUR));
104 updateState(CHANNEL_TEMPO_FRAME_BBRHPJW,
105 QuantityType.valueOf(frameTempoOption.getBbrhpjw(), SmartHomeUnits.WATT_HOUR));
106 updateState(CHANNEL_TEMPO_FRAME_BBRHCJW,
107 QuantityType.valueOf(frameTempoOption.getBbrhcjw(), SmartHomeUnits.WATT_HOUR));
108 updateState(CHANNEL_TEMPO_FRAME_BBRHPJB,
109 QuantityType.valueOf(frameTempoOption.getBbrhpjb(), SmartHomeUnits.WATT_HOUR));
110 updateState(CHANNEL_TEMPO_FRAME_BBRHCJB,
111 QuantityType.valueOf(frameTempoOption.getBbrhcjb(), SmartHomeUnits.WATT_HOUR));
112 updateState(CHANNEL_TEMPO_FRAME_HHPHC, new StringType(frameTempoOption.getHhphc().name()));
113 updateState(CHANNEL_TEMPO_FRAME_PROGRAMME_CIRCUIT_1,
114 new StringType(frameTempoOption.getProgrammeCircuit1().name()));
115 updateState(CHANNEL_TEMPO_FRAME_PROGRAMME_CIRCUIT_2,
116 new StringType(frameTempoOption.getProgrammeCircuit2().name()));
118 if (frameTempoOption.getDemain() == null) {
119 updateState(CHANNEL_TEMPO_FRAME_DEMAIN, UnDefType.NULL);
121 updateState(CHANNEL_TEMPO_FRAME_DEMAIN, new StringType(frameTempoOption.getDemain().name()));
125 protected void updateStatesForEjpFrameOption(FrameEjpOption frameEjpOption) {
126 updateState(CHANNEL_EJP_FRAME_EJPHN, QuantityType.valueOf(frameEjpOption.getEjphn(), SmartHomeUnits.WATT_HOUR));
127 updateState(CHANNEL_EJP_FRAME_EJPHPM,
128 QuantityType.valueOf(frameEjpOption.getEjphpm(), SmartHomeUnits.WATT_HOUR));
130 if (frameEjpOption.getPejp() == null) {
131 updateState(CHANNEL_EJP_FRAME_PEJP, UnDefType.NULL);
133 updateState(CHANNEL_EJP_FRAME_PEJP, QuantityType.valueOf(frameEjpOption.getPejp(), SmartHomeUnits.MINUTE));
138 protected void updateStatus(ThingStatus status, ThingStatusDetail statusDetail, @Nullable String description) {
139 super.updateStatus(status, statusDetail, description);
141 if (!(ThingStatus.ONLINE.equals(status))) {
142 for (Channel channel : getThing().getChannels()) {
143 if (!CHANNEL_LAST_UPDATE.equals(channel.getUID().getId())) {
144 updateState(channel.getUID(), UnDefType.UNDEF);
151 protected void updateStatus(ThingStatus status, ThingStatusDetail statusDetail) {
152 this.updateStatus(status, statusDetail, null);
156 protected void updateStatus(ThingStatus status) {
157 this.updateStatus(status, ThingStatusDetail.NONE, null);