Browse Source

Add birthday notify script with callbacks

Joachim M. Giæver 1 year ago
parent
commit
0506c70c2e
1 changed files with 128 additions and 0 deletions
  1. 128 0
      General/birthdays.yaml

+ 128 - 0
General/birthdays.yaml

@@ -0,0 +1,128 @@
+# Callback script when anyone presses «Send SMS».
+script:
+  birthday_wishes:
+    mode: parallel
+    sequence:
+      - variables:
+          blang: "{{ lang if lang is defined else none }}"
+          bname: "{{ name|lower|title }}"
+          bage: "{{ age|string + {1: 'st', 2: 'nd', 3: 'rd'}.get(4 if 10 <= age|int % 100 < 20 else age|int % 10, 'th') }}"
+      
+      - service: "{{ notify_device }}"
+        data:
+          message: command_activity
+          data:
+            intent_action: "android.intent.action.SENDTO"
+            intent_uri: >-
+              sms:{{ number }}?body={{-
+                "🎼 «Hurra for deg som fyller ditt år, ja - deg vil jeg gratulere» 🎶 🎉 \n\n"
+              }}{%- if blang == 'no_nb' -%}
+                Gratulere med {{ age|default('') }}årsdagen, {{ bname }}! 🎂🎁 Håpe du får en fin dag 🥳
+                {%- else -%}
+                Happy {{ bage ~ ' ' if age else '' }}birthday, {{ bname }}! 🎂🎁 Hope'll get a beautiful day 🥳
+              {%- endif -%}
+
+# Require: https://github.com/jmgiaever/home-assistant-public-config/blob/master/General/notify_device.yaml
+automation:
+  - alias: "Birthday: Notify on birthday!"
+    trigger: 
+      - platform: calendar
+        event: start
+        entity_id: calendar.ical_birthdays
+        offset:
+          hours: 10
+    action: 
+      - variables:
+          birthdays: >-
+            {% set birthday = namespace(calendars=[]) %}
+            {% for ical in integration_entities('ical') if 
+              ical.startswith('sensor.ical_birthdays_event') and not is_state(ical, 'unavailable') and 
+              state_attr(ical, 'start').strftime('%d%m%y') == now().strftime('%d%m%y') 
+            %} 
+              {% set birthday.calendars = birthday.calendars + [ ical ] %}
+            {% endfor %}
+            {{ birthday.calendars }}
+          rexp_phone: '(?:(?<=phone)|(?<=\d,)):?\s?(\+?\d[ \d]+\d)'
+          rexp_born: '.*?born:?\s*([\d]{2,4})'
+          rexp_lang: '.*?lang:?\s*([a-zA-Z\_]+)'
+          langs:
+            no_nb: '+47'
+          notify_devices: >-
+            {{ state_attr('group.owners_notify_devices', 'entity_id')|list }}
+      - repeat:
+          for_each: "{{ birthdays }}"
+          sequence:
+            - variables:
+                name: "{{ (states(repeat.item).split('-')[:-1]|join('-')|trim).split(' ')[0]|lower|title }}"
+                details: '{{ state_attr(repeat.item, "description")|striptags }}'
+                phones: >-
+                  {{ details|regex_findall(rexp_phone, ignorecase=true) }}
+                born: >-
+                  {{ details|regex_findall_index(rexp_born, ignorecase=true) if
+                    details is match(rexp_born, ignorecase=true) else
+                  false }}
+                lang: >-
+                  {% set lang = namespace(lang=false) %}
+                  {% set lang.lang = details|regex_findall_index(rexp_lang, ignorecase=true) if
+                    details is match(rexp_lang, ignorecase=true) else
+                    False
+                  %}
+                  {% if lang.lang is false %}
+                    {% for klang in langs.keys() %}
+                      {% for phone in phones if phone.startswith(langs[klang]) %}
+                        {% set lang.lang = klang %}
+                      {% endfor %}
+                    {% endfor %}
+                  {% endif %}
+                  {{ lang.lang if lang.lang else 'eng' }}
+                age: >-
+                  {{ false if born is false else
+                    100 + now().strftime('%y')|int - born if born <= 100 else
+                    now().strftime('%Y')|int - born|int
+                  }}
+                index: "{{ repeat.index }}"
+                data:
+                  title: "It is {{ name }}'s birthday. 🎁"
+                  message: >-
+                    {% if age is not false %}
+                      {{ name }} is turning {{ age }} today.
+                    {% else %}
+                      {{ name }} is getting one year older today.
+                    {% endif %}
+                    Send your congratulations 🎉 to let him/her
+                    know that you appreciate them. 🥳
+                  data:
+                    actions:
+                      - action: SEND_SMS
+                        title: Send SMS 💌
+                      - action: DISMISS
+                        title: Dismiss 💩
+                    color: '#ebb1bb'
+                    group: "birthday-notifications"
+                    channel: "Birthdays"
+                    importance: high
+                    icon_url: /local/icons/notifications/cake.png
+                    notification_icon: "mdi:cake"
+                  
+            - repeat:
+                for_each: "{{ notify_devices }}"
+                sequence:
+                  service: script.turn_on
+                  target:
+                    entity_id: script.notify_device
+                  data:
+                    variables:
+                      notify_device: "{{ repeat.item }}"
+                      index: "{{ index }}-{{ repeat.index }}"
+                      timeout:
+                        hours: 14
+                      data: "{{ data }}"
+                      action_scripts: # Callback handles for «data.actions»
+                        SEND_SMS:
+                          script: script.birthday_wishes
+                          variables:
+                            lang: "{{ lang }}"
+                            name: "{{ name }}"
+                            age: "{{ age }}"
+                            notify_device: "{{ repeat.item }}"
+                            number: "{{ phones|join(',') }}"