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.freeboxos.internal.handler;
15 import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.*;
17 import java.util.List;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.freeboxos.internal.api.FreeboxException;
21 import org.openhab.binding.freeboxos.internal.api.rest.HomeManager;
22 import org.openhab.binding.freeboxos.internal.api.rest.HomeManager.Endpoint;
23 import org.openhab.binding.freeboxos.internal.api.rest.HomeManager.EndpointState;
24 import org.openhab.core.config.core.Configuration;
25 import org.openhab.core.library.types.QuantityType;
26 import org.openhab.core.library.types.StopMoveType;
27 import org.openhab.core.library.types.UpDownType;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.State;
31 import org.openhab.core.types.UnDefType;
34 * The {@link ShutterHandler} is responsible for handling everything associated to any Freebox Home shutter.
36 * @author Gaƫl L'hopital - Initial contribution
39 public class ShutterHandler extends HomeNodeHandler {
41 public ShutterHandler(Thing thing) {
46 protected void internalConfigureChannel(String channelId, Configuration conf, List<Endpoint> endpoints) {
47 endpoints.stream().filter(ep -> channelId.equals(SHUTTER_POSITION) && ep.name().equals(SHUTTER_STOP))
48 .forEach(endPoint -> conf.put(endPoint.name(), endPoint.id()));
52 protected State getChannelState(HomeManager homeManager, String channelId, EndpointState state) {
53 String value = state.value();
54 return value != null && channelId.equals(SHUTTER_POSITION) ? QuantityType.valueOf(value + " %")
59 protected boolean executeSlotCommand(HomeManager homeManager, String channelId, Command command,
60 Configuration config, int positionSlot) throws FreeboxException {
61 Integer stopSlot = getSlotId(config, SHUTTER_STOP);
62 if (SHUTTER_POSITION.equals(channelId) && stopSlot instanceof Integer) {
63 if (command instanceof UpDownType upDownCmd) {
64 return operateShutter(homeManager, stopSlot, positionSlot, upDownCmd == UpDownType.DOWN ? 100 : 0);
65 } else if (command instanceof StopMoveType stopMove && stopMove == StopMoveType.STOP) {
66 return operateShutter(homeManager, stopSlot, positionSlot, -1);
67 } else if (command instanceof Number numberCmd) {
68 return operateShutter(homeManager, stopSlot, positionSlot, numberCmd.intValue());
74 private boolean operateShutter(HomeManager homeManager, int stopSlot, int positionSlot, int target)
75 throws FreeboxException {
76 homeManager.putCommand(getClientId(), stopSlot, true);
78 homeManager.putCommand(getClientId(), positionSlot, target);