diff --git a/matter-bridge/config.yml b/matter-bridge/config.yml index 83a0ba4..bc6570d 100644 --- a/matter-bridge/config.yml +++ b/matter-bridge/config.yml @@ -1,6 +1,6 @@ --- name: 'HA Matter Bridge' -version: '1.0.0' +version: '0.0.1-alpha' slug: ha-matter-bridge description: This project serves as a proof of concept to connect HomeAssistant devices to Voice Assistants through the Matter Protocol. init: false @@ -16,3 +16,4 @@ homeassistant_api: true auth_api: true map: - addon_config:rw +image: ghcr.io/jatus93/ha-matter-bridge:main diff --git a/matter-bridge/src/home-assistant/HAmiddleware.ts b/matter-bridge/src/home-assistant/HAmiddleware.ts index eb42dce..80817ab 100644 --- a/matter-bridge/src/home-assistant/HAmiddleware.ts +++ b/matter-bridge/src/home-assistant/HAmiddleware.ts @@ -8,6 +8,7 @@ export class HAMiddleware { private logger = new Logger('HAMiddleware'); private hassClient: HassApi; private static instance: HAMiddleware; + private static callerOptions: Partial | undefined; private requestFulfilled: boolean = true; private entities: { [k: string]: HassEntity } = {}; private functionsToCallOnChange: { @@ -93,6 +94,7 @@ export class HAMiddleware { callerOptions?: Partial | undefined ): Promise { if (!HAMiddleware.instance) { + HAMiddleware.callerOptions = callerOptions; const client = await hass(callerOptions); HAMiddleware.instance = new HAMiddleware(client); } diff --git a/matter-bridge/src/mapper/Mapper.ts b/matter-bridge/src/mapper/Mapper.ts index 38fc88e..2b695e7 100644 --- a/matter-bridge/src/mapper/Mapper.ts +++ b/matter-bridge/src/mapper/Mapper.ts @@ -1,44 +1,11 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ -import { - addDimmerableLightDevice, - addOnOffLightDevice, -} from './devices/lights'; -import { HassEntity } from '../home-assistant/HAssTypes'; +import { setLights } from './devices/lights'; import { Bridge } from '../matter'; import { Logger } from '@project-chip/matter-node.js/log'; import { HAMiddleware } from '../home-assistant/HAmiddleware'; const LOGGER = new Logger('Mapper'); -const lightsMap: Map< - string, - (haEntity: HassEntity, haMiddleware: HAMiddleware, bridge: Bridge) => void -> = new Map< - string, - (haEntity: HassEntity, haMiddleware: HAMiddleware, bridge: Bridge) => void ->([ - ['onoff', addOnOffLightDevice], - ['rgb', addDimmerableLightDevice], - ['brightness', addDimmerableLightDevice], -]); - -function setLights( - lights: HassEntity[], - haMiddleware: HAMiddleware, - 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({ key }); - const lightBuildFunction = lightsMap.get(key); - if (!lightBuildFunction) { - throw new Error('Missing ' + key); - } - return lightBuildFunction(entity, haMiddleware, bridge); - }); -} - async function setHasEnties( haMiddleware: HAMiddleware, bridge: Bridge diff --git a/matter-bridge/src/mapper/devices/lights/DimmerableLightDevice.ts b/matter-bridge/src/mapper/devices/lights/DimmerableLightDevice.ts index d77b97c..5147fad 100644 --- a/matter-bridge/src/mapper/devices/lights/DimmerableLightDevice.ts +++ b/matter-bridge/src/mapper/devices/lights/DimmerableLightDevice.ts @@ -16,7 +16,14 @@ export const addDimmerableLightDevice: AddHaDeviceToBridge = ( haMiddleware: HAMiddleware, bridge: Bridge ): Device => { - const device = new DimmableLightDevice(); + const device = new DimmableLightDevice( + { onOff: haEntity.state === 'on' }, + { + currentLevel: Number(haEntity.attributes['brightness']) || null, + onLevel: 0.1, + options: { coupleColorTempToLevel: false, executeIfOff: false }, + } + ); const serialFromId = MD5(haEntity.entity_id).toString(); device.addOnOffListener((value, oldValue) => { if (value !== oldValue) { diff --git a/matter-bridge/src/mapper/devices/lights/index.ts b/matter-bridge/src/mapper/devices/lights/index.ts index 39c83c6..8224fe6 100644 --- a/matter-bridge/src/mapper/devices/lights/index.ts +++ b/matter-bridge/src/mapper/devices/lights/index.ts @@ -1,2 +1,43 @@ +import { Logger } from '@project-chip/matter-node.js/log'; +import { HAMiddleware } from '../../../home-assistant/HAmiddleware'; +import { HassEntity } from '../../../home-assistant/HAssTypes'; +import { AddHaDeviceToBridge, Bridge } from '../MapperType'; +import { addDimmerableLightDevice } from './DimmerableLightDevice'; +import { addOnOffLightDevice } from './OnOffLightDevice'; +import { Device } from '@project-chip/matter-node.js/device'; + export * from './DimmerableLightDevice'; export * from './OnOffLightDevice'; + +const LOGGER = new Logger('Lights'); + +const LIGHTS_MAP_FUNCTIONS: Map = new Map< + string, + AddHaDeviceToBridge +>([ + ['onoff', addOnOffLightDevice], + ['rgb', addDimmerableLightDevice], + ['brightness', addDimmerableLightDevice], +]); + +const LIGHTS_MAP: Map = new Map(); + +export function setLights( + lights: HassEntity[], + haMiddleware: HAMiddleware, + 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({ key }); + const lightBuildFunction = LIGHTS_MAP_FUNCTIONS.get(key); + if (!lightBuildFunction) { + throw new Error('Missing ' + key); + } + LIGHTS_MAP.set( + entity.entity_id, + lightBuildFunction(entity, haMiddleware, bridge) + ); + }); +}