Skip to content

Commit

Permalink
Added better notification format
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabefire committed Oct 14, 2024
1 parent f0f5c1a commit 1ff18a1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
1 change: 1 addition & 0 deletions custom_components/keymaster/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 13 additions & 0 deletions custom_components/keymaster/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
9 changes: 3 additions & 6 deletions custom_components/keymaster/keymaster_common.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
9 changes: 3 additions & 6 deletions custom_components/keymaster/keymaster_common_child.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
9 changes: 7 additions & 2 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Test keymaster helpers """
"""Test keymaster helpers"""

from unittest.mock import patch

Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1ff18a1

Please sign in to comment.