Skip to content

Commit

Permalink
Added unit sitorr for air pressure in mmhg
Browse files Browse the repository at this point in the history
  • Loading branch information
naofireblade committed Feb 29, 2020
1 parent d539465 commit d34e530
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 26 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,7 @@

## 3.1.0
* Added support for config-ui-x
* Added custom tresholds for apple home sensors
* Added custom tresholds for apple home sensors

## 3.1.1
* Added unit sitorr for air pressure in mmhg
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ Option to hide the Now accessory if you only need forecasts. Default is `true` w
**units**
Conversions used for reporting values. The default is `"metric"`. The options are:
`"si"` or `"metric"`
`"sitorr"` to report air pressure in mmhg
`"us"` or `"imperial"`
`"ca"` to report wind speeds in km/h instead of m/s
`"uk"` to report visibility in miles and wind speeds in km/h instead of m/s
Expand Down
4 changes: 4 additions & 0 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
"title": "Metric (m/s)",
"const": "si"
},
{
"title": "Metric (m/s + mmhg)",
"const": "sitorr"
},
{
"title": "Metric (km/h)",
"const": "ca"
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ WeatherPlusPlatform.prototype = {
{
accessory.AirPressureService.setCharacteristic(Characteristic.OccupancyDetected, convertedValue >= config.tresholdAirPressure);
}
accessory.AirPressureService.setCharacteristic(Characteristic.Name, "Air Pressure: " + convertedValue + "hPa");
accessory.AirPressureService.setCharacteristic(Characteristic.Name, "Air Pressure: " + convertedValue + " " + accessory.AirPressureService.unit);
accessory.AirPressureService.value = convertedValue; // Save value to use in history
}
else if (name === "CloudCover")
Expand Down Expand Up @@ -362,7 +362,7 @@ WeatherPlusPlatform.prototype = {
}
else if (name === "Visibility")
{
accessory.VisibilityService.setCharacteristic(Characteristic.Name, "Visibility: " + convertedValue + accessory.VisibilityService.unit);
accessory.VisibilityService.setCharacteristic(Characteristic.Name, "Visibility: " + convertedValue + " " + accessory.VisibilityService.unit);
}
else if (name === "WindDirection")
{
Expand All @@ -378,7 +378,7 @@ WeatherPlusPlatform.prototype = {
{
accessory.WindSpeedService.setCharacteristic(Characteristic.OccupancyDetected, convertedValue >= config.tresholdWindSpeed);
}
accessory.WindSpeedService.setCharacteristic(Characteristic.Name, "Wind Speed: " + convertedValue + accessory.WindSpeedService.unit);
accessory.WindSpeedService.setCharacteristic(Characteristic.Name, "Wind Speed: " + convertedValue + " " + accessory.WindSpeedService.unit);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "homebridge-weather-plus",
"version": "3.1.0",
"version": "3.1.1",
"description": "A comprehensive weather plugin for homekit with current observations, forecasts and history.",
"license": "MIT",
"keywords": [
Expand Down
59 changes: 38 additions & 21 deletions util/characteristics.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,21 @@ let CustomCharacteristic = {};

// A more accurate way of rounding decimals in Javascript compared to the usual multiply & divide
// See https://www.jacklmoore.com/notes/rounding-in-javascript/ for explanation
function round(value, decimals) {
return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}
function round(value, decimals)
{
return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals);
}

module.exports = function (Characteristic, units)
{

units = // rainfail temperature visibility windspeed
units = // rainfail temperature visibility windspeed airpressure
{
ca: 'ca' // mm celsius kilometers km/hour
, imperial: 'imperial' // inches fahrenheit miles miles/hour
, si: 'si' // mm celsius kilometers m/second
, uk: 'uk' // mm celsius miles miles/hour
ca: 'ca' // mm celsius kilometers km/hour hPa
, imperial: 'imperial' // inches fahrenheit miles miles/hour hPa
, si: 'si' // mm celsius kilometers m/second hPa
, sitorr: 'sitorr' // mm celsius kilometers m/second mmhg
, uk: 'uk' // mm celsius miles miles/hour hPa

, metric: 'si'
, us: 'imperial'
Expand Down Expand Up @@ -90,11 +92,11 @@ module.exports = function (Characteristic, units)

var km2mi = (km) =>
{
return Math.round(km / 1.60934);
return Math.round(km / 1.60934);
};
var visibilityProps = (max) =>
{
var range = ((units === 'si') || (units === 'ca')) ? {unit: 'km', maxValue: max, minValue: 0}
var range = ((units === 'si') || (units === 'sitorr') || (units === 'ca')) ? {unit: 'km', maxValue: max, minValue: 0}
: {unit: 'mi', maxValue: km2mi(max), minValue: 0};

return underscore.extend(
Expand All @@ -106,7 +108,7 @@ module.exports = function (Characteristic, units)
};
var visibilityValue = (val) =>
{
return ((units === 'si') || (units === 'ca')) ? val : km2mi(val);
return ((units === 'si') || (units === 'sitorr') || (units === 'ca')) ? val : km2mi(val);
};

var mtos2kmh = (m) =>
Expand All @@ -119,7 +121,7 @@ module.exports = function (Characteristic, units)
};
var windspeedProps = (max) =>
{
var range = (units === 'si') ? {unit: 'm/s', maxValue: max, minValue: 0}
var range = ((units === 'si') || (units === 'sitorr')) ? {unit: 'm/s', maxValue: max, minValue: 0}
: (units === 'ca') ? {unit: 'km/h', maxValue: mtos2kmh(max), minValue: 0}
: {unit: 'mph', maxValue: mtos2mih(max), minValue: 0};

Expand All @@ -132,25 +134,40 @@ module.exports = function (Characteristic, units)
};
var windspeedValue = (val) =>
{
return (units === 'si') ? val
return ((units === 'si') || (units === 'sitorr')) ? val
: (units === 'ca') ? mtos2kmh(val)
: mtos2mih(val);
};

let hpa2mmhg = (hpa) =>
{
return (round(hpa / 1.3332, 0));
};
let airpressureProps = (min, max) =>
{
let range = (units === 'sitorr') ? {unit: 'mmhg', maxValue: hpa2mmhg(max), minValue: hpa2mmhg(min)}
: {unit: 'hPa', maxValue: max, minValue: min};

return underscore.extend(
{
format: Characteristic.Formats.UINT8
, minStep: 1
, perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY]
}, range);
};
let airpressureValue = (val) =>
{
return (units === 'sitorr') ? hpa2mmhg(val) : val;
};

CustomCharacteristic.AirPressure = function ()
{
Characteristic.call(this, 'Air Pressure', CustomUUID.AirPressure);
this.setProps({
format: Characteristic.Formats.UINT16,
unit: "hPa",
maxValue: 1100,
minValue: 700,
minStep: 1,
perms: [Characteristic.Perms.READ, Characteristic.Perms.NOTIFY]
});
this.setProps(airpressureProps(700, 1100));
this.value = this.getDefaultValue();
};
inherits(CustomCharacteristic.AirPressure, Characteristic);
CustomCharacteristic.AirPressure._unitvalue = airpressureValue;

CustomCharacteristic.CloudCover = function ()
{
Expand Down
5 changes: 5 additions & 0 deletions util/compatibility.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ const createService = function (that, name, Service, CustomCharacteristic)
{
if (name === "AirPressure")
{
// Get unit
let temporaryService = new Service.OccupancySensor("Temporary");
temporaryService.addCharacteristic(CustomCharacteristic.AirPressure);

that.AirPressureService = new Service.OccupancySensor("Air Pressure", "Air Pressure");
that.AirPressureService.unit = temporaryService.getCharacteristic(CustomCharacteristic.AirPressure).props.unit;
}
if (name === "CloudCover")
{
Expand Down

0 comments on commit d34e530

Please sign in to comment.