mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 17:57:11 +00:00
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
This commit is contained in:
parent
f6811a85b6
commit
0f4c2ccd1e
@ -337,12 +337,18 @@ class Person(RestoreEntity):
|
|||||||
if state:
|
if state:
|
||||||
self._parse_source_state(state)
|
self._parse_source_state(state)
|
||||||
|
|
||||||
@callback
|
if self.hass.is_running:
|
||||||
def person_start_hass(now):
|
# Update person now if hass is already running.
|
||||||
self.person_updated()
|
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(
|
self.hass.bus.async_listen_once(
|
||||||
EVENT_HOMEASSISTANT_START, person_start_hass)
|
EVENT_HOMEASSISTANT_START, person_start_hass)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def person_updated(self):
|
def person_updated(self):
|
||||||
|
@ -101,6 +101,7 @@ async def test_valid_invalid_user_ids(hass, hass_admin_user):
|
|||||||
|
|
||||||
async def test_setup_tracker(hass, hass_admin_user):
|
async def test_setup_tracker(hass, hass_admin_user):
|
||||||
"""Test set up person with one device tracker."""
|
"""Test set up person with one device tracker."""
|
||||||
|
hass.state = CoreState.not_running
|
||||||
user_id = hass_admin_user.id
|
user_id = hass_admin_user.id
|
||||||
config = {DOMAIN: {
|
config = {DOMAIN: {
|
||||||
'id': '1234', 'name': 'tracked person', 'user_id': user_id,
|
'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):
|
async def test_setup_two_trackers(hass, hass_admin_user):
|
||||||
"""Test set up person with two device trackers."""
|
"""Test set up person with two device trackers."""
|
||||||
|
hass.state = CoreState.not_running
|
||||||
user_id = hass_admin_user.id
|
user_id = hass_admin_user.id
|
||||||
config = {DOMAIN: {
|
config = {DOMAIN: {
|
||||||
'id': '1234', 'name': 'tracked person', 'user_id': user_id,
|
'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):
|
async def test_ignore_unavailable_states(hass, hass_admin_user):
|
||||||
"""Test set up person with two device trackers, one unavailable."""
|
"""Test set up person with two device trackers, one unavailable."""
|
||||||
|
hass.state = CoreState.not_running
|
||||||
user_id = hass_admin_user.id
|
user_id = hass_admin_user.id
|
||||||
config = {DOMAIN: {
|
config = {DOMAIN: {
|
||||||
'id': '1234', 'name': 'tracked person', 'user_id': user_id,
|
'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}
|
ATTR_SOURCE: DEVICE_TRACKER, ATTR_USER_ID: user_id}
|
||||||
state = State('person.tracked_person', 'home', attrs)
|
state = State('person.tracked_person', 'home', attrs)
|
||||||
mock_restore_cache(hass, (state, ))
|
mock_restore_cache(hass, (state, ))
|
||||||
hass.state = CoreState.starting
|
hass.state = CoreState.not_running
|
||||||
mock_component(hass, 'recorder')
|
mock_component(hass, 'recorder')
|
||||||
config = {DOMAIN: {
|
config = {DOMAIN: {
|
||||||
'id': '1234', 'name': 'tracked person', 'user_id': user_id,
|
'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
|
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):
|
async def test_load_person_storage(hass, hass_admin_user, storage_setup):
|
||||||
"""Test set up person from storage."""
|
"""Test set up person from storage."""
|
||||||
state = hass.states.get('person.tracked_person')
|
state = hass.states.get('person.tracked_person')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user