Skip to content

Commit

Permalink
Revert "Revert "Add Battery level for Meter Plus #781 #782"" (#786)
Browse files Browse the repository at this point in the history
  • Loading branch information
donavanbecker authored Aug 1, 2023
1 parent ca81848 commit a2317ea
Showing 1 changed file with 54 additions and 51 deletions.
105 changes: 54 additions & 51 deletions src/device/meterplus.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Context } from 'vm';
import { hostname } from 'os';
import { request } from 'undici';
import { sleep } from '../utils';
import { MqttClient } from 'mqtt';
import { interval, Subject } from 'rxjs';
import { connectAsync } from 'async-mqtt';
import { CharacteristicValue, PlatformAccessory, Service, Units } from 'homebridge';
import { MqttClient } from 'mqtt';
import { hostname } from 'os';
import { Subject, interval } from 'rxjs';
import { skipWhile } from 'rxjs/operators';
import { request } from 'undici';
import { Context } from 'vm';
import { SwitchBotPlatform } from '../platform';
import { Service, PlatformAccessory, Units, CharacteristicValue } from 'homebridge';
import { device, devicesConfig, serviceData, ad, switchbot, temperature, deviceStatus, Devices } from '../settings';
import { Devices, ad, device, deviceStatus, devicesConfig, serviceData, switchbot, temperature } from '../settings';
import { sleep } from '../utils';

/**
* Platform Accessory
Expand Down Expand Up @@ -104,7 +104,7 @@ export class MeterPlus {
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Temperature Sensor Service`);
(this.temperatureservice =
this.accessory.getService(this.platform.Service.TemperatureSensor) || this.accessory.addService(this.platform.Service.TemperatureSensor)),
`${accessory.displayName} Temperature Sensor`;
`${accessory.displayName} Temperature Sensor`;

this.temperatureservice.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Temperature Sensor`);
if (!this.temperatureservice.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
Expand Down Expand Up @@ -135,7 +135,7 @@ export class MeterPlus {
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Humidity Sensor Service`);
(this.humidityservice =
this.accessory.getService(this.platform.Service.HumiditySensor) || this.accessory.addService(this.platform.Service.HumiditySensor)),
`${accessory.displayName} Humidity Sensor`;
`${accessory.displayName} Humidity Sensor`;

this.humidityservice.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Humidity Sensor`);
if (!this.humidityservice.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
Expand All @@ -154,14 +154,10 @@ export class MeterPlus {
}

// Battery Service
if (!this.BLE) {
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Removing Battery Service`);
this.batteryService = this.accessory.getService(this.platform.Service.Battery);
accessory.removeService(this.batteryService!);
} else if (this.BLE && !this.batteryService) {
if (!this.batteryService) {
this.debugLog(`${this.device.deviceType}: ${accessory.displayName} Add Battery Service`);
(this.batteryService = this.accessory.getService(this.platform.Service.Battery) || this.accessory.addService(this.platform.Service.Battery)),
`${accessory.displayName} Battery`;
`${accessory.displayName} Battery`;

this.batteryService.setCharacteristic(this.platform.Characteristic.Name, `${accessory.displayName} Battery`);
if (!this.batteryService.testCharacteristic(this.platform.Characteristic.ConfiguredName)) {
Expand Down Expand Up @@ -229,6 +225,16 @@ export class MeterPlus {

async openAPIparseStatus(): Promise<void> {
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} openAPIparseStatus`);

// Battery
this.BatteryLevel = Number(this.battery);
if (this.BatteryLevel < 15) {
this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
} else {
this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
}
this.debugLog(`${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}, StatusLowBattery: ${this.StatusLowBattery}`);

// Current Relative Humidity
if (!this.device.meter?.hide_humidity) {
this.CurrentRelativeHumidity = this.Humidity!;
Expand Down Expand Up @@ -395,28 +401,29 @@ export class MeterPlus {
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic CurrentTemperature: ${this.CurrentTemperature}`);
}
}
if (this.BLE) {
if (this.BatteryLevel === undefined) {
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
} else {
if (this.device.mqttURL) {
mqttmessage.push(`"battery": ${this.BatteryLevel}`);
}
this.accessory.context.BatteryLevel = this.BatteryLevel;
this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);

if (this.BatteryLevel === undefined) {
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} BatteryLevel: ${this.BatteryLevel}`);
} else {
if (this.device.mqttURL) {
mqttmessage.push(`"battery": ${this.BatteryLevel}`);
}
if (this.StatusLowBattery === undefined) {
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
} else {
if (this.device.mqttURL) {
mqttmessage.push(`"lowBattery": ${this.StatusLowBattery}`);
}
this.accessory.context.StatusLowBattery = this.StatusLowBattery;
this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
this.accessory.context.BatteryLevel = this.BatteryLevel;
this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, this.BatteryLevel);
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic BatteryLevel: ${this.BatteryLevel}`);
}

if (this.StatusLowBattery === undefined) {
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} StatusLowBattery: ${this.StatusLowBattery}`);
} else {
if (this.device.mqttURL) {
mqttmessage.push(`"lowBattery": ${this.StatusLowBattery}`);
}
this.accessory.context.StatusLowBattery = this.StatusLowBattery;
this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, this.StatusLowBattery);
this.debugLog(`${this.device.deviceType}: ${this.accessory.displayName} updateCharacteristic StatusLowBattery: ${this.StatusLowBattery}`);
}

if (this.device.mqttURL) {
this.mqttPublish(`{${mqttmessage.join(',')}}`);
}
Expand Down Expand Up @@ -593,10 +600,8 @@ export class MeterPlus {
if (!this.device.meter?.hide_temperature) {
this.temperatureservice?.updateCharacteristic(this.platform.Characteristic.CurrentTemperature, e);
}
if (this.BLE) {
this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
}
this.batteryService?.updateCharacteristic(this.platform.Characteristic.BatteryLevel, e);
this.batteryService?.updateCharacteristic(this.platform.Characteristic.StatusLowBattery, e);
}

FirmwareRevision(accessory: PlatformAccessory<Context>, device: device & devicesConfig): CharacteristicValue {
Expand Down Expand Up @@ -627,18 +632,16 @@ export class MeterPlus {
} else {
this.CurrentTemperature = this.accessory.context.CurrentTemperature;
}
if (this.BLE) {
if (this.BatteryLevel === undefined) {
this.BatteryLevel = 100;
} else {
this.BatteryLevel = this.accessory.context.BatteryLevel;
}
if (this.StatusLowBattery === undefined) {
this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
this.accessory.context.StatusLowBattery = this.StatusLowBattery;
} else {
this.StatusLowBattery = this.accessory.context.StatusLowBattery;
}
if (this.BatteryLevel === undefined) {
this.BatteryLevel = 100;
} else {
this.BatteryLevel = this.accessory.context.BatteryLevel;
}
if (this.StatusLowBattery === undefined) {
this.StatusLowBattery = this.platform.Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL;
this.accessory.context.StatusLowBattery = this.StatusLowBattery;
} else {
this.StatusLowBattery = this.accessory.context.StatusLowBattery;
}
}

Expand Down

0 comments on commit a2317ea

Please sign in to comment.