Skip to content

Commit ed00cdc

Browse files
committed
bugfixes and documentation updates
- Updated README.md to reflect changes in trigger variable references - Modified trigger.py to handle asynchronous action execution more effectively, ensuring proper task scheduling. - Enhanced logging for trigger events to improve debugging and monitoring capabilities.
1 parent c69ec9d commit ed00cdc

2 files changed

Lines changed: 18 additions & 16 deletions

File tree

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ actions:
250250
data:
251251
title: "🚨 Network Security Alert"
252252
message: >
253-
Firewall rule "{{ trigger.rule_name }}" was {{ trigger.type.replace('rule_', '') }}
254-
Rule ID: {{ trigger.rule_id }}
253+
Firewall rule "{{ trigger.event.rule_name }}" was {{ trigger.event.rule_type.replace('rule_', '') }}
254+
Rule ID: {{ trigger.event.rule_id }}
255255
data:
256256
priority: high
257257
category: security
@@ -286,7 +286,7 @@ actions:
286286
- action: switch.turn_off
287287
target:
288288
entity_id: >
289-
{% set rule_id = trigger.rule_id %}
289+
{% set rule_id = trigger.event.rule_id %}
290290
{% set entities = states.switch | selectattr('attributes.rule_id', 'eq', rule_id) | map(attribute='entity_id') | list %}
291291
{{ entities[0] if entities else none }}
292292
- action: notify.family_devices
@@ -328,8 +328,8 @@ actions:
328328
data:
329329
title: "📁 Network Rules Backed Up"
330330
message: >
331-
Automatic backup created due to {{ trigger.type.replace('rule_', '') }}
332-
of {{ trigger.rule_type.replace('_', ' ').title() }}: "{{ trigger.rule_name }}"
331+
Automatic backup created due to {{ trigger.event.trigger_type.replace('rule_', '') }}
332+
of {{ trigger.event.rule_type.replace('_', ' ').title() }}: "{{ trigger.event.rule_name }}"
333333
mode: single
334334
```
335335

@@ -352,8 +352,8 @@ actions:
352352
data:
353353
title: "🔒 VPN Status Change"
354354
message: >
355-
VPN "{{ trigger.rule_name }}" was {{ 'connected' if trigger.type == 'rule_enabled' else 'disconnected' }}
356-
{% if trigger.type == 'rule_enabled' %}
355+
VPN "{{ trigger.event.rule_name }}" was {{ 'connected' if trigger.event.trigger_type == 'rule_enabled' else 'disconnected' }}
356+
{% if trigger.event.trigger_type == 'rule_enabled' %}
357357
🟢 Secure connection established
358358
{% else %}
359359
🔴 Connection terminated
@@ -383,10 +383,10 @@ actions:
383383
data:
384384
name: "Parental Controls"
385385
message: >
386-
{{ trigger.rule_name }} was {{ trigger.type.replace('rule_', '') }}
387-
{% if trigger.type == 'rule_enabled' %}
386+
{{ trigger.event.rule_name }} was {{ trigger.event.trigger_type.replace('rule_', '') }}
387+
{% if trigger.event.trigger_type == 'rule_enabled' %}
388388
✅ Internet access restored
389-
{% elif trigger.type == 'rule_disabled' %}
389+
{% elif trigger.event.trigger_type == 'rule_disabled' %}
390390
🚫 Internet access blocked
391391
{% else %}
392392
🔧 Settings modified
@@ -395,7 +395,7 @@ actions:
395395
- action: notify.parents_devices
396396
data:
397397
title: "👨‍👩‍👧‍👦 Parental Control Update"
398-
message: "{{ trigger.rule_name }} - {{ trigger.type.replace('rule_', '').title() }}"
398+
message: "{{ trigger.event.rule_name }} - {{ trigger.event.trigger_type.replace('rule_', '').title() }}"
399399
mode: parallel
400400
```
401401

@@ -422,7 +422,7 @@ actions:
422422
entity_id: input_text.last_network_change
423423
data:
424424
value: >
425-
{{ now().strftime('%H:%M') }}: {{ trigger.rule_name }} ({{ trigger.type.replace('rule_', '') }})
425+
{{ now().strftime('%H:%M') }}: {{ trigger.event.rule_name }} ({{ trigger.event.trigger_type.replace('rule_', '') }})
426426
- action: input_datetime.set_datetime
427427
target:
428428
entity_id: input_datetime.last_rule_change

custom_components/unifi_network_rules/trigger.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,9 @@ def _handle_websocket_msg(msg: Dict[str, Any]) -> None:
485485
if LOG_TRIGGERS:
486486
LOGGER.info("Calling action with trigger vars: %s", trigger_vars)
487487
# Schedule the action execution
488-
self.hass.async_create_task(
489-
self.action({"trigger": trigger_vars})
490-
)
488+
result = self.action({"trigger": trigger_vars})
489+
if asyncio.iscoroutine(result):
490+
self.hass.async_create_task(result)
491491

492492
# Also trigger coordinator refresh since rule changed
493493
self._dispatch_coordinator_refresh(f"Rule change detected: {rule_id} ({detected_rule_type})")
@@ -921,7 +921,9 @@ async def _fire_trigger(self, trigger_type: str, rule_id: str, rule_type: str, r
921921
}
922922

923923
# Schedule the action execution
924-
await self.action({"trigger": trigger_vars})
924+
result = self.action({"trigger": trigger_vars})
925+
if asyncio.iscoroutine(result):
926+
await result
925927

926928
except Exception as err:
927929
LOGGER.error("Error firing trigger: %s", err)

0 commit comments

Comments
 (0)