/
test_init.py
534 lines (427 loc) · 18 KB
/
test_init.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
"""The tests for the person component."""
from unittest.mock import Mock
from homeassistant.components.person import (
ATTR_SOURCE, ATTR_USER_ID, DOMAIN, PersonManager)
from homeassistant.const import (
ATTR_ID, ATTR_LATITUDE, ATTR_LONGITUDE, STATE_UNKNOWN,
EVENT_HOMEASSISTANT_START)
from homeassistant.core import CoreState, State
from homeassistant.setup import async_setup_component
import pytest
from tests.common import mock_component, mock_restore_cache, mock_coro_func
DEVICE_TRACKER = 'device_tracker.test_tracker'
DEVICE_TRACKER_2 = 'device_tracker.test_tracker_2'
@pytest.fixture
def storage_setup(hass, hass_storage, hass_admin_user):
"""Storage setup."""
hass_storage[DOMAIN] = {
'key': DOMAIN,
'version': 1,
'data': {
'persons': [
{
'id': '1234',
'name': 'tracked person',
'user_id': hass_admin_user.id,
'device_trackers': [DEVICE_TRACKER]
}
]
}
}
assert hass.loop.run_until_complete(
async_setup_component(hass, DOMAIN, {})
)
async def test_minimal_setup(hass):
"""Test minimal config with only name."""
config = {DOMAIN: {'id': '1234', 'name': 'test person'}}
assert await async_setup_component(hass, DOMAIN, config)
state = hass.states.get('person.test_person')
assert state.state == STATE_UNKNOWN
assert state.attributes.get(ATTR_LATITUDE) is None
assert state.attributes.get(ATTR_LONGITUDE) is None
assert state.attributes.get(ATTR_SOURCE) is None
assert state.attributes.get(ATTR_USER_ID) is None
async def test_setup_no_id(hass):
"""Test config with no id."""
config = {DOMAIN: {'name': 'test user'}}
assert not await async_setup_component(hass, DOMAIN, config)
async def test_setup_no_name(hass):
"""Test config with no name."""
config = {DOMAIN: {'id': '1234'}}
assert not await async_setup_component(hass, DOMAIN, config)
async def test_setup_user_id(hass, hass_admin_user):
"""Test config with user id."""
user_id = hass_admin_user.id
config = {
DOMAIN: {'id': '1234', 'name': 'test person', 'user_id': user_id}}
assert await async_setup_component(hass, DOMAIN, config)
state = hass.states.get('person.test_person')
assert state.state == STATE_UNKNOWN
assert state.attributes.get(ATTR_ID) == '1234'
assert state.attributes.get(ATTR_LATITUDE) is None
assert state.attributes.get(ATTR_LONGITUDE) is None
assert state.attributes.get(ATTR_SOURCE) is None
assert state.attributes.get(ATTR_USER_ID) == user_id
async def test_valid_invalid_user_ids(hass, hass_admin_user):
"""Test a person with valid user id and a person with invalid user id ."""
user_id = hass_admin_user.id
config = {DOMAIN: [
{'id': '1234', 'name': 'test valid user', 'user_id': user_id},
{'id': '5678', 'name': 'test bad user', 'user_id': 'bad_user_id'}]}
assert await async_setup_component(hass, DOMAIN, config)
state = hass.states.get('person.test_valid_user')
assert state.state == STATE_UNKNOWN
assert state.attributes.get(ATTR_ID) == '1234'
assert state.attributes.get(ATTR_LATITUDE) is None
assert state.attributes.get(ATTR_LONGITUDE) is None
assert state.attributes.get(ATTR_SOURCE) is None
assert state.attributes.get(ATTR_USER_ID) == user_id
state = hass.states.get('person.test_bad_user')
assert state is None
async def test_setup_tracker(hass, hass_admin_user):
"""Test set up person with one device tracker."""
user_id = hass_admin_user.id
config = {DOMAIN: {
'id': '1234', 'name': 'tracked person', 'user_id': user_id,
'device_trackers': DEVICE_TRACKER}}
assert await async_setup_component(hass, DOMAIN, config)
state = hass.states.get('person.tracked_person')
assert state.state == STATE_UNKNOWN
assert state.attributes.get(ATTR_ID) == '1234'
assert state.attributes.get(ATTR_LATITUDE) is None
assert state.attributes.get(ATTR_LONGITUDE) is None
assert state.attributes.get(ATTR_SOURCE) is None
assert state.attributes.get(ATTR_USER_ID) == user_id
hass.states.async_set(DEVICE_TRACKER, 'home')
await hass.async_block_till_done()
state = hass.states.get('person.tracked_person')
assert state.state == STATE_UNKNOWN
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
await hass.async_block_till_done()
state = hass.states.get('person.tracked_person')
assert state.state == 'home'
assert state.attributes.get(ATTR_ID) == '1234'
assert state.attributes.get(ATTR_LATITUDE) is None
assert state.attributes.get(ATTR_LONGITUDE) is None
assert state.attributes.get(ATTR_SOURCE) == DEVICE_TRACKER
assert state.attributes.get(ATTR_USER_ID) == user_id
hass.states.async_set(
DEVICE_TRACKER, 'not_home',
{ATTR_LATITUDE: 10.123456, ATTR_LONGITUDE: 11.123456})
await hass.async_block_till_done()
state = hass.states.get('person.tracked_person')
assert state.state == 'not_home'
assert state.attributes.get(ATTR_ID) == '1234'
assert state.attributes.get(ATTR_LATITUDE) == 10.12346
assert state.attributes.get(ATTR_LONGITUDE) == 11.12346
assert state.attributes.get(ATTR_SOURCE) == DEVICE_TRACKER
assert state.attributes.get(ATTR_USER_ID) == user_id
async def test_setup_two_trackers(hass, hass_admin_user):
"""Test set up person with two device trackers."""
user_id = hass_admin_user.id
config = {DOMAIN: {
'id': '1234', 'name': 'tracked person', 'user_id': user_id,
'device_trackers': [DEVICE_TRACKER, DEVICE_TRACKER_2]}}
assert await async_setup_component(hass, DOMAIN, config)
state = hass.states.get('person.tracked_person')
assert state.state == STATE_UNKNOWN
assert state.attributes.get(ATTR_ID) == '1234'
assert state.attributes.get(ATTR_LATITUDE) is None
assert state.attributes.get(ATTR_LONGITUDE) is None
assert state.attributes.get(ATTR_SOURCE) is None
assert state.attributes.get(ATTR_USER_ID) == user_id
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
await hass.async_block_till_done()
hass.states.async_set(DEVICE_TRACKER, 'home')
await hass.async_block_till_done()
state = hass.states.get('person.tracked_person')
assert state.state == 'home'
assert state.attributes.get(ATTR_ID) == '1234'
assert state.attributes.get(ATTR_LATITUDE) is None
assert state.attributes.get(ATTR_LONGITUDE) is None
assert state.attributes.get(ATTR_SOURCE) == DEVICE_TRACKER
assert state.attributes.get(ATTR_USER_ID) == user_id
hass.states.async_set(
DEVICE_TRACKER_2, 'not_home',
{ATTR_LATITUDE: 12.123456, ATTR_LONGITUDE: 13.123456})
await hass.async_block_till_done()
state = hass.states.get('person.tracked_person')
assert state.state == 'not_home'
assert state.attributes.get(ATTR_ID) == '1234'
assert state.attributes.get(ATTR_LATITUDE) == 12.12346
assert state.attributes.get(ATTR_LONGITUDE) == 13.12346
assert state.attributes.get(ATTR_SOURCE) == DEVICE_TRACKER_2
assert state.attributes.get(ATTR_USER_ID) == user_id
async def test_ignore_unavailable_states(hass, hass_admin_user):
"""Test set up person with two device trackers, one unavailable."""
user_id = hass_admin_user.id
config = {DOMAIN: {
'id': '1234', 'name': 'tracked person', 'user_id': user_id,
'device_trackers': [DEVICE_TRACKER, DEVICE_TRACKER_2]}}
assert await async_setup_component(hass, DOMAIN, config)
state = hass.states.get('person.tracked_person')
assert state.state == STATE_UNKNOWN
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
await hass.async_block_till_done()
hass.states.async_set(DEVICE_TRACKER, 'home')
await hass.async_block_till_done()
hass.states.async_set(DEVICE_TRACKER, 'unavailable')
await hass.async_block_till_done()
# Unknown, as only 1 device tracker has a state, but we ignore that one
state = hass.states.get('person.tracked_person')
assert state.state == STATE_UNKNOWN
hass.states.async_set(DEVICE_TRACKER_2, 'not_home')
await hass.async_block_till_done()
# Take state of tracker 2
state = hass.states.get('person.tracked_person')
assert state.state == 'not_home'
# state 1 is newer but ignored, keep tracker 2 state
hass.states.async_set(DEVICE_TRACKER, 'unknown')
await hass.async_block_till_done()
state = hass.states.get('person.tracked_person')
assert state.state == 'not_home'
async def test_restore_home_state(hass, hass_admin_user):
"""Test that the state is restored for a person on startup."""
user_id = hass_admin_user.id
attrs = {
ATTR_ID: '1234', ATTR_LATITUDE: 10.12346, ATTR_LONGITUDE: 11.12346,
ATTR_SOURCE: DEVICE_TRACKER, ATTR_USER_ID: user_id}
state = State('person.tracked_person', 'home', attrs)
mock_restore_cache(hass, (state, ))
hass.state = CoreState.starting
mock_component(hass, 'recorder')
config = {DOMAIN: {
'id': '1234', 'name': 'tracked person', 'user_id': user_id,
'device_trackers': DEVICE_TRACKER}}
assert await async_setup_component(hass, DOMAIN, config)
state = hass.states.get('person.tracked_person')
assert state.state == 'home'
assert state.attributes.get(ATTR_ID) == '1234'
assert state.attributes.get(ATTR_LATITUDE) == 10.12346
assert state.attributes.get(ATTR_LONGITUDE) == 11.12346
# When restoring state the entity_id of the person will be used as source.
assert state.attributes.get(ATTR_SOURCE) == 'person.tracked_person'
assert state.attributes.get(ATTR_USER_ID) == user_id
async def test_duplicate_ids(hass, hass_admin_user):
"""Test we don't allow duplicate IDs."""
config = {DOMAIN: [
{'id': '1234', 'name': 'test user 1'},
{'id': '1234', 'name': 'test user 2'}]}
assert await async_setup_component(hass, DOMAIN, config)
assert len(hass.states.async_entity_ids('person')) == 1
assert hass.states.get('person.test_user_1') is not None
assert hass.states.get('person.test_user_2') is None
async def test_load_person_storage(hass, hass_admin_user, storage_setup):
"""Test set up person from storage."""
state = hass.states.get('person.tracked_person')
assert state.state == STATE_UNKNOWN
assert state.attributes.get(ATTR_ID) == '1234'
assert state.attributes.get(ATTR_LATITUDE) is None
assert state.attributes.get(ATTR_LONGITUDE) is None
assert state.attributes.get(ATTR_SOURCE) is None
assert state.attributes.get(ATTR_USER_ID) == hass_admin_user.id
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
await hass.async_block_till_done()
hass.states.async_set(DEVICE_TRACKER, 'home')
await hass.async_block_till_done()
state = hass.states.get('person.tracked_person')
assert state.state == 'home'
assert state.attributes.get(ATTR_ID) == '1234'
assert state.attributes.get(ATTR_LATITUDE) is None
assert state.attributes.get(ATTR_LONGITUDE) is None
assert state.attributes.get(ATTR_SOURCE) == DEVICE_TRACKER
assert state.attributes.get(ATTR_USER_ID) == hass_admin_user.id
async def test_ws_list(hass, hass_ws_client, storage_setup):
"""Test listing via WS."""
manager = hass.data[DOMAIN]
client = await hass_ws_client(hass)
resp = await client.send_json({
'id': 6,
'type': 'person/list',
})
resp = await client.receive_json()
assert resp['success']
assert resp['result']['storage'] == manager.storage_persons
assert len(resp['result']['storage']) == 1
assert len(resp['result']['config']) == 0
async def test_ws_create(hass, hass_ws_client, storage_setup,
hass_read_only_user):
"""Test creating via WS."""
manager = hass.data[DOMAIN]
client = await hass_ws_client(hass)
resp = await client.send_json({
'id': 6,
'type': 'person/create',
'name': 'Hello',
'device_trackers': [DEVICE_TRACKER],
'user_id': hass_read_only_user.id,
})
resp = await client.receive_json()
persons = manager.storage_persons
assert len(persons) == 2
assert resp['success']
assert resp['result'] == persons[1]
async def test_ws_create_requires_admin(hass, hass_ws_client, storage_setup,
hass_admin_user, hass_read_only_user):
"""Test creating via WS requires admin."""
hass_admin_user.groups = []
manager = hass.data[DOMAIN]
client = await hass_ws_client(hass)
resp = await client.send_json({
'id': 6,
'type': 'person/create',
'name': 'Hello',
'device_trackers': [DEVICE_TRACKER],
'user_id': hass_read_only_user.id,
})
resp = await client.receive_json()
persons = manager.storage_persons
assert len(persons) == 1
assert not resp['success']
async def test_ws_update(hass, hass_ws_client, storage_setup):
"""Test updating via WS."""
manager = hass.data[DOMAIN]
client = await hass_ws_client(hass)
persons = manager.storage_persons
resp = await client.send_json({
'id': 6,
'type': 'person/update',
'person_id': persons[0]['id'],
'name': 'Updated Name',
'device_trackers': [DEVICE_TRACKER_2],
'user_id': None,
})
resp = await client.receive_json()
persons = manager.storage_persons
assert len(persons) == 1
assert resp['success']
assert resp['result'] == persons[0]
assert persons[0]['name'] == 'Updated Name'
assert persons[0]['name'] == 'Updated Name'
assert persons[0]['device_trackers'] == [DEVICE_TRACKER_2]
assert persons[0]['user_id'] is None
state = hass.states.get('person.tracked_person')
assert state.name == 'Updated Name'
async def test_ws_update_require_admin(hass, hass_ws_client, storage_setup,
hass_admin_user):
"""Test updating via WS requires admin."""
hass_admin_user.groups = []
manager = hass.data[DOMAIN]
client = await hass_ws_client(hass)
original = dict(manager.storage_persons[0])
resp = await client.send_json({
'id': 6,
'type': 'person/update',
'person_id': original['id'],
'name': 'Updated Name',
'device_trackers': [DEVICE_TRACKER_2],
'user_id': None,
})
resp = await client.receive_json()
assert not resp['success']
not_updated = dict(manager.storage_persons[0])
assert original == not_updated
async def test_ws_delete(hass, hass_ws_client, storage_setup):
"""Test deleting via WS."""
manager = hass.data[DOMAIN]
client = await hass_ws_client(hass)
persons = manager.storage_persons
resp = await client.send_json({
'id': 6,
'type': 'person/delete',
'person_id': persons[0]['id'],
})
resp = await client.receive_json()
persons = manager.storage_persons
assert len(persons) == 0
assert resp['success']
assert len(hass.states.async_entity_ids('person')) == 0
ent_reg = await hass.helpers.entity_registry.async_get_registry()
assert not ent_reg.async_is_registered('person.tracked_person')
async def test_ws_delete_require_admin(hass, hass_ws_client, storage_setup,
hass_admin_user):
"""Test deleting via WS requires admin."""
hass_admin_user.groups = []
manager = hass.data[DOMAIN]
client = await hass_ws_client(hass)
resp = await client.send_json({
'id': 6,
'type': 'person/delete',
'person_id': manager.storage_persons[0]['id'],
'name': 'Updated Name',
'device_trackers': [DEVICE_TRACKER_2],
'user_id': None,
})
resp = await client.receive_json()
assert not resp['success']
persons = manager.storage_persons
assert len(persons) == 1
async def test_create_invalid_user_id(hass):
"""Test we do not allow invalid user ID during creation."""
manager = PersonManager(hass, Mock(), [])
await manager.async_initialize()
with pytest.raises(ValueError):
await manager.async_create_person(
name='Hello',
user_id='non-existing'
)
async def test_create_duplicate_user_id(hass, hass_admin_user):
"""Test we do not allow duplicate user ID during creation."""
manager = PersonManager(
hass, Mock(async_add_entities=mock_coro_func()), []
)
await manager.async_initialize()
await manager.async_create_person(
name='Hello',
user_id=hass_admin_user.id
)
with pytest.raises(ValueError):
await manager.async_create_person(
name='Hello',
user_id=hass_admin_user.id
)
async def test_update_double_user_id(hass, hass_admin_user):
"""Test we do not allow double user ID during update."""
manager = PersonManager(
hass, Mock(async_add_entities=mock_coro_func()), []
)
await manager.async_initialize()
await manager.async_create_person(
name='Hello',
user_id=hass_admin_user.id
)
person = await manager.async_create_person(
name='Hello',
)
with pytest.raises(ValueError):
await manager.async_update_person(
person_id=person['id'],
user_id=hass_admin_user.id
)
async def test_update_invalid_user_id(hass):
"""Test updating to invalid user ID."""
manager = PersonManager(
hass, Mock(async_add_entities=mock_coro_func()), []
)
await manager.async_initialize()
person = await manager.async_create_person(
name='Hello',
)
with pytest.raises(ValueError):
await manager.async_update_person(
person_id=person['id'],
user_id='non-existing'
)
async def test_update_person_when_user_removed(hass, hass_read_only_user):
"""Update person when user is removed."""
manager = PersonManager(
hass, Mock(async_add_entities=mock_coro_func()), []
)
await manager.async_initialize()
person = await manager.async_create_person(
name='Hello',
user_id=hass_read_only_user.id
)
await hass.auth.async_remove_user(hass_read_only_user)
await hass.async_block_till_done()
assert person['user_id'] is None