UNPKG

matterbridge-eve-room

Version:
79 lines (78 loc) 5.26 kB
import { EveHistory, MatterHistory } from 'matter-history'; import { airQualitySensor, MatterbridgeAccessoryPlatform, MatterbridgeEndpoint, powerSource } from 'matterbridge'; import { AirQuality, PowerSource, RelativeHumidityMeasurement, TemperatureMeasurement, TotalVolatileOrganicCompoundsConcentrationMeasurement } from 'matterbridge/matter/clusters'; export default function initializePlugin(matterbridge, log, config) { return new EveRoomPlatform(matterbridge, log, config); } export class EveRoomPlatform extends MatterbridgeAccessoryPlatform { room; history; interval; minTemperature = 0; maxTemperature = 0; constructor(matterbridge, log, config) { super(matterbridge, log, config); if (this.verifyMatterbridgeVersion === undefined || typeof this.verifyMatterbridgeVersion !== 'function' || !this.verifyMatterbridgeVersion('3.5.0')) { throw new Error(`This plugin requires Matterbridge version >= "3.5.0". Please update Matterbridge from ${this.matterbridge.matterbridgeVersion} to the latest version in the frontend."`); } this.log.info('Initializing platform:', this.config.name); } async onStart(reason) { this.log.info('onStart called with reason:', reason ?? 'none'); this.history = new MatterHistory(this.log, 'Eve room', { filePath: this.matterbridge.matterbridgeDirectory, enableDebug: this.config.debug }); this.room = new MatterbridgeEndpoint([airQualitySensor, powerSource], { id: 'Eve room', mode: this.matterbridge.bridgeMode === 'bridge' ? 'server' : undefined }, this.config.debug); this.room.createDefaultIdentifyClusterServer(); this.room.createDefaultBasicInformationClusterServer('Eve room' + (this.matterbridge.bridgeMode === 'bridge' ? ' server' : ''), '0x84224975', 4874, 'Eve Systems', 0x27, 'Eve Room 20EAM9901', 1416, '1.2.11', 1, '1.0.0'); this.room.createDefaultAirQualityClusterServer(AirQuality.AirQualityEnum.Good); this.room.createDefaultTvocMeasurementClusterServer(100); this.room.createDefaultTemperatureMeasurementClusterServer(20 * 100); this.room.createDefaultRelativeHumidityMeasurementClusterServer(50 * 100); this.room.createDefaultPowerSourceRechargeableBatteryClusterServer(87, PowerSource.BatChargeLevel.Ok, 1500, PowerSource.BatReplaceability.UserReplaceable); this.history.createRoomEveHistoryClusterServer(this.room, this.log); await this.registerDevice(this.room); this.history.autoPilot(this.room); this.room.addCommandHandler('identify', async ({ request: { identifyTime } }) => { this.log.warn(`Command identify called identifyTime:${identifyTime}`); this.history?.logHistory(false); }); this.room.addCommandHandler('triggerEffect', async ({ request: { effectIdentifier, effectVariant } }) => { this.log.warn(`Command triggerEffect called effect ${effectIdentifier} variant ${effectVariant}`); this.history?.logHistory(false); }); this.history.setMaxMinTemperature(20, 20); } async onConfigure() { await super.onConfigure(); this.log.info('onConfigure called'); await this.room?.setAttribute(EveHistory.Cluster.id, 'temperatureDisplayUnits', 0, this.log); this.interval = setInterval(async () => { if (!this.room || !this.history) return; const airquality = AirQuality.AirQualityEnum.Good; const voc = this.history.getFakeLevel(0, 1000, 0); const temperature = this.history.getFakeLevel(10, 30, 2); if (this.minTemperature === 0) this.minTemperature = temperature; if (this.maxTemperature === 0) this.maxTemperature = temperature; this.minTemperature = Math.min(this.minTemperature, temperature); this.maxTemperature = Math.max(this.maxTemperature, temperature); const humidity = this.history.getFakeLevel(1, 99, 2); await this.room.setAttribute(AirQuality.Cluster.id, 'airQuality', airquality); await this.room.setAttribute(TotalVolatileOrganicCompoundsConcentrationMeasurement.Cluster.id, 'measuredValue', voc, this.log); await this.room.setAttribute(TemperatureMeasurement.Cluster.id, 'measuredValue', temperature * 100, this.log); await this.room.setAttribute(RelativeHumidityMeasurement.Cluster.id, 'measuredValue', humidity * 100, this.log); this.history.setMaxMinTemperature(this.maxTemperature, this.minTemperature); this.history.addEntry({ time: this.history.now(), airquality, voc, temperature, humidity }); this.log.info(`Set airquality: ${airquality} voc: ${voc} temperature: ${temperature} (min: ${this.minTemperature} max: ${this.maxTemperature}) humidity: ${humidity}`); }, 60 * 1000 - 700); } async onShutdown(reason) { await super.onShutdown(reason); this.log.info('onShutdown called with reason:', reason ?? 'none'); await this.history?.close(); clearInterval(this.interval); if (this.config.unregisterOnShutdown === true) await this.unregisterAllDevices(); } }