|
9 | 9 | from homeassistant.components.device_tracker import ( |
10 | 10 | ATTR_BATTERY, |
11 | 11 | ATTR_SOURCE_TYPE, |
| 12 | + DOMAIN as DT_DOMAIN, |
12 | 13 | PLATFORM_SCHEMA, |
13 | 14 | SOURCE_TYPE_BLUETOOTH, |
14 | 15 | SOURCE_TYPE_BLUETOOTH_LE, |
|
26 | 27 | ATTR_LONGITUDE, |
27 | 28 | CONF_ENTITY_ID, |
28 | 29 | CONF_NAME, |
29 | | - EVENT_HOMEASSISTANT_START, |
30 | 30 | STATE_HOME, |
31 | 31 | STATE_NOT_HOME, |
32 | 32 | STATE_ON, |
|
40 | 40 | from homeassistant.util.location import distance |
41 | 41 |
|
42 | 42 | from .const import ( |
| 43 | + CONF_ALL_STATES, |
| 44 | + CONF_ENTITY, |
43 | 45 | CONF_REQ_MOVEMENT, |
44 | 46 | CONF_TIME_AS, |
45 | 47 | DOMAIN, |
46 | 48 | TIME_AS_OPTS, |
47 | 49 | TZ_DEVICE_LOCAL, |
48 | 50 | TZ_DEVICE_UTC, |
49 | 51 | TZ_LOCAL, |
50 | | - TZ_UTC, |
51 | 52 | ) |
52 | 53 |
|
53 | 54 | _LOGGER = logging.getLogger(__name__) |
|
60 | 61 | INACTIVE = "inactive" |
61 | 62 | ACTIVE = "active" |
62 | 63 | WARNED = "warned" |
| 64 | +USE_ALL_STATES = "use_all_states" |
63 | 65 | STATUS = "status" |
64 | 66 | SEEN = "seen" |
65 | 67 | SOURCE_TYPE = ATTR_SOURCE_TYPE |
|
75 | 77 | SOURCE_TYPE_ROUTER, |
76 | 78 | ) |
77 | 79 |
|
| 80 | + |
| 81 | +def _entities(entities): |
| 82 | + result = [] |
| 83 | + for entity in entities: |
| 84 | + if isinstance(entity, dict): |
| 85 | + result.append(entity) |
| 86 | + else: |
| 87 | + result.append( |
| 88 | + { |
| 89 | + CONF_ENTITY: entity, |
| 90 | + CONF_ALL_STATES: False, |
| 91 | + } |
| 92 | + ) |
| 93 | + return result |
| 94 | + |
| 95 | + |
| 96 | +ENTITIES = vol.All( |
| 97 | + cv.ensure_list, |
| 98 | + [ |
| 99 | + vol.Any( |
| 100 | + { |
| 101 | + vol.Required(CONF_ENTITY): cv.entity_id, |
| 102 | + vol.Required(CONF_ALL_STATES): cv.boolean, |
| 103 | + }, |
| 104 | + cv.entity_id, |
| 105 | + msg="Expected an entity ID", |
| 106 | + ) |
| 107 | + ], |
| 108 | + _entities, |
| 109 | +) |
78 | 110 | PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( |
79 | 111 | { |
80 | 112 | vol.Required(CONF_NAME): cv.slugify, |
81 | | - vol.Required(CONF_ENTITY_ID): cv.entity_ids, |
| 113 | + vol.Required(CONF_ENTITY_ID): ENTITIES, |
82 | 114 | vol.Optional(CONF_TIME_AS, default=TIME_AS_OPTS[0]): vol.In(TIME_AS_OPTS), |
83 | 115 | vol.Optional(CONF_REQ_MOVEMENT, default=False): cv.boolean, |
84 | 116 | } |
@@ -107,25 +139,29 @@ def __init__(self, hass, config, see): |
107 | 139 | self._see = see |
108 | 140 | entities = config[CONF_ENTITY_ID] |
109 | 141 | self._entities = {} |
110 | | - for entity_id in entities: |
| 142 | + entity_ids = [] |
| 143 | + for entity in entities: |
| 144 | + entity_id = entity[CONF_ENTITY] |
111 | 145 | self._entities[entity_id] = { |
| 146 | + USE_ALL_STATES: entity[CONF_ALL_STATES], |
112 | 147 | STATUS: INACTIVE, |
113 | 148 | SEEN: None, |
114 | 149 | SOURCE_TYPE: None, |
115 | 150 | DATA: None, |
116 | 151 | } |
| 152 | + entity_ids.append(entity_id) |
117 | 153 | self._dev_id = config[CONF_NAME] |
118 | | - self._entity_id = f"device_tracker.{self._dev_id}" |
| 154 | + self._entity_id = f"{DT_DOMAIN}.{self._dev_id}" |
119 | 155 | self._time_as = config[CONF_TIME_AS] |
120 | 156 | if self._time_as in [TZ_DEVICE_UTC, TZ_DEVICE_LOCAL]: |
121 | 157 | self._tf = hass.data[DOMAIN] |
122 | 158 | self._req_movement = config[CONF_REQ_MOVEMENT] |
123 | 159 | self._lock = threading.Lock() |
124 | 160 | self._prev_seen = None |
125 | 161 |
|
126 | | - self._remove = track_state_change(hass, entities, self._update_info) |
| 162 | + self._remove = track_state_change(hass, entity_ids, self._update_info) |
127 | 163 |
|
128 | | - for entity_id in entities: |
| 164 | + for entity_id in entity_ids: |
129 | 165 | self._update_info(entity_id, None, hass.states.get(entity_id)) |
130 | 166 |
|
131 | 167 | def _bad_entity(self, entity_id, message): |
@@ -153,8 +189,8 @@ def _good_entity(self, entity_id, seen, source_type, data): |
153 | 189 | {STATUS: ACTIVE, SEEN: seen, SOURCE_TYPE: source_type, DATA: data} |
154 | 190 | ) |
155 | 191 |
|
156 | | - def _use_non_gps_data(self, state): |
157 | | - if state == STATE_HOME: |
| 192 | + def _use_non_gps_data(self, entity_id, state): |
| 193 | + if state == STATE_HOME or self._entities[entity_id][USE_ALL_STATES]: |
158 | 194 | return True |
159 | 195 | entities = self._entities.values() |
160 | 196 | if any(entity[SOURCE_TYPE] == SOURCE_TYPE_GPS for entity in entities): |
@@ -262,7 +298,7 @@ def _update_info(self, entity_id, old_state, new_state): |
262 | 298 |
|
263 | 299 | self._good_entity(entity_id, last_seen, source_type, state) |
264 | 300 |
|
265 | | - if not self._use_non_gps_data(state): |
| 301 | + if not self._use_non_gps_data(entity_id, state): |
266 | 302 | return |
267 | 303 |
|
268 | 304 | # Don't use new GPS data if it's not complete. |
|
0 commit comments