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.enphase.internal.handler;
15 import static org.openhab.binding.enphase.internal.EnphaseBindingConstants.*;
17 import java.time.Instant;
18 import java.time.ZoneId;
19 import java.time.ZonedDateTime;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.enphase.internal.MessageTranslator;
24 import org.openhab.binding.enphase.internal.dto.InverterDTO;
25 import org.openhab.core.library.types.DateTimeType;
26 import org.openhab.core.library.types.QuantityType;
27 import org.openhab.core.library.unit.Units;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.types.Command;
31 import org.openhab.core.types.RefreshType;
32 import org.openhab.core.types.State;
33 import org.openhab.core.types.UnDefType;
36 * The {@link EnphaseInverterHandler} is responsible for handling commands, which are
37 * sent to one of the channels.
39 * @author Hilbrand Bouwkamp - Initial contribution
42 public class EnphaseInverterHandler extends EnphaseDeviceHandler {
44 private @Nullable InverterDTO lastKnownState;
46 public EnphaseInverterHandler(final Thing thing, MessageTranslator messageTranslator) {
47 super(thing, messageTranslator);
51 public void handleCommand(final ChannelUID channelUID, final Command command) {
52 if (command instanceof RefreshType) {
53 final String channelId = channelUID.getId();
56 case INVERTER_CHANNEL_LAST_REPORT_WATTS:
57 refreshLastReportWatts(lastKnownState);
59 case INVERTER_CHANNEL_MAX_REPORT_WATTS:
60 refreshMaxReportWatts(lastKnownState);
62 case INVERTER_CHANNEL_LAST_REPORT_DATE:
63 refreshLastReportDate(lastKnownState);
66 super.handleCommandRefresh(channelId);
72 public void refreshInverterChannels(final @Nullable InverterDTO inverterDTO) {
73 refreshLastReportWatts(inverterDTO);
74 refreshMaxReportWatts(inverterDTO);
75 refreshLastReportDate(inverterDTO);
76 lastKnownState = inverterDTO;
79 private void refreshLastReportWatts(final @Nullable InverterDTO inverterDTO) {
80 updateState(INVERTER_CHANNEL_LAST_REPORT_WATTS,
81 inverterDTO == null ? UnDefType.UNDEF : new QuantityType<>(inverterDTO.lastReportWatts, Units.WATT));
84 private void refreshMaxReportWatts(final @Nullable InverterDTO inverterDTO) {
85 updateState(INVERTER_CHANNEL_MAX_REPORT_WATTS,
86 inverterDTO == null ? UnDefType.UNDEF : new QuantityType<>(inverterDTO.maxReportWatts, Units.WATT));
89 private void refreshLastReportDate(final @Nullable InverterDTO inverterDTO) {
92 if (inverterDTO == null) {
93 state = UnDefType.UNDEF;
95 final Instant instant = Instant.ofEpochSecond(inverterDTO.lastReportDate);
96 final ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());
97 logger.trace("[{}] Epoch time {}, zonedDateTime: {}", getThing().getUID(), inverterDTO.lastReportDate,
99 state = new DateTimeType(zonedDateTime);
101 updateState(INVERTER_CHANNEL_LAST_REPORT_DATE, state);