2 * Copyright (c) 2010-2022 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.netatmo.internal.handler.channelhelper;
15 import java.util.HashSet;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.MeasureClass;
21 import org.openhab.binding.netatmo.internal.api.dto.Dashboard;
22 import org.openhab.binding.netatmo.internal.api.dto.Event;
23 import org.openhab.binding.netatmo.internal.api.dto.NAObject;
24 import org.openhab.binding.netatmo.internal.api.dto.NAThing;
25 import org.openhab.binding.netatmo.internal.providers.NetatmoThingTypeProvider;
26 import org.openhab.core.config.core.Configuration;
27 import org.openhab.core.types.State;
30 * The {@link ChannelHelper} is the base class for all channel helpers.
32 * @author Gaƫl L'hopital - Initial contribution
36 public abstract class ChannelHelper {
37 private @Nullable NAObject data;
38 private final Set<String> channelGroupTypes;
39 private final Set<String> channelGroups = new HashSet<>();
40 private Set<String> extensibleChannels = Set.of();
42 ChannelHelper(String... providedGroups) {
43 this.channelGroupTypes = Set.of(providedGroups);
44 channelGroupTypes.forEach(groupType -> channelGroups.add(NetatmoThingTypeProvider.toGroupName(groupType)));
47 ChannelHelper(String providedGroup, MeasureClass measureClass) {
49 this.extensibleChannels = measureClass.channels.keySet();
52 public void setNewData(@Nullable NAObject data) {
56 public final @Nullable State getChannelState(String channelId, @Nullable String groupId, Configuration config) {
58 if (channelGroups.isEmpty() || (groupId != null && channelGroups.contains(groupId))) {
59 NAObject localData = data;
60 if (localData instanceof Event) {
61 result = internalGetEvent(channelId, (Event) localData);
66 if (localData instanceof NAThing) {
67 NAThing naThing = (NAThing) localData;
68 result = internalGetProperty(channelId, naThing, config);
72 Dashboard dashboard = naThing.getDashboardData();
73 if (dashboard != null) {
74 result = internalGetDashboard(channelId, dashboard);
80 if (localData instanceof NAObject) {
81 result = internalGetObject(channelId, localData);
86 result = internalGetOther(channelId);
91 protected @Nullable State internalGetObject(String channelId, NAObject localData) {
95 protected @Nullable State internalGetOther(String channelId) {
99 protected @Nullable State internalGetDashboard(String channelId, Dashboard dashboard) {
103 protected @Nullable State internalGetProperty(String channelId, NAThing naThing, Configuration config) {
107 protected @Nullable State internalGetEvent(String channelId, Event event) {
111 public Set<String> getChannelGroupTypes() {
112 return channelGroupTypes;
115 public Set<String> getExtensibleChannels() {
116 return extensibleChannels;