Skip to content

Commit

Permalink
Improving Home Assistant notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
yozik04 committed Aug 26, 2024
1 parent 33510ce commit 5c7f051
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 19 deletions.
19 changes: 11 additions & 8 deletions config/pai.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ import logging
### MQTT Home Assistant Auto Discovery
# MQTT_HOMEASSISTANT_AUTODISCOVERY_ENABLE = False
# MQTT_HOMEASSISTANT_CODE = None
# HOMEASSISTANT_PUBLISH_PARTITION_PROPERTIES = [
# 'target_state',
# 'current_state'
# ]
# HOMEASSISTANT_PUBLISH_ZONE_PROPERTIES = [
# 'open',
# 'tamper'
# ]
#
### Dash App
# MQTT_DASH_PUBLISH = False
Expand All @@ -139,16 +147,11 @@ import logging
#
### Home Assistant Notifications (HASS.io required)
# HOMEASSISTANT_NOTIFICATIONS_ENABLE = False
# HOMEASSISTANT_NOTIFICATIONS_API_URL = "http://supervisor/core/api/services/:domain/:service"
# HOMEASSISTANT_NOTIFICATIONS_API_TOKEN = "" # Long-Lived Access Token. Required if you do not use HA Supervisor
# HOMEASSISTANT_NOTIFICATIONS_LOVELACE_URI = "" # URI to open when notification is clicked
# HOMEASSISTANT_NOTIFICATIONS_NOTIFIER_NAME = 'notify'
# HOMEASSISTANT_NOTIFICATIONS_MIN_EVENT_LEVEL = 'INFO'
# HOMEASSISTANT_PUBLISH_PARTITION_PROPERTIES = [
# 'target_state',
# 'current_state'
# ]
# HOMEASSISTANT_PUBLISH_ZONE_PROPERTIES = [
# 'open',
# 'tamper'
# ]
## Event filtering by tags:
# HOMEASSISTANT_NOTIFICATIONS_EVENT_FILTERS = [ # list of tags to include or exclude see hardware event.py for tag list
# 'live,alarm,-restore', # or
Expand Down
19 changes: 11 additions & 8 deletions paradox/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,6 @@ class Config:
"armed_away": "arm",
"disarmed": "disarm",
},
# Home Assistant Notifications (HASS.io required)
"HOMEASSISTANT_NOTIFICATIONS_ENABLE": False,
"HOMEASSISTANT_NOTIFICATIONS_NOTIFIER_NAME": "notify",
"HOMEASSISTANT_NOTIFICATIONS_MIN_EVENT_LEVEL": (
"INFO",
str,
["DEBUG", "INFO", "WARN", "ERROR", "CRITICAL"],
),
"HOMEASSISTANT_PUBLISH_PARTITION_PROPERTIES": [ # List of partition properties to publish
"target_state",
"current_state",
Expand All @@ -186,6 +178,17 @@ class Config:
"open",
"tamper",
],
# Home Assistant Notifications (HASS.io required)
"HOMEASSISTANT_NOTIFICATIONS_ENABLE": False,
"HOMEASSISTANT_NOTIFICATIONS_API_URL": "http://supervisor/core/api/services/:domain/:service",
"HOMEASSISTANT_NOTIFICATIONS_API_TOKEN": "", # Authentication token used for Home Assistant if not using Supervisor
"HOMEASSISTANT_NOTIFICATIONS_LOVELACE_URI": "", # URI to open when notification is clicked
"HOMEASSISTANT_NOTIFICATIONS_NOTIFIER_NAME": "notify",
"HOMEASSISTANT_NOTIFICATIONS_MIN_EVENT_LEVEL": (
"INFO",
str,
["DEBUG", "INFO", "WARN", "ERROR", "CRITICAL"],
),
"HOMEASSISTANT_NOTIFICATIONS_IGNORE_EVENTS": [], # List of tuples or regexp matching "type,label,property=value,property2=value" eg. [(major, minor), "zone:HOME:entry_delay=True", ...]
"HOMEASSISTANT_NOTIFICATIONS_ALLOW_EVENTS": [], # Same as before but as a white list. Default is use EVENT_FILTERS
"HOMEASSISTANT_NOTIFICATIONS_EVENT_FILTERS": [ # list of tags, property changes to include or exclude. See event.py for tag list
Expand Down
36 changes: 33 additions & 3 deletions paradox/interfaces/text/homeassistant_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ def __init__(self, alarm):
cfg.HOMEASSISTANT_NOTIFICATIONS_MIN_EVENT_LEVEL,
)

self.api_url = "http://supervisor/core/api/services/:domain/:service"
self.token = os.environ.get("SUPERVISOR_TOKEN")
if cfg.HOMEASSISTANT_NOTIFICATIONS_API_TOKEN:
self.token = cfg.HOMEASSISTANT_NOTIFICATIONS_API_TOKEN

if cfg.HOMEASSISTANT_NOTIFICATIONS_API_URL:
self.api_url = cfg.HOMEASSISTANT_NOTIFICATIONS_API_URL

if not self.token:
logger.error(
f'"SUPERVISOR_TOKEN" environment variable must be set to use {__class__.__name__}'
Expand All @@ -35,10 +42,33 @@ def send_message(self, message: str, level: EventLevel):
)
return

notifier_name = cfg.HOMEASSISTANT_NOTIFICATIONS_NOTIFIER_NAME
url = f"http://supervisor/core/api/services/notify/{notifier_name}"
url = self.api_url.replace(":domain", "notify").replace(
":service", cfg.HOMEASSISTANT_NOTIFICATIONS_NOTIFIER_NAME
)

data = {}

if cfg.HOMEASSISTANT_NOTIFICATIONS_LOVELACE_URI:
# iOS
data["url"] = cfg.HOMEASSISTANT_NOTIFICATIONS_LOVELACE_URI
# Android
data["clickAction"] = cfg.HOMEASSISTANT_NOTIFICATIONS_LOVELACE_URI

if level == EventLevel.CRITICAL:
data.update(
{
# iOS
"push": {
"interruption-level": "critical",
},
# Android
"ttl": 0,
"priority": "high",
"channel": "alarm_stream",
}
)

payload = {"message": message, "title": "Paradox", "data": {"level": level}}
payload = {"message": message, "title": "Paradox", "data": data}

headers = {"Authorization": f"Bearer {self.token}"}

Expand Down

2 comments on commit 5c7f051

@camsaway
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
Since this update all my devices and panels sound an alarm if the alarm is triggered and I have to clear them all individually (4 phones and 3 panels.
What is the correct way to disable this audible notification? Do I have to silence alarm_stream notifications on each device or can it be done centrally? ( I dont want to disable the notifications toggle in add-on config in case it stops all event notifications to HASS and not just the push notifications on the stream.

Alternatively, can you exposed the notification level as a configuration parameter?

Regards
Cameron
camsaway@gmail.com

@yozik04
Copy link
Collaborator Author

@yozik04 yozik04 commented on 5c7f051 Nov 10, 2024 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.