diff --git a/README.md b/README.md index 461c65b..5da6416 100755 --- a/README.md +++ b/README.md @@ -60,9 +60,10 @@ Ensure you know the MAC address of your Xiaomi Flower Care. You can use `hcitool | interval | Frequency of data refresh in seconds. Minimum: 1 (not recommended); Maximum: 600 (due to FakeGato). | Required | | humidityAlertLevel | Humidity level in percent used to trigger the humidity alert contact sensor. | Optional | | lowLightAlertLevel | Low light level in Lux used to trigger a low light alert contact sensor. | Optional | +| lowFertilityAlertLevel | Low fertility level in µS/cm used to trigger a low fertility alert contact sensor. | Optional | | lowBatteryWarningLevel | Battery level used to trigger low battery warning. | Optional | -Typical values for `humidityAlertLevel`are 30 (%), 2000 (Lux) for `lowLightAlertLevel`, and 10 (%) for `lowBatteryWarningLevel`. +Typical values for `humidityAlertLevel`are 30 (%), 2000 (Lux) for `lowLightAlertLevel`, 350 (µS/cm) for `lowFertilityAlertLevel`, and 10 (%) for `lowBatteryWarningLevel`. ## Running diff --git a/index.js b/index.js index 42daf3d..2a7cd57 100755 --- a/index.js +++ b/index.js @@ -40,6 +40,13 @@ function MiFlowerCarePlugin(log, config) { this.lowLightAlert = false; } + if (config.lowFertilityAlertLevel != null) { + this.lowFertilityAlert = true; + this.lowFertilityAlertLevel = config.lowFertilityAlertLevel; + } else { + this.lowFertilityAlert = false; + } + if (config.lowBatteryWarningLevel != null && typeof config.lowBatteryWarningLevel === "number") { this.lowBatteryWarningLevel = config.lowBatteryWarningLevel; } else { @@ -78,6 +85,11 @@ function MiFlowerCarePlugin(log, config) { that.humidityService.getCharacteristic(Characteristic.StatusActive) .updateValue(true); + that.fertilityService.getCharacteristic(Characteristic.CurrentFertility) + .updateValue(data.moisture); + that.fertilityService.getCharacteristic(Characteristic.StatusActive) + .updateValue(true); + if (that.humidityAlert) { that.humidityAlertService.getCharacteristic(Characteristic.ContactSensorState) .updateValue(data.moisture <= that.humidityAlertLevel ? Characteristic.ContactSensorState.CONTACT_NOT_DETECTED : Characteristic.ContactSensorState.CONTACT_DETECTED); @@ -91,6 +103,13 @@ function MiFlowerCarePlugin(log, config) { that.lowLightAlertService.getCharacteristic(Characteristic.StatusActive) .updateValue(true); } + + if (that.lowFertilityAlert) { + that.lowFertilityAlertService.getCharacteristic(Characteristic.ContactSensorState) + .updateValue(data.lux <= that.lowFertilityAlertLevel ? Characteristic.ContactSensorState.CONTACT_NOT_DETECTED : Characteristic.ContactSensorState.CONTACT_DETECTED); + that.lowFertilityAlertService.getCharacteristic(Characteristic.StatusActive) + .updateValue(true); + } } }); @@ -119,12 +138,17 @@ function MiFlowerCarePlugin(log, config) { if (that.humidityAlert) { that.humidityAlertService.getCharacteristic(Characteristic.StatusLowBattery) - .updateValue(data.batteryLevel <= that.lowBatteryWarningLevel ? Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL); + .updateValue(data.moisture <= that.humidityAlertLevel ? Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL); } if (that.lowLightAlert) { that.lowLightAlertService.getCharacteristic(Characteristic.StatusLowBattery) - .updateValue(data.batteryLevel <= that.lowBatteryWarningLevel ? Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL); + .updateValue(data.lux <= that.lowLightAlertLevel ? Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL); + } + + if (that.lowFertilityAlert) { + that.lowFertilityAlertService.getCharacteristic(Characteristic.StatusLowBattery) + .updateValue(data.fertility <= that.lowFertilityAlertLevel ? Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW : Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL); } } }); @@ -176,6 +200,14 @@ MiFlowerCarePlugin.prototype.getStatusLowLight = function (callback) { } }; +MiFlowerCarePlugin.prototype.getStatusLowFertility = function (callback) { + if (this.storedData.data) { + callback(null, this.storedData.data.fertility <= this.lowFertilityAlertLevel ? Characteristic.ContactSensorState.CONTACT_NOT_DETECTED : Characteristic.ContactSensorState.CONTACT_DETECTED); + } else { + callback(null, Characteristic.ContactSensorState.CONTACT_DETECTED); + } +}; + MiFlowerCarePlugin.prototype.getCurrentAmbientLightLevel = function (callback) { callback(null, this.storedData.data ? this.storedData.data.lux : 0); }; @@ -235,6 +267,14 @@ MiFlowerCarePlugin.prototype.setUpServices = function () { this.humidityService.getCharacteristic(Characteristic.StatusActive) .on('get', this.getStatusActive.bind(this)); + this.fertilityService = new Service.FertilitySensor(this.name); + this.fertilityService.getCharacteristic(Characteristic.CurrentFertility) + .on('get', this.getCurrentFertility.bind(this)); + this.fertilityService.getCharacteristic(Characteristic.StatusLowBattery) + .on('get', this.getStatusLowBattery.bind(this)); + this.fertilityService.getCharacteristic(Characteristic.StatusActive) + .on('get', this.getStatusActive.bind(this)); + if (this.humidityAlert) { this.humidityAlertService = new Service.ContactSensor(this.name + " Low Humidity", "humidity"); this.humidityAlertService.getCharacteristic(Characteristic.ContactSensorState) @@ -255,6 +295,16 @@ MiFlowerCarePlugin.prototype.setUpServices = function () { .on('get', this.getStatusActive.bind(this)); } + if (this.lowFertilityAlert) { + this.lowFertilityAlertService = new Service.ContactSensor(this.name + " Low Fertility", "fertility"); + this.lowFertilityAlertService.getCharacteristic(Characteristic.ContactSensorState) + .on('get', this.getStatusLowFertility.bind(this)); + this.lowFertilityAlertService.getCharacteristic(Characteristic.StatusLowBattery) + .on('get', this.getStatusLowBattery.bind(this)); + this.lowFertilityAlertService.getCharacteristic(Characteristic.StatusActive) + .on('get', this.getStatusActive.bind(this)); + } + this.fakeGatoHistoryService = new FakeGatoHistoryService("room", this, { storage: 'fs' }); /* @@ -329,5 +379,8 @@ MiFlowerCarePlugin.prototype.getServices = function () { if (this.lowLightAlert) { services[services.length] = this.lowLightAlertService; } + if (this.lowFertilityAlert) { + services[services.length] = this.lowLightFertilityService; + } return services; };