From ee8830cc77333356b9bb440bdd63cd182f65dc79 Mon Sep 17 00:00:00 2001 From: Pete Sage <76050312+PeteRager@users.noreply.github.com> Date: Mon, 30 Jun 2025 07:35:19 -0400 Subject: [PATCH] Person ble_trackers for non-home zones not processed correctly (#138475) Co-authored-by: Erik Montnemery Co-authored-by: Joost Lekkerkerker --- homeassistant/components/person/__init__.py | 3 +- tests/components/person/test_init.py | 75 +++++++++++++++++++++ 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/person/__init__.py b/homeassistant/components/person/__init__.py index 856e07bb2ee..0dd8646b17e 100644 --- a/homeassistant/components/person/__init__.py +++ b/homeassistant/components/person/__init__.py @@ -27,7 +27,6 @@ from homeassistant.const import ( EVENT_HOMEASSISTANT_START, SERVICE_RELOAD, STATE_HOME, - STATE_NOT_HOME, STATE_UNAVAILABLE, STATE_UNKNOWN, ) @@ -526,7 +525,7 @@ class Person( latest_gps = _get_latest(latest_gps, state) elif state.state == STATE_HOME: latest_non_gps_home = _get_latest(latest_non_gps_home, state) - elif state.state == STATE_NOT_HOME: + else: latest_not_home = _get_latest(latest_not_home, state) if latest_non_gps_home: diff --git a/tests/components/person/test_init.py b/tests/components/person/test_init.py index 1d6c398c444..c001da86adb 100644 --- a/tests/components/person/test_init.py +++ b/tests/components/person/test_init.py @@ -244,6 +244,81 @@ async def test_setup_two_trackers( assert state.attributes.get(ATTR_SOURCE) == DEVICE_TRACKER +async def test_setup_router_ble_trackers( + hass: HomeAssistant, hass_admin_user: MockUser +) -> None: + """Test router and BLE trackers.""" + # BLE trackers are considered stationary trackers; however unlike a router based tracker + # whose states are home and not_home, a BLE tracker may have the value of any zone that the + # beacon is configured for. + hass.set_state(CoreState.not_running) + 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, "not_home", {ATTR_SOURCE_TYPE: SourceType.ROUTER} + ) + 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) is None + assert state.attributes.get(ATTR_LONGITUDE) is None + assert state.attributes.get(ATTR_GPS_ACCURACY) is None + assert state.attributes.get(ATTR_SOURCE) == DEVICE_TRACKER + assert state.attributes.get(ATTR_USER_ID) == user_id + assert state.attributes.get(ATTR_DEVICE_TRACKERS) == [ + DEVICE_TRACKER, + DEVICE_TRACKER_2, + ] + + # Set the BLE tracker to the "office" zone. + hass.states.async_set( + DEVICE_TRACKER_2, + "office", + { + ATTR_LATITUDE: 12.123456, + ATTR_LONGITUDE: 13.123456, + ATTR_GPS_ACCURACY: 12, + ATTR_SOURCE_TYPE: SourceType.BLUETOOTH_LE, + }, + ) + await hass.async_block_till_done() + + # The person should be in the office. + state = hass.states.get("person.tracked_person") + assert state.state == "office" + assert state.attributes.get(ATTR_ID) == "1234" + assert state.attributes.get(ATTR_LATITUDE) == 12.123456 + assert state.attributes.get(ATTR_LONGITUDE) == 13.123456 + assert state.attributes.get(ATTR_GPS_ACCURACY) == 12 + assert state.attributes.get(ATTR_SOURCE) == DEVICE_TRACKER_2 + assert state.attributes.get(ATTR_USER_ID) == user_id + assert state.attributes.get(ATTR_DEVICE_TRACKERS) == [ + DEVICE_TRACKER, + DEVICE_TRACKER_2, + ] + + async def test_ignore_unavailable_states( hass: HomeAssistant, hass_admin_user: MockUser ) -> None: