]> git.basschouten.com Git - openhab-addons.git/blob
07d1bee09a736b5286ee8335ea330e6e3b57c41c
[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)) {
59             return null;
60         }
61         SmartHomeDeviceHandler smartHomeHandler = (SmartHomeDeviceHandler) handler;
62         return smartHomeHandler;
63     }
64
65     @Override
66     public @Nullable StateDescription getStateDescription(Channel channel,
67             @Nullable StateDescription originalStateDescription, @Nullable Locale locale) {
68         ChannelTypeUID channelTypeUID = channel.getChannelTypeUID();
69         if (channelTypeUID == null || !BINDING_ID.equals(channelTypeUID.getBindingId())) {
70             return null;
71         }
72         if (originalStateDescription == null) {
73             return null;
74         }
75         SmartHomeDeviceHandler handler = findHandler(channel);
76         if (handler != null) {
77             return handler.findStateDescription(channel, originalStateDescription, locale);
78         }
79         return null;
80     }
81 }