]> git.basschouten.com Git - openhab-addons.git/blob
7225baa0ec33f732c743a7b8facab82f2a931087
[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.somfytahoma.internal.handler;
14
15 import static org.openhab.binding.somfytahoma.internal.SomfyTahomaBindingConstants.*;
16 import static org.openhab.core.thing.Thing.PROPERTY_FIRMWARE_VERSION;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.somfytahoma.internal.SomfyTahomaStateDescriptionOptionProvider;
24 import org.openhab.binding.somfytahoma.internal.model.SomfyTahomaActionGroup;
25 import org.openhab.binding.somfytahoma.internal.model.SomfyTahomaStatus;
26 import org.openhab.core.library.types.StringType;
27 import org.openhab.core.thing.Channel;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.ThingStatusDetail;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.RefreshType;
34 import org.openhab.core.types.StateOption;
35
36 /**
37  * The {@link SomfyTahomaGatewayHandler} is responsible for handling commands,
38  * which are sent to one of the channels of the gateway thing.
39  *
40  * @author Ondrej Pecta - Initial contribution
41  */
42 @NonNullByDefault
43 public class SomfyTahomaGatewayHandler extends SomfyTahomaBaseThingHandler {
44
45     private final SomfyTahomaStateDescriptionOptionProvider stateDescriptionProvider;
46
47     public SomfyTahomaGatewayHandler(Thing thing, SomfyTahomaStateDescriptionOptionProvider stateDescriptionProvider) {
48         super(thing);
49         this.stateDescriptionProvider = stateDescriptionProvider;
50     }
51
52     @Override
53     public void initializeThing(@Nullable ThingStatus bridgeStatus) {
54         if (bridgeStatus != null) {
55             if (bridgeStatus == ThingStatus.ONLINE) {
56                 refresh(STATUS);
57                 refresh(SCENARIOS);
58             } else {
59                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
60             }
61         } else {
62             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED);
63         }
64     }
65
66     @Override
67     public void refresh(String channel) {
68         if (channel.equals(STATUS)) {
69             String id = getGateWayId();
70             SomfyTahomaStatus status = getTahomaStatus(id);
71             String tahomaStatus = status.getStatus();
72             Channel ch = thing.getChannel(channel);
73             if (ch != null) {
74                 updateState(ch.getUID(), new StringType(tahomaStatus));
75             }
76             // update the firmware property
77             String fw = status.getProtocolVersion();
78             updateProperty(PROPERTY_FIRMWARE_VERSION, fw);
79
80             updateStatus("DISCONNECTED".equals(tahomaStatus) ? ThingStatus.OFFLINE : ThingStatus.ONLINE);
81         } else if (channel.equals(SCENARIOS)) {
82             SomfyTahomaBridgeHandler handler = getBridgeHandler();
83             if (handler != null) {
84                 List<StateOption> options = new ArrayList<>();
85                 for (SomfyTahomaActionGroup actionGroup : handler.listActionGroups()) {
86                     options.add(new StateOption(actionGroup.getOid(), actionGroup.getLabel()));
87                 }
88                 stateDescriptionProvider.setStateOptions(new ChannelUID(getThing().getUID(), channel), options);
89             }
90         }
91     }
92
93     public String getGateWayId() {
94         return getThing().getConfiguration().get("id").toString();
95     }
96
97     @Override
98     public void handleCommand(ChannelUID channelUID, Command command) {
99         super.handleCommand(channelUID, command);
100         if (command instanceof RefreshType) {
101             return;
102         }
103         if (channelUID.getId().equals(SCENARIOS)) {
104             SomfyTahomaBridgeHandler handler = getBridgeHandler();
105             if (handler != null && command instanceof StringType) {
106                 handler.executeActionGroup(command.toString());
107             }
108         }
109     }
110 }