From 1ff18a1af869ea34094499751caab974a5f4933f Mon Sep 17 00:00:00 2001 From: Gabefire <33893811+Gabefire@users.noreply.github.com> Date: Sun, 13 Oct 2024 15:55:57 -0500 Subject: [PATCH] Added better notification format --- custom_components/keymaster/const.py | 1 + custom_components/keymaster/helpers.py | 13 +++++++++++++ custom_components/keymaster/keymaster_common.yaml | 9 +++------ .../keymaster/keymaster_common_child.yaml | 9 +++------ tests/test_helpers.py | 9 +++++++-- 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/custom_components/keymaster/const.py b/custom_components/keymaster/const.py index 20791d18..efd4956b 100644 --- a/custom_components/keymaster/const.py +++ b/custom_components/keymaster/const.py @@ -26,6 +26,7 @@ ATTR_ACTION_TEXT = "action_text" ATTR_CODE_SLOT_NAME = "code_slot_name" ATTR_NOTIFICATION_SOURCE = "notification_source" +ATTR_DATE_TIME_NOW = "date_time_now" # Attributes ATTR_CODE_SLOT = "code_slot" diff --git a/custom_components/keymaster/helpers.py b/custom_components/keymaster/helpers.py index b81486e3..31655b6a 100644 --- a/custom_components/keymaster/helpers.py +++ b/custom_components/keymaster/helpers.py @@ -5,6 +5,7 @@ import logging import os from typing import Dict, List, Optional, Tuple +import time from homeassistant.components.automation import DOMAIN as AUTO_DOMAIN from homeassistant.components.input_boolean import DOMAIN as IN_BOOL_DOMAIN @@ -39,6 +40,7 @@ ATTR_ACTION_CODE, ATTR_ACTION_TEXT, ATTR_CODE_SLOT_NAME, + ATTR_DATE_TIME_NOW, ATTR_NAME, ATTR_NOTIFICATION_SOURCE, CHILD_LOCKS, @@ -257,6 +259,7 @@ def handle_zwave_js_event(hass: HomeAssistant, config_entry: ConfigEntry, evt: E if code_slot_name_state is not None else "" ), + ATTR_DATE_TIME_NOW: get_friendly_datetime(), }, ) return @@ -360,6 +363,7 @@ def handle_state_change( if code_slot_name_state is not None else "" ), + ATTR_DATE_TIME_NOW: get_friendly_datetime(), }, ) return @@ -424,3 +428,12 @@ async def async_reload_package_platforms(hass: HomeAssistant) -> bool: except ServiceNotFound: return False return True + + +def get_friendly_datetime(): + """Get the current time with local timezone information and format to hh:mm PM/AM abr_month_name DD""" + local_time = time.localtime() + + friendly_time = time.strftime("%I:%M %p %b %d", local_time) + + return friendly_time diff --git a/custom_components/keymaster/keymaster_common.yaml b/custom_components/keymaster/keymaster_common.yaml index f804094c..eea2fbf3 100644 --- a/custom_components/keymaster/keymaster_common.yaml +++ b/custom_components/keymaster/keymaster_common.yaml @@ -161,8 +161,7 @@ automation: action: - service: script.keymaster_LOCKNAME_manual_notify data_template: - title: CASE_LOCK_NAME - message: "{{ trigger.event.data.action_text }} {% if trigger.event.data.code_slot > 0 %}({{ trigger.event.data.code_slot_name }}){% endif %}" + message: "LOCKNAME {{ trigger.event.data.state }}{% if trigger.event.data.code_slot > 0 %} by {{ trigger.event.data.code_slot_name }}{% endif %} {{ trigger.event.data.date_time_now }}" - alias: keymaster_CASE_LOCK_NAME User Notifications id: keymaster_CASE_LOCK_NAME User Notifications @@ -182,8 +181,7 @@ automation: action: - service: script.keymaster_LOCKNAME_manual_notify data_template: - title: CASE_LOCK_NAME - message: "{{ trigger.event.data.action_text }} ({{ trigger.event.data.code_slot_name }})" + message: "LOCKNAME {{ trigger.event.data.state }}{% if trigger.event.data.code_slot > 0 %} by {{ trigger.event.data.code_slot_name }}{% endif %} {{ trigger.event.data.date_time_now }}" - alias: keymaster_CASE_LOCK_NAME Door Open and Close id: keymaster_CASE_LOCK_NAME Door Open and Close @@ -199,8 +197,7 @@ automation: action: - service: script.keymaster_LOCKNAME_manual_notify data_template: - title: CASE_LOCK_NAME - message: "{% if trigger.to_state.state == 'on' %}Door Opened{% else %}Door Closed{% endif %}" + message: "LOCKNAME {% if trigger.to_state.state == 'on' %}opened{% else %}closed{% endif %} {{ trigger.event.data.date_time_now }}" - alias: keymaster_CASE_LOCK_NAME Changed Code id: keymaster_CASE_LOCK_NAME Changed Code diff --git a/custom_components/keymaster/keymaster_common_child.yaml b/custom_components/keymaster/keymaster_common_child.yaml index 7d9eac3e..0fa4d6f2 100644 --- a/custom_components/keymaster/keymaster_common_child.yaml +++ b/custom_components/keymaster/keymaster_common_child.yaml @@ -161,8 +161,7 @@ automation: action: - service: script.keymaster_LOCKNAME_manual_notify data_template: - title: CASE_LOCK_NAME - message: "{{ trigger.event.data.action_text }} {% if trigger.event.data.code_slot > 0 %}({{ trigger.event.data.code_slot_name }}){% endif %}" + message: "LOCKNAME {{ trigger.event.data.state }}{% if trigger.event.data.code_slot > 0 %} by {{ trigger.event.data.code_slot_name }}{% endif %} {{ trigger.event.data.date_time_now }}" - alias: keymaster_CASE_LOCK_NAME User Notifications id: keymaster_CASE_LOCK_NAME User Notifications @@ -182,8 +181,7 @@ automation: action: - service: script.keymaster_LOCKNAME_manual_notify data_template: - title: CASE_LOCK_NAME - message: "{{ trigger.event.data.action_text }} ({{ trigger.event.data.code_slot_name }})" + message: "LOCKNAME {{ trigger.event.data.state }}{% if trigger.event.data.code_slot > 0 %} by {{ trigger.event.data.code_slot_name }}{% endif %} {{ trigger.event.data.date_time_now }}" - alias: keymaster_CASE_LOCK_NAME Door Open and Close id: keymaster_CASE_LOCK_NAME Door Open and Close @@ -199,8 +197,7 @@ automation: action: - service: script.keymaster_LOCKNAME_manual_notify data_template: - title: CASE_LOCK_NAME - message: "{% if trigger.to_state.state == 'on' %}Door Opened{% else %}Door Closed{% endif %}" + message: "LOCKNAME {% if trigger.to_state.state == 'on' %}opened{% else %}closed{% endif %} {{ trigger.event.data.date_time_now }}" - alias: keymaster_CASE_LOCK_NAME Changed Code id: keymaster_CASE_LOCK_NAME Changed Code diff --git a/tests/test_helpers.py b/tests/test_helpers.py index f4d8141f..1f4b70ba 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -1,4 +1,4 @@ -""" Test keymaster helpers """ +"""Test keymaster helpers""" from unittest.mock import patch @@ -9,11 +9,15 @@ ATTR_ACTION_TEXT, ATTR_CODE_SLOT, ATTR_CODE_SLOT_NAME, + ATTR_DATE_TIME_NOW, ATTR_NAME, DOMAIN, EVENT_KEYMASTER_LOCK_STATE_CHANGED, ) -from custom_components.keymaster.helpers import delete_lock_and_base_folder +from custom_components.keymaster.helpers import ( + delete_lock_and_base_folder, + get_friendly_datetime, +) from homeassistant.const import ( ATTR_STATE, EVENT_HOMEASSISTANT_STARTED, @@ -142,6 +146,7 @@ async def test_handle_state_change_zwave_js( assert events[0].data[ATTR_ACTION_TEXT] == "Keypad unlock operation" assert events[0].data[ATTR_CODE_SLOT] == 3 assert events[0].data[ATTR_CODE_SLOT_NAME] == "" + assert events[0].data[ATTR_DATE_TIME_NOW] == get_friendly_datetime() assert events_js[0].data["type"] == 6 assert events_js[0].data["event"] == 5