homebridge-eveatmo
Version:
Homebridge plugin which adds a Netatmo weatherstation as HomeKit device and tries to act like Elgato Eve Room/Weather
175 lines (145 loc) • 5.82 kB
JavaScript
'use strict';
/* eslint-disable-next-line @typescript-eslint/no-require-imports */
var path = require('path');
var homebridge;
var NetatmoAccessory;
var mainDeviceId = false;
var FakeGatoHistoryService;
module.exports = function (pHomebridge) {
if (pHomebridge && !homebridge) {
homebridge = pHomebridge;
/* eslint-disable @typescript-eslint/no-require-imports */
NetatmoAccessory = require('../lib/netatmo-accessory')(homebridge);
FakeGatoHistoryService = require('fakegato-history')(homebridge);
/* eslint-enable @typescript-eslint/no-require-imports */
}
class EveatmoWeatherAccessory extends NetatmoAccessory {
constructor(deviceData, netatmoDevice) {
for (var deviceId in netatmoDevice.deviceData) {
if (!Object.prototype.hasOwnProperty.call(netatmoDevice.deviceData, deviceId)) {
continue;
}
var device = netatmoDevice.deviceData[deviceId];
if (device.dashboard_data && device.dashboard_data.Pressure) {
mainDeviceId = deviceId;
}
}
var accessoryConfig = {
'id': deviceData._id,
'model': 'Eve Weather',
'netatmoType': deviceData.type,
'firmware': String(deviceData.firmware),
'name': deviceData._name || 'Eveatmo ' + netatmoDevice.deviceType + ' ' + deviceData._id,
'hasBattery': (deviceData.battery_vp) ? true : false,
};
super(homebridge, accessoryConfig, netatmoDevice);
this.buildServices(accessoryConfig);
this.currentTemperature = 11.1;
this.batteryPercent = 100;
this.lowBattery = false;
this.humidity = 50;
this.pressure = 1000.0;
this.refreshData(() => { });
}
buildServices(accessoryConfig) {
var serviceDir = path.resolve(__dirname, '../service');
try {
/* eslint-disable @typescript-eslint/no-require-imports */
var TemperatureService = require(path.join(serviceDir, 'eveatmo-temperature'))(homebridge);
var serviceTemperature = new TemperatureService(this);
this.addService(serviceTemperature);
var HumidityService = require(path.join(serviceDir, 'eveatmo-humidity'))(homebridge);
var serviceHumidity = new HumidityService(this);
this.addService(serviceHumidity);
var EveatmoWeatherPressureService = require(path.join(serviceDir, 'eveatmo-weather-pressure'))(homebridge);
var servicePressure = new EveatmoWeatherPressureService(this);
this.addService(servicePressure);
if (accessoryConfig.hasBattery) {
var EveatmoBatteryService = require(path.join(serviceDir, 'eveatmo-battery'))(homebridge);
var serviceBattery = new EveatmoBatteryService(this);
this.addService(serviceBattery);
}
/* eslint-enable @typescript-eslint/no-require-imports */
this.historyService = new FakeGatoHistoryService('weather', this, { storage: 'fs' });
} catch (err) {
this.log.warn('Could not process service files for ' + accessoryConfig.name);
this.log.warn(err);
this.log.warn(err.stack);
}
}
notifyUpdate(deviceData, force) {
var accessoryData = this.extractAccessoryData(deviceData);
if (!accessoryData) {
return;
}
if (!accessoryData.reachable && !force) {
return;
}
var weatherData = this.mapAccessoryDataToWeatherData(accessoryData);
// transfer NAMain's pressure value to outdoor-module
if (mainDeviceId) {
if (deviceData[mainDeviceId]) {
weatherData.pressure = deviceData[mainDeviceId].dashboard_data.Pressure;
}
}
this.historyService.addEntry({
time: new Date().getTime() / 1000,
temp: weatherData.currentTemperature,
pressure: weatherData.pressure,
humidity: weatherData.humidity,
});
this.applyWeatherData(weatherData);
}
mapAccessoryDataToWeatherData(accessoryData) {
var result = {};
var dashboardData = accessoryData.dashboard_data;
if (dashboardData) {
if (Object.prototype.hasOwnProperty.call(dashboardData, 'Temperature')) {
result.currentTemperature = dashboardData.Temperature;
}
if (dashboardData.Humidity) {
result.humidity = dashboardData.Humidity;
}
}
result.batteryPercent = accessoryData.battery_percent;
if (!result.batteryPercent) {
result.batteryPercent = 100;
}
result.lowBattery = (result.batteryPercent <= 20) ? true : false;
return result;
}
applyWeatherData(weatherData) {
var dataChanged = false;
if (Object.prototype.hasOwnProperty.call(weatherData, 'currentTemperature') && this.currentTemperature !== weatherData.currentTemperature) {
this.currentTemperature = weatherData.currentTemperature;
dataChanged = true;
}
if (weatherData.humidity && this.humidity !== weatherData.humidity) {
this.humidity = weatherData.humidity;
dataChanged = true;
}
if (weatherData.pressure && this.pressure !== weatherData.pressure) {
this.pressure = weatherData.pressure;
dataChanged = true;
}
if (weatherData.batteryPercent && this.batteryPercent !== weatherData.batteryPercent) {
this.batteryPercent = weatherData.batteryPercent;
dataChanged = true;
}
if (this.lowBattery !== weatherData.lowBattery) {
this.lowBattery = weatherData.lowBattery;
dataChanged = true;
}
if (dataChanged) {
this.getServices().forEach(
(svc) => {
if (svc.updateCharacteristics) {
svc.updateCharacteristics();
}
},
);
}
}
}
return EveatmoWeatherAccessory;
};