diff --git a/homeassistant/components/mqtt/device_tracker.py b/homeassistant/components/mqtt/device_tracker.py index 141e0478f2f..4bb23a9fa7e 100644 --- a/homeassistant/components/mqtt/device_tracker.py +++ b/homeassistant/components/mqtt/device_tracker.py @@ -163,8 +163,6 @@ class MqttDeviceTracker(MqttEntity, TrackerEntity): latitude: float | None longitude: float | None gps_accuracy: float - # Reset manually set location to allow automatic zone detection - self._attr_location_name = None if isinstance( latitude := extra_state_attributes.get(ATTR_LATITUDE), (int, float) ) and isinstance( diff --git a/tests/components/mqtt/test_device_tracker.py b/tests/components/mqtt/test_device_tracker.py index eda54d8efee..45fc7fa27c2 100644 --- a/tests/components/mqtt/test_device_tracker.py +++ b/tests/components/mqtt/test_device_tracker.py @@ -644,6 +644,31 @@ async def test_setting_device_tracker_location_via_abbr_reset_message( assert state.attributes["source_type"] == "gps" assert state.state == STATE_HOME + # Override the GPS state via a direct state update + async_fire_mqtt_message(hass, "test-topic", "office") + state = hass.states.get("device_tracker.test") + assert state.state == "office" + + # Test a GPS attributes update without a reset + async_fire_mqtt_message( + hass, + "attributes-topic", + '{"latitude":32.87336,"longitude": -117.22743, "gps_accuracy":1.5}', + ) + + state = hass.states.get("device_tracker.test") + assert state.state == "office" + + # Reset the manual set location + # This should calculate the location from GPS attributes + async_fire_mqtt_message(hass, "test-topic", "reset") + state = hass.states.get("device_tracker.test") + assert state.attributes["latitude"] == 32.87336 + assert state.attributes["longitude"] == -117.22743 + assert state.attributes["gps_accuracy"] == 1.5 + assert state.attributes["source_type"] == "gps" + assert state.state == STATE_HOME + async def test_setting_blocked_attribute_via_mqtt_json_message( hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator