Skip to content

Commit

Permalink
refactor: move indicators from config into code
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCalzone committed Jun 13, 2024
1 parent cad6154 commit 7475f1b
Show file tree
Hide file tree
Showing 14 changed files with 224 additions and 641 deletions.
28 changes: 0 additions & 28 deletions docs/api/config-manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,6 @@ Looks up the name of the manufacturer with the given ID in the configuration DB

> [!NOTE] `loadManufacturers` must be used first.
### `loadIndicators`

```ts
loadIndicators(): Promise<void>;
```

Loads the indicators config which is used to lookup indicators and indicator properties.

### `lookupIndicator`

```ts
lookupIndicator(indicatorId: number): string | undefined;
```

Looks up the label of the indicator with the given ID in the configuration DB and returns it if it was found.

> [!NOTE] `loadIndicators` must be used first.
### `lookupProperty`

```ts
lookupProperty(propertyId: number): IndicatorProperty | undefined;
```

Looks up the property definition for a given indicator property id

> [!NOTE] `loadIndicators` must be used first.
### `loadDeviceIndex`

```ts
Expand Down
44 changes: 16 additions & 28 deletions packages/cc/src/cc/IndicatorCC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { ConfigManager } from "@zwave-js/config";
import {
CommandClasses,
type IZWaveEndpoint,
Indicator,
type MaybeNotKnown,
type MessageOrCCLogEntry,
MessagePriority,
Expand All @@ -11,6 +12,7 @@ import {
ZWaveError,
ZWaveErrorCodes,
encodeBitMask,
getIndicatorProperty,
parseBitMask,
validatePayload,
} from "@zwave-js/core/safe";
Expand Down Expand Up @@ -197,13 +199,13 @@ export const IndicatorCCValues = Object.freeze({
*/
function getIndicatorMetadata(
configManager: ConfigManager,
indicatorId: number,
indicatorId: Indicator,
propertyId: number,
overrideIndicatorLabel?: string,
): ValueMetadata {
const label = overrideIndicatorLabel
|| configManager.lookupIndicator(indicatorId);
const prop = configManager.lookupProperty(propertyId);
|| getIndicatorName(indicatorId);
const prop = getIndicatorProperty(propertyId);
const baseMetadata = IndicatorCCValues.valueV2(
indicatorId,
propertyId,
Expand Down Expand Up @@ -244,16 +246,15 @@ function getIndicatorMetadata(
}

function getIndicatorName(
configManager: ConfigManager,
indicatorId: number | undefined,
): string {
let indicatorName = "0 (default)";
if (indicatorId) {
indicatorName = `${num2hex(indicatorId)} (${
configManager.lookupIndicator(indicatorId) ?? `Unknown`
return `${num2hex(indicatorId)} (${
indicatorId in Indicator ? Indicator[indicatorId] : "Unknown"
})`;
} else {
return "0 (default)";
}
return indicatorName;
}

const MAX_INDICATOR_OBJECTS = 31;
Expand Down Expand Up @@ -808,7 +809,7 @@ export class IndicatorCC extends CommandClass {
&& typeof propertyKey === "number"
) {
// The indicator property is our property key
const prop = applHost.configManager.lookupProperty(propertyKey);
const prop = getIndicatorProperty(propertyKey);
if (prop) return prop.label;
}
return super.translatePropertyKey(applHost, property, propertyKey);
Expand All @@ -821,8 +822,7 @@ export class IndicatorCC extends CommandClass {
): string {
if (typeof property === "number" && typeof propertyKey === "number") {
// The indicator corresponds to our property
const label = applHost.configManager.lookupIndicator(property);
if (label) return label;
if (property in Indicator) return Indicator[property];
}
return super.translateProperty(applHost, property, propertyKey);
}
Expand Down Expand Up @@ -1232,10 +1232,7 @@ export class IndicatorCCGet extends IndicatorCC {
return {
...super.toLogEntry(applHost),
message: {
indicator: getIndicatorName(
applHost.configManager,
this.indicatorId,
),
indicator: getIndicatorName(this.indicatorId),
},
};
}
Expand Down Expand Up @@ -1318,23 +1315,17 @@ export class IndicatorCCSupportedReport extends IndicatorCC {
return {
...super.toLogEntry(applHost),
message: {
indicator: getIndicatorName(
applHost.configManager,
this.indicatorId,
),
indicator: getIndicatorName(this.indicatorId),
"supported properties": `${
this.supportedProperties
.map(
(id) =>
applHost.configManager.lookupProperty(id)?.label
getIndicatorProperty(id)?.label
?? `Unknown (${num2hex(id)})`,
)
.join(", ")
}`,
"next indicator": getIndicatorName(
applHost.configManager,
this.nextIndicatorId,
),
"next indicator": getIndicatorName(this.nextIndicatorId),
},
};
}
Expand Down Expand Up @@ -1384,10 +1375,7 @@ export class IndicatorCCSupportedGet extends IndicatorCC {
return {
...super.toLogEntry(applHost),
message: {
indicator: getIndicatorName(
applHost.configManager,
this.indicatorId,
),
indicator: getIndicatorName(this.indicatorId),
},
};
}
Expand Down
166 changes: 0 additions & 166 deletions packages/config/config/indicators.json

This file was deleted.

15 changes: 0 additions & 15 deletions packages/config/maintenance/lintConfigFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,6 @@ async function lintManufacturers(): Promise<void> {
// TODO: Validate that the file is semantically correct
}

async function lintIndicators(): Promise<void> {
await configManager.loadIndicators();
const properties = configManager.indicatorProperties;

if (!(properties.get(1)?.label === "Multilevel")) {
throw new Error(
`The indicator property Multilevel (0x01) is required!`,
);
}
if (!(properties.get(2)?.label === "Binary")) {
throw new Error(`The indicator property Binary (0x02) is required!`);
}
}

function getAllConditions(
config: ConditionalDeviceConfig,
): Map<string, Set<string>> {
Expand Down Expand Up @@ -1273,7 +1259,6 @@ export async function lintConfigFiles(): Promise<void> {
await lintManufacturers();
await lintDevices();
await lintNotifications();
await lintIndicators();

console.log();
console.log(green("The config files are valid!"));
Expand Down
Loading

0 comments on commit 7475f1b

Please sign in to comment.