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.tradfri.internal.handler;
15 import static org.openhab.binding.tradfri.internal.TradfriBindingConstants.*;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.tradfri.internal.TradfriCoapClient;
19 import org.openhab.binding.tradfri.internal.model.TradfriBlindData;
20 import org.openhab.core.library.types.DecimalType;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.library.types.PercentType;
23 import org.openhab.core.library.types.StopMoveType;
24 import org.openhab.core.library.types.UpDownType;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.types.Command;
29 import org.openhab.core.types.RefreshType;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import com.google.gson.JsonElement;
36 * The {@link TradfriBlindHandler} is responsible for handling commands for individual blinds.
38 * @author Manuel Raffel - Initial contribution
41 public class TradfriBlindHandler extends TradfriThingHandler {
43 private final Logger logger = LoggerFactory.getLogger(TradfriBlindHandler.class);
45 public TradfriBlindHandler(Thing thing) {
50 public void onUpdate(JsonElement data) {
51 if (active && !(data.isJsonNull())) {
52 TradfriBlindData state = new TradfriBlindData(data);
53 updateStatus(state.getReachabilityStatus() ? ThingStatus.ONLINE : ThingStatus.OFFLINE);
55 PercentType position = state.getPosition();
56 if (position != null) {
57 updateState(CHANNEL_POSITION, position);
60 DecimalType batteryLevel = state.getBatteryLevel();
61 if (batteryLevel != null) {
62 updateState(CHANNEL_BATTERY_LEVEL, batteryLevel);
65 OnOffType batteryLow = state.getBatteryLow();
66 if (batteryLow != null) {
67 updateState(CHANNEL_BATTERY_LOW, batteryLow);
70 updateDeviceProperties(state);
73 "Updating thing for blindId {} to state {position: {}, firmwareVersion: {}, modelId: {}, vendor: {}}",
74 state.getDeviceId(), position, state.getFirmwareVersion(), state.getModelId(), state.getVendor());
78 private void setPosition(PercentType percent) {
79 set(new TradfriBlindData().setPosition(percent).getJsonString());
82 private void triggerStop() {
83 set(new TradfriBlindData().stop().getJsonString());
87 public void handleCommand(ChannelUID channelUID, Command command) {
89 if (command instanceof RefreshType) {
90 TradfriCoapClient coapClient = this.coapClient;
91 if (coapClient != null) {
92 logger.debug("Refreshing channel {}", channelUID);
93 coapClient.asyncGet(this);
95 logger.debug("coapClient is null!");
100 switch (channelUID.getId()) {
101 case CHANNEL_POSITION:
102 handlePositionCommand(command);
105 logger.error("Unknown channel UID {}", channelUID);
110 private void handlePositionCommand(Command command) {
111 if (command instanceof PercentType percentCommand) {
112 setPosition(percentCommand);
113 } else if (command instanceof StopMoveType) {
114 if (StopMoveType.STOP.equals(command)) {
117 logger.debug("Cannot handle command '{}' for channel '{}'", command, CHANNEL_POSITION);
119 } else if (command instanceof UpDownType) {
120 if (UpDownType.UP.equals(command)) {
121 setPosition(PercentType.ZERO);
123 setPosition(PercentType.HUNDRED);
126 logger.debug("Cannot handle command '{}' for channel '{}'", command, CHANNEL_POSITION);