-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_presence.py
43 lines (36 loc) · 2.04 KB
/
auto_presence.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
from constants.data import DATA_PRESENCE
from constants.entities import ENTITIES_PRESENCE
from constants.mappings import MAP_PERSISTENCE_ENTITY_PRESENCE
from utils import *
trigger = []
def presence_factory(room):
trigger_conditions = [f"state.{entity} == '{condition['condition']}'" for entity, condition in ENTITIES_PRESENCE[room]['indicators'].items()]
@state_trigger(trigger_conditions)
@logged
def presence(var_name=None):
indicators_weight = sum(condition.get('weight', 1) for entity, condition in ENTITIES_PRESENCE[room]['indicators'].items() if state.get(entity) == condition['condition'])
exclusions_weight = sum(condition.get('weight', 1) for entity, condition in ENTITIES_PRESENCE[room]['exclusions'].items() if state.get(entity) == condition['condition'])
if indicators_weight >= 1 and exclusions_weight == 0:
update_presence(room, 'on')
else:
update_presence(room, 'off')
trigger.append(presence)
@time_trigger('startup')
def presence_init():
existing_attributes = state.get(f"{MAP_PERSISTENCE_ENTITY_PRESENCE}", {}).get('attributes', {})
initial_attributes = {room: existing_attributes.get(room, 'off') for room in ENTITIES_PRESENCE}
state.persist(f"{MAP_PERSISTENCE_ENTITY_PRESENCE}", default_value="", attributes=initial_attributes)
homeassistant.update_entity(entity_id=MAP_PERSISTENCE_ENTITY_PRESENCE)
def update_presence(room, action):
current_state = state.get(f"{MAP_PERSISTENCE_ENTITY_PRESENCE}", {})
attributes = current_state.get('attributes', {}) if current_state else {}
attributes[room] = action
global_state = 'on' if any(status == 'on' for status in attributes.values()) else 'off'
state.set(f"{MAP_PERSISTENCE_ENTITY_PRESENCE}", global_state, attributes=attributes)
homeassistant.update_entity(entity_id=MAP_PERSISTENCE_ENTITY_PRESENCE)
state.persist(f"{MAP_PERSISTENCE_ENTITY_PRESENCE}")
for transition in DATA_PRESENCE.get(room, {}).get(action, []):
if eval(transition['condition']):
transition['action']()
for room in ENTITIES_PRESENCE:
presence_factory(room)