]> git.basschouten.com Git - openhab-addons.git/blob
106f3d508afd4c0a54f49677e96aa55c34115f4f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.shelly.internal.discovery;
14
15 import static org.openhab.binding.shelly.internal.ShellyBindingConstants.*;
16 import static org.openhab.binding.shelly.internal.api.ShellyApiJsonDTO.*;
17 import static org.openhab.binding.shelly.internal.util.ShellyUtils.*;
18
19 import java.util.LinkedHashMap;
20 import java.util.Map;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.core.thing.ThingTypeUID;
24 import org.openhab.core.thing.ThingUID;
25
26 /**
27  * The {@link ShellyThingCreator} maps the device id into the thing type id
28  *
29  * @author Markus Michels - Initial contribution
30  */
31 @NonNullByDefault
32 public class ShellyThingCreator {
33     private static final Map<String, String> THING_TYPE_MAPPING = new LinkedHashMap<>();
34     static {
35         // mapping by device type id
36         THING_TYPE_MAPPING.put(SHELLYDT_1PM, THING_TYPE_SHELLY1PM_STR);
37         THING_TYPE_MAPPING.put(SHELLYDT_1L, THING_TYPE_SHELLY1L_STR);
38         THING_TYPE_MAPPING.put(SHELLYDT_1, THING_TYPE_SHELLY1_STR);
39         THING_TYPE_MAPPING.put(SHELLYDT_3EM, THING_TYPE_SHELLY3EM_STR);
40         THING_TYPE_MAPPING.put(SHELLYDT_EM, THING_TYPE_SHELLYEM_STR);
41         THING_TYPE_MAPPING.put(SHELLYDT_SHPLG_S, THING_TYPE_SHELLYPLUGS_STR);
42         THING_TYPE_MAPPING.put(SHELLYDT_SHPLG_U1, THING_TYPE_SHELLYPLUGU1_STR);
43         THING_TYPE_MAPPING.put(SHELLYDT_GAS, THING_TYPE_SHELLYGAS_STR);
44         THING_TYPE_MAPPING.put(SHELLYDT_DW, THING_TYPE_SHELLYDOORWIN_STR);
45         THING_TYPE_MAPPING.put(SHELLYDT_DW2, THING_TYPE_SHELLYDOORWIN2_STR);
46         THING_TYPE_MAPPING.put(SHELLYDT_DUO, THING_TYPE_SHELLYDUO_STR);
47         THING_TYPE_MAPPING.put(SHELLYDT_DUORGBW, THING_TYPE_SHELLYDUORGBW_STR);
48         THING_TYPE_MAPPING.put(SHELLYDT_BULB, THING_TYPE_SHELLYBULB_STR);
49         THING_TYPE_MAPPING.put(SHELLYDT_VINTAGE, THING_TYPE_SHELLYVINTAGE_STR);
50         THING_TYPE_MAPPING.put(SHELLYDT_DIMMER, THING_TYPE_SHELLYDIMMER_STR);
51         THING_TYPE_MAPPING.put(SHELLYDT_DIMMER2, THING_TYPE_SHELLYDIMMER2_STR);
52         THING_TYPE_MAPPING.put(SHELLYDT_IX3, THING_TYPE_SHELLYIX3_STR);
53         THING_TYPE_MAPPING.put(SHELLYDT_BUTTON1, THING_TYPE_SHELLYBUTTON1_STR);
54         THING_TYPE_MAPPING.put(SHELLYDT_UNI, THING_TYPE_SHELLYUNI_STR);
55         THING_TYPE_MAPPING.put(SHELLYDT_HT, THING_TYPE_SHELLYHT_STR);
56
57         // mapping by thing type
58         THING_TYPE_MAPPING.put(THING_TYPE_SHELLY1_STR, THING_TYPE_SHELLY1_STR);
59         THING_TYPE_MAPPING.put(THING_TYPE_SHELLY1PM_STR, THING_TYPE_SHELLY1PM_STR);
60         THING_TYPE_MAPPING.put(THING_TYPE_SHELLY1L_STR, THING_TYPE_SHELLY1L_STR);
61         THING_TYPE_MAPPING.put(THING_TYPE_SHELLY4PRO_STR, THING_TYPE_SHELLY4PRO_STR);
62         THING_TYPE_MAPPING.put(THING_TYPE_SHELLYDIMMER2_STR, THING_TYPE_SHELLYDIMMER2_STR);
63         THING_TYPE_MAPPING.put(THING_TYPE_SHELLYDIMMER_STR, THING_TYPE_SHELLYDIMMER_STR);
64         THING_TYPE_MAPPING.put(THING_TYPE_SHELLYIX3_STR, THING_TYPE_SHELLYIX3_STR);
65         THING_TYPE_MAPPING.put(THING_TYPE_SHELLY3EM_STR, THING_TYPE_SHELLY3EM_STR);
66         THING_TYPE_MAPPING.put(THING_TYPE_SHELLYEM_STR, THING_TYPE_SHELLYEM_STR);
67         THING_TYPE_MAPPING.put(THING_TYPE_SHELLYDUORGBW_STR, THING_TYPE_SHELLYDUORGBW_STR);
68         THING_TYPE_MAPPING.put(THING_TYPE_SHELLYDUO_STR, THING_TYPE_SHELLYDUO_STR);
69         THING_TYPE_MAPPING.put(THING_TYPE_SHELLYVINTAGE_STR, THING_TYPE_SHELLYVINTAGE_STR);
70         THING_TYPE_MAPPING.put(THING_TYPE_SHELLYBULB_STR, THING_TYPE_SHELLYBULB_STR);
71         THING_TYPE_MAPPING.put(THING_TYPE_SHELLYDUO_STR, THING_TYPE_SHELLYDUO_STR);
72         THING_TYPE_MAPPING.put(THING_TYPE_SHELLYHT_STR, THING_TYPE_SHELLYHT_STR);
73         THING_TYPE_MAPPING.put(THING_TYPE_SHELLYSMOKE_STR, THING_TYPE_SHELLYSMOKE_STR);
74         THING_TYPE_MAPPING.put(THING_TYPE_SHELLYGAS_STR, THING_TYPE_SHELLYGAS_STR);
75         THING_TYPE_MAPPING.put(THING_TYPE_SHELLYFLOOD_STR, THING_TYPE_SHELLYFLOOD_STR);
76         THING_TYPE_MAPPING.put(THING_TYPE_SHELLYDOORWIN_STR, THING_TYPE_SHELLYDOORWIN_STR);
77         THING_TYPE_MAPPING.put(THING_TYPE_SHELLYDOORWIN2_STR, THING_TYPE_SHELLYDOORWIN2_STR);
78         THING_TYPE_MAPPING.put(THING_TYPE_SHELLYSENSE_STR, THING_TYPE_SHELLYSENSE_STR);
79         THING_TYPE_MAPPING.put(THING_TYPE_SHELLYEYE_STR, THING_TYPE_SHELLYEYE_STR);
80         THING_TYPE_MAPPING.put(THING_TYPE_SHELLYBUTTON1_STR, THING_TYPE_SHELLYBUTTON1_STR);
81         THING_TYPE_MAPPING.put(THING_TYPE_SHELLYUNI_STR, THING_TYPE_SHELLYUNI_STR);
82
83         THING_TYPE_MAPPING.put(THING_TYPE_SHELLYPROTECTED_STR, THING_TYPE_SHELLYPROTECTED_STR);
84     }
85
86     public static ThingUID getThingUID(String serviceName, String deviceType, String mode, boolean unknown) {
87         String devid = substringAfterLast(serviceName, "-");
88         if (devid.isEmpty()) {
89             throw new IllegalArgumentException("serviceName has improper format: " + serviceName);
90         }
91         return new ThingUID(!unknown ? getThingTypeUID(serviceName, deviceType, mode)
92                 : getThingTypeUID(THING_TYPE_SHELLYPROTECTED_STR + "-" + devid, deviceType, mode), devid);
93     }
94
95     public static ThingTypeUID getThingTypeUID(String serviceName, String deviceType, String mode) {
96         return new ThingTypeUID(BINDING_ID, getThingType(serviceName, deviceType, mode));
97     }
98
99     public static ThingTypeUID getUnknownTTUID() {
100         return new ThingTypeUID(BINDING_ID, THING_TYPE_SHELLYPROTECTED_STR);
101     }
102
103     public static String getThingType(String hostname, String deviceType, String mode) {
104         String name = hostname.toLowerCase();
105         String type = substringBefore(name, "-").toLowerCase();
106         String devid = substringAfterLast(name, "-");
107         if (devid.isEmpty() || type.isEmpty()) {
108             throw new IllegalArgumentException("Invalid device name format: " + hostname);
109         }
110
111         // First check for special handling
112         if (name.startsWith(THING_TYPE_SHELLY25_PREFIX)) { // Shelly v2.5
113             return mode.equals(SHELLY_MODE_RELAY) ? THING_TYPE_SHELLY25_RELAY_STR : THING_TYPE_SHELLY25_ROLLER_STR;
114         }
115         if (name.startsWith(THING_TYPE_SHELLY2_PREFIX)) { // Shelly v2
116             return mode.equals(SHELLY_MODE_RELAY) ? THING_TYPE_SHELLY2_RELAY_STR : THING_TYPE_SHELLY2_ROLLER_STR;
117         }
118         if (name.startsWith(THING_TYPE_SHELLYPLUG_STR)) {
119             // shellyplug-s needs to be mapped to shellyplugs to follow the schema
120             // for the thing types: <thing type>-<mode>
121             if (name.startsWith(THING_TYPE_SHELLYPLUGS_STR) || name.contains("-s")) {
122                 return THING_TYPE_SHELLYPLUGS_STR;
123             }
124             if (name.startsWith(THING_TYPE_SHELLYPLUGU1_STR)) {
125                 return THING_TYPE_SHELLYPLUGU1_STR;
126             }
127             return THING_TYPE_SHELLYPLUG_STR;
128         }
129         if (name.startsWith(THING_TYPE_SHELLYRGBW2_PREFIX)) {
130             return mode.equals(SHELLY_MODE_COLOR) ? THING_TYPE_SHELLYRGBW2_COLOR_STR : THING_TYPE_SHELLYRGBW2_WHITE_STR;
131         }
132         if (name.startsWith(THING_TYPE_SHELLYMOTION_STR)) {
133             // depending on firmware release the Motion advertises under shellymotion-xxx or shellymotionsensor-xxxx
134             return THING_TYPE_SHELLYMOTION_STR;
135         }
136
137         // Check general mapping
138         if (!deviceType.isEmpty()) {
139             String res = THING_TYPE_MAPPING.get(deviceType);
140             if (res != null) {
141                 return res;
142             }
143         }
144         String res = THING_TYPE_MAPPING.get(type);
145         if (res != null) {
146             return res;
147         }
148         return THING_TYPE_SHELLYUNKNOWN_STR;
149     }
150 }