-
Notifications
You must be signed in to change notification settings - Fork 6
/
security.py
executable file
·335 lines (282 loc) · 11.5 KB
/
security.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
"""Define automations for home security system."""
from enum import Enum
from typing import Union
import voluptuous as vol
import voluptuous_helper as vol_help
from appbase import AppBase, APP_SCHEMA
from constants import CONF_ENTITIES, CONF_NOTIFICATIONS, CONF_TARGETS, ON
from house_config import HOUSE, MODES, PERSONS
##############################################################################
# App to control security system
#
# ARGS:
# yaml args:
#
# entities:
# motion: motion sensor that will trigger alarm if armed_motion, comma list
# other_sensors: other sensors that trigger if armed_no_motion or
# armed_motion, comma list
# alarm_lights: lights that will flash red when alert, comma list
#
##############################################################################
ALARM_STATE = "alarm_state"
CONF_ALARM_LIGHTS = "alarm_lights"
CONF_MOTION_SENSORS = "motion_sensors"
CONF_DOOR_SENSORS = "door_sensors"
PRESENCE_STATE = "presence_state"
CLEANING_MODE = "cleaning_mode"
WRONG_ALARM = "wrong_alarm"
class SecurityAutomation(AppBase):
"""Define a base for security automations."""
APP_SCHEMA = APP_SCHEMA.extend(
{
CONF_ENTITIES: vol.Schema(
{
vol.Required(CONF_ALARM_LIGHTS): vol.Schema(
[vol.Optional(vol_help.entity_id)]
),
vol.Required(CONF_MOTION_SENSORS): vol.Schema(
[vol.Optional(vol_help.entity_id)]
),
vol.Required(CONF_DOOR_SENSORS): vol.Schema(
[vol.Optional(vol_help.entity_id)]
),
},
extra=vol.ALLOW_EXTRA,
)
}
)
class AlarmType(Enum):
"""Define an enum for Alarm types."""
armed_no_motion = "Scharf ohne Bewegung"
armed_motion = "Scharf mit Bewegung"
disarmed = "Ungesichert"
alert = "Einbrecher"
def configure(self) -> None:
"""Configure."""
self.alarm_lights = self.entities[CONF_ALARM_LIGHTS]
self.motion_sensors = self.entities[CONF_MOTION_SENSORS]
self.door_sensors = self.entities[CONF_DOOR_SENSORS]
self.code = self.args["code"]
for entity in self.motion_sensors:
self.listen_state(
self.motion_triggered, entity, new=ON, constrain_app_enabled=1
)
for entity in self.door_sensors:
self.listen_state(
self.door_opened, entity, new=ON, constrain_app_enabled=1
)
@property
def alarm_state(self) -> "AlarmType":
"""Return the current state of the security system."""
return self.AlarmType(self.get_state(HOUSE[ALARM_STATE]))
@alarm_state.setter
def alarm_state(self, alarm_state: AlarmType) -> None:
"""Set the the security system to given state."""
self.select_option(HOUSE[ALARM_STATE], alarm_state.value)
if alarm_state == self.AlarmType.armed_motion:
self.call_service(
"alarm_control_panel/alarm_arm_away",
entity_id=HOUSE["alarm_panel"],
code=self.code
)
elif alarm_state == self.AlarmType.armed_no_motion:
self.call_service(
"alarm_control_panel/alarm_arm_home",
entity_id=HOUSE["alarm_panel"],
code=self.code
)
elif alarm_state == self.AlarmType.disarmed:
self.call_service(
"alarm_control_panel/alarm_disarm",
entity_id=HOUSE["alarm_panel"],
code=self.code
)
elif alarm_state == self.AlarmType.alert:
self.call_service(
"alarm_control_panel/alarm_trigger",
entity_id=HOUSE["alarm_panel"],
code=self.code
)
def motion_triggered(
self, entity: Union[str, dict], attribute: str, old: str, new: str, kwargs: dict
) -> None:
"""Take action when motion sensor is triggered based on alarm state."""
if self.alarm_state == self.AlarmType.armed_motion:
self.alarm_state = self.AlarmType.alert
self.log(
f"Bewegung im " f"{entity.split('.')[1].split('_')[1].capitalize()}!!!"
)
def door_opened(
self, entity: Union[str, dict], attribute: str, old: str, new: str, kwargs: dict
) -> None:
"""Take action when a door is opened based on alarm state."""
if self.alarm_state in (
self.AlarmType.armed_motion,
self.AlarmType.armed_no_motion,
):
self.alarm_state = self.AlarmType.alert
self.log(
f"{entity.split('.')[1].split('_')[0].capitalize()}"
f" im/in der "
f"{entity.split('.')[1].split('_')[1].capitalize()}"
f" wurde geöffnet!!!"
)
# test presence first
# need a way to cancel flash lights
# if new == 'on' or new == 'offen':
# self.log("Lichter werden jetzt blinken!")
# for light in self.alarm_lights:
# self.turn_on(light, brightness=255, color_name='white')
# self.flash_lights(light)
def flash_lights(self, light: str) -> None:
"""Flash lights as long as alarm state is alert."""
self.toggle(light)
if self.self.alarm_state == self.AlarmType.alert:
self.run_in(self.flash_lights(light), 1)
class ArmOnDeparture(AppBase):
"""Define a feature to arm the security system when everyone left."""
def configure(self):
"""Configure."""
self.listen_state(
self.noone_home, HOUSE[PRESENCE_STATE], constrain_app_enabled=1
)
def noone_home(
self, entity: Union[str, dict], attribute: str, old: str, new: str, kwargs: dict
) -> None:
"""Arm the security system when everyone left."""
someone_home_states = [
self.presence_app.HouseState.someone.value,
self.presence_app.HouseState.everyone.value,
]
if (new not in someone_home_states) and (old in someone_home_states):
self.security_app.alarm_state = self.security_app.AlarmType.armed_motion
self.log("Alle sind gegangen. Stelle Alarm scharf!")
class DisarmOnArrival(AppBase):
"""Define a feature to disarm the security system when someone arrives."""
def configure(self):
"""Configure."""
self.listen_state(
self.someone_home, HOUSE[PRESENCE_STATE], constrain_app_enabled=1
)
def someone_home(
self, entity: Union[str, dict], attribute: str, old: str, new: str, kwargs: dict
) -> None:
"""Disarm the security system when someone arrives."""
someone_home_states = [
self.presence_app.HouseState.someone.value,
self.presence_app.HouseState.everyone.value,
]
if (new in someone_home_states) and (old not in someone_home_states):
self.security_app.alarm_state = self.security_app.AlarmType.disarmed
self.log("Jemand ist jetzt zu Hause. Schalte Alarm aus!")
class ArmDisarmCleaning(AppBase):
"""Define a feature to disable the motion sensors when the vacuum runs."""
def configure(self):
"""Configure."""
self.listen_state(
self.cleaning_mode_changed, MODES[CLEANING_MODE], constrain_app_enabled=1
)
def cleaning_mode_changed(
self, entity: Union[str, dict], attribute: str, old: str, new: str, kwargs: dict
) -> None:
"""Set the security system based on the state of the vacuum cleaner."""
if new == "on" and self.presence_app.noone_home:
self.security_app.alarm_state = self.security_app.AlarmType.armed_no_motion
self.log("Pedro putzt jetzt. Schalte Bewegungssensoren aus!")
elif new == "off" and self.presence_app.noone_home:
self.security_app.alarm_state = self.security_app.AlarmType.armed_motion
self.log("Pedro ist fertig. Schalte Bewegungssensoren wieder ein!")
class NotificationOnChange(AppBase):
"""Define a feature to send a notification when the alarm state changed."""
APP_SCHEMA = APP_SCHEMA.extend(
{
CONF_NOTIFICATIONS: vol.Schema(
{vol.Required(CONF_TARGETS): vol.In(PERSONS.keys())},
extra=vol.ALLOW_EXTRA,
)
}
)
def configure(self):
"""Configure."""
self.listen_state(
self.alarm_state_changed, HOUSE[ALARM_STATE], constrain_app_enabled=1
)
self.listen_event(
self.disarm_on_push_notification,
"html5_notification.clicked",
action=WRONG_ALARM,
constrain_app_enabled=1,
)
def alarm_state_changed(
self, entity: Union[str, dict], attribute: str, old: str, new: str, kwargs: dict
) -> None:
"""Send notification when alarm state changed."""
self.notification_app.notify(
kind="single",
level="emergency",
title="Alarm Status gewechselt",
message=f"Der neue Alarm Status ist {new}",
targets=self.notifications["targets"],
data={"actions": [{"action": WRONG_ALARM, "title": "Fehlalarm"}]},
)
def disarm_on_push_notification(
self, event_name: str, data: dict, kwargs: dict
) -> None:
"""Disarm when push notification got clicked."""
self.security_app.alarm_state = self.security_app.AlarmType.disarmed
self.log("Fehlalarm, Alarmanlage wird ausgeschaltet!")
class NotifyOnBadLoginAttempt(AppBase):
"""Define a feature to send a notification when bad login happened."""
APP_SCHEMA = APP_SCHEMA.extend(
{
CONF_NOTIFICATIONS: vol.Schema(
{vol.Required(CONF_TARGETS): vol.In(PERSONS.keys())},
extra=vol.ALLOW_EXTRA,
)
}
)
def configure(self):
"""Configure."""
self.listen_state(
self.bad_login_attempt,
"persistent_notification.http_login",
new="notifying",
)
def bad_login_attempt(
self, entity: Union[str, dict], attribute: str, old: str, new: str, kwargs: dict
) -> None:
"""Send notification when bad login happened."""
msg = self.get_state("persistent_notification.http_login", attribute="message")
self.notification_app.notify(
kind="single",
level="emergency",
title="Falscher Loginversuch",
message=msg,
targets=self.notifications["targets"],
)
class LastMotion(AppBase):
"""Define a feature to update a sensor with the
name of the room where last motion was detected"""
APP_SCHEMA = APP_SCHEMA.extend(
{
CONF_ENTITIES: vol.Schema(
{
vol.Required(CONF_MOTION_SENSORS): vol.Schema(
[vol.Optional(vol_help.entity_id)]
)
},
extra=vol.ALLOW_EXTRA,
)
}
)
def configure(self):
"""Configure."""
for sensor in self.entities[CONF_MOTION_SENSORS]:
self.listen_state(self.motion_detected, sensor, new="on")
def motion_detected(
self, entity: Union[str, dict], attribute: str, old: str, new: str, kwargs: dict
) -> None:
"""Select the room input select based on the triggered entity."""
room_name = entity.split(".")[1].split("_", 1)[-1].capitalize()
self.select_option(HOUSE["last_motion"], room_name)