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.plugwise.internal.handler;
15 import static org.openhab.binding.plugwise.internal.PlugwiseBindingConstants.CHANNEL_TRIGGERED;
16 import static org.openhab.core.thing.ThingStatus.*;
18 import java.time.Duration;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.plugwise.internal.protocol.AcknowledgementMessage;
22 import org.openhab.binding.plugwise.internal.protocol.AnnounceAwakeRequestMessage;
23 import org.openhab.binding.plugwise.internal.protocol.AnnounceAwakeRequestMessage.AwakeReason;
24 import org.openhab.binding.plugwise.internal.protocol.BroadcastGroupSwitchResponseMessage;
25 import org.openhab.binding.plugwise.internal.protocol.InformationResponseMessage;
26 import org.openhab.binding.plugwise.internal.protocol.Message;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.thing.Thing;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * The {@link AbstractPlugwiseThingHandler} handles common Plugwise sleeping end device (SED) channel updates and
36 * @author Wouter Born - Initial contribution
39 public abstract class AbstractSleepingEndDeviceHandler extends AbstractPlugwiseThingHandler {
41 private static final int SED_PROPERTIES_COUNT = 3;
43 private final Logger logger = LoggerFactory.getLogger(AbstractSleepingEndDeviceHandler.class);
45 protected AbstractSleepingEndDeviceHandler(Thing thing) {
49 protected abstract Duration getWakeupDuration();
51 protected void handleAcknowledgement(AcknowledgementMessage message) {
52 updateStatusOnDetailChange();
55 protected void handleAnnounceAwakeRequest(AnnounceAwakeRequestMessage message) {
56 AwakeReason awakeReason = message.getAwakeReason();
57 if (awakeReason == AwakeReason.MAINTENANCE || awakeReason == AwakeReason.WAKEUP_BUTTON
58 || editProperties().size() < SED_PROPERTIES_COUNT) {
60 if (isConfigurationPending() && !recentlySendConfigurationUpdate()) {
61 sendConfigurationUpdateCommands();
66 protected void handleBroadcastGroupSwitchResponseMessage(BroadcastGroupSwitchResponseMessage message) {
67 updateState(CHANNEL_TRIGGERED, message.getPowerState() ? OnOffType.ON : OnOffType.OFF);
70 protected void handleInformationResponse(InformationResponseMessage message) {
71 updateProperties(message);
75 public void handleResponseMessage(Message message) {
78 switch (message.getType()) {
79 case ACKNOWLEDGEMENT_V1, ACKNOWLEDGEMENT_V2:
80 handleAcknowledgement((AcknowledgementMessage) message);
82 case ANNOUNCE_AWAKE_REQUEST:
83 handleAnnounceAwakeRequest((AnnounceAwakeRequestMessage) message);
85 case BROADCAST_GROUP_SWITCH_RESPONSE:
86 handleBroadcastGroupSwitchResponseMessage((BroadcastGroupSwitchResponseMessage) message);
88 case DEVICE_INFORMATION_RESPONSE:
89 handleInformationResponse((InformationResponseMessage) message);
92 logger.trace("Received unhandled {} message from {} ({})", message.getType(), getDeviceType(),
99 protected boolean shouldOnlineTaskBeScheduled() {
100 return thing.getStatus() == ONLINE;
104 protected void updateOnlineState() {
105 if (thing.getStatus() == ONLINE && getWakeupDuration().minus(durationSinceLastSeen()).isNegative()) {
106 updateStatus(OFFLINE, getThingStatusDetail());