]> git.basschouten.com Git - openhab-addons.git/blob
ffa4ffae8ee9c087fa79f47e0b28f24f7a5c0810
[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.bticinosmarther.internal.handler;
14
15 import static org.openhab.binding.bticinosmarther.internal.SmartherBindingConstants.DTF_DATE;
16
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;
22
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;
32
33 /**
34  * Dynamically create the users list of programs and setting dates.
35  *
36  * @author Fabio Possieri - Initial contribution
37  */
38 @Component(service = { DynamicStateDescriptionProvider.class, SmartherDynamicStateDescriptionProvider.class })
39 @NonNullByDefault
40 public class SmartherDynamicStateDescriptionProvider extends BaseDynamicStateDescriptionProvider {
41
42     private static final String LABEL_FOREVER = "Forever";
43     private static final String LABEL_TODAY = "Today";
44     private static final String LABEL_TOMORROW = "Tomorrow";
45
46     public void setEndDates(ChannelUID channelUID, int maxEndDays) {
47         List<StateOption> endDates = new ArrayList<>();
48
49         endDates.add(new StateOption("", LABEL_FOREVER));
50
51         final LocalDateTime today = LocalDate.now().atStartOfDay();
52
53         endDates.add(new StateOption(DateUtil.format(today, DTF_DATE), LABEL_TODAY));
54         if (maxEndDays > 1) {
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));
59             }
60         }
61
62         setStateOptions(channelUID, endDates);
63     }
64
65     public void setPrograms(ChannelUID channelUID, @Nullable List<Program> programs) {
66         if (programs != null) {
67             setStateOptions(channelUID,
68                     programs.stream()
69                             .map(program -> new StateOption(String.valueOf(program.getNumber()), program.getName()))
70                             .collect(Collectors.toList()));
71         }
72     }
73 }