]> git.basschouten.com Git - openhab-addons.git/blob
20ff18d1c7740277bc6a04ad381b4baab990b1be
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.freeboxos.internal.handler;
14
15 import static org.openhab.binding.freeboxos.internal.FreeboxOsBindingConstants.*;
16
17 import java.util.List;
18
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;
32
33 /**
34  * The {@link ShutterHandler} is responsible for handling everything associated to any Freebox Home shutter.
35  *
36  * @author GaĆ«l L'hopital - Initial contribution
37  */
38 @NonNullByDefault
39 public class ShutterHandler extends HomeNodeHandler {
40
41     public ShutterHandler(Thing thing) {
42         super(thing);
43     }
44
45     @Override
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()));
49     }
50
51     @Override
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 + " %")
55                 : UnDefType.NULL;
56     }
57
58     @Override
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());
69             }
70         }
71         return false;
72     }
73
74     private boolean operateShutter(HomeManager homeManager, int stopSlot, int positionSlot, int target)
75             throws FreeboxException {
76         homeManager.putCommand(getClientId(), stopSlot, true);
77         if (target >= 0) {
78             homeManager.putCommand(getClientId(), positionSlot, target);
79         }
80         return true;
81     }
82 }