Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fertility level warning config #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
57 changes: 55 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}
});

Expand Down Expand Up @@ -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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines are for the low battery status of the humidityAlertService. Here, moisture should not be used. The previous behavior was correct. The same applies to the other changes made.

}

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);
}
}
});
Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -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)
Expand All @@ -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' });

/*
Expand Down Expand Up @@ -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;
};