matterbridge-eve-weather
Version:
Matterbridge eve weather with history
90 lines (89 loc) • 6.05 kB
JavaScript
import { EveHistory, MatterHistory } from 'matter-history';
import { humiditySensor, MatterbridgeAccessoryPlatform, MatterbridgeEndpoint, powerSource, pressureSensor, temperatureSensor, } from 'matterbridge';
import { PowerSource, PressureMeasurement, RelativeHumidityMeasurement, TemperatureMeasurement } from 'matterbridge/matter/clusters';
import { fireAndForget } from 'matterbridge/utils';
export default function initializePlugin(matterbridge, log, config) {
return new EveWeatherPlatform(matterbridge, log, config);
}
export class EveWeatherPlatform extends MatterbridgeAccessoryPlatform {
weather;
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.8.0')) {
throw new Error(`This plugin requires Matterbridge version >= "3.8.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 weather', { filePath: this.matterbridge.matterbridgeDirectory, enableDebug: this.config.debug });
this.weather = new MatterbridgeEndpoint([temperatureSensor, humiditySensor, pressureSensor, powerSource], { id: 'Eve weather', mode: this.matterbridge.bridgeMode === 'bridge' ? 'server' : undefined }, this.config.debug);
this.weather.createDefaultIdentifyClusterServer();
this.weather.createDefaultBasicInformationClusterServer('Eve weather' + (this.matterbridge.bridgeMode === 'bridge' ? ' server' : ''), '0x84286995', 4874, 'Eve Systems', 0x57, 'Eve Weather 20EBS9901', 2996, '2.1.3', 1, '1.1');
this.weather.createDefaultTemperatureMeasurementClusterServer(20 * 100);
this.weather.createDefaultRelativeHumidityMeasurementClusterServer(50 * 100);
this.weather.createDefaultPressureMeasurementClusterServer(950);
this.weather.createDefaultPowerSourceReplaceableBatteryClusterServer(64, PowerSource.BatChargeLevel.Ok, 3000, 'CR2450', 1);
this.weather.addRequiredClusters();
this.history.createWeatherEveHistoryClusterServer(this.weather, this.log);
await this.registerDevice(this.weather);
this.history.autoPilot(this.weather);
this.weather.addCommandHandler('identify', ({ request: { identifyTime } }) => {
this.log.warn(`Command identify called identifyTime:${identifyTime}`);
this.history?.logHistory(false);
});
this.weather.addCommandHandler('triggerEffect', ({ request: { effectIdentifier, effectVariant } }) => {
this.log.warn(`Command triggerEffect called effect ${effectIdentifier} variant ${effectVariant}`);
this.history?.logHistory(false);
});
}
async onConfigure() {
await super.onConfigure();
this.log.info('onConfigure called');
await this.weather?.setAttribute(EveHistory.Cluster.id, 'elevation', 250);
await this.weather?.setAttribute(EveHistory.Cluster.id, 'weatherTrend', 1);
await this.weather?.setAttribute(EveHistory.Cluster.id, 'temperatureDisplayUnits', 0);
await this.weather?.setAttribute(EveHistory.Cluster.id, 'airPressure', 950);
this.interval = setInterval(() => {
fireAndForget((async () => {
if (!this.weather || !this.history)
return;
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);
const pressure = this.history.getFakeLevel(700, 1100, 1);
await this.weather.setAttribute(TemperatureMeasurement.Cluster.id, 'measuredValue', temperature * 100, this.log);
await this.weather.setAttribute(RelativeHumidityMeasurement.Cluster.id, 'measuredValue', humidity * 100, this.log);
await this.weather.setAttribute(PressureMeasurement.Cluster.id, 'measuredValue', pressure, this.log);
await this.weather.setAttribute(EveHistory.Cluster.id, 'weatherTrend', 1);
if (pressure < 800)
await this.weather.setAttribute(EveHistory.Cluster.id, 'weatherTrend', 12);
else if (pressure < 900)
await this.weather.setAttribute(EveHistory.Cluster.id, 'weatherTrend', 5);
else if (pressure < 1000)
await this.weather.setAttribute(EveHistory.Cluster.id, 'weatherTrend', 3);
await this.weather.setAttribute(EveHistory.Cluster.id, 'airPressure', pressure);
this.history.setMaxMinTemperature(this.maxTemperature, this.minTemperature);
this.history.addEntry({ time: this.history.now(), temperature, humidity, pressure });
this.log.info(`Set temperature: ${temperature} (min: ${this.minTemperature} max: ${this.maxTemperature}) humidity: ${humidity} pressure: ${pressure}`);
})(), this.log, 'setInterval');
}, 60 * 1000 - 100);
}
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();
}
}