Format the codebase using prettier

This commit is contained in:
Adrian Jagielak
2025-07-24 16:34:58 +02:00
parent 7b4309a596
commit 3c56a30d01
69 changed files with 984 additions and 895 deletions

View File

@@ -1,6 +1,6 @@
import { v4 as uuidv4 } from "uuid";
import { log } from "../logger";
import { IMqttClient } from "../mqtt/interface";
import { v4 as uuidv4 } from 'uuid';
import { log } from '../logger';
import { IMqttClient } from '../mqtt/interface';
let fimp: IMqttClient | undefined = undefined;
@@ -21,7 +21,21 @@ export type FimpResponse = {
ver?: any;
};
type FimpValueType = 'string' | 'int' | 'float' | 'bool' | 'null' | 'str_array' | 'int_array' | 'float_array' | 'str_map' | 'int_map' | 'float_map' | 'bool_map' | 'object' | 'bin';
type FimpValueType =
| 'string'
| 'int'
| 'float'
| 'bool'
| 'null'
| 'str_array'
| 'int_array'
| 'float_array'
| 'str_map'
| 'int_map'
| 'float_map'
| 'bool_map'
| 'object'
| 'bin';
export async function sendFimpMsg({
address,
@@ -40,27 +54,27 @@ export async function sendFimpMsg({
}): Promise<FimpResponse> {
const uid = uuidv4();
const topic = `pt:j1/mt:cmd${address}`;
const message = JSON.stringify(
{
corid: null,
ctime: new Date().toISOString(),
props: {},
resp_to: 'pt:j1/mt:rsp/rt:app/rn:ha-futurehome/ad:addon',
serv: service,
src: 'ha-futurehome',
tags: [],
'type': cmd,
uid: uid,
val: val,
val_t: val_t,
ver: '1',
},
);
const message = JSON.stringify({
corid: null,
ctime: new Date().toISOString(),
props: {},
resp_to: 'pt:j1/mt:rsp/rt:app/rn:ha-futurehome/ad:addon',
serv: service,
src: 'ha-futurehome',
tags: [],
type: cmd,
uid: uid,
val: val,
val_t: val_t,
ver: '1',
});
return new Promise((resolve, reject) => {
const timeout = setTimeout(() => {
fimp?.removeListener('message', onResponse);
const error = new Error(`Timeout waiting for FIMP response (uid: ${uid}, service: ${service}, cmd: ${cmd})`);
const error = new Error(
`Timeout waiting for FIMP response (uid: ${uid}, service: ${service}, cmd: ${cmd})`,
);
log.warn(error.message, error.stack);
reject(error);
}, timeoutMs);
@@ -72,13 +86,17 @@ export async function sendFimpMsg({
if (msg.type === 'evt.error.report') {
fimp?.removeListener('message', onResponse);
const error = new Error(`Received FIMP response for message ${uid}: error (evt.error.report) (matched using uid)`);
const error = new Error(
`Received FIMP response for message ${uid}: error (evt.error.report) (matched using uid)`,
);
log.warn(error.message, error.stack);
reject(error);
return;
}
log.debug(`Received FIMP response for message ${uid} (matched using uid).`);
log.debug(
`Received FIMP response for message ${uid} (matched using uid).`,
);
clearTimeout(timeout);
fimp?.removeListener('message', onResponse);
@@ -90,13 +108,17 @@ export async function sendFimpMsg({
if (msg.type === 'evt.error.report') {
fimp?.removeListener('message', onResponse);
const error = new Error(`Received FIMP response for message ${uid}: error (evt.error.report) (matched using topic)`);
const error = new Error(
`Received FIMP response for message ${uid}: error (evt.error.report) (matched using topic)`,
);
log.warn(error.message, error.stack);
reject(error);
return;
}
log.debug(`Received FIMP response for message ${uid} (matched using topic).`);
log.debug(
`Received FIMP response for message ${uid} (matched using topic).`,
);
clearTimeout(timeout);
fimp?.removeListener('message', onResponse);
@@ -107,13 +129,23 @@ export async function sendFimpMsg({
const hasValidType = msg.type != null && msg.type.startsWith('evt.');
const reqCmdParts = cmd.split('.');
const resCmdParts = msg.type?.split('.') ?? [];
const hasThreeParts = resCmdParts.length === 3 && reqCmdParts.length === 3;
const hasThreeParts =
resCmdParts.length === 3 && reqCmdParts.length === 3;
const middlePartMatches = resCmdParts[1] === reqCmdParts[1];
const endsWithLastPart = cmd.endsWith(resCmdParts.at(-1)!);
const reqEndsWithSetAndResEndsWithReport = reqCmdParts[2] === 'set' && resCmdParts[2] === 'report'
const reqEndsWithSetAndResEndsWithReport =
reqCmdParts[2] === 'set' && resCmdParts[2] === 'report';
const sameService = msg.serv === service;
if (hasValidType && hasThreeParts && middlePartMatches && (endsWithLastPart || reqEndsWithSetAndResEndsWithReport) && sameService) {
log.debug(`Received FIMP response for message ${uid} (matched using event name).`);
if (
hasValidType &&
hasThreeParts &&
middlePartMatches &&
(endsWithLastPart || reqEndsWithSetAndResEndsWithReport) &&
sameService
) {
log.debug(
`Received FIMP response for message ${uid} (matched using event name).`,
);
clearTimeout(timeout);
fimp?.removeListener('message', onResponse);
@@ -136,4 +168,4 @@ val_t: "${val_t}"
fimp?.publish(topic, message, { qos: 1 });
});
}
}

View File

@@ -1,10 +1,12 @@
/// Returns the adapter address of the device associated with the given service address.
/// The service address may belong to any service on the device.
export function adapterAddressFromServiceAddress(serviceAddress: string): string {
export function adapterAddressFromServiceAddress(
serviceAddress: string,
): string {
const parts = serviceAddress.split('/');
if (parts.length < 4) {
throw new Error("Invalid address format");
throw new Error('Invalid address format');
}
const adapterPart = parts[2]; // e.g., "rn:zigbee"
@@ -16,11 +18,13 @@ export function adapterAddressFromServiceAddress(serviceAddress: string): string
/// Returns the adapter service name of the device associated with the given service address.
/// The service address may belong to any service on the device.
export function adapterServiceFromServiceAddress(serviceAddress: string): string {
export function adapterServiceFromServiceAddress(
serviceAddress: string,
): string {
const parts = serviceAddress.split('/');
if (parts.length < 3) {
throw new Error("Invalid address format");
throw new Error('Invalid address format');
}
const adapterPart = parts[2]; // e.g., "rn:zigbee"
@@ -31,5 +35,4 @@ export function adapterServiceFromServiceAddress(serviceAddress: string): string
}
return adapterName;
}

View File

@@ -1,27 +1,31 @@
import { log } from "../logger";
import { sendFimpMsg } from "./fimp";
import { log } from '../logger';
import { sendFimpMsg } from './fimp';
export type InclusionReport = {
address?: string | null; // Household device ID
product_name?: string | null; // e.g. "SWITCH-ZR03-1"
product_hash?: string | null; // e.g. "zb - eWeLink - SWITCH-ZR03-1"
product_id?: string | null; // e.g. "SWITCH-ZR03-1"
manufacturer_id?: string | null; // e.g. "eWeLink"
device_id?: string | null; // e.g. "b4:0e:cf:d1:bc:2a:00:00"
hw_ver?: string | null; // e.g. "0"
sw_ver?: string | null; // e.g. "0x0"
comm_tech?: string | null; // e.g. "zigbee"
power_source?: string | null; // e.g. "battery"
product_hash?: string | null; // e.g. "zb - eWeLink - SWITCH-ZR03-1"
product_id?: string | null; // e.g. "SWITCH-ZR03-1"
manufacturer_id?: string | null; // e.g. "eWeLink"
device_id?: string | null; // e.g. "b4:0e:cf:d1:bc:2a:00:00"
hw_ver?: string | null; // e.g. "0"
sw_ver?: string | null; // e.g. "0x0"
comm_tech?: string | null; // e.g. "zigbee"
power_source?: string | null; // e.g. "battery"
services?: InclusionReportService[] | null;
};
export type InclusionReportService = {
name?: string | null;
address?: string | null;
props?: { [key: string]: any } | null
props?: { [key: string]: any } | null;
};
export async function getInclusionReport(parameters: { adapterAddress: string; adapterService: string; deviceId: string }): Promise<InclusionReport | undefined> {
export async function getInclusionReport(parameters: {
adapterAddress: string;
adapterService: string;
deviceId: string;
}): Promise<InclusionReport | undefined> {
try {
const inclusionReport = await sendFimpMsg({
address: parameters.adapterAddress,
@@ -33,6 +37,9 @@ export async function getInclusionReport(parameters: { adapterAddress: string; a
return inclusionReport.val;
} catch (e) {
log.error(`Failed getting inclusion report for adapterAddress: ${parameters.adapterAddress}, adapterService: ${parameters.adapterService}, deviceId: ${parameters.deviceId}`, e)
log.error(
`Failed getting inclusion report for adapterAddress: ${parameters.adapterAddress}, adapterService: ${parameters.adapterService}, deviceId: ${parameters.deviceId}`,
e,
);
}
}

View File

@@ -1,20 +1,34 @@
export type DeviceState = {
id?: number | null;
services?: DeviceStateService[] | null
services?: DeviceStateService[] | null;
};
export type DeviceStateService = {
addr?: string;
attributes?: Attribute[];
name?: string;
}
};
export type Attribute = {
name: string;
values: AttributeValue[];
}
};
export type AttributeValue = StringValue | IntValue | FloatValue | BoolValue | NullValue | StrArrayValue | IntArrayValue | FloatArrayValue | StrMapValue | IntMapValue | FloatMapValue | BoolMapValue | ObjectValue | BinValue;
export type AttributeValue =
| StringValue
| IntValue
| FloatValue
| BoolValue
| NullValue
| StrArrayValue
| IntArrayValue
| FloatArrayValue
| StrMapValue
| IntMapValue
| FloatMapValue
| BoolMapValue
| ObjectValue
| BinValue;
export type Timestamp = string;
@@ -22,49 +36,49 @@ export type StringValue = {
ts: Timestamp;
val: string;
val_t: 'string';
}
};
export type IntValue = {
ts: Timestamp;
val: number;
val_t: 'int';
}
};
export type FloatValue = {
ts: Timestamp;
val: number;
val_t: 'float';
}
};
export type BoolValue = {
ts: Timestamp;
val: boolean;
val_t: 'bool';
}
};
export type NullValue = {
ts: Timestamp;
val?: null;
val_t: 'null';
}
};
export type StrArrayValue = {
ts: Timestamp;
val: string[];
val_t: 'str_array';
}
};
export type IntArrayValue = {
ts: Timestamp;
val: number[];
val_t: 'int_array';
}
};
export type FloatArrayValue = {
ts: Timestamp;
val: number[];
val_t: 'float_array';
}
};
export type StrMapValue = {
ts: Timestamp;
@@ -72,7 +86,7 @@ export type StrMapValue = {
[key: string]: string;
};
val_t: 'str_map';
}
};
export type IntMapValue = {
ts: Timestamp;
@@ -80,7 +94,7 @@ export type IntMapValue = {
[key: string]: number;
};
val_t: 'int_map';
}
};
export type FloatMapValue = {
ts: Timestamp;
@@ -88,7 +102,7 @@ export type FloatMapValue = {
[key: string]: number;
};
val_t: 'float_map';
}
};
export type BoolMapValue = {
ts: Timestamp;
@@ -96,7 +110,7 @@ export type BoolMapValue = {
[key: string]: boolean;
};
val_t: 'bool_map';
}
};
export type ObjectValue = {
ts: Timestamp;
@@ -104,10 +118,10 @@ export type ObjectValue = {
[key: string]: any;
};
val_t: 'object';
}
};
export type BinValue = {
ts: Timestamp;
val: string;
val_t: 'bin';
}
};

View File

@@ -8,7 +8,20 @@ export type VinculumPd7Device = {
model?: string | null;
// "Model alias", e.g. "TS0202"
modelAlias?: string | null;
functionality?: 'appliance' | 'climate' | 'energy' | 'ev_charger' | 'lighting' | 'media' | 'other' | 'power' | 'safety' | 'security' | 'shading' | string | null;
functionality?:
| 'appliance'
| 'climate'
| 'energy'
| 'ev_charger'
| 'lighting'
| 'media'
| 'other'
| 'power'
| 'safety'
| 'security'
| 'shading'
| string
| null;
services?: Record<string, VinculumPd7Service> | null;
type?: {
// User-defined device type (e.g. "sensor", "chargepoint", or "light")