]> git.basschouten.com Git - openhab-addons.git/blob
1a04911c67ea635827309e2cfa6a30e9654d3d7c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.mqtt.homeassistant.internal;
14
15 import java.io.IOException;
16 import java.util.HashMap;
17 import java.util.Map;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.mqtt.generic.tools.JsonReaderDelegate;
21
22 import com.google.gson.stream.JsonReader;
23
24 /**
25  * JsonReader which will replace specific names.
26  * Abbreviations are defined at
27  * https://github.com/home-assistant/core/blob/dev/homeassistant/components/mqtt/abbreviations.py
28  * The documentation at https://www.home-assistant.io/integrations/mqtt/#discovery-payload is often out of date
29  *
30  * @author Jochen Klein - Initial contribution
31  */
32 @NonNullByDefault
33 public class MappingJsonReader extends JsonReaderDelegate {
34
35     private static final Map<String, String> ABBREVIATIONS = new HashMap<>();
36     private static final Map<String, String> DEVICE_ABBREVIATIONS = new HashMap<>();
37
38     static {
39         ABBREVIATIONS.put("act_t", "action_topic");
40         ABBREVIATIONS.put("act_tpl", "action_template");
41         ABBREVIATIONS.put("act_stat_t", "activity_state_topic");
42         ABBREVIATIONS.put("act_val_tpl", "activity_value_template");
43         ABBREVIATIONS.put("atype", "automation_type");
44         ABBREVIATIONS.put("aux_cmd_t", "aux_command_topic");
45         ABBREVIATIONS.put("aux_stat_tpl", "aux_state_template");
46         ABBREVIATIONS.put("aux_stat_t", "aux_state_topic");
47         ABBREVIATIONS.put("av_tones", "available_tones");
48         ABBREVIATIONS.put("avty", "availability");
49         ABBREVIATIONS.put("avty_mode", "availability_mode");
50         ABBREVIATIONS.put("avty_t", "availability_topic");
51         ABBREVIATIONS.put("avty_tpl", "availability_template");
52         ABBREVIATIONS.put("b_tpl", "blue_template");
53         ABBREVIATIONS.put("bri_cmd_tpl", "brightness_command_template");
54         ABBREVIATIONS.put("bri_cmd_t", "brightness_command_topic");
55         ABBREVIATIONS.put("bri_scl", "brightness_scale");
56         ABBREVIATIONS.put("bri_stat_t", "brightness_state_topic");
57         ABBREVIATIONS.put("bri_tpl", "brightness_template");
58         ABBREVIATIONS.put("bri_val_tpl", "brightness_value_template");
59         ABBREVIATIONS.put("clr_temp_cmd_tpl", "color_temp_command_template");
60         ABBREVIATIONS.put("clrm", "color_mode");
61         ABBREVIATIONS.put("clrm_stat_t", "color_mode_state_topic");
62         ABBREVIATIONS.put("clrm_val_tpl", "color_mode_value_template");
63         ABBREVIATIONS.put("clr_temp_cmd_t", "color_temp_command_topic");
64         ABBREVIATIONS.put("clr_temp_stat_t", "color_temp_state_topic");
65         ABBREVIATIONS.put("clr_temp_tpl", "color_temp_template");
66         ABBREVIATIONS.put("clr_temp_val_tpl", "color_temp_value_template");
67         ABBREVIATIONS.put("cmd_off_tpl", "command_off_template");
68         ABBREVIATIONS.put("cmd_on_tpl", "command_on_template");
69         ABBREVIATIONS.put("cmd_t", "command_topic");
70         ABBREVIATIONS.put("cmd_tpl", "command_template");
71         ABBREVIATIONS.put("cod_arm_req", "code_arm_required");
72         ABBREVIATIONS.put("cod_dis_req", "code_disarm_required");
73         ABBREVIATIONS.put("cod_form", "code_format");
74         ABBREVIATIONS.put("cod_trig_req", "code_trigger_required");
75         ABBREVIATIONS.put("cont_type", "content_type");
76         ABBREVIATIONS.put("curr_hum_t", "current_humidity_topic");
77         ABBREVIATIONS.put("curr_hum_tpl", "current_humidity_template");
78         ABBREVIATIONS.put("curr_temp_t", "current_temperature_topic");
79         ABBREVIATIONS.put("curr_temp_tpl", "current_temperature_template");
80         ABBREVIATIONS.put("dev", "device");
81         ABBREVIATIONS.put("dev_cla", "device_class");
82         ABBREVIATIONS.put("dir_cmd_t", "direction_command_topic");
83         ABBREVIATIONS.put("dir_cmd_tpl", "direction_command_template");
84         ABBREVIATIONS.put("dir_stat_t", "direction_state_topic");
85         ABBREVIATIONS.put("dir_val_tpl", "direction_value_template");
86         ABBREVIATIONS.put("dock_cmd_t", "dock_command_topic");
87         ABBREVIATIONS.put("dock_cmd_tpl", "dock_command_template");
88         ABBREVIATIONS.put("e", "encoding");
89         ABBREVIATIONS.put("en", "enabled_by_default");
90         ABBREVIATIONS.put("ent_cat", "entity_category");
91         ABBREVIATIONS.put("ent_pic", "entity_picture");
92         ABBREVIATIONS.put("evt_typ", "event_types");
93         ABBREVIATIONS.put("fanspd_lst", "fan_speed_list");
94         ABBREVIATIONS.put("flsh_tlng", "flash_time_long");
95         ABBREVIATIONS.put("flsh_tsht", "flash_time_short");
96         ABBREVIATIONS.put("fx_cmd_tpl", "effect_command_template");
97         ABBREVIATIONS.put("fx_cmd_t", "effect_command_topic");
98         ABBREVIATIONS.put("fx_list", "effect_list");
99         ABBREVIATIONS.put("fx_stat_t", "effect_state_topic");
100         ABBREVIATIONS.put("fx_tpl", "effect_template");
101         ABBREVIATIONS.put("fx_val_tpl", "effect_value_template");
102         ABBREVIATIONS.put("exp_aft", "expire_after");
103         ABBREVIATIONS.put("fan_mode_cmd_tpl", "fan_mode_command_template");
104         ABBREVIATIONS.put("fan_mode_cmd_t", "fan_mode_command_topic");
105         ABBREVIATIONS.put("fan_mode_stat_tpl", "fan_mode_state_template");
106         ABBREVIATIONS.put("fan_mode_stat_t", "fan_mode_state_topic");
107         ABBREVIATIONS.put("frc_upd", "force_update");
108         ABBREVIATIONS.put("g_tpl", "green_template");
109         ABBREVIATIONS.put("hs_cmd_t", "hs_command_topic");
110         ABBREVIATIONS.put("hs_cmd_tpl", "hs_command_template");
111         ABBREVIATIONS.put("hs_stat_t", "hs_state_topic");
112         ABBREVIATIONS.put("hs_val_tpl", "hs_value_template");
113         ABBREVIATIONS.put("ic", "icon");
114         ABBREVIATIONS.put("img_e", "image_encoding");
115         ABBREVIATIONS.put("img_t", "image_topic");
116         ABBREVIATIONS.put("init", "initial");
117         ABBREVIATIONS.put("hum_cmd_t", "target_humidity_command_topic");
118         ABBREVIATIONS.put("hum_cmd_tpl", "target_humidity_command_template");
119         ABBREVIATIONS.put("hum_stat_t", "target_humidity_state_topic");
120         ABBREVIATIONS.put("hum_state_tpl", "target_humidity_state_template");
121         ABBREVIATIONS.put("json_attr", "json_attributes");
122         ABBREVIATIONS.put("json_attr_t", "json_attributes_topic");
123         ABBREVIATIONS.put("json_attr_tpl", "json_attributes_template");
124         ABBREVIATIONS.put("lrst_t", "last_reset_topic");
125         ABBREVIATIONS.put("lrst_val_tpl", "last_reset_value_template");
126         ABBREVIATIONS.put("max", "max");
127         ABBREVIATIONS.put("min", "min");
128         ABBREVIATIONS.put("max_hum", "max_humidity");
129         ABBREVIATIONS.put("min_hum", "min_humidity");
130         ABBREVIATIONS.put("max_mirs", "max_mireds");
131         ABBREVIATIONS.put("min_mirs", "min_mireds");
132         ABBREVIATIONS.put("max_temp", "max_temp");
133         ABBREVIATIONS.put("min_temp", "min_temp");
134         ABBREVIATIONS.put("mode", "mode");
135         ABBREVIATIONS.put("mode_cmd_tpl", "mode_command_template");
136         ABBREVIATIONS.put("mode_cmd_t", "mode_command_topic");
137         ABBREVIATIONS.put("mode_stat_t", "mode_state_topic");
138         ABBREVIATIONS.put("mode_stat_tpl", "mode_state_template");
139         ABBREVIATIONS.put("modes", "modes");
140         ABBREVIATIONS.put("name", "name");
141         ABBREVIATIONS.put("o", "origin");
142         ABBREVIATIONS.put("obj_id", "object_id");
143         ABBREVIATIONS.put("off_dly", "off_delay");
144         ABBREVIATIONS.put("on_cmd_type", "on_command_type");
145         ABBREVIATIONS.put("ops", "options");
146         ABBREVIATIONS.put("opt", "optimistic");
147         ABBREVIATIONS.put("osc_cmd_t", "oscillation_command_topic");
148         ABBREVIATIONS.put("osc_cmd_tpl", "oscillation_command_template");
149         ABBREVIATIONS.put("osc_stat_t", "oscillation_state_topic");
150         ABBREVIATIONS.put("osc_val_tpl", "oscillation_value_template");
151         ABBREVIATIONS.put("pause_cmd_t", "pause_command_topic");
152         ABBREVIATIONS.put("pause_mw_cmd_tpl", "pause_command_template");
153         ABBREVIATIONS.put("pct_cmd_t", "percentage_command_topic");
154         ABBREVIATIONS.put("pct_cmd_tpl", "percentage_command_template");
155         ABBREVIATIONS.put("pct_stat_t", "percentage_state_topic");
156         ABBREVIATIONS.put("pct_val_tpl", "percentage_value_template");
157         ABBREVIATIONS.put("pl", "payload");
158         ABBREVIATIONS.put("pl_arm_away", "payload_arm_away");
159         ABBREVIATIONS.put("pl_arm_home", "payload_arm_home");
160         ABBREVIATIONS.put("pl_arm_nite", "payload_arm_night");
161         ABBREVIATIONS.put("pl_arm_vacation", "payload_arm_vacation");
162         ABBREVIATIONS.put("pl_arm_custom_b", "payload_arm_custom_bypass");
163         ABBREVIATIONS.put("pl_avail", "payload_available");
164         ABBREVIATIONS.put("pl_cln_sp", "payload_clean_spot");
165         ABBREVIATIONS.put("pl_cls", "payload_close");
166         ABBREVIATIONS.put("pl_disarm", "payload_disarm");
167         ABBREVIATIONS.put("pl_home", "payload_home");
168         ABBREVIATIONS.put("pl_lock", "payload_lock");
169         ABBREVIATIONS.put("pl_loc", "payload_locate");
170         ABBREVIATIONS.put("pl_not_avail", "payload_not_available");
171         ABBREVIATIONS.put("pl_not_home", "payload_not_home");
172         ABBREVIATIONS.put("pl_off", "payload_off");
173         ABBREVIATIONS.put("pl_on", "payload_on");
174         ABBREVIATIONS.put("pl_open", "payload_open");
175         ABBREVIATIONS.put("pl_osc_off", "payload_oscillation_off");
176         ABBREVIATIONS.put("pl_osc_on", "payload_oscillation_on");
177         ABBREVIATIONS.put("pl_paus", "payload_pause");
178         ABBREVIATIONS.put("pl_prs", "payload_press");
179         ABBREVIATIONS.put("pl_rst", "payload_reset");
180         ABBREVIATIONS.put("pl_rst_hum", "payload_reset_humidity");
181         ABBREVIATIONS.put("pl_rst_mode", "payload_reset_mode");
182         ABBREVIATIONS.put("pl_rst_pct", "payload_reset_percentage");
183         ABBREVIATIONS.put("pl_rst_pr_mode", "payload_reset_preset_mode");
184         ABBREVIATIONS.put("pl_stop", "payload_stop");
185         ABBREVIATIONS.put("pl_strt", "payload_start");
186         ABBREVIATIONS.put("pl_ret", "payload_return_to_base");
187         ABBREVIATIONS.put("pl_toff", "payload_turn_off");
188         ABBREVIATIONS.put("pl_ton", "payload_turn_on");
189         ABBREVIATIONS.put("pl_trig", "payload_trigger");
190         ABBREVIATIONS.put("pl_unlk", "payload_unlock");
191         ABBREVIATIONS.put("pos", "reports_position");
192         ABBREVIATIONS.put("pos_clsd", "position_closed");
193         ABBREVIATIONS.put("pos_open", "position_open");
194         ABBREVIATIONS.put("pow_cmd_t", "power_command_topic");
195         ABBREVIATIONS.put("pow_cmd_tpl", "power_command_template");
196         ABBREVIATIONS.put("pow_stat_t", "power_state_topic");
197         ABBREVIATIONS.put("pow_stat_tpl", "power_state_template");
198         ABBREVIATIONS.put("pr_mode_cmd_t", "preset_mode_command_topic");
199         ABBREVIATIONS.put("pr_mode_cmd_tpl", "preset_mode_command_template");
200         ABBREVIATIONS.put("pr_mode_stat_t", "preset_mode_state_topic");
201         ABBREVIATIONS.put("pr_mode_val_tpl", "preset_mode_value_template");
202         ABBREVIATIONS.put("pr_modes", "preset_modes");
203         ABBREVIATIONS.put("ptrn", "pattern");
204         ABBREVIATIONS.put("r_tpl", "red_template");
205         ABBREVIATIONS.put("rel_s", "release_summary");
206         ABBREVIATIONS.put("rel_u", "release_url");
207         ABBREVIATIONS.put("ret", "retain");
208         ABBREVIATIONS.put("rgb_cmd_tpl", "rgb_command_template");
209         ABBREVIATIONS.put("rgb_cmd_t", "rgb_command_topic");
210         ABBREVIATIONS.put("rgb_stat_t", "rgb_state_topic");
211         ABBREVIATIONS.put("rgb_val_tpl", "rgb_value_template");
212         ABBREVIATIONS.put("rgbw_cmd_tpl", "rgbw_command_template");
213         ABBREVIATIONS.put("rgbw_cmd_t", "rgbw_command_topic");
214         ABBREVIATIONS.put("rgbw_stat_t", "rgbw_state_topic");
215         ABBREVIATIONS.put("rgbw_val_tpl", "rgbw_value_template");
216         ABBREVIATIONS.put("rgbww_cmd_tpl", "rgbww_command_template");
217         ABBREVIATIONS.put("rgbww_cmd_t", "rgbww_command_topic");
218         ABBREVIATIONS.put("rgbww_stat_t", "rgbww_state_topic");
219         ABBREVIATIONS.put("rgbww_val_tpl", "rgbww_value_template");
220         ABBREVIATIONS.put("send_cmd_t", "send_command_topic");
221         ABBREVIATIONS.put("send_if_off", "send_if_off");
222         ABBREVIATIONS.put("set_fan_spd_t", "set_fan_speed_topic");
223         ABBREVIATIONS.put("set_pos_tpl", "set_position_template");
224         ABBREVIATIONS.put("set_pos_t", "set_position_topic");
225         ABBREVIATIONS.put("pos_t", "position_topic");
226         ABBREVIATIONS.put("pos_tpl", "position_template");
227         ABBREVIATIONS.put("spd_rng_min", "speed_range_min");
228         ABBREVIATIONS.put("spd_rng_max", "speed_range_max");
229         ABBREVIATIONS.put("src_type", "source_type");
230         ABBREVIATIONS.put("stat_cla", "state_class");
231         ABBREVIATIONS.put("stat_clsd", "state_closed");
232         ABBREVIATIONS.put("stat_closing", "state_closing");
233         ABBREVIATIONS.put("stat_jam", "state_jammed");
234         ABBREVIATIONS.put("stat_off", "state_off");
235         ABBREVIATIONS.put("stat_on", "state_on");
236         ABBREVIATIONS.put("stat_open", "state_open");
237         ABBREVIATIONS.put("stat_opening", "state_opening");
238         ABBREVIATIONS.put("stat_stopped", "state_stopped");
239         ABBREVIATIONS.put("stat_locked", "state_locked");
240         ABBREVIATIONS.put("stat_locking", "state_locking");
241         ABBREVIATIONS.put("stat_unlocked", "state_unlocked");
242         ABBREVIATIONS.put("stat_unlocking", "state_unlocking");
243         ABBREVIATIONS.put("stat_t", "state_topic");
244         ABBREVIATIONS.put("stat_tpl", "state_template");
245         ABBREVIATIONS.put("stat_val_tpl", "state_value_template");
246         ABBREVIATIONS.put("step", "step");
247         ABBREVIATIONS.put("strt_mw_cmd_t", "start_mowing_command_topic");
248         ABBREVIATIONS.put("strt_mw_cmd_tpl", "start_mowing_command_template");
249         ABBREVIATIONS.put("stype", "subtype");
250         ABBREVIATIONS.put("sug_dsp_prc", "suggested_display_precision");
251         ABBREVIATIONS.put("sup_dur", "support_duration");
252         ABBREVIATIONS.put("sup_vol", "support_volume_set");
253         ABBREVIATIONS.put("sup_feat", "supported_features");
254         ABBREVIATIONS.put("sup_clrm", "supported_color_modes");
255         ABBREVIATIONS.put("swing_mode_cmd_tpl", "swing_mode_command_template");
256         ABBREVIATIONS.put("swing_mode_cmd_t", "swing_mode_command_topic");
257         ABBREVIATIONS.put("swing_mode_stat_tpl", "swing_mode_state_template");
258         ABBREVIATIONS.put("swing_mode_stat_t", "swing_mode_state_topic");
259         ABBREVIATIONS.put("temp_cmd_tpl", "temperature_command_template");
260         ABBREVIATIONS.put("temp_cmd_t", "temperature_command_topic");
261         ABBREVIATIONS.put("temp_hi_cmd_tpl", "temperature_high_command_template");
262         ABBREVIATIONS.put("temp_hi_cmd_t", "temperature_high_command_topic");
263         ABBREVIATIONS.put("temp_hi_stat_tpl", "temperature_high_state_template");
264         ABBREVIATIONS.put("temp_hi_stat_t", "temperature_high_state_topic");
265         ABBREVIATIONS.put("temp_lo_cmd_tpl", "temperature_low_command_template");
266         ABBREVIATIONS.put("temp_lo_cmd_t", "temperature_low_command_topic");
267         ABBREVIATIONS.put("temp_lo_stat_tpl", "temperature_low_state_template");
268         ABBREVIATIONS.put("temp_lo_stat_t", "temperature_low_state_topic");
269         ABBREVIATIONS.put("temp_stat_tpl", "temperature_state_template");
270         ABBREVIATIONS.put("temp_stat_t", "temperature_state_topic");
271         ABBREVIATIONS.put("temp_unit", "temperature_unit");
272         ABBREVIATIONS.put("tilt_clsd_val", "tilt_closed_value");
273         ABBREVIATIONS.put("tilt_cmd_t", "tilt_command_topic");
274         ABBREVIATIONS.put("tilt_cmd_tpl", "tilt_command_template");
275         ABBREVIATIONS.put("tilt_max", "tilt_max");
276         ABBREVIATIONS.put("tilt_min", "tilt_min");
277         ABBREVIATIONS.put("tilt_opnd_val", "tilt_opened_value");
278         ABBREVIATIONS.put("tilt_opt", "tilt_optimistic");
279         ABBREVIATIONS.put("tilt_status_t", "tilt_status_topic");
280         ABBREVIATIONS.put("tilt_status_tpl", "tilt_status_template");
281         ABBREVIATIONS.put("tit", "title");
282         ABBREVIATIONS.put("t", "topic");
283         ABBREVIATIONS.put("uniq_id", "unique_id");
284         ABBREVIATIONS.put("unit_of_meas", "unit_of_measurement");
285         ABBREVIATIONS.put("url_t", "url_topic");
286         ABBREVIATIONS.put("url_tpl", "url_template");
287         ABBREVIATIONS.put("val_tpl", "value_template");
288         ABBREVIATIONS.put("whit_cmd_t", "white_command_topic");
289         ABBREVIATIONS.put("whit_scl", "white_scale");
290         ABBREVIATIONS.put("xy_cmd_t", "xy_command_topic");
291         ABBREVIATIONS.put("xy_cmd_tpl", "xy_command_template");
292         ABBREVIATIONS.put("xy_stat_t", "xy_state_topic");
293         ABBREVIATIONS.put("xy_val_tpl", "xy_value_template");
294         ABBREVIATIONS.put("l_ver_t", "latest_version_topic");
295         ABBREVIATIONS.put("l_ver_tpl", "latest_version_template");
296         ABBREVIATIONS.put("pl_inst", "payload_install");
297
298         DEVICE_ABBREVIATIONS.put("cu", "configuration_url");
299         DEVICE_ABBREVIATIONS.put("cns", "connections");
300         DEVICE_ABBREVIATIONS.put("hw", "hw_version");
301         DEVICE_ABBREVIATIONS.put("ids", "identifiers");
302         DEVICE_ABBREVIATIONS.put("mf", "manufacturer");
303         DEVICE_ABBREVIATIONS.put("mdl", "model");
304         DEVICE_ABBREVIATIONS.put("sa", "suggested_area");
305         DEVICE_ABBREVIATIONS.put("sw", "sw_version");
306     }
307
308     private final Map<String, String> mapping;
309
310     /**
311      *
312      * @param delegate
313      * @return return a JsonReader which replaces all config abbreviations
314      */
315     public static MappingJsonReader getConfigMapper(JsonReader delegate) {
316         return new MappingJsonReader(JsonReaderDelegate.getDelegate(delegate), ABBREVIATIONS);
317     }
318
319     /**
320      *
321      * @param delegate
322      * @return return a JsonReader which replaces all config.device abbreviations
323      */
324     public static MappingJsonReader getDeviceMapper(JsonReader delegate) {
325         return new MappingJsonReader(JsonReaderDelegate.getDelegate(delegate), DEVICE_ABBREVIATIONS);
326     }
327
328     private MappingJsonReader(JsonReader delegate, Map<String, String> mapping) {
329         super(delegate);
330         this.mapping = mapping;
331     }
332
333     @Override
334     public String nextName() throws IOException {
335         String name = super.nextName();
336         return mapping.getOrDefault(name, name);
337     }
338 }