Skip to content

Commit

Permalink
feat: add a message on the bus for new alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
erikarenhill committed Sep 2, 2024
1 parent 56e9e11 commit ae777bf
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 3 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,32 @@ After installation and configuration, you'll have the following entities:
- `switch.vma_test_mode`: Toggles between the live API and the test API.

You can use these entities in your automations, scripts, and Lovelace UI.


## HOW TO SEND NOTIFICATION

Set up a new automation with trigger EVENT, type in "vma_new_alert" and leave the data field empty.

On actions, add a notification, then move over to manual yaml editing mode and copy paste this:

data:
title: 'VMA Alert: {{ trigger.event.data.info[0].event }}'
message: >
{{ trigger.event.data.info[0].description }} Urgency: {{
trigger.event.data.info[0].urgency }} Severity: {{
trigger.event.data.info[0].severity }} Certainty: {{
trigger.event.data.info[0].certainty }} Affected Area: {{
trigger.event.data.info[0].area[0].areaDesc }}
action: notify.YOUR_NOTIFICATION_SERVICE_HERE # <--------- dont forget to change this

## HOW TO TEST

Go to developer tools, actions then trigger VMA Alert: simulate_vma_event
this will send a fake event on the bus for the vma_new_alert

OR

Switch on the Test mode, exposed as a control switch on the device for the integration.



9 changes: 8 additions & 1 deletion custom_components/vma_alert_hass/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def __init__(self, hass: HomeAssistant, poll_interval: int):
)
self.data = {"latest_alert": None, "active_alerts": [], "last_update": None}
self.test_mode = False
self.last_alert_id = None

async def _async_update_data(self):
"""Fetch data from API."""
Expand All @@ -71,8 +72,14 @@ async def _async_update_data(self):

_LOGGER.debug("Processed %d active alerts", len(active_alerts))

latest_alert = active_alerts[0] if active_alerts else None
if latest_alert and latest_alert["identifier"] != self.last_alert_id:
self.last_alert_id = latest_alert["identifier"]
self.hass.bus.async_fire("vma_new_alert", latest_alert)
_LOGGER.info("New VMA alert detected: %s", latest_alert["identifier"])

return {
"latest_alert": active_alerts[0] if active_alerts else None,
"latest_alert": latest_alert,
"active_alerts": active_alerts,
"last_update": dt_util.utcnow()
}
Expand Down
2 changes: 1 addition & 1 deletion custom_components/vma_alert_hass/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ async def simulate_vma_event(call: ServiceCall) -> None:
}]
}

hass.bus.fire("vma_alert_event", simulated_data)
hass.bus.fire("vma_new_alert", simulated_data)

hass.services.async_register(DOMAIN, "simulate_vma_event", simulate_vma_event)
63 changes: 62 additions & 1 deletion sample-response.json
Original file line number Diff line number Diff line change
@@ -1 +1,62 @@
{ "timestamp": "2024-09-02T17:26:32+02:00", "alerts": [ { "identifier": "SRCAP20240902172321T", "sender": "https://vmaapi.sr.se/api/v2", "sent": "2024-09-02T17:23:21+02:00", "status": "Test", "msgType": "Alert", "scope": "Public", "references": null, "incidents": "SRVMA20240902172321T", "info": [ { "language": "sv-SE", "category": "Safety", "event": "Viktigt meddelande till allmänheten (VMA)", "urgency": "Immediate", "severity": "Severe", "certainty": "Observed", "senderName": "Sveriges Radio", "description": "TEST - Detta är sändningsledningen på Sveriges Radio med ett testmeddelande för utskick av VMA-meddelanden. Berört område: Kiruna kommun (Norrbottens län). Ingen åtgärds krävs – Slut på testet.", "web": "https://sverigesradio.se/artikel/vma-vad-ar-det", "area": [ { "areaDesc": "Kiruna kommun (Norrbottens län)", "geocode": [ { "valueName": "Kommun", "value": "2584" } ] } ] }, { "language": "en-US", "category": "Safety", "event": "Important Public Announcement (IPA)", "urgency": "Immediate", "severity": "Severe", "certainty": "Observed", "senderName": "Sveriges Radio", "description": "TEST - This is the broadcast management at Swedish Radio with a test message for sending VMA messages. Area concerned: Kiruna municipality (Norrbottens county). No action required - End of test.", "web": "https://sverigesradio.se/artikel/vma-vad-ar-det", "area": [ { "areaDesc": "Kiruna kommun (Norrbottens län)", "geocode": [ { "valueName": "Kommun", "value": "2584" } ] } ] } ] }, { "identifier": "SRCAP20240902171224T", "sender": "https://vmaapi.sr.se/api/v2", "sent": "2024-09-02T17:12:24+02:00", "status": "Test", "msgType": "Cancel", "scope": "Public", "references": "https://vmaapi.sr.se/api/v2,SRCAP20240902165224T,2024-09-02T16:52:24+02:00", "incidents": "SRVMA20240902165224T", "info": null } ] }
{
"timestamp": "2024-09-02T17:26:32+02:00",
"alerts": [
{
"identifier": "SRCAP20240902172321T",
"sender": "https://vmaapi.sr.se/api/v2",
"sent": "2024-09-02T17:23:21+02:00",
"status": "Test",
"msgType": "Alert",
"scope": "Public",
"references": null,
"incidents": "SRVMA20240902172321T",
"info": [
{
"language": "sv-SE",
"category": "Safety",
"event": "Viktigt meddelande till allmänheten (VMA)",
"urgency": "Immediate",
"severity": "Severe",
"certainty": "Observed",
"senderName": "Sveriges Radio",
"description": "TEST - Detta är sändningsledningen på Sveriges Radio med ett testmeddelande för utskick av VMA-meddelanden. Berört område: Kiruna kommun (Norrbottens län). Ingen åtgärds krävs – Slut på testet.",
"web": "https://sverigesradio.se/artikel/vma-vad-ar-det",
"area": [
{
"areaDesc": "Kiruna kommun (Norrbottens län)",
"geocode": [{ "valueName": "Kommun", "value": "2584" }]
}
]
},
{
"language": "en-US",
"category": "Safety",
"event": "Important Public Announcement (IPA)",
"urgency": "Immediate",
"severity": "Severe",
"certainty": "Observed",
"senderName": "Sveriges Radio",
"description": "TEST - This is the broadcast management at Swedish Radio with a test message for sending VMA messages. Area concerned: Kiruna municipality (Norrbottens county). No action required - End of test.",
"web": "https://sverigesradio.se/artikel/vma-vad-ar-det",
"area": [
{
"areaDesc": "Kiruna kommun (Norrbottens län)",
"geocode": [{ "valueName": "Kommun", "value": "2584" }]
}
]
}
]
},
{
"identifier": "SRCAP20240902171224T",
"sender": "https://vmaapi.sr.se/api/v2",
"sent": "2024-09-02T17:12:24+02:00",
"status": "Test",
"msgType": "Cancel",
"scope": "Public",
"references": "https://vmaapi.sr.se/api/v2,SRCAP20240902165224T,2024-09-02T16:52:24+02:00",
"incidents": "SRVMA20240902165224T",
"info": null
}
]
}

0 comments on commit ae777bf

Please sign in to comment.