mirror of
https://github.com/house-of-abbey/GarminHomeAssistant.git
synced 2025-06-17 20:08:33 +00:00
Send heartrate, floor, and respiration rate values to Home Assistant (#162)
This change sends the heartrate, respiration rate, stepcount, and floors climbed/descended to Home Assistant as mobile app entities. There are a lot more sensors that could potentially be added. I have only tested this on a Vivoactive 4s as that is the only device I own. Let me know what you think and if there are any changes you would like to see.
This commit is contained in:
@ -24,8 +24,8 @@
|
|||||||
| 2.9 | Added an option to enable confirmation vibration so it can be turned off by request of a user. Removed a redundant setting for the alternative Widget version that was not removed previously, and fixed a bug with dereferencing Null. |
|
| 2.9 | Added an option to enable confirmation vibration so it can be turned off by request of a user. Removed a redundant setting for the alternative Widget version that was not removed previously, and fixed a bug with dereferencing Null. |
|
||||||
| 2.10 | Added a user requested feature to slow down the rate of API calls in order to reduce battery wear for a situation where the application is kept open permanently on the device for convenience. Added 4 new devices. |
|
| 2.10 | Added a user requested feature to slow down the rate of API calls in order to reduce battery wear for a situation where the application is kept open permanently on the device for convenience. Added 4 new devices. |
|
||||||
| 2.11 | Bug fix release for menu caching being turned off and language corrections (Czech & Slovenian). |
|
| 2.11 | Bug fix release for menu caching being turned off and language corrections (Czech & Slovenian). |
|
||||||
| 2.12 | Re-enabled Edge 540 and Edge 840 devices which we are unable to support due to simulator issues, but the Edge 840 device has been confirmed as working by a @Petucky. |
|
| 2.12 | Re-enabled Edge 540 and Edge 840 devices which we are unable to support due to simulator issues, but the Edge 840 device has been confirmed as working by a [Petucky](https://github.com/Petucky). |
|
||||||
| 2.13 | Moved the template status queries to Webhooks in order to fix the situation where an account is a non-privileged user. Added telemetry update on activity completion to make automations more timely at the end of an activity. When using a polling delay, there is no longer a startup delay for status updates and an action will trigger an immediate round of updates. |
|
| 2.13 | Moved the template status queries to Webhooks in order to fix the situation where an account is a non-privileged user. Added telemetry update on activity completion to make automations more timely at the end of an activity. When using a polling delay, there is no longer a startup delay for status updates and an action will trigger an immediate round of updates. |
|
||||||
| 2.14 | Cautionary bug fix for the background service code where refactorisation spoilt some API level guard clauses. |
|
| 2.14 | Cautionary bug fix for the background service code where refactorisation spoilt some API level guard clauses. |
|
||||||
| 2.15 | Better support for templates by isolating erroneous returns and marking the menu item. |
|
| 2.15 | Better support for templates by isolating erroneous returns and marking the menu item. |
|
||||||
| 2.16 | Bug fix for lack of phone connection when starting the application. |
|
| 2.16 | Bug fix for lack of phone connection when starting the application. Includes new activity reporting features from [KPWhiver](https://github.com/KPWhiver) covering steps, heart rate, floors climbed and descended, and respiration rate. |
|
||||||
|
@ -124,18 +124,56 @@ class BackgroundServiceDelegate extends System.ServiceDelegate {
|
|||||||
method(:onReturnBatteryUpdate)
|
method(:onReturnBatteryUpdate)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
var activityInfo = ActivityMonitor.getInfo();
|
||||||
|
var heartRate = Activity.getActivityInfo().currentHeartRate;
|
||||||
var data = [
|
var data = [
|
||||||
{
|
{
|
||||||
"state" => System.getSystemStats().battery,
|
"state" => System.getSystemStats().battery,
|
||||||
"type" => "sensor",
|
"type" => "sensor",
|
||||||
"unique_id" => "battery_level"
|
"unique_id" => "battery_level",
|
||||||
|
"icon" => "mdi:battery"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"state" => System.getSystemStats().charging,
|
"state" => System.getSystemStats().charging,
|
||||||
"type" => "binary_sensor",
|
"type" => "binary_sensor",
|
||||||
"unique_id" => "battery_is_charging"
|
"unique_id" => "battery_is_charging",
|
||||||
|
"icon" => System.getSystemStats().charging ? "mdi:battery-plus" : "mdi:battery-minus"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"state" => activityInfo.steps == null ? "unknown" : activityInfo.steps,
|
||||||
|
"type" => "sensor",
|
||||||
|
"unique_id" => "steps_today",
|
||||||
|
"icon" => "mdi:walk"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"state" => heartRate == null ? "unknown" : heartRate,
|
||||||
|
"type" => "sensor",
|
||||||
|
"unique_id" => "heart_rate",
|
||||||
|
"icon" => "mdi:heart-pulse"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"state" => activityInfo.floorsClimbed == null ? "unknown" : activityInfo.floorsClimbed,
|
||||||
|
"type" => "sensor",
|
||||||
|
"unique_id" => "floors_climbed_today",
|
||||||
|
"icon" => "mdi:stairs-up"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"state" => activityInfo.floorsDescended == null ? "unknown" : activityInfo.floorsDescended,
|
||||||
|
"type" => "sensor",
|
||||||
|
"unique_id" => "floors_descended_today",
|
||||||
|
"icon" => "mdi:stairs-down"
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
|
if (ActivityMonitor.Info has :respirationRate) {
|
||||||
|
data.add({
|
||||||
|
"state" => activityInfo.respirationRate == null ? "unknown" : activityInfo.respirationRate,
|
||||||
|
"type" => "sensor",
|
||||||
|
"unique_id" => "respiration_rate",
|
||||||
|
"icon" => "mdi:lungs"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (activity != null) {
|
if (activity != null) {
|
||||||
data.add({
|
data.add({
|
||||||
"state" => activity,
|
"state" => activity,
|
||||||
|
@ -78,18 +78,7 @@ class Settings {
|
|||||||
} else {
|
} else {
|
||||||
// System.println("Settings update(): Doing just sensor creation.");
|
// System.println("Settings update(): Doing just sensor creation.");
|
||||||
// We already have a Webhook ID, so just enable or disable the sensor in Home Assistant.
|
// We already have a Webhook ID, so just enable or disable the sensor in Home Assistant.
|
||||||
// Its a multiple step process, hence starting at step 0.
|
mWebhookManager.registerWebhookSensors();
|
||||||
mWebhookManager.registerWebhookSensor({
|
|
||||||
"device_class" => "battery",
|
|
||||||
"name" => "Battery Level",
|
|
||||||
"state" => System.getSystemStats().battery,
|
|
||||||
"type" => "sensor",
|
|
||||||
"unique_id" => "battery_level",
|
|
||||||
"unit_of_measurement" => "%",
|
|
||||||
"state_class" => "measurement",
|
|
||||||
"entity_category" => "diagnostic",
|
|
||||||
"disabled" => !Settings.isSensorsLevelEnabled()
|
|
||||||
}, 0);
|
|
||||||
}
|
}
|
||||||
if (mIsSensorsLevelEnabled) {
|
if (mIsSensorsLevelEnabled) {
|
||||||
// Create the timed activity
|
// Create the timed activity
|
||||||
|
@ -69,17 +69,7 @@ class WebhookManager {
|
|||||||
if (id != null) {
|
if (id != null) {
|
||||||
Settings.setWebhookId(id);
|
Settings.setWebhookId(id);
|
||||||
// System.println("WebhookManager onReturnRegisterWebhookSensor(): Registering first sensor: Battery Level");
|
// System.println("WebhookManager onReturnRegisterWebhookSensor(): Registering first sensor: Battery Level");
|
||||||
registerWebhookSensor({
|
registerWebhookSensors();
|
||||||
"device_class" => "battery",
|
|
||||||
"name" => "Battery Level",
|
|
||||||
"state" => System.getSystemStats().battery,
|
|
||||||
"type" => "sensor",
|
|
||||||
"unique_id" => "battery_level",
|
|
||||||
"unit_of_measurement" => "%",
|
|
||||||
"state_class" => "measurement",
|
|
||||||
"entity_category" => "diagnostic",
|
|
||||||
"disabled" => !Settings.isSensorsLevelEnabled()
|
|
||||||
}, 0);
|
|
||||||
} else {
|
} else {
|
||||||
// System.println("WebhookManager onReturnRequestWebhookId(): No webhook id in response data.");
|
// System.println("WebhookManager onReturnRequestWebhookId(): No webhook id in response data.");
|
||||||
Settings.unsetIsSensorsLevelEnabled();
|
Settings.unsetIsSensorsLevelEnabled();
|
||||||
@ -125,7 +115,7 @@ class WebhookManager {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function onReturnRegisterWebhookSensor(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String, step as Lang.Number) as Void {
|
function onReturnRegisterWebhookSensor(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String, sensors as Lang.Array<Lang.Object>) as Void {
|
||||||
switch (responseCode) {
|
switch (responseCode) {
|
||||||
case Communications.BLE_HOST_TIMEOUT:
|
case Communications.BLE_HOST_TIMEOUT:
|
||||||
case Communications.BLE_CONNECTION_UNAVAILABLE:
|
case Communications.BLE_CONNECTION_UNAVAILABLE:
|
||||||
@ -172,63 +162,10 @@ class WebhookManager {
|
|||||||
case 201:
|
case 201:
|
||||||
var d = data as Lang.Dictionary;
|
var d = data as Lang.Dictionary;
|
||||||
if ((d.get("success") as Lang.Boolean or Null) != false) {
|
if ((d.get("success") as Lang.Boolean or Null) != false) {
|
||||||
// System.println("WebhookManager onReturnRegisterWebhookSensor(): Success");
|
if (sensors.size() == 0) {
|
||||||
switch (step) {
|
getApp().startUpdates();
|
||||||
case 0:
|
} else {
|
||||||
// System.println("WebhookManager onReturnRegisterWebhookSensor(): Registering next sensor: Battery is Charging");
|
registerWebhookSensor(sensors);
|
||||||
registerWebhookSensor({
|
|
||||||
"device_class" => "battery_charging",
|
|
||||||
"name" => "Battery is Charging",
|
|
||||||
"state" => System.getSystemStats().charging,
|
|
||||||
"type" => "binary_sensor",
|
|
||||||
"unique_id" => "battery_is_charging",
|
|
||||||
"entity_category" => "diagnostic",
|
|
||||||
"disabled" => !Settings.isSensorsLevelEnabled()
|
|
||||||
}, 1);
|
|
||||||
break;
|
|
||||||
case 1:
|
|
||||||
// System.println("WebhookManager onReturnRegisterWebhookSensor(): Registering next sensor: Activity");
|
|
||||||
if (Activity has :getProfileInfo) {
|
|
||||||
var activity = Activity.getProfileInfo().sport;
|
|
||||||
if ((Activity.getActivityInfo() != null) and
|
|
||||||
((Activity.getActivityInfo().elapsedTime == null) or
|
|
||||||
(Activity.getActivityInfo().elapsedTime == 0))) {
|
|
||||||
// Indicate no activity with -1, not part of Garmin's activity codes.
|
|
||||||
// https://developer.garmin.com/connect-iq/api-docs/Toybox/Activity.html#Sport-module
|
|
||||||
activity = -1;
|
|
||||||
}
|
|
||||||
registerWebhookSensor({
|
|
||||||
"name" => "Activity",
|
|
||||||
"state" => activity,
|
|
||||||
"type" => "sensor",
|
|
||||||
"unique_id" => "activity",
|
|
||||||
"disabled" => !Settings.isSensorsLevelEnabled()
|
|
||||||
}, 2);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 2:
|
|
||||||
// System.println("WebhookManager onReturnRegisterWebhookSensor(): Registering next sensor: Sub-Activity");
|
|
||||||
if (Activity has :getProfileInfo) {
|
|
||||||
var sub_activity = Activity.getProfileInfo().subSport;
|
|
||||||
if ((Activity.getActivityInfo() != null) and
|
|
||||||
((Activity.getActivityInfo().elapsedTime == null) or
|
|
||||||
(Activity.getActivityInfo().elapsedTime == 0))) {
|
|
||||||
// Indicate no activity with -1, not part of Garmin's activity codes.
|
|
||||||
// https://developer.garmin.com/connect-iq/api-docs/Toybox/Activity.html#Sport-module
|
|
||||||
sub_activity = -1;
|
|
||||||
}
|
|
||||||
registerWebhookSensor({
|
|
||||||
"name" => "Sub-activity",
|
|
||||||
"state" => sub_activity,
|
|
||||||
"type" => "sensor",
|
|
||||||
"unique_id" => "sub_activity",
|
|
||||||
"disabled" => !Settings.isSensorsLevelEnabled()
|
|
||||||
}, 3);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case 3:
|
|
||||||
getApp().startUpdates();
|
|
||||||
default:
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// System.println("WebhookManager onReturnRegisterWebhookSensor(): Failure");
|
// System.println("WebhookManager onReturnRegisterWebhookSensor(): Failure");
|
||||||
@ -246,7 +183,7 @@ class WebhookManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function registerWebhookSensor(sensor as Lang.Object, step as Lang.Number) {
|
function registerWebhookSensor(sensors as Lang.Array<Lang.Object>) {
|
||||||
var url = Settings.getApiUrl() + "/webhook/" + Settings.getWebhookId();
|
var url = Settings.getApiUrl() + "/webhook/" + Settings.getWebhookId();
|
||||||
// System.println("WebhookManager registerWebhookSensor(): Registering webhook sensor: " + sensor.toString());
|
// System.println("WebhookManager registerWebhookSensor(): Registering webhook sensor: " + sensor.toString());
|
||||||
// System.println("WebhookManager registerWebhookSensor(): URL=" + url);
|
// System.println("WebhookManager registerWebhookSensor(): URL=" + url);
|
||||||
@ -255,7 +192,7 @@ class WebhookManager {
|
|||||||
url,
|
url,
|
||||||
{
|
{
|
||||||
"type" => "register_sensor",
|
"type" => "register_sensor",
|
||||||
"data" => sensor
|
"data" => sensors[0]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
:method => Communications.HTTP_REQUEST_METHOD_POST,
|
:method => Communications.HTTP_REQUEST_METHOD_POST,
|
||||||
@ -263,10 +200,120 @@ class WebhookManager {
|
|||||||
"Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON
|
"Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON
|
||||||
},
|
},
|
||||||
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON,
|
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON,
|
||||||
:context => step
|
:context => sensors.slice(1, null)
|
||||||
},
|
},
|
||||||
method(:onReturnRegisterWebhookSensor)
|
method(:onReturnRegisterWebhookSensor)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function registerWebhookSensors() {
|
||||||
|
var activityInfo = ActivityMonitor.getInfo();
|
||||||
|
var heartRate = Activity.getActivityInfo().currentHeartRate;
|
||||||
|
|
||||||
|
var sensors = [
|
||||||
|
{
|
||||||
|
"device_class" => "battery",
|
||||||
|
"name" => "Battery Level",
|
||||||
|
"state" => System.getSystemStats().battery,
|
||||||
|
"type" => "sensor",
|
||||||
|
"unique_id" => "battery_level",
|
||||||
|
"icon" => "mdi:battery",
|
||||||
|
"unit_of_measurement" => "%",
|
||||||
|
"state_class" => "measurement",
|
||||||
|
"entity_category" => "diagnostic",
|
||||||
|
"disabled" => !Settings.isSensorsLevelEnabled()
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"device_class" => "battery_charging",
|
||||||
|
"name" => "Battery is Charging",
|
||||||
|
"state" => System.getSystemStats().charging,
|
||||||
|
"type" => "binary_sensor",
|
||||||
|
"unique_id" => "battery_is_charging",
|
||||||
|
"icon" => System.getSystemStats().charging ? "mdi:battery-plus" : "mdi:battery-minus",
|
||||||
|
"entity_category" => "diagnostic",
|
||||||
|
"disabled" => !Settings.isSensorsLevelEnabled()
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name" => "Steps today",
|
||||||
|
"state" => activityInfo.steps == null ? "unknown" : activityInfo.steps,
|
||||||
|
"type" => "sensor",
|
||||||
|
"unique_id" => "steps_today",
|
||||||
|
"icon" => "mdi:walk",
|
||||||
|
"state_class" => "total",
|
||||||
|
"disabled" => !Settings.isSensorsLevelEnabled()
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name" => "Heart rate",
|
||||||
|
"state" => heartRate == null ? "unknown" : heartRate,
|
||||||
|
"type" => "sensor",
|
||||||
|
"unique_id" => "heart_rate",
|
||||||
|
"icon" => "mdi:heart-pulse",
|
||||||
|
"unit_of_measurement" => "bpm",
|
||||||
|
"state_class" => "measurement",
|
||||||
|
"disabled" => !Settings.isSensorsLevelEnabled()
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name" => "Floors climbed today",
|
||||||
|
"state" => activityInfo.floorsClimbed == null ? "unknown" : activityInfo.floorsClimbed,
|
||||||
|
"type" => "sensor",
|
||||||
|
"unique_id" => "floors_climbed_today",
|
||||||
|
"icon" => "mdi:stairs-up",
|
||||||
|
"state_class" => "total",
|
||||||
|
"disabled" => !Settings.isSensorsLevelEnabled()
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name" => "Floors descended today",
|
||||||
|
"state" => activityInfo.floorsDescended == null ? "unknown" : activityInfo.floorsDescended,
|
||||||
|
"type" => "sensor",
|
||||||
|
"unique_id" => "floors_descended_today",
|
||||||
|
"icon" => "mdi:stairs-down",
|
||||||
|
"state_class" => "total",
|
||||||
|
"disabled" => !Settings.isSensorsLevelEnabled()
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
|
if (ActivityMonitor.Info has :respirationRate) {
|
||||||
|
sensors.add({
|
||||||
|
"name" => "Respiration rate",
|
||||||
|
"state" => activityInfo.respirationRate == null ? "unknown" : activityInfo.respirationRate,
|
||||||
|
"type" => "sensor",
|
||||||
|
"unique_id" => "respiration_rate",
|
||||||
|
"icon" => "mdi:lungs",
|
||||||
|
"unit_of_measurement" => "bpm",
|
||||||
|
"state_class" => "measurement",
|
||||||
|
"disabled" => !Settings.isSensorsLevelEnabled()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Activity has :getProfileInfo) {
|
||||||
|
var activity = Activity.getProfileInfo().sport;
|
||||||
|
var sub_activity = Activity.getProfileInfo().subSport;
|
||||||
|
if ((Activity.getActivityInfo() != null) and
|
||||||
|
((Activity.getActivityInfo().elapsedTime == null) or
|
||||||
|
(Activity.getActivityInfo().elapsedTime == 0))) {
|
||||||
|
// Indicate no activity with -1, not part of Garmin's activity codes.
|
||||||
|
// https://developer.garmin.com/connect-iq/api-docs/Toybox/Activity.html#Sport-module
|
||||||
|
activity = -1;
|
||||||
|
sub_activity = -1;
|
||||||
|
}
|
||||||
|
sensors.add({
|
||||||
|
"name" => "Activity",
|
||||||
|
"state" => activity,
|
||||||
|
"type" => "sensor",
|
||||||
|
"unique_id" => "activity",
|
||||||
|
"disabled" => !Settings.isSensorsLevelEnabled()
|
||||||
|
});
|
||||||
|
sensors.add({
|
||||||
|
"name" => "Sub-activity",
|
||||||
|
"state" => sub_activity,
|
||||||
|
"type" => "sensor",
|
||||||
|
"unique_id" => "sub_activity",
|
||||||
|
"disabled" => !Settings.isSensorsLevelEnabled()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
registerWebhookSensor(sensors);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user