Fix person update on create (#21355)

* Update tests to set correct hass running state

* Update person on adding person if hass is running

* Test creating person when hass is running
pull/21508/head
Martin Hjelmare 2019-02-23 22:38:21 +01:00 committed by Paulus Schoutsen
parent f6811a85b6
commit 0f4c2ccd1e
2 changed files with 29 additions and 5 deletions

View File

@ -337,12 +337,18 @@ class Person(RestoreEntity):
if state:
self._parse_source_state(state)
@callback
def person_start_hass(now):
if self.hass.is_running:
# Update person now if hass is already running.
self.person_updated()
else:
# Wait for hass start to not have race between person
# and device trackers finishing setup.
@callback
def person_start_hass(now):
self.person_updated()
self.hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_START, person_start_hass)
self.hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_START, person_start_hass)
@callback
def person_updated(self):

View File

@ -101,6 +101,7 @@ async def test_valid_invalid_user_ids(hass, hass_admin_user):
async def test_setup_tracker(hass, hass_admin_user):
"""Test set up person with one device tracker."""
hass.state = CoreState.not_running
user_id = hass_admin_user.id
config = {DOMAIN: {
'id': '1234', 'name': 'tracked person', 'user_id': user_id,
@ -148,6 +149,7 @@ async def test_setup_tracker(hass, hass_admin_user):
async def test_setup_two_trackers(hass, hass_admin_user):
"""Test set up person with two device trackers."""
hass.state = CoreState.not_running
user_id = hass_admin_user.id
config = {DOMAIN: {
'id': '1234', 'name': 'tracked person', 'user_id': user_id,
@ -191,6 +193,7 @@ async def test_setup_two_trackers(hass, hass_admin_user):
async def test_ignore_unavailable_states(hass, hass_admin_user):
"""Test set up person with two device trackers, one unavailable."""
hass.state = CoreState.not_running
user_id = hass_admin_user.id
config = {DOMAIN: {
'id': '1234', 'name': 'tracked person', 'user_id': user_id,
@ -234,7 +237,7 @@ async def test_restore_home_state(hass, hass_admin_user):
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
hass.state = CoreState.not_running
mock_component(hass, 'recorder')
config = {DOMAIN: {
'id': '1234', 'name': 'tracked person', 'user_id': user_id,
@ -263,6 +266,21 @@ async def test_duplicate_ids(hass, hass_admin_user):
assert hass.states.get('person.test_user_2') is None
async def test_create_person_during_run(hass):
"""Test that person is updated if created while hass is running."""
config = {DOMAIN: {}}
assert await async_setup_component(hass, DOMAIN, config)
hass.states.async_set(DEVICE_TRACKER, 'home')
await hass.async_block_till_done()
await hass.components.person.async_create_person(
'tracked person', device_trackers=[DEVICE_TRACKER])
await hass.async_block_till_done()
state = hass.states.get('person.tracked_person')
assert state.state == 'home'
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')