]> git.basschouten.com Git - openhab-addons.git/blob
098da95b4dd722bda299f2f3e1f33077a7d6fd17
[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.amazonechocontrol.internal.smarthome;
14
15 import static org.openhab.binding.amazonechocontrol.internal.AmazonEchoControlBindingConstants.BINDING_ID;
16
17 import java.util.Locale;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.amazonechocontrol.internal.handler.SmartHomeDeviceHandler;
22 import org.openhab.core.thing.Channel;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.thing.ThingRegistry;
25 import org.openhab.core.thing.binding.ThingHandler;
26 import org.openhab.core.thing.type.ChannelTypeUID;
27 import org.openhab.core.thing.type.DynamicStateDescriptionProvider;
28 import org.openhab.core.types.StateDescription;
29 import org.osgi.service.component.annotations.Activate;
30 import org.osgi.service.component.annotations.Component;
31 import org.osgi.service.component.annotations.Reference;
32
33 /**
34  *
35  * Dynamic channel state description provider
36  * Overrides the state description for the colors of the smart bulbs
37  *
38  * @author Lukas Knoeller - Initial contribution
39  *
40  */
41
42 @Component(service = { DynamicStateDescriptionProvider.class, DynamicStateDescriptionSmartHome.class })
43 @NonNullByDefault
44 public class DynamicStateDescriptionSmartHome implements DynamicStateDescriptionProvider {
45     private final ThingRegistry thingRegistry;
46
47     @Activate
48     public DynamicStateDescriptionSmartHome(@Reference ThingRegistry thingRegistry) {
49         this.thingRegistry = thingRegistry;
50     }
51
52     public @Nullable SmartHomeDeviceHandler findHandler(Channel channel) {
53         Thing thing = thingRegistry.get(channel.getUID().getThingUID());
54         if (thing == null) {
55             return null;
56         }
57         ThingHandler handler = thing.getHandler();
58         if (handler instanceof SmartHomeDeviceHandler smartHomeHandler) {
59             return smartHomeHandler;
60         }
61         return null;
62     }
63
64     @Override
65     public @Nullable StateDescription getStateDescription(Channel channel,
66             @Nullable StateDescription originalStateDescription, @Nullable Locale locale) {
67         ChannelTypeUID channelTypeUID = channel.getChannelTypeUID();
68         if (channelTypeUID == null || !BINDING_ID.equals(channelTypeUID.getBindingId())) {
69             return null;
70         }
71         if (originalStateDescription == null) {
72             return null;
73         }
74         SmartHomeDeviceHandler handler = findHandler(channel);
75         if (handler != null) {
76             return handler.findStateDescription(channel, originalStateDescription, locale);
77         }
78         return null;
79     }
80 }