]> git.basschouten.com Git - openhab-addons.git/blob
e8ca0c6366c3dac26a038a412712a66dc8799790
[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.adorne.internal.handler;
14
15 import static org.openhab.binding.adorne.internal.AdorneBindingConstants.CHANNEL_BRIGHTNESS;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.adorne.internal.hub.AdorneHubController;
19 import org.openhab.core.library.types.PercentType;
20 import org.openhab.core.thing.ChannelUID;
21 import org.openhab.core.thing.Thing;
22 import org.openhab.core.types.Command;
23 import org.openhab.core.types.RefreshType;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * The {@link AdorneDimmerHandler} is responsible for handling commands, which are
29  * sent to one of the channels. It supports the brightness channel in addition to the inherited switch channel.
30  *
31  * @author Mark Theiding - Initial contribution
32  */
33 @NonNullByDefault
34 public class AdorneDimmerHandler extends AdorneSwitchHandler {
35     private final Logger logger = LoggerFactory.getLogger(AdorneDimmerHandler.class);
36
37     public AdorneDimmerHandler(Thing thing) {
38         super(thing);
39     }
40
41     /**
42      * Handles refresh and percent commands for channel
43      * {@link org.openhab.binding.adorne.internal.AdorneBindingConstants#CHANNEL_BRIGHTNESS}
44      * It delegates all other commands to its parent class.
45      */
46     @Override
47     public void handleCommand(ChannelUID channelUID, Command command) {
48         logger.trace("handleCommand (channelUID:{} command:{}", channelUID, command);
49         try {
50             if (channelUID.getId().equals(CHANNEL_BRIGHTNESS)) {
51                 if (command instanceof RefreshType) {
52                     refreshBrightness();
53                 } else if (command instanceof PercentType percentCommand) {
54                     // Change the brightness through the hub controller
55                     AdorneHubController adorneHubController = getAdorneHubController();
56                     int level = percentCommand.intValue();
57                     if (level >= 1 && level <= 100) { // Ignore commands outside of the supported 1-100 range
58                         adorneHubController.setBrightness(zoneId, level);
59                     } else {
60                         logger.debug("Ignored command to set brightness to level {}", level);
61                     }
62                 }
63             } else {
64                 super.handleCommand(channelUID, command); // Parent can handle everything else
65             }
66         } catch (IllegalStateException e) {
67             // Hub controller could't handle our commands. Unfortunately the framework has no mechanism to report
68             // runtime errors. If we throw the exception up the framework logs it as an error - we don't want that - we
69             // want the framework to handle it gracefully. No point to update the thing status, since the
70             // AdorneHubController already does that. So we are forced to swallow the exception here.
71             logger.debug("Failed to execute command {} for channel {} for thing {} ({})", command, channelUID,
72                     getThing().getLabel(), e.getMessage());
73         }
74     }
75
76     /**
77      * Refreshes the brightness of our thing to the actual state of the device.
78      *
79      */
80     public void refreshBrightness() {
81         // Asynchronously get our brightness from the hub controller and update our state accordingly
82         AdorneHubController adorneHubController = getAdorneHubController();
83         adorneHubController.getState(zoneId).thenAccept(state -> {
84             updateState(CHANNEL_BRIGHTNESS, new PercentType(state.brightness));
85             logger.debug("Refreshed dimmer {} with brightness {}", getThing().getLabel(), state.brightness);
86         });
87     }
88
89     /**
90      * Refreshes all supported channels.
91      *
92      */
93     @Override
94     public void refresh() {
95         super.refresh();
96         refreshBrightness();
97     }
98 }