]> git.basschouten.com Git - openhab-addons.git/blob
a03a5d6809cfc9829744a2b965c95869994c3f69
[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
14 package org.openhab.binding.mqtt.espmilighthub.internal;
15
16 import static org.openhab.binding.mqtt.espmilighthub.internal.EspMilightHubBindingConstants.*;
17
18 import java.math.BigDecimal;
19 import java.util.Locale;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.core.thing.Channel;
24 import org.openhab.core.thing.ThingRegistry;
25 import org.openhab.core.thing.type.DynamicStateDescriptionProvider;
26 import org.openhab.core.types.StateDescription;
27 import org.openhab.core.types.StateDescriptionFragmentBuilder;
28 import org.osgi.service.component.annotations.Activate;
29 import org.osgi.service.component.annotations.Component;
30 import org.osgi.service.component.annotations.Reference;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Provides custom state descriptions for system.color-temperature-absolute channels.
35  *
36  * @author Cody Cutrer - Initial contribution
37  */
38 @Component(service = DynamicStateDescriptionProvider.class)
39 @NonNullByDefault
40 public class EspMilightStateDescriptionProvider implements DynamicStateDescriptionProvider {
41     private final ThingRegistry thingRegistry;
42
43     @Activate
44     public EspMilightStateDescriptionProvider(@Reference ThingRegistry thingRegistry) {
45         this.thingRegistry = thingRegistry;
46     }
47
48     @Override
49     public @Nullable StateDescription getStateDescription(Channel channel, @Nullable StateDescription original,
50             @Nullable Locale locale) {
51         var logger = LoggerFactory.getLogger(EspMilightStateDescriptionProvider.class);
52         var channelUID = channel.getUID();
53         var thing = thingRegistry.get(channelUID.getThingUID());
54         if (thing == null || !SUPPORTED_THING_TYPES.contains(thing.getThingTypeUID())
55                 || !channelUID.getId().equals(CHANNEL_COLOURTEMP_ABS)) {
56             return null;
57         }
58
59         StateDescriptionFragmentBuilder builder;
60         if (original != null) {
61             builder = StateDescriptionFragmentBuilder.create(original);
62         } else {
63             builder = StateDescriptionFragmentBuilder.create();
64         }
65         builder.withMinimum(BIG_DECIMAL_153).withMaximum(BIG_DECIMAL_370).withStep(BigDecimal.ONE)
66                 .withPattern("%d mired");
67         return builder.build().toStateDescription();
68     }
69 }