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.somfytahoma.internal.handler;
15 import static org.openhab.binding.somfytahoma.internal.SomfyTahomaBindingConstants.*;
16 import static org.openhab.core.thing.Thing.PROPERTY_FIRMWARE_VERSION;
18 import java.util.ArrayList;
19 import java.util.List;
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;
37 * The {@link SomfyTahomaGatewayHandler} is responsible for handling commands,
38 * which are sent to one of the channels of the gateway thing.
40 * @author Ondrej Pecta - Initial contribution
43 public class SomfyTahomaGatewayHandler extends SomfyTahomaBaseThingHandler {
45 private final SomfyTahomaStateDescriptionOptionProvider stateDescriptionProvider;
47 public SomfyTahomaGatewayHandler(Thing thing, SomfyTahomaStateDescriptionOptionProvider stateDescriptionProvider) {
49 this.stateDescriptionProvider = stateDescriptionProvider;
53 public void initializeThing(@Nullable ThingStatus bridgeStatus) {
54 if (bridgeStatus != null) {
55 if (bridgeStatus == ThingStatus.ONLINE) {
59 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
62 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_UNINITIALIZED);
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);
74 updateState(ch.getUID(), new StringType(tahomaStatus));
76 // update the firmware property
77 String fw = status.getProtocolVersion();
78 updateProperty(PROPERTY_FIRMWARE_VERSION, fw);
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()));
88 stateDescriptionProvider.setStateOptions(new ChannelUID(getThing().getUID(), channel), options);
93 public String getGateWayId() {
94 return getThing().getConfiguration().get("id").toString();
98 public void handleCommand(ChannelUID channelUID, Command command) {
99 super.handleCommand(channelUID, command);
100 if (command instanceof RefreshType) {
103 if (channelUID.getId().equals(SCENARIOS)) {
104 SomfyTahomaBridgeHandler handler = getBridgeHandler();
105 if (handler != null && command instanceof StringType) {
106 handler.executeActionGroup(command.toString());