chore(readme.md eslintrc.js pre-commit prepare-commit-msg): update README.md add eslint commit rules
This commit is contained in:
@@ -15,14 +15,19 @@ export class HAMiddleware {
|
||||
this.hassClient.rawClient.ws.close();
|
||||
}
|
||||
|
||||
async callAService(domain: string, service: string, extraArgs?: unknown) {
|
||||
async callAService(
|
||||
domain: string,
|
||||
service: string,
|
||||
extraArgs?: unknown,
|
||||
) {
|
||||
await this.hassClient.callService(domain, service, extraArgs);
|
||||
}
|
||||
|
||||
subscribe() {
|
||||
this.hassClient.on('state_changed', (event) => {
|
||||
this.logger.debug(JSON.stringify(event));
|
||||
const toDo = this.functionsToCallOnChange[event.data.entity_id];
|
||||
const toDo =
|
||||
this.functionsToCallOnChange[event.data.entity_id];
|
||||
if (toDo) {
|
||||
toDo(event);
|
||||
}
|
||||
@@ -31,7 +36,7 @@ export class HAMiddleware {
|
||||
|
||||
subscribeToDevice(
|
||||
deviceId: string,
|
||||
fn: (event: StateChangedEvent) => void
|
||||
fn: (event: StateChangedEvent) => void,
|
||||
) {
|
||||
this.functionsToCallOnChange[deviceId] = fn;
|
||||
this.logger.debug(this.functionsToCallOnChange);
|
||||
@@ -39,19 +44,20 @@ export class HAMiddleware {
|
||||
|
||||
async getStates(): Promise<{ [k: string]: HassEntity }> {
|
||||
const states = await this.hassClient.getStates();
|
||||
const sorted = states.reduceRight<{ [k: string]: HassEntity }>(
|
||||
(last, current) => {
|
||||
last[current['entity_id']] = current;
|
||||
return last;
|
||||
},
|
||||
{}
|
||||
);
|
||||
const sorted = states.reduceRight<{
|
||||
[k: string]: HassEntity;
|
||||
}>((last, current) => {
|
||||
last[current['entity_id']] = current;
|
||||
return last;
|
||||
}, {});
|
||||
this.logger.debug(JSON.stringify({ getStates: sorted }));
|
||||
this.entities = sorted;
|
||||
return this.entities;
|
||||
}
|
||||
|
||||
async getStatesPartitionedByType(): Promise<{ [k: string]: HassEntity[] }> {
|
||||
async getStatesPartitionedByType(): Promise<{
|
||||
[k: string]: HassEntity[];
|
||||
}> {
|
||||
const states = await this.getStates();
|
||||
const toReturn = Object.keys(states).reduceRight<{
|
||||
[k: string]: HassEntity[];
|
||||
@@ -64,7 +70,7 @@ export class HAMiddleware {
|
||||
return prev;
|
||||
}, {});
|
||||
this.logger.debug(
|
||||
JSON.stringify({ getStatesPartitionedByType: toReturn })
|
||||
JSON.stringify({ getStatesPartitionedByType: toReturn }),
|
||||
);
|
||||
return toReturn;
|
||||
}
|
||||
@@ -79,7 +85,7 @@ export class HAMiddleware {
|
||||
}
|
||||
|
||||
public static async getInstance(
|
||||
callerOptions?: Partial<HassWsOptions> | undefined
|
||||
callerOptions?: Partial<HassWsOptions> | undefined,
|
||||
): Promise<HAMiddleware> {
|
||||
if (!HAMiddleware.instance) {
|
||||
const client = await hass(callerOptions);
|
||||
|
||||
@@ -60,7 +60,12 @@ export type HassConfig = {
|
||||
config_source: string;
|
||||
recovery_mode: boolean;
|
||||
safe_mode: boolean;
|
||||
state: 'NOT_RUNNING' | 'STARTING' | 'RUNNING' | 'STOPPING' | 'FINAL_WRITE';
|
||||
state:
|
||||
| 'NOT_RUNNING'
|
||||
| 'STARTING'
|
||||
| 'RUNNING'
|
||||
| 'STOPPING'
|
||||
| 'FINAL_WRITE';
|
||||
external_url: string | null;
|
||||
internal_url: string | null;
|
||||
currency: string;
|
||||
|
||||
@@ -8,19 +8,23 @@ const LOGGER = new Logger('Mapper');
|
||||
|
||||
async function setHasEntities(
|
||||
haMiddleware: HAMiddleware,
|
||||
bridge: Bridge
|
||||
bridge: Bridge,
|
||||
): Promise<void> {
|
||||
const entities = await haMiddleware.getStatesPartitionedByType();
|
||||
LOGGER.info({ entities });
|
||||
if (entities['light']) {
|
||||
LOGGER.info('adding ', entities['light'].length, 'light devices');
|
||||
LOGGER.info(
|
||||
'adding ',
|
||||
entities['light'].length,
|
||||
'light devices',
|
||||
);
|
||||
setLights(entities['light'], haMiddleware, bridge);
|
||||
}
|
||||
}
|
||||
|
||||
export async function addAllDevicesToBridge(
|
||||
haMiddleware: HAMiddleware,
|
||||
bridge: Bridge
|
||||
bridge: Bridge,
|
||||
): Promise<void> {
|
||||
await setHasEntities(haMiddleware, bridge);
|
||||
haMiddleware.subscribe();
|
||||
|
||||
@@ -7,7 +7,7 @@ import { Bridge } from '../../matter';
|
||||
export type AddHaDeviceToBridge = (
|
||||
haEntity: HassEntity,
|
||||
haMiddleware: HAMiddleware,
|
||||
bridge: Bridge
|
||||
bridge: Bridge,
|
||||
) => Device;
|
||||
|
||||
export { HAMiddleware } from '../../home-assistant/HAmiddleware';
|
||||
|
||||
@@ -7,54 +7,68 @@ import {
|
||||
HassEntity,
|
||||
StateChangedEvent,
|
||||
} from '../../../home-assistant/HAssTypes';
|
||||
import { AddHaDeviceToBridge, Bridge, HAMiddleware } from '../MapperType';
|
||||
import {
|
||||
AddHaDeviceToBridge,
|
||||
Bridge,
|
||||
HAMiddleware,
|
||||
} from '../MapperType';
|
||||
import { Logger } from '@project-chip/matter-node.js/log';
|
||||
|
||||
const LOGGER = new Logger('DimmableLight');
|
||||
export const addDimmableLightDevice: AddHaDeviceToBridge = (
|
||||
haEntity: HassEntity,
|
||||
haMiddleware: HAMiddleware,
|
||||
bridge: Bridge
|
||||
bridge: Bridge,
|
||||
): Device => {
|
||||
LOGGER.debug(
|
||||
`Building device ${haEntity.entity_id} \n ${JSON.stringify({
|
||||
haEntity,
|
||||
})}`
|
||||
})}`,
|
||||
);
|
||||
const device = new DimmableLightDevice(
|
||||
{ onOff: haEntity.state === 'on' },
|
||||
{
|
||||
currentLevel:
|
||||
Number(haEntity.attributes['brightness']) / 255 || null,
|
||||
Number(haEntity.attributes['brightness']) / 255 ||
|
||||
null,
|
||||
onLevel: 0.1,
|
||||
options: { coupleColorTempToLevel: false, executeIfOff: false },
|
||||
}
|
||||
options: {
|
||||
coupleColorTempToLevel: false,
|
||||
executeIfOff: false,
|
||||
},
|
||||
},
|
||||
);
|
||||
const serialFromId = MD5(haEntity.entity_id).toString();
|
||||
device.addOnOffListener((value, oldValue) => {
|
||||
LOGGER.debug(
|
||||
`OnOff Event for device ${haEntity.entity_id}, ${JSON.stringify({
|
||||
value,
|
||||
oldValue,
|
||||
})}`
|
||||
`OnOff Event for device ${haEntity.entity_id}, ${JSON.stringify(
|
||||
{
|
||||
value,
|
||||
oldValue,
|
||||
},
|
||||
)}`,
|
||||
);
|
||||
if (value !== oldValue) {
|
||||
haMiddleware.callAService('light', value ? 'turn_on' : 'turn_off', {
|
||||
entity_id: haEntity.entity_id,
|
||||
});
|
||||
haMiddleware.callAService(
|
||||
'light',
|
||||
value ? 'turn_on' : 'turn_off',
|
||||
{
|
||||
entity_id: haEntity.entity_id,
|
||||
},
|
||||
);
|
||||
}
|
||||
});
|
||||
device.addCommandHandler(
|
||||
'identify',
|
||||
async ({ request: { identifyTime } }) =>
|
||||
({ request: { identifyTime } }) =>
|
||||
LOGGER.info(
|
||||
`Identify called for OnOffDevice ${haEntity.attributes['friendly_name']} with id: ${serialFromId} and identifyTime: ${identifyTime}`
|
||||
)
|
||||
`Identify called for OnOffDevice ${haEntity.attributes['friendly_name']} with id: ${serialFromId} and identifyTime: ${identifyTime}`,
|
||||
),
|
||||
);
|
||||
|
||||
device.addCurrentLevelListener((value) => {
|
||||
LOGGER.debug(
|
||||
`CurrentLevel Event for device ${haEntity.entity_id} value: ${value}`
|
||||
`CurrentLevel Event for device ${haEntity.entity_id} value: ${value}`,
|
||||
);
|
||||
let extraArgs = { entity_id: haEntity.entity_id } as object;
|
||||
if (Number(value) > 0) {
|
||||
@@ -63,7 +77,7 @@ export const addDimmableLightDevice: AddHaDeviceToBridge = (
|
||||
haMiddleware.callAService(
|
||||
'light',
|
||||
Number(value) > 0 ? 'turn_on' : 'turn_off',
|
||||
extraArgs
|
||||
extraArgs,
|
||||
);
|
||||
});
|
||||
|
||||
@@ -74,9 +88,11 @@ export const addDimmableLightDevice: AddHaDeviceToBridge = (
|
||||
LOGGER.debug(JSON.stringify(event));
|
||||
device.setOnOff(event.data.new_state?.state === 'on');
|
||||
device.setCurrentLevel(
|
||||
(event.data.new_state?.attributes as never)['brightness']
|
||||
(event.data.new_state?.attributes as never)[
|
||||
'brightness'
|
||||
],
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
bridge.addDevice(device, {
|
||||
|
||||
@@ -1,46 +1,59 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import { Device, OnOffLightDevice } from '@project-chip/matter-node.js/device';
|
||||
import {
|
||||
Device,
|
||||
OnOffLightDevice,
|
||||
} from '@project-chip/matter-node.js/device';
|
||||
import { MD5 } from 'crypto-js';
|
||||
import {
|
||||
HassEntity,
|
||||
StateChangedEvent,
|
||||
} from '../../../home-assistant/HAssTypes';
|
||||
import { AddHaDeviceToBridge, Bridge, HAMiddleware } from '../MapperType';
|
||||
import {
|
||||
AddHaDeviceToBridge,
|
||||
Bridge,
|
||||
HAMiddleware,
|
||||
} from '../MapperType';
|
||||
import { Logger } from '@project-chip/matter-node.js/log';
|
||||
|
||||
const LOGGER = new Logger('OnOffLight');
|
||||
export const addOnOffLightDevice: AddHaDeviceToBridge = (
|
||||
haEntity: HassEntity,
|
||||
haMiddleware: HAMiddleware,
|
||||
bridge: Bridge
|
||||
bridge: Bridge,
|
||||
): Device => {
|
||||
LOGGER.debug(
|
||||
`Building device ${haEntity.entity_id} \n ${JSON.stringify({
|
||||
haEntity,
|
||||
})}`
|
||||
})}`,
|
||||
);
|
||||
const device = new OnOffLightDevice();
|
||||
const serialFromId = MD5(haEntity.entity_id).toString();
|
||||
device.addOnOffListener((value, oldValue) => {
|
||||
LOGGER.debug(
|
||||
`OnOff Event for device ${haEntity.entity_id}, ${JSON.stringify({
|
||||
value,
|
||||
oldValue,
|
||||
})}`
|
||||
`OnOff Event for device ${haEntity.entity_id}, ${JSON.stringify(
|
||||
{
|
||||
value,
|
||||
oldValue,
|
||||
},
|
||||
)}`,
|
||||
);
|
||||
|
||||
if (value !== oldValue) {
|
||||
haMiddleware.callAService('light', value ? 'turn_on' : 'turn_off', {
|
||||
entity_id: haEntity.entity_id,
|
||||
});
|
||||
haMiddleware.callAService(
|
||||
'light',
|
||||
value ? 'turn_on' : 'turn_off',
|
||||
{
|
||||
entity_id: haEntity.entity_id,
|
||||
},
|
||||
);
|
||||
}
|
||||
});
|
||||
device.addCommandHandler(
|
||||
'identify',
|
||||
async ({ request: { identifyTime } }) =>
|
||||
({ request: { identifyTime } }) =>
|
||||
LOGGER.info(
|
||||
`Identify called for OnOffDevice ${haEntity.attributes['friendly_name']} with id: ${serialFromId} and identifyTime: ${identifyTime}`
|
||||
)
|
||||
`Identify called for OnOffDevice ${haEntity.attributes['friendly_name']} with id: ${serialFromId} and identifyTime: ${identifyTime}`,
|
||||
),
|
||||
);
|
||||
haMiddleware.subscribeToDevice(
|
||||
haEntity.entity_id,
|
||||
@@ -48,7 +61,7 @@ export const addOnOffLightDevice: AddHaDeviceToBridge = (
|
||||
LOGGER.debug(`Event for device ${haEntity.entity_id}`);
|
||||
LOGGER.debug(JSON.stringify(event));
|
||||
device.setOnOff(event.data.new_state?.state === 'on');
|
||||
}
|
||||
},
|
||||
);
|
||||
bridge.addDevice(device, {
|
||||
nodeLabel: haEntity.attributes['friendly_name'],
|
||||
|
||||
@@ -11,25 +11,27 @@ export * from './OnOffLightDevice';
|
||||
|
||||
const LOGGER = new Logger('Lights');
|
||||
|
||||
const LIGHTS_MAP_FUNCTIONS: Map<string, AddHaDeviceToBridge> = new Map<
|
||||
string,
|
||||
AddHaDeviceToBridge
|
||||
>([
|
||||
['onoff', addOnOffLightDevice],
|
||||
['rgb', addDimmableLightDevice],
|
||||
['brightness', addDimmableLightDevice],
|
||||
]);
|
||||
const LIGHTS_MAP_FUNCTIONS: Map<string, AddHaDeviceToBridge> =
|
||||
new Map<string, AddHaDeviceToBridge>([
|
||||
['onoff', addOnOffLightDevice],
|
||||
['rgb', addDimmableLightDevice],
|
||||
['brightness', addDimmableLightDevice],
|
||||
]);
|
||||
|
||||
const LIGHTS_MAP: Map<string, Device> = new Map<string, Device>();
|
||||
|
||||
export function setLights(
|
||||
lights: HassEntity[],
|
||||
haMiddleware: HAMiddleware,
|
||||
bridge: Bridge
|
||||
bridge: Bridge,
|
||||
) {
|
||||
lights.forEach((entity) => {
|
||||
LOGGER.info({ colormodes: entity.attributes['supported_color_modes'] });
|
||||
const key = (entity.attributes['supported_color_modes'] as string[])[0];
|
||||
LOGGER.info({
|
||||
colormodes: entity.attributes['supported_color_modes'],
|
||||
});
|
||||
const key = (
|
||||
entity.attributes['supported_color_modes'] as string[]
|
||||
)[0];
|
||||
LOGGER.info({ key });
|
||||
const lightBuildFunction = LIGHTS_MAP_FUNCTIONS.get(key);
|
||||
if (!lightBuildFunction) {
|
||||
@@ -37,7 +39,7 @@ export function setLights(
|
||||
}
|
||||
LIGHTS_MAP.set(
|
||||
entity.entity_id,
|
||||
lightBuildFunction(entity, haMiddleware, bridge)
|
||||
lightBuildFunction(entity, haMiddleware, bridge),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -23,10 +23,10 @@ export class Bridge {
|
||||
private static readonly deviceName =
|
||||
getParameter('name') || 'Matter Bridge';
|
||||
private static readonly deviceType = DeviceTypes.AGGREGATOR.code;
|
||||
private static readonly vendorName = getParameter('vendor') || 'Jatus';
|
||||
private static readonly vendorName =
|
||||
getParameter('vendor') || 'Jatus';
|
||||
private static readonly productName = 'HomeAssistant';
|
||||
private static readonly port = getIntParameter('port') ?? 5540;
|
||||
private ready = false;
|
||||
|
||||
private matterServer: MatterServer;
|
||||
private static instance: Bridge;
|
||||
@@ -37,7 +37,7 @@ export class Bridge {
|
||||
|
||||
private constructor(
|
||||
matterServer: MatterServer,
|
||||
storageManager: StorageManager
|
||||
storageManager: StorageManager,
|
||||
) {
|
||||
this.matterServer = matterServer;
|
||||
this.storageManager = storageManager;
|
||||
@@ -47,7 +47,7 @@ export class Bridge {
|
||||
|
||||
public static getInstance(
|
||||
matterServer: MatterServer,
|
||||
storageManager: StorageManager
|
||||
storageManager: StorageManager,
|
||||
): Bridge {
|
||||
if (!Bridge.instance) {
|
||||
this.instance = new Bridge(matterServer, storageManager);
|
||||
@@ -106,12 +106,17 @@ export class Bridge {
|
||||
device: Device | ComposedDevice,
|
||||
bridgedBasicInformation?: AttributeInitialValues<
|
||||
typeof BridgedDeviceBasicInformationCluster.attributes
|
||||
>
|
||||
>,
|
||||
) {
|
||||
if (!this.commissioningServer?.isCommissioned()) {
|
||||
this.logger.warn('System not initialized, may cause crashes');
|
||||
this.logger.warn(
|
||||
'System not initialized, may cause crashes',
|
||||
);
|
||||
}
|
||||
this.aggregator.addBridgedDevice(device, bridgedBasicInformation);
|
||||
this.aggregator.addBridgedDevice(
|
||||
device,
|
||||
bridgedBasicInformation,
|
||||
);
|
||||
}
|
||||
|
||||
async start() {
|
||||
@@ -119,30 +124,32 @@ export class Bridge {
|
||||
this.commissioningServer =
|
||||
await this.setupContextAndCommissioningServer();
|
||||
this.commissioningServer.addDevice(this.aggregator);
|
||||
this.matterServer.addCommissioningServer(this.commissioningServer);
|
||||
this.matterServer.addCommissioningServer(
|
||||
this.commissioningServer,
|
||||
);
|
||||
await this.matterServer.start();
|
||||
this.logger.info('Listening');
|
||||
if (!this.commissioningServer.isCommissioned()) {
|
||||
const pairingData = this.commissioningServer.getPairingCode();
|
||||
const pairingData =
|
||||
this.commissioningServer.getPairingCode();
|
||||
const { qrPairingCode, manualPairingCode } = pairingData;
|
||||
|
||||
console.log(QrCode.get(qrPairingCode));
|
||||
this.logger.info(
|
||||
`QR Code URL: https://project-chip.github.io/connectedhomeip/qrcode.html?data=${qrPairingCode}`
|
||||
`QR Code URL: https://project-chip.github.io/connectedhomeip/qrcode.html?data=${qrPairingCode}`,
|
||||
);
|
||||
this.logger.info(
|
||||
`Manual pairing code: ${manualPairingCode}`,
|
||||
);
|
||||
this.logger.info(`Manual pairing code: ${manualPairingCode}`);
|
||||
} else {
|
||||
this.logger.info(
|
||||
'Device is already commissioned. Waiting for controllers to connect ...'
|
||||
'Device is already commissioned. Waiting for controllers to connect ...',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async stop() {
|
||||
this.matterServer.close();
|
||||
this.storageManager
|
||||
.close()
|
||||
.then(() => process.exit(0))
|
||||
.catch((err) => this.logger.error(err));
|
||||
await this.matterServer.close();
|
||||
await this.storageManager.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,10 @@ import {
|
||||
StorageBackendDisk,
|
||||
StorageManager,
|
||||
} from '@project-chip/matter-node.js/storage';
|
||||
import { getParameter, hasParameter } from '@project-chip/matter-node.js/util';
|
||||
import {
|
||||
getParameter,
|
||||
hasParameter,
|
||||
} from '@project-chip/matter-node.js/util';
|
||||
export { Bridge } from './Bridge';
|
||||
import { Bridge } from './Bridge';
|
||||
|
||||
@@ -16,25 +19,29 @@ export function serverSetup(): {
|
||||
storageManager: StorageManager;
|
||||
} {
|
||||
if (!(MATTER_SERVER && STORAGE && STORAGE_MANAGER)) {
|
||||
const storageLocation = getParameter('store') || '/config/deviceData';
|
||||
const storageLocation =
|
||||
getParameter('store') || '/config/deviceData';
|
||||
|
||||
STORAGE = new StorageBackendDisk(
|
||||
storageLocation,
|
||||
hasParameter('clearstorage')
|
||||
hasParameter('clearstorage'),
|
||||
);
|
||||
STORAGE_MANAGER = new StorageManager(STORAGE);
|
||||
MATTER_SERVER = new MatterServer(STORAGE_MANAGER, {
|
||||
mdnsInterface: getParameter('netinterface'),
|
||||
});
|
||||
}
|
||||
return { matterServer: MATTER_SERVER, storageManager: STORAGE_MANAGER };
|
||||
return {
|
||||
matterServer: MATTER_SERVER,
|
||||
storageManager: STORAGE_MANAGER,
|
||||
};
|
||||
}
|
||||
|
||||
export function getBridge(): Bridge {
|
||||
const serverData = serverSetup();
|
||||
const bridge = Bridge.getInstance(
|
||||
serverData.matterServer,
|
||||
serverData.storageManager
|
||||
serverData.storageManager,
|
||||
);
|
||||
return bridge;
|
||||
}
|
||||
|
||||
@@ -14,20 +14,25 @@ export function hasParameter(name: string) {
|
||||
|
||||
export function getIntParameter(name: string) {
|
||||
const value = getParameter(name);
|
||||
if (value === undefined) return undefined;
|
||||
if (value === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
const intValue = parseInt(value, 10);
|
||||
if (isNaN(intValue))
|
||||
if (isNaN(intValue)) {
|
||||
throw new ValidationError(
|
||||
`Invalid value for parameter ${name}: ${value} is not a number`
|
||||
`Invalid value for parameter ${name}: ${value} is not a number`,
|
||||
);
|
||||
}
|
||||
return intValue;
|
||||
}
|
||||
|
||||
export function commandExecutor(scriptParamName: string) {
|
||||
const script = getParameter(scriptParamName);
|
||||
if (script === undefined) return undefined;
|
||||
if (script === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
return () =>
|
||||
console.log(
|
||||
`${scriptParamName}: ${execSync(script).toString().slice(0, -1)}`
|
||||
`${scriptParamName}: ${execSync(script).toString().slice(0, -1)}`,
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user