diff --git a/blueprints/automation/consumable_notification.yaml b/blueprints/automation/consumable_notification.yaml index 6c8e7d4..ba2f84f 100644 --- a/blueprints/automation/consumable_notification.yaml +++ b/blueprints/automation/consumable_notification.yaml @@ -1,9 +1,11 @@ blueprint: name: Consumable Low/Depleted Notification description: >- - Create a persistent notification when a consumable reaches its warning - threshold, and update it when the consumable is fully depleted. - The notification is dismissed when the consumable is replaced. + Create a notification when a consumable reaches its warning threshold, + and update it when the consumable is fully depleted. The notification + is dismissed when the consumable is replaced. Supports persistent + notifications (default), mobile app notifications, notification groups, + and Android/Fire TV. domain: automation input: consumable_sensor: @@ -25,6 +27,26 @@ blueprint: min: 0 max: 365 unit_of_measurement: days + notify_device: + name: Mobile Device (Optional) + description: >- + Select a device running the official Home Assistant app to receive + notifications. Leave empty to use persistent notifications in the + Home Assistant UI. + default: false + selector: + device: + filter: + - integration: mobile_app + notify_group: + name: Notification Group or Android/Fire TV (Optional) + description: >- + The name of the notification group or TV to send notifications to + (e.g., "notify.all_phones" or "living_room_tv"). If set, this + overrides the Mobile Device selection above. + default: "" + selector: + text: mode: restart @@ -37,6 +59,13 @@ triggers: variables: consumable_sensor: !input consumable_sensor warning_days_override: !input warning_days_override + notify_device: !input notify_device + notify_group: !input notify_group + notify_group_target: >- + {{ notify_group | lower | regex_replace('^notify\\.', '') | replace(' ', '_') }} + use_notify_group: "{{ notify_group_target | length > 0 }}" + use_notify_device: "{{ not use_notify_group and notify_device is defined and notify_device != false }}" + use_persistent: "{{ not use_notify_group and not use_notify_device }}" device_name: "{{ device_attr(consumable_sensor, 'name') }}" consumable_name: "{{ state_attr(consumable_sensor, 'consumable_name') }}" display_name: "{{ device_name }} {{ consumable_name }}" @@ -47,6 +76,7 @@ variables: is_depleted: "{{ days_remaining == 0 }}" is_normal: "{{ days_remaining > warning_days }}" device_link: "[{{ device_name }}](/config/devices/device/{{ device_id(consumable_sensor) }})" + device_url: "/config/devices/device/{{ device_id(consumable_sensor) }}" actions: - choose: @@ -54,24 +84,95 @@ actions: - condition: template value_template: "{{ is_depleted }}" sequence: - - action: persistent_notification.create - data: - title: "{{ display_name }} Needs Replacement" - message: "{{ device_link }} {{ consumable_name }} is due for replacement." - notification_id: "{{ notification_id }}" + - choose: + - conditions: + - condition: template + value_template: "{{ use_notify_group }}" + sequence: + - action: "notify.{{ notify_group_target }}" + data: + title: "{{ display_name }} Needs Replacement" + message: "{{ device_name }} {{ consumable_name }} is due for replacement." + data: + tag: "{{ notification_id }}" + clickAction: "{{ device_url }}" + - conditions: + - condition: template + value_template: "{{ use_notify_device }}" + sequence: + - device_id: !input notify_device + domain: mobile_app + type: notify + title: "{{ display_name }} Needs Replacement" + message: "{{ device_name }} {{ consumable_name }} is due for replacement." + data: + tag: "{{ notification_id }}" + clickAction: "{{ device_url }}" + default: + - action: persistent_notification.create + data: + title: "{{ display_name }} Needs Replacement" + message: "{{ device_link }} {{ consumable_name }} is due for replacement." + notification_id: "{{ notification_id }}" - conditions: - condition: template value_template: "{{ is_warning }}" sequence: - - action: persistent_notification.create - data: - title: "{{ display_name }} Replacement Soon" - message: "{{ device_link }} {{ consumable_name }} has {{ days_remaining }} days remaining." - notification_id: "{{ notification_id }}" + - choose: + - conditions: + - condition: template + value_template: "{{ use_notify_group }}" + sequence: + - action: "notify.{{ notify_group_target }}" + data: + title: "{{ display_name }} Replacement Soon" + message: "{{ device_name }} {{ consumable_name }} has {{ days_remaining }} days remaining." + data: + tag: "{{ notification_id }}" + clickAction: "{{ device_url }}" + - conditions: + - condition: template + value_template: "{{ use_notify_device }}" + sequence: + - device_id: !input notify_device + domain: mobile_app + type: notify + title: "{{ display_name }} Replacement Soon" + message: "{{ device_name }} {{ consumable_name }} has {{ days_remaining }} days remaining." + data: + tag: "{{ notification_id }}" + clickAction: "{{ device_url }}" + default: + - action: persistent_notification.create + data: + title: "{{ display_name }} Replacement Soon" + message: "{{ device_link }} {{ consumable_name }} has {{ days_remaining }} days remaining." + notification_id: "{{ notification_id }}" - conditions: - condition: template value_template: "{{ is_normal }}" sequence: - - action: persistent_notification.dismiss - data: - notification_id: "{{ notification_id }}" + - choose: + - conditions: + - condition: template + value_template: "{{ use_notify_group }}" + sequence: + - action: "notify.{{ notify_group_target }}" + data: + message: "clear_notification" + data: + tag: "{{ notification_id }}" + - conditions: + - condition: template + value_template: "{{ use_notify_device }}" + sequence: + - device_id: !input notify_device + domain: mobile_app + type: notify + message: "clear_notification" + data: + tag: "{{ notification_id }}" + default: + - action: persistent_notification.dismiss + data: + notification_id: "{{ notification_id }}" diff --git a/custom_components/consumable_tracker/manifest.json b/custom_components/consumable_tracker/manifest.json index ae22fab..2bd0a88 100644 --- a/custom_components/consumable_tracker/manifest.json +++ b/custom_components/consumable_tracker/manifest.json @@ -7,5 +7,5 @@ "iot_class": "calculated", "issue_tracker": "https://github.com/thetic/hass-consumable-tracker/issues", "requirements": [], - "version": "0.2.4" + "version": "0.3.0" }