[miio] suggest correct devicenames in readmehelper (#9379)

* [miio] suggest correct devicenames in readmehelper
* add newline at file end to make git happy
* [miio] update logic for mapping in readmeComments
* Allow readme maker to fix mappings based on the current options.
* [miio] make readmeOptionMapping static

Signed-off-by: Marcel Verpaalen <marcel@verpaalen.com>
This commit is contained in:
Marcel 2020-12-27 02:04:14 +01:00 committed by GitHub
parent fc0745e302
commit 0874df702c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 2786 additions and 5 deletions

File diff suppressed because it is too large Load Diff

View File

@ -23,6 +23,7 @@ import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import org.eclipse.jdt.annotation.NonNullByDefault;
@ -30,6 +31,8 @@ import org.eclipse.jdt.annotation.Nullable;
import org.junit.jupiter.api.Disabled;
import org.openhab.binding.miio.internal.basic.MiIoBasicChannel;
import org.openhab.binding.miio.internal.basic.MiIoBasicDevice;
import org.openhab.binding.miio.internal.basic.OptionsValueListDTO;
import org.openhab.binding.miio.internal.basic.StateDescriptionDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -38,6 +41,7 @@ import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;
/**
* Support creation of the miio readme doc
@ -55,6 +59,8 @@ public class ReadmeHelper {
private static final Logger LOGGER = LoggerFactory.getLogger(ReadmeHelper.class);
private static final String BASEFILE = "./README.base.md";
private static final String OUTPUTFILE = "./README.md";
private static final String DEVICE_NAMES_FILE = "./src/main/resources/misc/device_names.json";
private static final boolean UPDATE_OPTION_MAPPING_README_COMMENTS = true;
@Disabled
public static void main(String[] args) {
@ -127,7 +133,6 @@ public class ReadmeHelper {
private StringWriter channelList() {
StringWriter sw = new StringWriter();
Arrays.asList(MiIoDevices.values()).forEach(device -> {
if (device.getThingType().equals(MiIoBindingConstants.THING_TYPE_BASIC)) {
MiIoBasicDevice dev = findDatabaseEntry(device.getModel());
@ -139,12 +144,15 @@ public class ReadmeHelper {
sw.write("|------------------|---------|-------------------------------------|------------|\n");
for (MiIoBasicChannel ch : dev.getDevice().getChannels()) {
if (UPDATE_OPTION_MAPPING_README_COMMENTS
&& ch.getReadmeComment().startsWith("Value mapping")) {
ch.setReadmeComment(readmeOptionMapping(ch, device.getModel()));
}
sw.write("| " + minLengthString(ch.getChannel(), 16) + " | " + minLengthString(ch.getType(), 7)
+ " | " + minLengthString(ch.getFriendlyName(), 35) + " | "
+ minLengthString(ch.getReadmeComment(), 10) + " |\n");
}
sw.write("\n");
} else {
LOGGER.info("Pls check: Device not found in db: {}", device);
}
@ -153,6 +161,30 @@ public class ReadmeHelper {
return sw;
}
public static String readmeOptionMapping(MiIoBasicChannel channel, String model) {
StateDescriptionDTO stateDescription = channel.getStateDescription();
if (stateDescription != null && stateDescription.getOptions() != null) {
final List<OptionsValueListDTO> options = stateDescription.getOptions();
if (options != null && options.size() > 0) {
StringBuilder mapping = new StringBuilder();
mapping.append("Value mapping [");
options.forEach((option) -> {
mapping.append(String.format("\"%s\"=\"%s\",", String.valueOf(option.value),
String.valueOf(option.label)));
});
mapping.deleteCharAt(mapping.length() - 1);
mapping.append("]");
String newComment = mapping.toString();
if (!channel.getReadmeComment().contentEquals(newComment)) {
LOGGER.info("Channel {} - {} readme comment updated to '{}'", model, channel.getChannel(),
newComment);
}
return newComment;
}
}
return channel.getReadmeComment();
}
private StringWriter itemFileExamples() {
StringWriter sw = new StringWriter();
Arrays.asList(MiIoDevices.values()).forEach(device -> {
@ -180,15 +212,39 @@ public class ReadmeHelper {
}
private void checkDatabaseEntrys() {
StringBuilder sb = new StringBuilder();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
HashMap<String, String> names = new HashMap<String, String>();
try {
JsonReader reader = new JsonReader(new FileReader(DEVICE_NAMES_FILE));
names = gson.fromJson(reader, names.getClass());
} catch (IOException e) {
LOGGER.info("Error reading name list {}: ", DEVICE_NAMES_FILE, e.getMessage());
}
for (MiIoBasicDevice entry : findDatabaseEntrys()) {
for (String id : entry.getDevice().getId()) {
if (!MiIoDevices.getType(id).getThingType().equals(MiIoBindingConstants.THING_TYPE_BASIC)) {
LOGGER.info("id : {} " + id
+ " not found. Suggested line to add to MiIoDevices.java: {}(\"{}\", \"{}\", THING_TYPE_BASIC),",
id, id.toUpperCase().replace(".", "_"), id, id);
sb.append(id.toUpperCase().replace(".", "_"));
sb.append("(\"");
sb.append(id);
sb.append("\",\"");
if (names.containsKey(id)) {
sb.append(names.get(id));
LOGGER.info("id: {} not found in MiIoDevices.java.", id);
} else {
sb.append(id);
LOGGER.info(
"id: {} not found in MiIoDevices.java and name unavilable in the device names list.",
id);
}
sb.append("\", THING_TYPE_BASIC),\r\n");
}
}
}
if (sb.length() > 0) {
LOGGER.info("Model(s) not found. Suggested lines to add to MiIoDevices.java\r\n{}", sb.toString());
}
}
@Nullable