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.bticinosmarther.internal.handler;
15 import static org.openhab.binding.bticinosmarther.internal.SmartherBindingConstants.DTF_DATE;
17 import java.time.LocalDate;
18 import java.time.LocalDateTime;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.stream.Collectors;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.bticinosmarther.internal.api.dto.Program;
26 import org.openhab.binding.bticinosmarther.internal.util.DateUtil;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.binding.BaseDynamicStateDescriptionProvider;
29 import org.openhab.core.thing.type.DynamicStateDescriptionProvider;
30 import org.openhab.core.types.StateOption;
31 import org.osgi.service.component.annotations.Component;
34 * Dynamically create the users list of programs and setting dates.
36 * @author Fabio Possieri - Initial contribution
38 @Component(service = { DynamicStateDescriptionProvider.class, SmartherDynamicStateDescriptionProvider.class })
40 public class SmartherDynamicStateDescriptionProvider extends BaseDynamicStateDescriptionProvider {
42 private static final String LABEL_FOREVER = "Forever";
43 private static final String LABEL_TODAY = "Today";
44 private static final String LABEL_TOMORROW = "Tomorrow";
46 public void setEndDates(ChannelUID channelUID, int maxEndDays) {
47 List<StateOption> endDates = new ArrayList<>();
49 endDates.add(new StateOption("", LABEL_FOREVER));
51 final LocalDateTime today = LocalDate.now().atStartOfDay();
53 endDates.add(new StateOption(DateUtil.format(today, DTF_DATE), LABEL_TODAY));
55 endDates.add(new StateOption(DateUtil.format(today.plusDays(1), DTF_DATE), LABEL_TOMORROW));
56 for (int i = 2; i < maxEndDays; i++) {
57 final String newDate = DateUtil.format(today.plusDays(i), DTF_DATE);
58 endDates.add(new StateOption(newDate, newDate));
62 setStateOptions(channelUID, endDates);
65 public void setPrograms(ChannelUID channelUID, @Nullable List<Program> programs) {
66 if (programs != null) {
67 setStateOptions(channelUID,
69 .map(program -> new StateOption(String.valueOf(program.getNumber()), program.getName()))
70 .collect(Collectors.toList()));