Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: meaning of limit_options in OpenSmartHouse import script #6313

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 62 additions & 60 deletions packages/config/maintenance/importConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ import {
} from "@zwave-js/shared";
import { composeObject } from "alcalzone-shared/objects";
import { isArray, isObject } from "alcalzone-shared/typeguards";
import { AssertionError, ok } from "assert";
import * as child from "child_process";
import * as JSONC from "comment-json";
import * as fs from "fs-extra";
import * as JSON5 from "json5";
import * as path from "path";
import { AssertionError, ok } from "node:assert";
import * as child from "node:child_process";
import * as path from "node:path";
import { promisify } from "node:util";
import { compare } from "semver";
import { promisify } from "util";
import xml2js from "xml2js";
import xml2js_parsers from "xml2js/lib/processors.js";
import yargs from "yargs";
import { ConfigManager, DeviceConfigIndexEntry } from "../src";
import { ConfigManager, type DeviceConfigIndexEntry } from "../src";

const execPromise = promisify(child.exec);

Expand Down Expand Up @@ -1163,13 +1163,13 @@ function sanitizeFields(json: Record<string, any>[]) {
for (const param of file.ConfigurationParameters) {
param.Name = param.Name ? sanitizeString(param.Name) : "";
param.Name = param.Name
? param.Name.replace(/\.\"/g, "\"")
? param.Name.replaceAll(".\"", "\"")
: "";
param.Name = param.Name
? param.Name.replace(/[\,\.\:]$/, "\"")
: "";
param.Name = param.Name
? param.Name.replace(/\:\"/g, "\"")
? param.Name.replaceAll(":\"", "\"")
: "";
param.Description = param.Description
? sanitizeString(param.Description)
Expand Down Expand Up @@ -1320,7 +1320,7 @@ async function parseZWAProduct(
const website_root =
"https://products.z-wavealliance.org/ProductManual/File?folder=&filename=";
if (manual) {
manual = manual.replace(/ /g, "%20");
manual = manual.replaceAll(" ", "%20");
manual = website_root.concat(manual);
}

Expand Down Expand Up @@ -1573,9 +1573,9 @@ async function parseZWAProduct(

// Insert a TODO comment if necessary
if (
newConfig.devices.filter(
(d) => d.productType === "0x9999" || d.productId === "0x9999",
).length > 0
newConfig.devices.some(
(d) => d.productType === "0x9999" || d.productId === "0x9999",
)
|| newConfig.manufacturerIdHex === "0x9999"
) {
output =
Expand Down Expand Up @@ -1655,7 +1655,7 @@ async function maintenanceParse(): Promise<void> {
"https://products.z-wavealliance.org/ProductManual/File?folder=&filename=";

if (manual) {
manual = manual.replace(/ /g, "%20");
manual = manual.replaceAll(" ", "%20");
manual = website_root.concat(manual);

if (jsonData.metadata) {
Expand Down Expand Up @@ -1703,7 +1703,7 @@ async function retrieveZWADeviceIds(
for (const i of firstPage.match(/(?<=productId=).*?(?=[\&\"])/g)!) {
deviceIdsSet.add(i);
}
const pageNumbers = firstPage.match(/(?<=page=\d+">).*?(?=\<)/g)
const pageNumbers = /(?<=page=\d+">).*?(?=\<)/g.test(firstPage)
? firstPage.match(/(?<=page=\d+">).*?(?=\<)/g)!
: [1];
const lastPage = Math.max(...pageNumbers);
Expand Down Expand Up @@ -1815,8 +1815,8 @@ async function downloadManufacturersOH(): Promise<void> {
data.manufacturers.data.map(({ id, label }) => [
label
.replace("</a>", "")
.replace(/&quot;/g, `"`)
.replace(/&amp;/g, "&")
.replaceAll("&quot;", `"`)
.replaceAll("&amp;", "&")
.trim(),
formatId(id),
]),
Expand Down Expand Up @@ -1848,7 +1848,7 @@ function assertValid(json: any) {

/** Removes unnecessary whitespace from imported text */
function sanitizeText(text: string): string | undefined {
return text ? text.trim().replace(/[\t\r\n]+/g, " ") : undefined;
return text ? text.trim().replaceAll(/[\t\r\n]+/g, " ") : undefined;
}

/** Tries to coerce the input value into an integer */
Expand All @@ -1860,7 +1860,7 @@ function sanitizeNumber(

let ret = Number(value);
if (Number.isNaN(ret)) {
value = value.replace(/[^0-9-\.\,]/g, "");
value = value.replaceAll(/[^0-9-\.\,]/g, "");
ret = Number(value);
}
return ret;
Expand All @@ -1870,7 +1870,7 @@ function sanitizeNumber(
function labelToFilename(label: string): string {
return label
.trim()
.replace(/[^a-zA-Z0-9\-_]+/g, "_")
.replaceAll(/[^a-zA-Z0-9\-_]+/g, "_")
.replace(/^_/, "")
.replace(/_$/, "")
.toLowerCase();
Expand Down Expand Up @@ -1900,7 +1900,7 @@ async function parseOHConfigFile(
return { productType, productId };
}),
firmwareVersion: {
min: json.version_min.replace(/000/g, "0"),
min: json.version_min.replaceAll("000", "0"),
max: json.version_max,
},
};
Expand Down Expand Up @@ -1962,7 +1962,9 @@ async function parseOHConfigFile(
defaultValue: parseInt(param.default, 10),
readOnly: param.read_only === "1" ? true : undefined,
writeOnly: param.write_only === "1" ? true : undefined,
allowManualEntry: param.limit_options === "1",
allowManualEntry: param.limit_options === "1"
? false
: undefined,
};
if (param.options?.length) {
paramInfo.options = param.options.map((opt: any) => ({
Expand Down Expand Up @@ -2106,38 +2108,38 @@ function normalizeIdentifier(originalIdentifier: string) {
****************************************************************************/
function normalizeLabel(originalString: string) {
originalString = sanitizeString(originalString);
originalString = originalString.replace(/\n/g, " ");
originalString = originalString.replace(/\\"/g, "");
originalString = originalString.replaceAll("\n", " ");
originalString = originalString.replaceAll("\\\"", "");
let splitStr = originalString.toLowerCase().split(" ");
for (let i = 0; i < splitStr.length; i++) {
splitStr[i] = splitStr[i].charAt(0).toUpperCase()
+ splitStr[i].substring(1);
+ splitStr[i].slice(1);
}
originalString = splitStr.join(" ");

splitStr = originalString.split("/");
for (let i = 0; i < splitStr.length; i++) {
splitStr[i] = splitStr[i].charAt(0).toUpperCase()
+ splitStr[i].substring(1);
+ splitStr[i].slice(1);
}
originalString = splitStr.join("/");

originalString = originalString.replace(/ Led /g, " LED ");
originalString = originalString.replace(/ Rgb /g, " RGB ");
originalString = originalString.replace(/ Pir /g, " PIR ");
originalString = originalString.replace(/Z-wave/g, "Z-Wave");
originalString = originalString.replace(/basic set/g, "Basic Set");
originalString = originalString.replace(
/multi-level switch/g,
originalString = originalString.replaceAll(" Led ", " LED ");
originalString = originalString.replaceAll(" Rgb ", " RGB ");
originalString = originalString.replaceAll(" Pir ", " PIR ");
originalString = originalString.replaceAll("Z-wave", "Z-Wave");
originalString = originalString.replaceAll("basic set", "Basic Set");
originalString = originalString.replaceAll(
"multi-level switch",
"Multi-Level Switch",
);
originalString = originalString.replace(/Multi-level/g, "Multi-Level");
originalString = originalString.replace(/ Of /g, " of ");
originalString = originalString.replace(/ To /g, " to ");
originalString = originalString.replace(/ A /g, " a ");
originalString = originalString.replace(/ An /g, " an ");
originalString = originalString.replace(/ Is /g, " is ");
originalString = originalString.replace(/ In /g, " in ");
originalString = originalString.replaceAll("Multi-level", "Multi-Level");
originalString = originalString.replaceAll(" Of ", " of ");
originalString = originalString.replaceAll(" To ", " to ");
originalString = originalString.replaceAll(" A ", " a ");
originalString = originalString.replaceAll(" An ", " an ");
originalString = originalString.replaceAll(" Is ", " is ");
originalString = originalString.replaceAll(" In ", " in ");

const prohibitedEndChars = ["-", ".", "_", ",", " "];
// Clean-up the end of labels
Expand Down Expand Up @@ -2169,21 +2171,21 @@ function normalizeLabel(originalString: string) {
function normalizeDescription(originalString: string) {
originalString = sanitizeString(originalString)
.toLocaleLowerCase()
.replace(/\n/g, " ")
.replace(/\\"/g, "")
.replace(/ led /g, " LED ")
.replace(/ rgb /g, " RGB ")
.replace(/ pir /g, " PIR ")
.replace(/basic set/g, "Basic Set")
.replace(/multi-level/g, "Multi-Level")
.replace(/z-?wave/g, "Z-Wave");
.replaceAll("\n", " ")
.replaceAll("\\\"", "")
.replaceAll(" led ", " LED ")
.replaceAll(" rgb ", " RGB ")
.replaceAll(" pir ", " PIR ")
.replaceAll("basic set", "Basic Set")
.replaceAll("multi-level", "Multi-Level")
.replaceAll(/z-?wave/g, "Z-Wave");
originalString = originalString.charAt(0).toUpperCase()
+ originalString.slice(1);
originalString = originalString.replace(
/multi-level switch/g,
originalString = originalString.replaceAll(
"multi-level switch",
"Multi-Level Switch",
);
originalString = originalString.replace(/multi-level/g, "Multi-Level");
originalString = originalString.replaceAll("multi-level", "Multi-Level");

// Clean-up the end of labels
originalString = originalString.replace(/[._, -]+$/, "");
Expand All @@ -2199,16 +2201,16 @@ function normalizeDescription(originalString: string) {
****************************************************************************/
function sanitizeString(originalString: string) {
return originalString
.replace(/\r\n/g, "\n")
.replace(/\r/g, "\n")
.replace(/\n\n\n\n/g, "\n\n")
.replace(/\t/g, " ")
.replace(/\"\"/g, "\"")
.replace(/ {2,}/g, " ")
.replace(/\,s*$/g, "")
.replace(/\„s*$/g, "")
.replace(/\.s*$/g, "")
.replace(/\:s*$/g, "")
.replaceAll("\r\n", "\n")
.replaceAll("\r", "\n")
.replaceAll("\n\n\n\n", "\n\n")
.replaceAll("\t", " ")
.replaceAll("\"\"", "\"")
.replaceAll(/ {2,}/g, " ")
.replaceAll(/\,s*$/g, "")
.replaceAll(/\„s*$/g, "")
.replaceAll(/\.s*$/g, "")
.replaceAll(/\:s*$/g, "")
.trim();
}
/****************************************************************************
Expand Down Expand Up @@ -2287,7 +2289,7 @@ function getLatestConfigVersion(
return compare(vA, vB);
});

return configs[configs.length - 1];
return configs.at(-1);
}

/** Changes the manufacturer names in all device config files to match manufacturers.json */
Expand Down