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.*;
17 import java.time.Duration;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.plugwise.internal.config.PlugwiseSwitchConfig;
22 import org.openhab.binding.plugwise.internal.protocol.AcknowledgementMessage;
23 import org.openhab.binding.plugwise.internal.protocol.AcknowledgementMessage.ExtensionCode;
24 import org.openhab.binding.plugwise.internal.protocol.BroadcastGroupSwitchResponseMessage;
25 import org.openhab.binding.plugwise.internal.protocol.SleepSetRequestMessage;
26 import org.openhab.binding.plugwise.internal.protocol.field.DeviceType;
27 import org.openhab.binding.plugwise.internal.protocol.field.MACAddress;
28 import org.openhab.core.config.core.Configuration;
29 import org.openhab.core.library.types.OnOffType;
30 import org.openhab.core.thing.Thing;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
36 * The {@link PlugwiseSwitchHandler} handles channel updates and commands for a Plugwise Switch device.
39 * The Switch is a mountable wireless switch with one or two buttons depending on what parts are in place. When one
40 * button is used this corresponds to only using the left button.
43 * @author Wouter Born - Initial contribution
46 public class PlugwiseSwitchHandler extends AbstractSleepingEndDeviceHandler {
48 private final Logger logger = LoggerFactory.getLogger(PlugwiseSwitchHandler.class);
49 private final DeviceType deviceType = DeviceType.SWITCH;
51 private @NonNullByDefault({}) PlugwiseSwitchConfig configuration;
52 private @NonNullByDefault({}) MACAddress macAddress;
54 // Flag that keeps track of the pending "sleep parameters" Switch configuration update. When the corresponding
55 // Thing configuration parameters change it is set to true. When the Switch goes online a command is sent to
56 // update the device configuration. When the Switch acknowledges the command the flag is again set to false.
57 private boolean updateSleepParameters;
59 public PlugwiseSwitchHandler(Thing thing) {
64 protected MACAddress getMACAddress() {
69 protected Duration getWakeupDuration() {
70 return configuration.getWakeupDuration();
74 protected void handleAcknowledgement(AcknowledgementMessage message) {
75 boolean oldConfigurationPending = isConfigurationPending();
77 if (message.getExtensionCode() == ExtensionCode.SLEEP_SET_ACK) {
78 logger.debug("Received ACK for sleep set of {} ({})", deviceType, macAddress);
79 updateSleepParameters = false;
82 boolean newConfigurationPending = isConfigurationPending();
84 if (oldConfigurationPending != newConfigurationPending && !newConfigurationPending) {
85 Configuration newConfiguration = editConfiguration();
86 newConfiguration.put(CONFIG_PROPERTY_UPDATE_CONFIGURATION, false);
87 updateConfiguration(newConfiguration);
90 super.handleAcknowledgement(message);
94 protected void handleBroadcastGroupSwitchResponseMessage(BroadcastGroupSwitchResponseMessage message) {
95 if (message.getPortMask() == 1) {
96 updateState(CHANNEL_LEFT_BUTTON_STATE, OnOffType.from(message.getPowerState()));
97 } else if (message.getPortMask() == 2) {
98 updateState(CHANNEL_RIGHT_BUTTON_STATE, OnOffType.from(message.getPowerState()));
103 public void initialize() {
104 configuration = getConfigAs(PlugwiseSwitchConfig.class);
105 macAddress = configuration.getMACAddress();
106 if (!isInitialized()) {
107 setUpdateCommandFlags(null, configuration);
113 protected boolean isConfigurationPending() {
114 return updateSleepParameters;
118 protected void sendConfigurationUpdateCommands() {
119 logger.debug("Sending {} ({}) configuration update commands", deviceType, macAddress);
121 if (updateSleepParameters) {
122 logger.debug("Sending command to update {} ({}) sleep parameters", deviceType, macAddress);
123 sendCommandMessage(new SleepSetRequestMessage(macAddress, configuration.getWakeupDuration(),
124 configuration.getWakeupInterval()));
127 super.sendConfigurationUpdateCommands();
130 private void setUpdateCommandFlags(@Nullable PlugwiseSwitchConfig oldConfiguration,
131 PlugwiseSwitchConfig newConfiguration) {
132 boolean fullUpdate = newConfiguration.isUpdateConfiguration() && !isConfigurationPending();
134 logger.debug("Updating all configuration properties of {} ({})", deviceType, macAddress);
137 updateSleepParameters = fullUpdate
138 || (oldConfiguration != null && !oldConfiguration.equalSleepParameters(newConfiguration));
139 if (updateSleepParameters) {
140 logger.debug("Updating {} ({}) sleep parameters when online", deviceType, macAddress);
145 protected void updateConfiguration(Configuration configuration) {
146 PlugwiseSwitchConfig oldConfiguration = this.configuration;
147 PlugwiseSwitchConfig newConfiguration = configuration.as(PlugwiseSwitchConfig.class);
149 setUpdateCommandFlags(oldConfiguration, newConfiguration);
150 configuration.put(CONFIG_PROPERTY_UPDATE_CONFIGURATION, isConfigurationPending());
152 super.updateConfiguration(configuration);