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.adorne.internal.handler;
15 import static org.openhab.binding.adorne.internal.AdorneBindingConstants.CHANNEL_BRIGHTNESS;
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;
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.
31 * @author Mark Theiding - Initial contribution
34 public class AdorneDimmerHandler extends AdorneSwitchHandler {
35 private final Logger logger = LoggerFactory.getLogger(AdorneDimmerHandler.class);
37 public AdorneDimmerHandler(Thing thing) {
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.
47 public void handleCommand(ChannelUID channelUID, Command command) {
48 logger.trace("handleCommand (channelUID:{} command:{}", channelUID, command);
50 if (channelUID.getId().equals(CHANNEL_BRIGHTNESS)) {
51 if (command instanceof RefreshType) {
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);
60 logger.debug("Ignored command to set brightness to level {}", level);
64 super.handleCommand(channelUID, command); // Parent can handle everything else
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());
77 * Refreshes the brightness of our thing to the actual state of the device.
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);
90 * Refreshes all supported channels.
94 public void refresh() {