/
train_state.py
executable file
·53 lines (43 loc) · 1.84 KB
/
train_state.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
"""
Script to receive alerts for train of interest (TOI).
"""
SCHEDULED = False
# Set config
MAIN_ENTITY_ID = 'sensor.next_train_to_wim'
TOI_ENTITY_ID = 'sensor.morning_train'
TOI_TIME = '07:15'
# Set notification details
MSG_TITLE = "Train Status Update"
NOTIFY_ID = 'robins_and_marias_iphones'
if hass.states.get(MAIN_ENTITY_ID).state == "No departures":
logger.warn('No Departures')
else:
attributes = hass.states.get(MAIN_ENTITY_ID).attributes
try:
for train in attributes['next_trains']:
if train['scheduled'] == TOI_TIME:
SCHEDULED = True
train_status = train['status']
if train_status != hass.states.get(TOI_ENTITY_ID).state:
hass.states.set(TOI_ENTITY_ID, train_status)
if train_status == 'LATE':
hass.services.call('notify', NOTIFY_ID, {
"title": MSG_TITLE,
"message": "The {} will be late and its ETA is"
.format(TOI_TIME) + train['estimated']})
hass.services.call('light', 'turn_on', {
"entity_id": 'light.lamp',
'color_name': 'orange'})
elif train_status == 'CANCELLED':
hass.services.call('notify', NOTIFY_ID, {
"title": MSG_TITLE,
"message": "The {} has been cancelled."
.format(TOI_TIME)})
hass.services.call('light', 'turn_on', {
"entity_id": 'light.lamp',
'color_name': 'red'})
break
except:
logger.warn('Error in train_state.py')
if not SCHEDULED:
hass.states.set(TOI_ENTITY_ID, 'No data') # check no data