2 * Copyright (c) 2010-2022 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.model.TradfriBlindData;
19 import org.openhab.core.library.types.DecimalType;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.library.types.PercentType;
22 import org.openhab.core.library.types.StopMoveType;
23 import org.openhab.core.library.types.UpDownType;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.types.RefreshType;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import com.google.gson.JsonElement;
35 * The {@link TradfriBlindHandler} is responsible for handling commands for individual blinds.
37 * @author Manuel Raffel - Initial contribution
40 public class TradfriBlindHandler extends TradfriThingHandler {
42 private final Logger logger = LoggerFactory.getLogger(TradfriBlindHandler.class);
44 public TradfriBlindHandler(Thing thing) {
49 public void onUpdate(JsonElement data) {
50 if (active && !(data.isJsonNull())) {
51 TradfriBlindData state = new TradfriBlindData(data);
52 updateStatus(state.getReachabilityStatus() ? ThingStatus.ONLINE : ThingStatus.OFFLINE);
54 PercentType position = state.getPosition();
55 if (position != null) {
56 updateState(CHANNEL_POSITION, position);
59 DecimalType batteryLevel = state.getBatteryLevel();
60 if (batteryLevel != null) {
61 updateState(CHANNEL_BATTERY_LEVEL, batteryLevel);
64 OnOffType batteryLow = state.getBatteryLow();
65 if (batteryLow != null) {
66 updateState(CHANNEL_BATTERY_LOW, batteryLow);
69 updateDeviceProperties(state);
72 "Updating thing for blindId {} to state {position: {}, firmwareVersion: {}, modelId: {}, vendor: {}}",
73 state.getDeviceId(), position, state.getFirmwareVersion(), state.getModelId(), state.getVendor());
77 private void setPosition(PercentType percent) {
78 set(new TradfriBlindData().setPosition(percent).getJsonString());
81 private void triggerStop() {
82 set(new TradfriBlindData().stop().getJsonString());
86 public void handleCommand(ChannelUID channelUID, Command command) {
88 if (command instanceof RefreshType) {
89 logger.debug("Refreshing channel {}", channelUID);
90 coapClient.asyncGet(this);
94 switch (channelUID.getId()) {
95 case CHANNEL_POSITION:
96 handlePositionCommand(command);
99 logger.error("Unknown channel UID {}", channelUID);
104 private void handlePositionCommand(Command command) {
105 if (command instanceof PercentType) {
106 setPosition((PercentType) command);
107 } else if (command instanceof StopMoveType) {
108 if (StopMoveType.STOP.equals(command)) {
111 logger.debug("Cannot handle command '{}' for channel '{}'", command, CHANNEL_POSITION);
113 } else if (command instanceof UpDownType) {
114 if (UpDownType.UP.equals(command)) {
115 setPosition(PercentType.ZERO);
117 setPosition(PercentType.HUNDRED);
120 logger.debug("Cannot handle command '{}' for channel '{}'", command, CHANNEL_POSITION);