77)
88
99from homeassistant .const import (
10- ATTR_STATE ,
1110 ATTR_SERVICE ,
1211 ATTR_SERVICE_DATA ,
1312 ATTR_ENTITY_ID ,
13+ CONF_TYPE ,
1414 # STATE_UNKNOWN,
1515 # STATE_OPEN,
1616 # STATE_CLOSED,
3131EVENT_ARM_FAILURE = "arm_failure"
3232
3333
34+ def validate_area (trigger , area_id ):
35+ if const .ATTR_AREA not in trigger :
36+ return False
37+ elif trigger [const .ATTR_AREA ]:
38+ return trigger [const .ATTR_AREA ] == area_id
39+ else :
40+ return area_id is None
41+
42+
43+ def validate_modes (trigger , mode ):
44+ if const .ATTR_MODES not in trigger :
45+ return False
46+ elif not trigger [const .ATTR_MODES ] or not mode :
47+ return True
48+ else :
49+ return mode in trigger [const .ATTR_MODES ]
50+
51+
3452class AutomationHandler :
3553 def __init__ (self , hass : HomeAssistant ):
3654 self .hass = hass
@@ -65,20 +83,14 @@ async def async_alarm_state_changed(area_id: str, old_state: str, new_state: str
6583 new_state = "armed"
6684
6785 for automation_id , config in self ._config .items ():
68- if (
69- not config [const .ATTR_ENABLED ]
70- or (config [const .ATTR_AREA ] != area_id and len (self .hass .data [const .DOMAIN ]["areas" ]) > 1 )
71- ):
72- continue
73- elif (
74- len (config [const .ATTR_MODES ]) and alarm_entity .arm_mode
75- and alarm_entity .arm_mode not in config [const .ATTR_MODES ]
76- ):
77- continue
78- else :
79- for trigger in config [const .ATTR_TRIGGERS ]:
80- if ATTR_STATE in trigger and trigger [ATTR_STATE ] == new_state :
81- await self .async_execute_automation (automation_id , alarm_entity )
86+ for trigger in config [const .ATTR_TRIGGERS ]:
87+ if (
88+ validate_area (trigger , area_id ) and
89+ validate_modes (trigger , alarm_entity .arm_mode ) and
90+ const .ATTR_EVENT in trigger and
91+ trigger [const .ATTR_EVENT ] == new_state
92+ ):
93+ await self .async_execute_automation (automation_id , alarm_entity )
8294
8395 async_dispatcher_connect (self .hass , "alarmo_state_updated" , async_alarm_state_changed )
8496
@@ -94,20 +106,14 @@ async def async_handle_event(event: str, area_id: str, args: dict = {}):
94106 _LOGGER .debug ("{} has failed to arm" .format (alarm_entity .entity_id ))
95107
96108 for automation_id , config in self ._config .items ():
97- if (
98- not config [const .ATTR_ENABLED ]
99- or (config [const .ATTR_AREA ] != area_id and len (self .hass .data [const .DOMAIN ]["areas" ]) > 1 )
100- ):
101- continue
102- elif (
103- len (config [const .ATTR_MODES ]) and alarm_entity .arm_mode
104- and alarm_entity .arm_mode not in config [const .ATTR_MODES ]
105- ):
106- continue
107- else :
108- for trigger in config [const .ATTR_TRIGGERS ]:
109- if const .ATTR_EVENT in trigger and trigger [const .ATTR_EVENT ] == EVENT_ARM_FAILURE :
110- await self .async_execute_automation (automation_id , alarm_entity )
109+ for trigger in config [const .ATTR_TRIGGERS ]:
110+ if (
111+ validate_area (trigger , area_id ) and
112+ validate_modes (trigger , alarm_entity .arm_mode ) and
113+ const .ATTR_EVENT in trigger and
114+ trigger [const .ATTR_EVENT ] == EVENT_ARM_FAILURE
115+ ):
116+ await self .async_execute_automation (automation_id , alarm_entity )
111117
112118 async_dispatcher_connect (self .hass , "alarmo_event" , async_handle_event )
113119
@@ -121,12 +127,11 @@ async def async_execute_automation(self, automation_id: str, alarm_entity: Alarm
121127 service_call = {
122128 "service" : action [ATTR_SERVICE ]
123129 }
124- if ATTR_ENTITY_ID in action :
130+ if ATTR_ENTITY_ID in action and action [ ATTR_ENTITY_ID ] :
125131 service_call ["entity_id" ] = action [ATTR_ENTITY_ID ]
126132
127133 if (
128- const .ATTR_IS_NOTIFICATION in self ._config [automation_id ]
129- and self ._config [automation_id ][const .ATTR_IS_NOTIFICATION ]
134+ self ._config [automation_id ][CONF_TYPE ] == const .ATTR_NOTIFICATION
130135 and ATTR_MESSAGE in action [ATTR_SERVICE_DATA ]
131136 ):
132137 data = copy .copy (action [ATTR_SERVICE_DATA ])
@@ -153,7 +158,6 @@ async def async_execute_automation(self, automation_id: str, alarm_entity: Alarm
153158 data [ATTR_MESSAGE ] = data [ATTR_MESSAGE ].replace ("{{bypassed_sensors}}" , bypassed_sensors )
154159
155160 if "{{arm_mode}}" in data [ATTR_MESSAGE ]:
156- _LOGGER .debug (alarm_entity .arm_mode )
157161 arm_mode = alarm_entity .arm_mode if alarm_entity .arm_mode else ""
158162 arm_mode = " " .join (w .capitalize () for w in arm_mode .split ("_" ))
159163 data [ATTR_MESSAGE ] = data [ATTR_MESSAGE ].replace ("{{arm_mode}}" , arm_mode )
0 commit comments