]> git.basschouten.com Git - openhab-addons.git/blob
a7a1190dcea52c9a1bcd8c86d059f60fca88d77e
[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.ecovacs.internal;
14
15 import static org.openhab.binding.ecovacs.internal.EcovacsBindingConstants.*;
16
17 import java.util.List;
18 import java.util.Locale;
19 import java.util.stream.Collectors;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.core.events.EventPublisher;
24 import org.openhab.core.i18n.TranslationProvider;
25 import org.openhab.core.thing.Channel;
26 import org.openhab.core.thing.binding.BaseDynamicStateDescriptionProvider;
27 import org.openhab.core.thing.i18n.ChannelTypeI18nLocalizationService;
28 import org.openhab.core.thing.link.ItemChannelLinkRegistry;
29 import org.openhab.core.thing.type.ChannelTypeUID;
30 import org.openhab.core.thing.type.DynamicStateDescriptionProvider;
31 import org.openhab.core.types.StateOption;
32 import org.osgi.framework.Bundle;
33 import org.osgi.service.component.annotations.Activate;
34 import org.osgi.service.component.annotations.Component;
35 import org.osgi.service.component.annotations.Reference;
36
37 /**
38  * @author Danny Baumann - Initial contribution
39  */
40 @NonNullByDefault
41 @Component(service = { DynamicStateDescriptionProvider.class, EcovacsDynamicStateDescriptionProvider.class })
42 public class EcovacsDynamicStateDescriptionProvider extends BaseDynamicStateDescriptionProvider {
43     private final TranslationProvider i18nProvider;
44
45     @Activate
46     public EcovacsDynamicStateDescriptionProvider(final @Reference EventPublisher eventPublisher,
47             final @Reference TranslationProvider i18nProvider,
48             final @Reference ItemChannelLinkRegistry itemChannelLinkRegistry,
49             final @Reference ChannelTypeI18nLocalizationService channelTypeI18nLocalizationService) {
50         this.eventPublisher = eventPublisher;
51         this.i18nProvider = i18nProvider;
52         this.itemChannelLinkRegistry = itemChannelLinkRegistry;
53         this.channelTypeI18nLocalizationService = channelTypeI18nLocalizationService;
54     }
55
56     @Override
57     protected List<StateOption> localizedStateOptions(List<StateOption> options, Channel channel,
58             @Nullable Locale locale) {
59         @Nullable
60         ChannelTypeUID channelTypeUID = channel.getChannelTypeUID();
61         String channelTypeId = channelTypeUID != null ? channelTypeUID.getId() : "";
62         if (CHANNEL_TYPE_ID_CLEAN_MODE.equals(channelTypeId) || CHANNEL_TYPE_ID_LAST_CLEAN_MODE.equals(channelTypeId)) {
63             final Bundle bundle = bundleContext.getBundle();
64             return options.stream().map(opt -> {
65                 String key = "ecovacs.cleaning-mode." + opt.getValue();
66                 String label = this.i18nProvider.getText(bundle, key, opt.getLabel(), locale);
67                 return new StateOption(opt.getValue(), label);
68             }).collect(Collectors.toList());
69         }
70         return super.localizedStateOptions(options, channel, locale);
71     }
72 }