]> git.basschouten.com Git - openhab-addons.git/blob
4a51d8bf883da4992558bdb09652ace25515b6ec
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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 import java.util.Set;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.freeboxos.internal.api.FreeboxException;
22 import org.openhab.binding.freeboxos.internal.api.rest.HomeManager;
23 import org.openhab.binding.freeboxos.internal.api.rest.HomeManager.Endpoint;
24 import org.openhab.binding.freeboxos.internal.api.rest.HomeManager.EndpointState;
25 import org.openhab.core.config.core.Configuration;
26 import org.openhab.core.library.types.OpenClosedType;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.types.Command;
29 import org.openhab.core.types.State;
30 import org.openhab.core.types.UnDefType;
31
32 /**
33  * The {@link BasicShutterHandler} is responsible for handling everything associated to
34  * any Freebox Home basic-shutter thing type.
35  *
36  * @author ben12 - Initial contribution
37  */
38 @NonNullByDefault
39 public class BasicShutterHandler extends HomeNodeHandler {
40     private static final Set<String> SHUTTER_ENDPOINTS = Set.of(SHUTTER_STOP, BASIC_SHUTTER_UP, BASIC_SHUTTER_DOWN);
41
42     public BasicShutterHandler(Thing thing) {
43         super(thing);
44     }
45
46     @Override
47     protected void internalConfigureChannel(String channelId, Configuration conf, List<Endpoint> endpoints) {
48         endpoints.stream().filter(ep -> channelId.equals(BASIC_SHUTTER_STATE) && SHUTTER_ENDPOINTS.contains(ep.name()))
49                 .forEach(endPoint -> conf.put(endPoint.name(), endPoint.id()));
50     }
51
52     @Override
53     protected State getChannelState(HomeManager homeManager, String channelId, EndpointState state) {
54         String value = state.value();
55         return value != null && channelId.equals(BASIC_SHUTTER_STATE)
56                 ? state.asBoolean() ? OpenClosedType.CLOSED : OpenClosedType.OPEN
57                 : UnDefType.NULL;
58     }
59
60     @Override
61     protected boolean executeChannelCommand(HomeManager homeManager, String channelId, Command command,
62             Configuration config) throws FreeboxException {
63         Integer slot = getSlotId(config, command.toString().toLowerCase());
64         if (BASIC_SHUTTER_STATE.equals(channelId) && slot != null) {
65             return homeManager.putCommand(getClientId(), slot, true);
66         }
67         return false;
68     }
69 }