]> git.basschouten.com Git - openhab-addons.git/blob
ed56c31dd157a155ac728fdfb7636834b6479a35
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.plugwise.internal.handler;
14
15 import static org.openhab.binding.plugwise.internal.PlugwiseBindingConstants.*;
16
17 import java.time.Duration;
18
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;
33
34 /**
35  * <p>
36  * The {@link PlugwiseSwitchHandler} handles channel updates and commands for a Plugwise Switch device.
37  * </p>
38  * <p>
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.
41  * </p>
42  *
43  * @author Wouter Born - Initial contribution
44  */
45 @NonNullByDefault
46 public class PlugwiseSwitchHandler extends AbstractSleepingEndDeviceHandler {
47
48     private final Logger logger = LoggerFactory.getLogger(PlugwiseSwitchHandler.class);
49     private final DeviceType deviceType = DeviceType.SWITCH;
50
51     private @NonNullByDefault({}) PlugwiseSwitchConfig configuration;
52     private @NonNullByDefault({}) MACAddress macAddress;
53
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;
58
59     public PlugwiseSwitchHandler(Thing thing) {
60         super(thing);
61     }
62
63     @Override
64     protected MACAddress getMACAddress() {
65         return macAddress;
66     }
67
68     @Override
69     protected Duration getWakeupDuration() {
70         return configuration.getWakeupDuration();
71     }
72
73     @Override
74     protected void handleAcknowledgement(AcknowledgementMessage message) {
75         boolean oldConfigurationPending = isConfigurationPending();
76
77         if (message.getExtensionCode() == ExtensionCode.SLEEP_SET_ACK) {
78             logger.debug("Received ACK for sleep set of {} ({})", deviceType, macAddress);
79             updateSleepParameters = false;
80         }
81
82         boolean newConfigurationPending = isConfigurationPending();
83
84         if (oldConfigurationPending != newConfigurationPending && !newConfigurationPending) {
85             Configuration newConfiguration = editConfiguration();
86             newConfiguration.put(CONFIG_PROPERTY_UPDATE_CONFIGURATION, false);
87             updateConfiguration(newConfiguration);
88         }
89
90         super.handleAcknowledgement(message);
91     }
92
93     @Override
94     protected void handleBroadcastGroupSwitchResponseMessage(BroadcastGroupSwitchResponseMessage message) {
95         if (message.getPortMask() == 1) {
96             updateState(CHANNEL_LEFT_BUTTON_STATE, message.getPowerState() ? OnOffType.ON : OnOffType.OFF);
97         } else if (message.getPortMask() == 2) {
98             updateState(CHANNEL_RIGHT_BUTTON_STATE, message.getPowerState() ? OnOffType.ON : OnOffType.OFF);
99         }
100     }
101
102     @Override
103     public void initialize() {
104         configuration = getConfigAs(PlugwiseSwitchConfig.class);
105         macAddress = configuration.getMACAddress();
106         if (!isInitialized()) {
107             setUpdateCommandFlags(null, configuration);
108         }
109         super.initialize();
110     }
111
112     @Override
113     protected boolean isConfigurationPending() {
114         return updateSleepParameters;
115     }
116
117     @Override
118     protected void sendConfigurationUpdateCommands() {
119         logger.debug("Sending {} ({}) configuration update commands", deviceType, macAddress);
120
121         if (updateSleepParameters) {
122             logger.debug("Sending command to update {} ({}) sleep parameters", deviceType, macAddress);
123             sendCommandMessage(new SleepSetRequestMessage(macAddress, configuration.getWakeupDuration(),
124                     configuration.getWakeupInterval()));
125         }
126
127         super.sendConfigurationUpdateCommands();
128     }
129
130     private void setUpdateCommandFlags(@Nullable PlugwiseSwitchConfig oldConfiguration,
131             PlugwiseSwitchConfig newConfiguration) {
132         boolean fullUpdate = newConfiguration.isUpdateConfiguration() && !isConfigurationPending();
133         if (fullUpdate) {
134             logger.debug("Updating all configuration properties of {} ({})", deviceType, macAddress);
135         }
136
137         updateSleepParameters = fullUpdate
138                 || (oldConfiguration != null && !oldConfiguration.equalSleepParameters(newConfiguration));
139         if (updateSleepParameters) {
140             logger.debug("Updating {} ({}) sleep parameters when online", deviceType, macAddress);
141         }
142     }
143
144     @Override
145     protected void updateConfiguration(Configuration configuration) {
146         PlugwiseSwitchConfig oldConfiguration = this.configuration;
147         PlugwiseSwitchConfig newConfiguration = configuration.as(PlugwiseSwitchConfig.class);
148
149         setUpdateCommandFlags(oldConfiguration, newConfiguration);
150         configuration.put(CONFIG_PROPERTY_UPDATE_CONFIGURATION, isConfigurationPending());
151
152         super.updateConfiguration(configuration);
153     }
154 }