diff --git a/homeassistant/components/mobile_app/device_tracker.py b/homeassistant/components/mobile_app/device_tracker.py index 2719a4a14c0..3e2c6b9f1d0 100644 --- a/homeassistant/components/mobile_app/device_tracker.py +++ b/homeassistant/components/mobile_app/device_tracker.py @@ -8,6 +8,10 @@ from homeassistant.components.device_tracker import ( ATTR_LOCATION_NAME, TrackerEntity, ) +from homeassistant.components.zone import ( + ENTITY_ID_FORMAT as ZONE_ENTITY_ID_FORMAT, + HOME_ZONE, +) from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( ATTR_BATTERY_LEVEL, @@ -15,6 +19,7 @@ from homeassistant.const import ( ATTR_GPS_ACCURACY, ATTR_LATITUDE, ATTR_LONGITUDE, + STATE_HOME, ) from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.device_registry import DeviceInfo @@ -99,6 +104,12 @@ class MobileAppEntity(TrackerEntity, RestoreEntity): def location_name(self) -> str | None: """Return a location name for the current location of the device.""" if location_name := self._data.get(ATTR_LOCATION_NAME): + if location_name == HOME_ZONE: + return STATE_HOME + if zone_state := self.hass.states.get( + ZONE_ENTITY_ID_FORMAT.format(location_name) + ): + return zone_state.name return location_name return None diff --git a/tests/components/mobile_app/test_device_tracker.py b/tests/components/mobile_app/test_device_tracker.py index 6272b0ea893..46e3b6e5e6a 100644 --- a/tests/components/mobile_app/test_device_tracker.py +++ b/tests/components/mobile_app/test_device_tracker.py @@ -57,12 +57,12 @@ async def setup_zone(hass: HomeAssistant) -> None: ( {"gps": [20, 30], "location_name": "office"}, {"latitude": 20, "longitude": 30, "gps_accuracy": 30}, - "office", + "Office", ), ( {"gps": [30, 40], "location_name": "school"}, {"latitude": 30, "longitude": 40, "gps_accuracy": 30}, - "school", + "School", ), # Send wrong coordinates + location_name: Location name has precedence ( @@ -73,17 +73,17 @@ async def setup_zone(hass: HomeAssistant) -> None: ( {"gps": [10, 10], "location_name": "office"}, {"latitude": 10, "longitude": 10, "gps_accuracy": 30}, - "office", + "Office", ), ( {"gps": [10, 10], "location_name": "school"}, {"latitude": 10, "longitude": 10, "gps_accuracy": 30}, - "school", + "School", ), # Send location_name only ({"location_name": "home"}, {}, "home"), - ({"location_name": "office"}, {}, "office"), - ({"location_name": "school"}, {}, "school"), + ({"location_name": "office"}, {}, "Office"), + ({"location_name": "school"}, {}, "School"), # Send coordinates only - location is determined by coordinates ( {"gps": [10, 20]},