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.HomeEvent;
24 import org.openhab.binding.netatmo.internal.api.dto.NAObject;
25 import org.openhab.binding.netatmo.internal.api.dto.NAThing;
26 import org.openhab.binding.netatmo.internal.providers.NetatmoThingTypeProvider;
27 import org.openhab.core.config.core.Configuration;
28 import org.openhab.core.types.State;
31 * The {@link ChannelHelper} is the base class for all channel helpers.
33 * @author Gaƫl L'hopital - Initial contribution
37 public abstract class ChannelHelper {
38 private @Nullable NAObject data;
39 private final Set<String> channelGroupTypes;
40 private final Set<String> channelGroups = new HashSet<>();
41 private Set<String> extensibleChannels = Set.of();
43 ChannelHelper(String... providedGroups) {
44 this.channelGroupTypes = Set.of(providedGroups);
45 channelGroupTypes.forEach(groupType -> channelGroups.add(NetatmoThingTypeProvider.toGroupName(groupType)));
48 ChannelHelper(String providedGroup, MeasureClass measureClass) {
50 this.extensibleChannels = measureClass.channels.keySet();
53 public void setNewData(@Nullable NAObject data) {
57 public final @Nullable State getChannelState(String channelId, @Nullable String groupId, Configuration config) {
59 if (channelGroups.isEmpty() || (groupId != null && channelGroups.contains(groupId))) {
60 NAObject localData = data;
61 if (localData instanceof HomeEvent) {
62 result = internalGetHomeEvent(channelId, groupId, (HomeEvent) localData);
67 if (localData instanceof Event) {
68 result = internalGetEvent(channelId, (Event) localData);
73 if (localData instanceof NAThing) {
74 NAThing naThing = (NAThing) localData;
75 result = internalGetProperty(channelId, naThing, config);
79 Dashboard dashboard = naThing.getDashboardData();
80 if (dashboard != null) {
81 result = internalGetDashboard(channelId, dashboard);
87 if (localData instanceof NAObject) {
88 result = internalGetObject(channelId, localData);
93 result = internalGetOther(channelId);
98 protected @Nullable State internalGetObject(String channelId, NAObject localData) {
102 protected @Nullable State internalGetOther(String channelId) {
106 protected @Nullable State internalGetDashboard(String channelId, Dashboard dashboard) {
110 protected @Nullable State internalGetProperty(String channelId, NAThing naThing, Configuration config) {
114 protected @Nullable State internalGetEvent(String channelId, Event event) {
118 protected @Nullable State internalGetHomeEvent(String channelId, @Nullable String groupId, HomeEvent event) {
122 public Set<String> getChannelGroupTypes() {
123 return channelGroupTypes;
126 public Set<String> getExtensibleChannels() {
127 return extensibleChannels;